station.ts 5.0 KB

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