Videos.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. <script setup lang="ts">
  2. import { defineAsyncComponent, ref } from "vue";
  3. import Toast from "toasters";
  4. import { useWebsocketsStore } from "@/stores/websockets";
  5. import { useLongJobsStore } from "@/stores/longJobs";
  6. import { useModalsStore } from "@/stores/modals";
  7. const AdvancedTable = defineAsyncComponent(
  8. () => import("@/components/AdvancedTable.vue")
  9. );
  10. const RunJobDropdown = defineAsyncComponent(
  11. () => import("@/components/RunJobDropdown.vue")
  12. );
  13. const { setJob } = useLongJobsStore();
  14. const { socket } = useWebsocketsStore();
  15. const columnDefault = ref({
  16. sortable: true,
  17. hidable: true,
  18. defaultVisibility: "shown",
  19. draggable: true,
  20. resizable: true,
  21. minWidth: 200,
  22. maxWidth: 600
  23. });
  24. const columns = ref([
  25. {
  26. name: "options",
  27. displayName: "Options",
  28. properties: ["_id", "youtubeId"],
  29. sortable: false,
  30. hidable: false,
  31. resizable: false,
  32. minWidth: 129,
  33. defaultWidth: 129
  34. },
  35. {
  36. name: "thumbnailImage",
  37. displayName: "Thumb",
  38. properties: ["youtubeId"],
  39. sortable: false,
  40. minWidth: 75,
  41. defaultWidth: 75,
  42. maxWidth: 75,
  43. resizable: false
  44. },
  45. {
  46. name: "youtubeId",
  47. displayName: "YouTube ID",
  48. properties: ["youtubeId"],
  49. sortProperty: "youtubeId",
  50. minWidth: 120,
  51. defaultWidth: 120
  52. },
  53. {
  54. name: "_id",
  55. displayName: "Video ID",
  56. properties: ["_id"],
  57. sortProperty: "_id",
  58. minWidth: 215,
  59. defaultWidth: 215
  60. },
  61. {
  62. name: "title",
  63. displayName: "Title",
  64. properties: ["title"],
  65. sortProperty: "title"
  66. },
  67. {
  68. name: "author",
  69. displayName: "Author",
  70. properties: ["author"],
  71. sortProperty: "author"
  72. },
  73. {
  74. name: "duration",
  75. displayName: "Duration",
  76. properties: ["duration"],
  77. sortProperty: "duration",
  78. defaultWidth: 200
  79. },
  80. {
  81. name: "createdAt",
  82. displayName: "Created At",
  83. properties: ["createdAt"],
  84. sortProperty: "createdAt",
  85. defaultWidth: 200,
  86. defaultVisibility: "hidden"
  87. },
  88. {
  89. name: "songId",
  90. displayName: "Song ID",
  91. properties: ["songId"],
  92. sortProperty: "songId",
  93. defaultWidth: 220,
  94. defaultVisibility: "hidden"
  95. }
  96. ]);
  97. const filters = ref([
  98. {
  99. name: "_id",
  100. displayName: "Video ID",
  101. property: "_id",
  102. filterTypes: ["exact"],
  103. defaultFilterType: "exact"
  104. },
  105. {
  106. name: "youtubeId",
  107. displayName: "YouTube ID",
  108. property: "youtubeId",
  109. filterTypes: ["contains", "exact", "regex"],
  110. defaultFilterType: "contains"
  111. },
  112. {
  113. name: "title",
  114. displayName: "Title",
  115. property: "title",
  116. filterTypes: ["contains", "exact", "regex"],
  117. defaultFilterType: "contains"
  118. },
  119. {
  120. name: "author",
  121. displayName: "Author",
  122. property: "author",
  123. filterTypes: ["contains", "exact", "regex"],
  124. defaultFilterType: "contains"
  125. },
  126. {
  127. name: "duration",
  128. displayName: "Duration",
  129. property: "duration",
  130. filterTypes: [
  131. "numberLesserEqual",
  132. "numberLesser",
  133. "numberGreater",
  134. "numberGreaterEqual",
  135. "numberEquals"
  136. ],
  137. defaultFilterType: "numberLesser"
  138. },
  139. {
  140. name: "createdAt",
  141. displayName: "Created At",
  142. property: "createdAt",
  143. filterTypes: ["datetimeBefore", "datetimeAfter"],
  144. defaultFilterType: "datetimeBefore"
  145. },
  146. {
  147. name: "importJob",
  148. displayName: "Import Job",
  149. property: "importJob",
  150. filterTypes: ["special"],
  151. defaultFilterType: "special"
  152. },
  153. {
  154. name: "songId",
  155. displayName: "Song ID",
  156. property: "songId",
  157. filterTypes: ["contains", "exact", "regex"],
  158. defaultFilterType: "contains"
  159. }
  160. ]);
  161. const events = ref({
  162. adminRoom: "youtubeVideos",
  163. updated: {
  164. event: "admin.youtubeVideo.updated",
  165. id: "youtubeVideo._id",
  166. item: "youtubeVideo"
  167. },
  168. removed: {
  169. event: "admin.youtubeVideo.removed",
  170. id: "videoId"
  171. }
  172. });
  173. const jobs = ref([
  174. {
  175. name: "Recalculate all ratings",
  176. socket: "media.recalculateAllRatings"
  177. }
  178. ]);
  179. const { openModal } = useModalsStore();
  180. const editOne = song => {
  181. openModal({
  182. modal: "editSong",
  183. data: { song }
  184. });
  185. };
  186. const editMany = selectedRows => {
  187. if (selectedRows.length === 1) editOne(selectedRows[0]);
  188. else {
  189. const songs = selectedRows.map(row => ({
  190. youtubeId: row.youtubeId
  191. }));
  192. openModal({ modal: "editSongs", data: { songs } });
  193. }
  194. };
  195. const importAlbum = selectedRows => {
  196. const youtubeIds = selectedRows.map(({ youtubeId }) => youtubeId);
  197. socket.dispatch("songs.getSongsFromYoutubeIds", youtubeIds, res => {
  198. if (res.status === "success") {
  199. openModal({
  200. modal: "importAlbum",
  201. data: { songs: res.data.songs }
  202. });
  203. } else new Toast("Could not get songs.");
  204. });
  205. };
  206. const removeVideos = videoIds => {
  207. let id;
  208. let title;
  209. socket.dispatch("youtube.removeVideos", videoIds, {
  210. cb: () => {},
  211. onProgress: res => {
  212. if (res.status === "started") {
  213. id = res.id;
  214. title = res.title;
  215. }
  216. if (id)
  217. setJob({
  218. id,
  219. name: title,
  220. ...res
  221. });
  222. }
  223. });
  224. };
  225. const getDateFormatted = createdAt => {
  226. const date = new Date(createdAt);
  227. const year = date.getFullYear();
  228. const month = `${date.getMonth() + 1}`.padStart(2, 0);
  229. const day = `${date.getDate()}`.padStart(2, 0);
  230. const hour = `${date.getHours()}`.padStart(2, 0);
  231. const minute = `${date.getMinutes()}`.padStart(2, 0);
  232. return `${year}-${month}-${day} ${hour}:${minute}`;
  233. };
  234. const handleConfirmed = ({ action, params }) => {
  235. if (typeof action === "function") {
  236. if (params) action(params);
  237. else action();
  238. }
  239. };
  240. const confirmAction = ({ message, action, params }) => {
  241. openModal({
  242. modal: "confirm",
  243. data: {
  244. message,
  245. action,
  246. params,
  247. onCompleted: handleConfirmed
  248. }
  249. });
  250. };
  251. </script>
  252. <template>
  253. <div class="admin-tab container">
  254. <page-metadata title="Admin | YouTube | Videos" />
  255. <div class="card tab-info">
  256. <div class="info-row">
  257. <h1>YouTube Videos</h1>
  258. <p>Manage YouTube video cache</p>
  259. </div>
  260. <div class="button-row">
  261. <run-job-dropdown :jobs="jobs" />
  262. </div>
  263. </div>
  264. <advanced-table
  265. :column-default="columnDefault"
  266. :columns="columns"
  267. :filters="filters"
  268. :events="events"
  269. data-action="youtube.getVideos"
  270. name="admin-youtube-videos"
  271. :max-width="1140"
  272. :bulk-actions="{ width: 200 }"
  273. >
  274. <template #column-options="slotProps">
  275. <div class="row-options">
  276. <button
  277. class="button is-primary icon-with-button material-icons"
  278. @click="
  279. openModal({
  280. modal: 'viewYoutubeVideo',
  281. data: {
  282. videoId: slotProps.item._id
  283. }
  284. })
  285. "
  286. :disabled="slotProps.item.removed"
  287. content="View Video"
  288. v-tippy
  289. >
  290. open_in_full
  291. </button>
  292. <button
  293. class="button is-primary icon-with-button material-icons"
  294. @click="editOne(slotProps.item)"
  295. :disabled="slotProps.item.removed"
  296. content="Create/edit song from video"
  297. v-tippy
  298. >
  299. music_note
  300. </button>
  301. <button
  302. class="button is-danger icon-with-button material-icons"
  303. @click.prevent="
  304. confirmAction({
  305. message:
  306. 'Removing this video will remove it from all playlists and cause a ratings recalculation.',
  307. action: removeVideos,
  308. params: slotProps.item._id
  309. })
  310. "
  311. :disabled="slotProps.item.removed"
  312. content="Delete Video"
  313. v-tippy
  314. >
  315. delete_forever
  316. </button>
  317. </div>
  318. </template>
  319. <template #column-thumbnailImage="slotProps">
  320. <song-thumbnail class="song-thumbnail" :song="slotProps.item" />
  321. </template>
  322. <template #column-youtubeId="slotProps">
  323. <a
  324. :href="
  325. 'https://www.youtube.com/watch?v=' +
  326. `${slotProps.item.youtubeId}`
  327. "
  328. target="_blank"
  329. >
  330. {{ slotProps.item.youtubeId }}
  331. </a>
  332. </template>
  333. <template #column-_id="slotProps">
  334. <span :title="slotProps.item._id">{{
  335. slotProps.item._id
  336. }}</span>
  337. </template>
  338. <template #column-title="slotProps">
  339. <span :title="slotProps.item.title">{{
  340. slotProps.item.title
  341. }}</span>
  342. </template>
  343. <template #column-author="slotProps">
  344. <span :title="slotProps.item.author">{{
  345. slotProps.item.author
  346. }}</span>
  347. </template>
  348. <template #column-duration="slotProps">
  349. <span :title="slotProps.item.duration">{{
  350. slotProps.item.duration
  351. }}</span>
  352. </template>
  353. <template #column-createdAt="slotProps">
  354. <span :title="new Date(slotProps.item.createdAt)">{{
  355. getDateFormatted(slotProps.item.createdAt)
  356. }}</span>
  357. </template>
  358. <template #column-songId="slotProps">
  359. <span :title="slotProps.item.songId">{{
  360. slotProps.item.songId
  361. }}</span>
  362. </template>
  363. <template #bulk-actions="slotProps">
  364. <div class="bulk-actions">
  365. <i
  366. class="material-icons create-songs-icon"
  367. @click.prevent="editMany(slotProps.item)"
  368. content="Create/edit songs from videos"
  369. v-tippy
  370. tabindex="0"
  371. >
  372. music_note
  373. </i>
  374. <i
  375. class="material-icons import-album-icon"
  376. @click.prevent="importAlbum(slotProps.item)"
  377. content="Import album from videos"
  378. v-tippy
  379. tabindex="0"
  380. >
  381. album
  382. </i>
  383. <i
  384. class="material-icons delete-icon"
  385. @click.prevent="
  386. confirmAction({
  387. message:
  388. 'Removing these videos will remove them from all playlists and cause a ratings recalculation.',
  389. action: removeVideos,
  390. params: slotProps.item.map(video => video._id)
  391. })
  392. "
  393. content="Delete Videos"
  394. v-tippy
  395. tabindex="0"
  396. >
  397. delete_forever
  398. </i>
  399. </div>
  400. </template>
  401. </advanced-table>
  402. </div>
  403. </template>
  404. <style lang="less" scoped>
  405. :deep(.song-thumbnail) {
  406. width: 50px;
  407. height: 50px;
  408. min-width: 50px;
  409. min-height: 50px;
  410. margin: 0 auto;
  411. }
  412. </style>