index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. <script setup lang="ts">
  2. import {
  3. defineAsyncComponent,
  4. ref,
  5. computed,
  6. onMounted,
  7. onBeforeUnmount
  8. } from "vue";
  9. import Toast from "toasters";
  10. import { storeToRefs } from "pinia";
  11. import { DraggableList } from "vue-draggable-list";
  12. import { useWebsocketsStore } from "@/stores/websockets";
  13. import { useEditPlaylistStore } from "@/stores/editPlaylist";
  14. import { useStationStore } from "@/stores/station";
  15. import { useUserAuthStore } from "@/stores/userAuth";
  16. import { useModalsStore } from "@/stores/modals";
  17. import utils from "@/utils";
  18. const Modal = defineAsyncComponent(() => import("@/components/Modal.vue"));
  19. const SongItem = defineAsyncComponent(
  20. () => import("@/components/SongItem.vue")
  21. );
  22. const Settings = defineAsyncComponent(() => import("./Tabs/Settings.vue"));
  23. const AddSongs = defineAsyncComponent(() => import("./Tabs/AddSongs.vue"));
  24. const ImportPlaylists = defineAsyncComponent(
  25. () => import("./Tabs/ImportPlaylists.vue")
  26. );
  27. const QuickConfirm = defineAsyncComponent(
  28. () => import("@/components/QuickConfirm.vue")
  29. );
  30. const props = defineProps({
  31. modalUuid: { type: String, required: true },
  32. playlistId: { type: String, required: true }
  33. });
  34. const { socket } = useWebsocketsStore();
  35. const editPlaylistStore = useEditPlaylistStore({ modalUuid: props.modalUuid });
  36. const stationStore = useStationStore();
  37. const userAuthStore = useUserAuthStore();
  38. const { station } = storeToRefs(stationStore);
  39. const { loggedIn, userId, role: userRole } = storeToRefs(userAuthStore);
  40. const drag = ref(false);
  41. const apiDomain = ref("");
  42. const gettingSongs = ref(false);
  43. const tabs = ref([]);
  44. const songItems = ref([]);
  45. const playlistSongs = computed({
  46. get: () => editPlaylistStore.playlist.songs,
  47. set: value => {
  48. editPlaylistStore.updatePlaylistSongs(value);
  49. }
  50. });
  51. const { tab, playlist } = storeToRefs(editPlaylistStore);
  52. const { setPlaylist, clearPlaylist, addSong, removeSong, repositionedSong } =
  53. editPlaylistStore;
  54. const { closeCurrentModal } = useModalsStore();
  55. const showTab = payload => {
  56. if (tabs.value[`${payload}-tab`])
  57. tabs.value[`${payload}-tab`].scrollIntoView({ block: "nearest" });
  58. editPlaylistStore.showTab(payload);
  59. };
  60. const { hasPermission } = userAuthStore;
  61. const isOwner = () =>
  62. loggedIn.value && userId.value === playlist.value.createdBy;
  63. const isEditable = permission =>
  64. ((playlist.value.type === "user" ||
  65. playlist.value.type === "user-liked" ||
  66. playlist.value.type === "user-disliked" ||
  67. playlist.value.type === "admin") &&
  68. (isOwner() || hasPermission(permission))) ||
  69. (playlist.value.type === "genre" &&
  70. permission === "playlists.update.privacy" &&
  71. hasPermission(permission));
  72. const repositionSong = ({ moved }) => {
  73. const { oldIndex, newIndex } = moved;
  74. if (oldIndex === newIndex) return; // we only need to update when song is moved
  75. const song = playlistSongs.value[newIndex];
  76. socket.dispatch(
  77. "playlists.repositionSong",
  78. playlist.value._id,
  79. {
  80. ...song,
  81. oldIndex,
  82. newIndex
  83. },
  84. res => {
  85. if (res.status !== "success")
  86. repositionedSong({
  87. ...song,
  88. newIndex: oldIndex,
  89. oldIndex: newIndex
  90. });
  91. }
  92. );
  93. };
  94. const moveSongToTop = index => {
  95. songItems.value[`song-item-${index}`].$refs.songActions.tippy.hide();
  96. playlistSongs.value.splice(0, 0, playlistSongs.value.splice(index, 1)[0]);
  97. repositionSong({
  98. moved: {
  99. oldIndex: index,
  100. newIndex: 0
  101. }
  102. });
  103. };
  104. const moveSongToBottom = index => {
  105. songItems.value[`song-item-${index}`].$refs.songActions.tippy.hide();
  106. playlistSongs.value.splice(
  107. playlistSongs.value.length - 1,
  108. 0,
  109. playlistSongs.value.splice(index, 1)[0]
  110. );
  111. repositionSong({
  112. moved: {
  113. oldIndex: index,
  114. newIndex: playlistSongs.value.length - 1
  115. }
  116. });
  117. };
  118. const totalLength = () => {
  119. let length = 0;
  120. playlist.value.songs.forEach(song => {
  121. length += song.duration;
  122. });
  123. return utils.formatTimeLong(length);
  124. };
  125. // const shuffle = () => {
  126. // socket.dispatch("playlists.shuffle", playlist.value._id, res => {
  127. // new Toast(res.message);
  128. // if (res.status === "success") {
  129. // updatePlaylistSongs(
  130. // res.data.playlist.songs.sort((a, b) => a.position - b.position)
  131. // );
  132. // }
  133. // });
  134. // };
  135. const removeSongFromPlaylist = id =>
  136. socket.dispatch(
  137. "playlists.removeSongFromPlaylist",
  138. id,
  139. playlist.value._id,
  140. res => {
  141. new Toast(res.message);
  142. }
  143. );
  144. const removePlaylist = () => {
  145. if (isOwner()) {
  146. socket.dispatch("playlists.remove", playlist.value._id, res => {
  147. new Toast(res.message);
  148. if (res.status === "success") closeCurrentModal();
  149. });
  150. } else if (hasPermission("playlists.removeAdmin")) {
  151. socket.dispatch("playlists.removeAdmin", playlist.value._id, res => {
  152. new Toast(res.message);
  153. if (res.status === "success") closeCurrentModal();
  154. });
  155. }
  156. };
  157. const downloadPlaylist = async () => {
  158. if (apiDomain.value === "")
  159. apiDomain.value = await lofig.get("backend.apiDomain");
  160. fetch(`${apiDomain.value}/export/playlist/${playlist.value._id}`, {
  161. credentials: "include"
  162. })
  163. .then(res => res.blob())
  164. .then(blob => {
  165. const url = window.URL.createObjectURL(blob);
  166. const a = document.createElement("a");
  167. a.style.display = "none";
  168. a.href = url;
  169. a.download = `musare-playlist-${
  170. playlist.value._id
  171. }-${new Date().toISOString()}.json`;
  172. document.body.appendChild(a);
  173. a.click();
  174. window.URL.revokeObjectURL(url);
  175. new Toast("Successfully downloaded playlist.");
  176. })
  177. .catch(() => new Toast("Failed to export and download playlist."));
  178. };
  179. const addSongToQueue = youtubeId => {
  180. socket.dispatch(
  181. "stations.addToQueue",
  182. station.value._id,
  183. youtubeId,
  184. data => {
  185. if (data.status !== "success")
  186. new Toast({
  187. content: `Error: ${data.message}`,
  188. timeout: 8000
  189. });
  190. else new Toast({ content: data.message, timeout: 4000 });
  191. }
  192. );
  193. };
  194. const clearAndRefillStationPlaylist = () => {
  195. socket.dispatch(
  196. "playlists.clearAndRefillStationPlaylist",
  197. playlist.value._id,
  198. data => {
  199. if (data.status !== "success")
  200. new Toast({
  201. content: `Error: ${data.message}`,
  202. timeout: 8000
  203. });
  204. else new Toast({ content: data.message, timeout: 4000 });
  205. }
  206. );
  207. };
  208. const clearAndRefillGenrePlaylist = () => {
  209. socket.dispatch(
  210. "playlists.clearAndRefillGenrePlaylist",
  211. playlist.value._id,
  212. data => {
  213. if (data.status !== "success")
  214. new Toast({
  215. content: `Error: ${data.message}`,
  216. timeout: 8000
  217. });
  218. else new Toast({ content: data.message, timeout: 4000 });
  219. }
  220. );
  221. };
  222. onMounted(() => {
  223. socket.onConnect(() => {
  224. gettingSongs.value = true;
  225. socket.dispatch("playlists.getPlaylist", props.playlistId, res => {
  226. if (res.status === "success") {
  227. setPlaylist(res.data.playlist);
  228. } else new Toast(res.message);
  229. gettingSongs.value = false;
  230. });
  231. });
  232. socket.on(
  233. "event:playlist.song.added",
  234. res => {
  235. if (playlist.value._id === res.data.playlistId)
  236. addSong(res.data.song);
  237. },
  238. { modalUuid: props.modalUuid }
  239. );
  240. socket.on(
  241. "event:playlist.song.removed",
  242. res => {
  243. if (playlist.value._id === res.data.playlistId) {
  244. // remove song from array of playlists
  245. removeSong(res.data.youtubeId);
  246. }
  247. },
  248. { modalUuid: props.modalUuid }
  249. );
  250. socket.on(
  251. "event:playlist.displayName.updated",
  252. res => {
  253. if (playlist.value._id === res.data.playlistId) {
  254. setPlaylist({
  255. displayName: res.data.displayName,
  256. ...playlist.value
  257. });
  258. }
  259. },
  260. { modalUuid: props.modalUuid }
  261. );
  262. socket.on(
  263. "event:playlist.song.repositioned",
  264. res => {
  265. if (playlist.value._id === res.data.playlistId) {
  266. const { song, playlistId } = res.data;
  267. if (playlist.value._id === playlistId) {
  268. repositionedSong(song);
  269. }
  270. }
  271. },
  272. { modalUuid: props.modalUuid }
  273. );
  274. });
  275. onBeforeUnmount(() => {
  276. clearPlaylist();
  277. // Delete the Pinia store that was created for this modal, after all other cleanup tasks are performed
  278. editPlaylistStore.$dispose();
  279. });
  280. </script>
  281. <template>
  282. <modal
  283. :title="
  284. isEditable('playlists.update.privacy')
  285. ? `Edit ${playlist.type === 'admin' ? 'Admin ' : ''}Playlist`
  286. : `View ${playlist.type === 'admin' ? 'Admin ' : ''}Playlist`
  287. "
  288. :class="{
  289. 'edit-playlist-modal': true,
  290. 'view-only': !isEditable('playlists.update.privacy')
  291. }"
  292. :size="isEditable('playlists.update.privacy') ? 'wide' : null"
  293. :split="true"
  294. >
  295. <template #body>
  296. <div class="left-section">
  297. <div id="playlist-info-section" class="section">
  298. <h3>{{ playlist.displayName }}</h3>
  299. <h5>Song Count: {{ playlist.songs.length }}</h5>
  300. <h5>Duration: {{ totalLength() }}</h5>
  301. </div>
  302. <div class="tabs-container">
  303. <div class="tab-selection">
  304. <button
  305. class="button is-default"
  306. :class="{ selected: tab === 'settings' }"
  307. :ref="el => (tabs['settings-tab'] = el)"
  308. @click="showTab('settings')"
  309. v-if="isEditable('playlists.update.privacy')"
  310. >
  311. Settings
  312. </button>
  313. <button
  314. class="button is-default"
  315. :class="{ selected: tab === 'add-songs' }"
  316. :ref="el => (tabs['add-songs-tab'] = el)"
  317. @click="showTab('add-songs')"
  318. v-if="isEditable('playlists.songs.add')"
  319. >
  320. Add Songs
  321. </button>
  322. <button
  323. class="button is-default"
  324. :class="{
  325. selected: tab === 'import-playlists'
  326. }"
  327. :ref="el => (tabs['import-playlists-tab'] = el)"
  328. @click="showTab('import-playlists')"
  329. v-if="isEditable('playlists.songs.add')"
  330. >
  331. Import Songs
  332. </button>
  333. </div>
  334. <settings
  335. class="tab"
  336. v-show="tab === 'settings'"
  337. v-if="isEditable('playlists.update.privacy')"
  338. :modal-uuid="modalUuid"
  339. />
  340. <add-songs
  341. class="tab"
  342. v-show="tab === 'add-songs'"
  343. v-if="isEditable('playlists.songs.add')"
  344. :modal-uuid="modalUuid"
  345. />
  346. <import-playlists
  347. class="tab"
  348. v-show="tab === 'import-playlists'"
  349. v-if="isEditable('playlists.songs.add')"
  350. :modal-uuid="modalUuid"
  351. />
  352. </div>
  353. </div>
  354. <div class="right-section">
  355. <div id="rearrange-songs-section" class="section">
  356. <div v-if="isEditable('playlists.songs.reposition')">
  357. <h4 class="section-title">Rearrange Songs</h4>
  358. <p class="section-description">
  359. Drag and drop songs to change their order
  360. </p>
  361. <hr class="section-horizontal-rule" />
  362. </div>
  363. <aside class="menu">
  364. <draggable-list
  365. v-if="playlistSongs.length > 0"
  366. v-model:list="playlistSongs"
  367. item-key="youtubeId"
  368. @start="drag = true"
  369. @end="drag = false"
  370. @update="repositionSong"
  371. :disabled="
  372. !isEditable('playlists.songs.reposition')
  373. "
  374. >
  375. <template #item="{ element, index }">
  376. <song-item
  377. :song="element"
  378. :ref="
  379. el =>
  380. (songItems[`song-item-${index}`] =
  381. el)
  382. "
  383. :key="`playlist-song-${element.youtubeId}`"
  384. >
  385. <template #tippyActions>
  386. <i
  387. class="material-icons add-to-queue-icon"
  388. v-if="
  389. station &&
  390. station.requests &&
  391. station.requests.enabled &&
  392. (station.requests.access ===
  393. 'user' ||
  394. (station.requests.access ===
  395. 'owner' &&
  396. (userRole === 'admin' ||
  397. station.owner ===
  398. userId)))
  399. "
  400. @click="
  401. addSongToQueue(
  402. element.youtubeId
  403. )
  404. "
  405. content="Add Song to Queue"
  406. v-tippy
  407. >queue</i
  408. >
  409. <quick-confirm
  410. v-if="
  411. userId === playlist.createdBy ||
  412. isEditable(
  413. 'playlists.songs.reposition'
  414. )
  415. "
  416. placement="left"
  417. @confirm="
  418. removeSongFromPlaylist(
  419. element.youtubeId
  420. )
  421. "
  422. >
  423. <i
  424. class="material-icons delete-icon"
  425. content="Remove Song from Playlist"
  426. v-tippy
  427. >delete_forever</i
  428. >
  429. </quick-confirm>
  430. <i
  431. class="material-icons"
  432. v-if="
  433. isEditable(
  434. 'playlists.songs.reposition'
  435. ) && index > 0
  436. "
  437. @click="moveSongToTop(index)"
  438. content="Move to top of Playlist"
  439. v-tippy
  440. >vertical_align_top</i
  441. >
  442. <i
  443. v-if="
  444. isEditable(
  445. 'playlists.songs.reposition'
  446. ) &&
  447. playlistSongs.length - 1 !==
  448. index
  449. "
  450. @click="moveSongToBottom(index)"
  451. class="material-icons"
  452. content="Move to bottom of Playlist"
  453. v-tippy
  454. >vertical_align_bottom</i
  455. >
  456. </template>
  457. </song-item>
  458. </template>
  459. </draggable-list>
  460. <p v-else-if="gettingSongs" class="nothing-here-text">
  461. Loading songs...
  462. </p>
  463. <p v-else class="nothing-here-text">
  464. This playlist doesn't have any songs.
  465. </p>
  466. </aside>
  467. </div>
  468. </div>
  469. </template>
  470. <template #footer>
  471. <button
  472. class="button is-default"
  473. v-if="
  474. isOwner() ||
  475. hasPermission('playlists.get') ||
  476. playlist.privacy === 'public'
  477. "
  478. @click="downloadPlaylist()"
  479. >
  480. Download Playlist
  481. </button>
  482. <div class="right">
  483. <quick-confirm
  484. v-if="
  485. hasPermission('playlists.clearAndRefill') &&
  486. playlist.type === 'station'
  487. "
  488. @confirm="clearAndRefillStationPlaylist()"
  489. >
  490. <a class="button is-danger">
  491. Clear and refill station playlist
  492. </a>
  493. </quick-confirm>
  494. <quick-confirm
  495. v-if="
  496. hasPermission('playlists.clearAndRefill') &&
  497. playlist.type === 'genre'
  498. "
  499. @confirm="clearAndRefillGenrePlaylist()"
  500. >
  501. <a class="button is-danger">
  502. Clear and refill genre playlist
  503. </a>
  504. </quick-confirm>
  505. <quick-confirm
  506. v-if="
  507. isEditable('playlists.removeAdmin') &&
  508. !(
  509. playlist.type === 'user-liked' ||
  510. playlist.type === 'user-disliked'
  511. )
  512. "
  513. @confirm="removePlaylist()"
  514. >
  515. <a class="button is-danger"> Remove Playlist </a>
  516. </quick-confirm>
  517. </div>
  518. </template>
  519. </modal>
  520. </template>
  521. <style lang="less" scoped>
  522. .night-mode {
  523. .label,
  524. p,
  525. strong {
  526. color: var(--light-grey-2);
  527. }
  528. .edit-playlist-modal.modal .modal-card-body {
  529. .left-section {
  530. #playlist-info-section {
  531. background-color: var(--dark-grey-3) !important;
  532. border: 0;
  533. }
  534. .tabs-container {
  535. background-color: transparent !important;
  536. .tab-selection .button {
  537. background: var(--dark-grey);
  538. color: var(--white);
  539. }
  540. .tab {
  541. background-color: var(--dark-grey-3) !important;
  542. border: 0 !important;
  543. }
  544. }
  545. }
  546. .right-section .section {
  547. border-radius: @border-radius;
  548. }
  549. }
  550. }
  551. .controls {
  552. display: flex;
  553. a {
  554. display: flex;
  555. align-items: center;
  556. }
  557. }
  558. .tabs-container {
  559. .tab-selection {
  560. display: flex;
  561. margin: 24px 10px 0 10px;
  562. max-width: 100%;
  563. .button {
  564. border-radius: @border-radius @border-radius 0 0;
  565. border: 0;
  566. text-transform: uppercase;
  567. font-size: 14px;
  568. color: var(--dark-grey-3);
  569. background-color: var(--light-grey-2);
  570. flex-grow: 1;
  571. height: 32px;
  572. &:not(:first-of-type) {
  573. margin-left: 5px;
  574. }
  575. }
  576. .selected {
  577. background-color: var(--primary-color) !important;
  578. color: var(--white) !important;
  579. font-weight: 600;
  580. }
  581. }
  582. .tab {
  583. border: 1px solid var(--light-grey-3);
  584. border-radius: 0 0 @border-radius @border-radius;
  585. }
  586. }
  587. .edit-playlist-modal {
  588. &.view-only {
  589. height: auto !important;
  590. .left-section {
  591. flex-basis: 100% !important;
  592. }
  593. .right-section {
  594. max-height: unset !important;
  595. }
  596. :deep(.section) {
  597. max-width: 100% !important;
  598. }
  599. }
  600. .nothing-here-text {
  601. display: flex;
  602. align-items: center;
  603. justify-content: center;
  604. }
  605. .label {
  606. font-size: 1rem;
  607. font-weight: normal;
  608. }
  609. .input-with-button .button {
  610. width: 150px;
  611. }
  612. .left-section {
  613. #playlist-info-section {
  614. border: 1px solid var(--light-grey-3);
  615. border-radius: @border-radius;
  616. padding: 15px !important;
  617. h3 {
  618. font-weight: 600;
  619. font-size: 30px;
  620. }
  621. h5 {
  622. font-size: 18px;
  623. }
  624. h3,
  625. h5 {
  626. margin: 0;
  627. }
  628. }
  629. }
  630. }
  631. </style>