1
0

Request.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. <template>
  2. <div class="station-playlists">
  3. <p class="top-info has-text-centered">
  4. Search for songs to add to queue or auto request songs from
  5. playlists
  6. </p>
  7. <div class="tabs-container">
  8. <div class="tab-selection">
  9. <button
  10. class="button is-default"
  11. ref="songs-tab"
  12. :class="{ selected: tab === 'songs' }"
  13. @click="showTab('songs')"
  14. >
  15. Songs
  16. </button>
  17. <button
  18. v-if="!disableAutoRequest"
  19. class="button is-default"
  20. ref="autorequest-tab"
  21. :class="{ selected: tab === 'autorequest' }"
  22. @click="showTab('autorequest')"
  23. >
  24. Autorequest
  25. </button>
  26. <button
  27. v-else
  28. class="button is-default disabled"
  29. content="Only available on station pages"
  30. v-tippy
  31. >
  32. Autorequest
  33. </button>
  34. </div>
  35. <div class="tab" v-show="tab === 'songs'">
  36. <div class="musare-songs">
  37. <label class="label"> Search for a song on Musare </label>
  38. <div class="control is-grouped input-with-button">
  39. <p class="control is-expanded">
  40. <input
  41. class="input"
  42. type="text"
  43. placeholder="Enter your song query here..."
  44. v-model="musareSearch.query"
  45. @keyup.enter="searchForMusareSongs(1)"
  46. />
  47. </p>
  48. <p class="control">
  49. <a
  50. class="button is-info"
  51. @click="searchForMusareSongs(1)"
  52. ><i class="material-icons icon-with-button"
  53. >search</i
  54. >Search</a
  55. >
  56. </p>
  57. </div>
  58. <div v-if="musareSearch.results.length > 0">
  59. <song-item
  60. v-for="song in musareSearch.results"
  61. :key="song._id"
  62. :song="song"
  63. >
  64. <template #actions>
  65. <transition
  66. name="musare-search-query-actions"
  67. mode="out-in"
  68. >
  69. <i
  70. v-if="
  71. songsInQueue.indexOf(
  72. song.youtubeId
  73. ) !== -1
  74. "
  75. class="material-icons added-to-playlist-icon"
  76. content="Song is already in queue"
  77. v-tippy
  78. >done</i
  79. >
  80. <i
  81. v-else
  82. class="material-icons add-to-queue-icon"
  83. @click="addSongToQueue(song.youtubeId)"
  84. content="Add Song to Queue"
  85. v-tippy
  86. >queue</i
  87. >
  88. </transition>
  89. </template>
  90. </song-item>
  91. <button
  92. v-if="musareResultsLeftCount > 0"
  93. class="button is-primary load-more-button"
  94. @click="searchForMusareSongs(musareSearch.page + 1)"
  95. >
  96. Load {{ nextPageMusareResultsCount }} more results
  97. </button>
  98. </div>
  99. </div>
  100. <div class="youtube-search">
  101. <label class="label"> Search for a song on YouTube </label>
  102. <div class="control is-grouped input-with-button">
  103. <p class="control is-expanded">
  104. <input
  105. class="input"
  106. type="text"
  107. placeholder="Enter your YouTube query here..."
  108. v-model="youtubeSearch.songs.query"
  109. autofocus
  110. @keyup.enter="searchForSongs()"
  111. />
  112. </p>
  113. <p class="control">
  114. <a
  115. class="button is-info"
  116. @click.prevent="searchForSongs()"
  117. ><i class="material-icons icon-with-button"
  118. >search</i
  119. >Search</a
  120. >
  121. </p>
  122. </div>
  123. <div
  124. v-if="youtubeSearch.songs.results.length > 0"
  125. id="song-query-results"
  126. >
  127. <search-query-item
  128. v-for="(result, index) in youtubeSearch.songs
  129. .results"
  130. :key="result.id"
  131. :result="result"
  132. >
  133. <template #actions>
  134. <transition
  135. name="youtube-search-query-actions"
  136. mode="out-in"
  137. >
  138. <i
  139. v-if="
  140. songsInQueue.indexOf(result.id) !==
  141. -1
  142. "
  143. class="material-icons added-to-playlist-icon"
  144. content="Song is already in queue"
  145. v-tippy
  146. >done</i
  147. >
  148. <i
  149. v-else
  150. class="material-icons add-to-queue-icon"
  151. @click="
  152. addSongToQueue(result.id, index)
  153. "
  154. content="Add Song to Queue"
  155. v-tippy
  156. >queue</i
  157. >
  158. </transition>
  159. </template>
  160. </search-query-item>
  161. <a
  162. class="button is-primary load-more-button"
  163. @click.prevent="loadMoreSongs()"
  164. >
  165. Load more...
  166. </a>
  167. </div>
  168. </div>
  169. </div>
  170. <playlist-tab-base
  171. v-if="!disableAutoRequest"
  172. class="tab"
  173. v-show="tab === 'autorequest'"
  174. :type="'autorequest'"
  175. :sector="sector"
  176. @selected="autoRequestSong()"
  177. :modal-uuid="modalUuid"
  178. />
  179. </div>
  180. </div>
  181. </template>
  182. <script>
  183. import { mapActions, mapState, mapGetters } from "vuex";
  184. import Toast from "toasters";
  185. import SongItem from "@/components/SongItem.vue";
  186. import SearchQueryItem from "@/components/SearchQueryItem.vue";
  187. import PlaylistTabBase from "@/components/PlaylistTabBase.vue";
  188. import SearchYoutube from "@/mixins/SearchYoutube.vue";
  189. import SearchMusare from "@/mixins/SearchMusare.vue";
  190. export default {
  191. components: {
  192. SongItem,
  193. SearchQueryItem,
  194. PlaylistTabBase
  195. },
  196. mixins: [SearchYoutube, SearchMusare],
  197. props: {
  198. modalUuid: { type: String, default: "" },
  199. sector: { type: String, default: "station" },
  200. disableAutoRequest: { type: Boolean, default: false }
  201. },
  202. data() {
  203. return {
  204. tab: "songs"
  205. };
  206. },
  207. computed: {
  208. station: {
  209. get() {
  210. if (this.sector === "manageStation")
  211. return this.$store.state.modals.manageStation[
  212. this.modalUuid
  213. ].station;
  214. return this.$store.state.station.station;
  215. },
  216. set(station) {
  217. if (this.sector === "manageStation")
  218. this.$store.commit(
  219. `modals/manageStation/${this.modalUuid}/updateStation`,
  220. station
  221. );
  222. else this.$store.commit("station/updateStation", station);
  223. }
  224. },
  225. blacklist: {
  226. get() {
  227. if (this.sector === "manageStation")
  228. return this.$store.state.modals.manageStation[
  229. this.modalUuid
  230. ].blacklist;
  231. return this.$store.state.station.blacklist;
  232. },
  233. set(blacklist) {
  234. if (this.sector === "manageStation")
  235. this.$store.commit(
  236. `modals/manageStation/${this.modalUuid}/setBlacklist`,
  237. blacklist
  238. );
  239. else this.$store.commit("station/setBlacklist", blacklist);
  240. }
  241. },
  242. songsList: {
  243. get() {
  244. if (this.sector === "manageStation")
  245. return this.$store.state.modals.manageStation[
  246. this.modalUuid
  247. ].songsList;
  248. return this.$store.state.station.songsList;
  249. },
  250. set(songsList) {
  251. if (this.sector === "manageStation")
  252. this.$store.commit(
  253. `modals/manageStation/${this.modalUuid}/updateSongsList`,
  254. songsList
  255. );
  256. else this.$store.commit("station/updateSongsList", songsList);
  257. }
  258. },
  259. musareResultsLeftCount() {
  260. return this.musareSearch.count - this.musareSearch.results.length;
  261. },
  262. nextPageMusareResultsCount() {
  263. return Math.min(
  264. this.musareSearch.pageSize,
  265. this.musareResultsLeftCount
  266. );
  267. },
  268. songsInQueue() {
  269. if (this.station.currentSong)
  270. return this.songsList
  271. .map(song => song.youtubeId)
  272. .concat(this.station.currentSong.youtubeId);
  273. return this.songsList.map(song => song.youtubeId);
  274. },
  275. currentUserQueueSongs() {
  276. return this.songsList.filter(
  277. queueSong => queueSong.requestedBy === this.userId
  278. ).length;
  279. },
  280. ...mapState("user", {
  281. loggedIn: state => state.auth.loggedIn,
  282. role: state => state.auth.role,
  283. userId: state => state.auth.userId
  284. }),
  285. ...mapState("station", {
  286. autoRequest: state => state.autoRequest,
  287. autoRequestLock: state => state.autoRequestLock
  288. }),
  289. ...mapGetters({
  290. socket: "websockets/getSocket"
  291. })
  292. },
  293. mounted() {
  294. this.showTab("songs");
  295. this.socket.on("event:station.queue.updated", () =>
  296. this.autoRequestSong()
  297. );
  298. },
  299. methods: {
  300. showTab(tab) {
  301. this.$refs[`${tab}-tab`].scrollIntoView({ block: "nearest" });
  302. this.tab = tab;
  303. },
  304. autoRequestSong() {
  305. if (
  306. !this.autoRequestLock &&
  307. this.songsList.length < 50 &&
  308. this.currentUserQueueSongs <
  309. this.station.requests.limit * 0.5 &&
  310. this.autoRequest.length > 0
  311. ) {
  312. const selectedPlaylist =
  313. this.autoRequest[
  314. Math.floor(Math.random() * this.autoRequest.length)
  315. ];
  316. if (selectedPlaylist._id && selectedPlaylist.songs.length > 0) {
  317. const selectedSong =
  318. selectedPlaylist.songs[
  319. Math.floor(
  320. Math.random() * selectedPlaylist.songs.length
  321. )
  322. ];
  323. if (selectedSong.youtubeId) {
  324. this.updateAutoRequestLock(true);
  325. this.socket.dispatch(
  326. "stations.addToQueue",
  327. this.station._id,
  328. selectedSong.youtubeId,
  329. data => {
  330. this.updateAutoRequestLock(false);
  331. if (data.status !== "success")
  332. this.autoRequestSong();
  333. }
  334. );
  335. }
  336. }
  337. }
  338. },
  339. addSongToQueue(youtubeId, index) {
  340. this.socket.dispatch(
  341. "stations.addToQueue",
  342. this.station._id,
  343. youtubeId,
  344. res => {
  345. if (res.status !== "success")
  346. new Toast(`Error: ${res.message}`);
  347. else {
  348. if (index)
  349. this.youtubeSearch.songs.results[
  350. index
  351. ].isAddedToQueue = true;
  352. new Toast(res.message);
  353. }
  354. }
  355. );
  356. },
  357. ...mapActions("station", ["updateAutoRequest", "updateAutoRequestLock"])
  358. }
  359. };
  360. </script>
  361. <style lang="less" scoped>
  362. .night-mode {
  363. .tabs-container .tab-selection .button {
  364. background: var(--dark-grey) !important;
  365. color: var(--white) !important;
  366. }
  367. }
  368. :deep(#create-new-playlist-button) {
  369. width: 100%;
  370. }
  371. .station-playlists {
  372. .top-info {
  373. font-size: 15px;
  374. margin-bottom: 15px;
  375. }
  376. .tabs-container {
  377. .tab-selection {
  378. display: flex;
  379. overflow-x: auto;
  380. .button {
  381. border-radius: 0;
  382. border: 0;
  383. text-transform: uppercase;
  384. font-size: 14px;
  385. color: var(--dark-grey-3);
  386. background-color: var(--light-grey-2);
  387. flex-grow: 1;
  388. height: 32px;
  389. &:not(:first-of-type) {
  390. margin-left: 5px;
  391. }
  392. }
  393. .selected {
  394. background-color: var(--primary-color) !important;
  395. color: var(--white) !important;
  396. font-weight: 600;
  397. }
  398. }
  399. .tab {
  400. padding: 10px 0;
  401. border-radius: 0;
  402. .item.item-draggable:not(:last-of-type) {
  403. margin-bottom: 10px;
  404. }
  405. .load-more-button {
  406. width: 100%;
  407. margin-top: 10px;
  408. }
  409. }
  410. }
  411. }
  412. .youtube-search {
  413. margin-top: 10px;
  414. .search-query-item:not(:last-of-type) {
  415. margin-bottom: 10px;
  416. }
  417. }
  418. </style>