Videos.vue 10 KB

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