Videos.vue 8.7 KB

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