index.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  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, currentUser } = 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 && currentUser.value._id === 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. (currentUser.role ===
  412. 'admin' ||
  413. station.owner ===
  414. currentUser._id))) &&
  415. (element.mediaSource.split(
  416. ':'
  417. )[0] !== 'soundcloud' ||
  418. experimental.soundcloud)
  419. "
  420. @click="
  421. addSongToQueue(
  422. element.mediaSource
  423. )
  424. "
  425. content="Add Song to Queue"
  426. v-tippy
  427. >queue</i
  428. >
  429. <quick-confirm
  430. v-if="
  431. userId === playlist.createdBy ||
  432. isEditable(
  433. 'playlists.songs.reposition'
  434. )
  435. "
  436. placement="left"
  437. @confirm="
  438. removeSongFromPlaylist(
  439. element.mediaSource
  440. )
  441. "
  442. >
  443. <i
  444. class="material-icons delete-icon"
  445. content="Remove Song from Playlist"
  446. v-tippy
  447. >delete_forever</i
  448. >
  449. </quick-confirm>
  450. <i
  451. class="material-icons"
  452. v-if="
  453. isEditable(
  454. 'playlists.songs.reposition'
  455. ) && index > 0
  456. "
  457. @click="moveSongToTop(index)"
  458. content="Move to top of Playlist"
  459. v-tippy
  460. >vertical_align_top</i
  461. >
  462. <i
  463. v-if="
  464. isEditable(
  465. 'playlists.songs.reposition'
  466. ) &&
  467. playlistSongs.length - 1 !==
  468. index
  469. "
  470. @click="moveSongToBottom(index)"
  471. class="material-icons"
  472. content="Move to bottom of Playlist"
  473. v-tippy
  474. >vertical_align_bottom</i
  475. >
  476. </template>
  477. </media-item>
  478. </template>
  479. </draggable-list>
  480. <p v-else-if="gettingSongs" class="nothing-here-text">
  481. Loading songs...
  482. </p>
  483. <p v-else class="nothing-here-text">
  484. This playlist doesn't have any songs.
  485. </p>
  486. </aside>
  487. </div>
  488. </div>
  489. </template>
  490. <template #footer>
  491. <button
  492. class="button is-default"
  493. v-if="
  494. isOwner() ||
  495. hasPermission('playlists.get') ||
  496. playlist.privacy === 'public'
  497. "
  498. @click="downloadPlaylist()"
  499. >
  500. Download Playlist
  501. </button>
  502. <button
  503. class="button is-default"
  504. v-if="isOwner() && containsSpotifySongs"
  505. @click="
  506. openModal({
  507. modal: 'convertSpotifySongs',
  508. props: { playlistId: playlist._id }
  509. })
  510. "
  511. >
  512. Convert Spotify Songs
  513. </button>
  514. <div class="right">
  515. <quick-confirm
  516. v-if="
  517. hasPermission('playlists.clearAndRefill') &&
  518. playlist.type === 'station'
  519. "
  520. @confirm="clearAndRefillStationPlaylist()"
  521. >
  522. <a class="button is-danger">
  523. Clear and refill station playlist
  524. </a>
  525. </quick-confirm>
  526. <quick-confirm
  527. v-if="
  528. hasPermission('playlists.clearAndRefill') &&
  529. playlist.type === 'genre'
  530. "
  531. @confirm="clearAndRefillGenrePlaylist()"
  532. >
  533. <a class="button is-danger">
  534. Clear and refill genre playlist
  535. </a>
  536. </quick-confirm>
  537. <quick-confirm
  538. v-if="
  539. isEditable('playlists.removeAdmin') &&
  540. !(
  541. playlist.type === 'user-liked' ||
  542. playlist.type === 'user-disliked'
  543. )
  544. "
  545. @confirm="removePlaylist()"
  546. >
  547. <a class="button is-danger"> Remove Playlist </a>
  548. </quick-confirm>
  549. </div>
  550. </template>
  551. </modal>
  552. </template>
  553. <style lang="less" scoped>
  554. .night-mode {
  555. .label,
  556. p,
  557. strong {
  558. color: var(--light-grey-2);
  559. }
  560. .edit-playlist-modal.modal .modal-card-body {
  561. .left-section {
  562. #playlist-info-section {
  563. background-color: var(--dark-grey-3) !important;
  564. border: 0;
  565. }
  566. .tabs-container {
  567. background-color: transparent !important;
  568. .tab-selection .button {
  569. background: var(--dark-grey);
  570. color: var(--white);
  571. }
  572. .tab {
  573. background-color: var(--dark-grey-3) !important;
  574. border: 0 !important;
  575. }
  576. }
  577. }
  578. .right-section .section {
  579. border-radius: @border-radius;
  580. }
  581. }
  582. }
  583. .controls {
  584. display: flex;
  585. a {
  586. display: flex;
  587. align-items: center;
  588. }
  589. }
  590. .tabs-container {
  591. .tab-selection {
  592. display: flex;
  593. margin: 24px 10px 0 10px;
  594. max-width: 100%;
  595. .button {
  596. border-radius: @border-radius @border-radius 0 0;
  597. border: 0;
  598. text-transform: uppercase;
  599. font-size: 14px;
  600. color: var(--dark-grey-3);
  601. background-color: var(--light-grey-2);
  602. flex-grow: 1;
  603. height: 32px;
  604. &:not(:first-of-type) {
  605. margin-left: 5px;
  606. }
  607. }
  608. .selected {
  609. background-color: var(--primary-color) !important;
  610. color: var(--white) !important;
  611. font-weight: 600;
  612. }
  613. }
  614. .tab {
  615. border: 1px solid var(--light-grey-3);
  616. border-radius: 0 0 @border-radius @border-radius;
  617. }
  618. }
  619. .edit-playlist-modal {
  620. &.view-only {
  621. height: auto !important;
  622. .left-section {
  623. flex-basis: 100% !important;
  624. }
  625. .right-section {
  626. max-height: unset !important;
  627. }
  628. :deep(.section) {
  629. max-width: 100% !important;
  630. }
  631. }
  632. .nothing-here-text {
  633. display: flex;
  634. align-items: center;
  635. justify-content: center;
  636. }
  637. .label {
  638. font-size: 1rem;
  639. font-weight: normal;
  640. }
  641. .input-with-button .button {
  642. width: 150px;
  643. }
  644. .left-section {
  645. #playlist-info-section {
  646. border: 1px solid var(--light-grey-3);
  647. border-radius: @border-radius;
  648. padding: 15px !important;
  649. h3 {
  650. font-weight: 600;
  651. font-size: 30px;
  652. }
  653. h5 {
  654. font-size: 18px;
  655. }
  656. h3,
  657. h5 {
  658. margin: 0;
  659. }
  660. }
  661. }
  662. }
  663. </style>