station.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import { defineStore } from "pinia";
  2. import { Playlist } from "@/types/playlist";
  3. import { Song, CurrentSong } from "@/types/song";
  4. import { Station } from "@/types/station";
  5. import { User } from "@/types/user";
  6. import { useWebsocketsStore } from "@/stores/websockets";
  7. export const useStationStore = defineStore("station", {
  8. state: (): {
  9. station: Station;
  10. autoRequest: Playlist[];
  11. autoRequestLock: boolean;
  12. userCount: number;
  13. users: {
  14. loggedIn: User[];
  15. loggedOut: User[];
  16. };
  17. currentSong: CurrentSong | undefined;
  18. nextSong: Song | undefined | null;
  19. songsList: Song[];
  20. stationPaused: boolean;
  21. localPaused: boolean;
  22. noSong: boolean;
  23. autofill: Playlist[];
  24. blacklist: Playlist[];
  25. mediaModalPlayingAudio: boolean;
  26. permissions: Record<string, boolean>;
  27. history: any[];
  28. } => ({
  29. station: {},
  30. autoRequest: [],
  31. autoRequestLock: false,
  32. userCount: 0,
  33. users: {
  34. loggedIn: [],
  35. loggedOut: []
  36. },
  37. currentSong: {},
  38. nextSong: null,
  39. songsList: [],
  40. stationPaused: true,
  41. localPaused: false,
  42. noSong: true,
  43. autofill: [],
  44. blacklist: [],
  45. mediaModalPlayingAudio: false,
  46. permissions: {},
  47. history: []
  48. }),
  49. actions: {
  50. joinStation(station) {
  51. this.station = { ...station };
  52. },
  53. leaveStation() {
  54. this.station = {};
  55. this.autoRequest = [];
  56. this.autoRequestLock = false;
  57. this.editing = {};
  58. this.userCount = 0;
  59. this.users = {
  60. loggedIn: [],
  61. loggedOut: []
  62. };
  63. this.currentSong = {};
  64. this.nextSong = null;
  65. this.songsList = [];
  66. this.stationPaused = true;
  67. this.localPaused = false;
  68. this.noSong = true;
  69. this.autofill = [];
  70. this.blacklist = [];
  71. this.permissions = {};
  72. },
  73. editStation(station) {
  74. this.editing = { ...station };
  75. },
  76. updateStation(station) {
  77. this.station = { ...this.station, ...station };
  78. },
  79. updateUserCount(userCount) {
  80. this.userCount = userCount;
  81. },
  82. updateUsers(users) {
  83. this.users = users;
  84. },
  85. updateCurrentSong(currentSong) {
  86. this.currentSong = currentSong;
  87. },
  88. updateNextSong(nextSong) {
  89. this.nextSong = nextSong;
  90. },
  91. updateSongsList(songsList) {
  92. this.songsList = songsList;
  93. },
  94. repositionSongInList(song) {
  95. if (
  96. this.songsList[song.newIndex] &&
  97. this.songsList[song.newIndex].mediaSource === song.mediaSource
  98. )
  99. return;
  100. this.songsList.splice(
  101. song.newIndex,
  102. 0,
  103. this.songsList.splice(song.oldIndex, 1)[0]
  104. );
  105. },
  106. updateStationPaused(stationPaused) {
  107. this.stationPaused = stationPaused;
  108. },
  109. updateLocalPaused(localPaused) {
  110. this.localPaused = localPaused;
  111. },
  112. updateNoSong(noSong) {
  113. this.noSong = noSong;
  114. },
  115. updateAutoRequest(playlists) {
  116. this.autoRequest = playlists;
  117. },
  118. updateAutoRequestLock(lock) {
  119. this.autoRequestLock = lock;
  120. },
  121. updateIfStationIsFavorited(isFavorited) {
  122. this.station.isFavorited = isFavorited;
  123. },
  124. setAutofillPlaylists(autofillPlaylists) {
  125. this.autofill = JSON.parse(JSON.stringify(autofillPlaylists));
  126. },
  127. setBlacklist(blacklist) {
  128. this.blacklist = JSON.parse(JSON.stringify(blacklist));
  129. },
  130. updateCurrentSongRatings(songRatings) {
  131. this.currentSong.likes = songRatings.likes;
  132. this.currentSong.dislikes = songRatings.dislikes;
  133. },
  134. updateOwnCurrentSongRatings(ownSongRatings) {
  135. this.currentSong.liked = ownSongRatings.liked;
  136. this.currentSong.disliked = ownSongRatings.disliked;
  137. },
  138. updateCurrentSongSkipVotes({ skipVotes, skipVotesCurrent, voted }) {
  139. this.currentSong.skipVotes = skipVotes;
  140. if (skipVotesCurrent !== null)
  141. this.currentSong.skipVotesCurrent = skipVotesCurrent;
  142. this.currentSong.voted = voted;
  143. },
  144. addAutorequestPlaylists(playlists) {
  145. playlists.forEach(playlist => {
  146. this.autoRequest.push(playlist);
  147. });
  148. this.updateAutorequestLocalStorage();
  149. },
  150. addPlaylistToAutoRequest(playlist) {
  151. this.autoRequest.push(playlist);
  152. this.updateAutorequestLocalStorage();
  153. },
  154. removePlaylistFromAutoRequest(playlistId) {
  155. this.autoRequest.forEach((playlist, index) => {
  156. if (playlist._id === playlistId) {
  157. this.autoRequest.splice(index, 1);
  158. }
  159. });
  160. this.updateAutorequestLocalStorage();
  161. },
  162. updateAutorequestLocalStorage() {
  163. const key = `autorequest-${this.station._id}`;
  164. const playlistIds = Array.from(
  165. new Set(this.autoRequest.map(playlist => playlist._id))
  166. );
  167. const value = {
  168. updatedAt: new Date(),
  169. playlistIds
  170. };
  171. localStorage.setItem(key, JSON.stringify(value));
  172. },
  173. updateMediaModalPlayingAudio(mediaModalPlayingAudio) {
  174. this.mediaModalPlayingAudio = mediaModalPlayingAudio;
  175. },
  176. hasPermission(permission) {
  177. return !!(this.permissions && this.permissions[permission]);
  178. },
  179. updatePermissions() {
  180. return new Promise(resolve => {
  181. const { socket } = useWebsocketsStore();
  182. socket.dispatch(
  183. "utils.getPermissions",
  184. this.station._id,
  185. res => {
  186. this.permissions = res.data.permissions;
  187. resolve(this.permissions);
  188. }
  189. );
  190. });
  191. },
  192. addDj(user) {
  193. this.station.djs.push(user);
  194. },
  195. removeDj(user) {
  196. this.station.djs.forEach((dj, index) => {
  197. if (dj._id === user._id) {
  198. this.station.djs.splice(index, 1);
  199. }
  200. });
  201. },
  202. setHistory(history) {
  203. this.history = history;
  204. },
  205. addHistoryItem(historyItem) {
  206. this.history.unshift(historyItem);
  207. }
  208. }
  209. });