index.vue 17 KB

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