Playlists.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <template>
  2. <div>
  3. <page-metadata title="Admin | Playlists" />
  4. <div class="container">
  5. <button
  6. class="button is-primary"
  7. @click="deleteOrphanedStationPlaylists()"
  8. >
  9. Delete orphaned station playlists
  10. </button>
  11. <button
  12. class="button is-primary"
  13. @click="deleteOrphanedGenrePlaylists()"
  14. >
  15. Delete orphaned genre playlists
  16. </button>
  17. <button
  18. class="button is-primary"
  19. @click="requestOrphanedPlaylistSongs()"
  20. >
  21. Request orphaned playlist songs
  22. </button>
  23. <button
  24. class="button is-primary"
  25. @click="clearAndRefillAllStationPlaylists()"
  26. >
  27. Clear and refill all station playlists
  28. </button>
  29. <button
  30. class="button is-primary"
  31. @click="clearAndRefillAllGenrePlaylists()"
  32. >
  33. Clear and refill all genre playlists
  34. </button>
  35. <br />
  36. <br />
  37. <table class="table is-striped">
  38. <thead>
  39. <tr>
  40. <td>Display name</td>
  41. <td>Type</td>
  42. <td>Is user modifiable</td>
  43. <td>Privacy</td>
  44. <td>Songs #</td>
  45. <td>Playlist length</td>
  46. <td>Created by</td>
  47. <td>Created at</td>
  48. <td>Created for</td>
  49. <td>Playlist id</td>
  50. <td>Options</td>
  51. </tr>
  52. </thead>
  53. <tbody>
  54. <tr v-for="playlist in playlists" :key="playlist._id">
  55. <td>{{ playlist.displayName }}</td>
  56. <td>{{ playlist.type }}</td>
  57. <td>{{ playlist.isUserModifiable }}</td>
  58. <td>{{ playlist.privacy }}</td>
  59. <td>{{ playlist.songs.length }}</td>
  60. <td>{{ totalLengthForPlaylist(playlist.songs) }}</td>
  61. <td v-if="playlist.createdBy === 'Musare'">Musare</td>
  62. <td v-else>
  63. <user-id-to-username
  64. :user-id="playlist.createdBy"
  65. :link="true"
  66. />
  67. </td>
  68. <td :title="new Date(playlist.createdAt)">
  69. {{ getDateFormatted(playlist.createdAt) }}
  70. </td>
  71. <td>{{ playlist.createdFor }}</td>
  72. <td>{{ playlist._id }}</td>
  73. <td>
  74. <button
  75. class="button is-primary"
  76. @click="edit(playlist._id)"
  77. >
  78. View
  79. </button>
  80. </td>
  81. </tr>
  82. </tbody>
  83. </table>
  84. </div>
  85. <edit-playlist v-if="modals.editPlaylist" sector="admin" />
  86. <report v-if="modals.report" />
  87. <edit-song v-if="modals.editSong" song-type="songs" />
  88. </div>
  89. </template>
  90. <script>
  91. import { mapState, mapActions, mapGetters } from "vuex";
  92. import { defineAsyncComponent } from "vue";
  93. import Toast from "toasters";
  94. import UserIdToUsername from "@/components/UserIdToUsername.vue";
  95. import ws from "@/ws";
  96. import utils from "../../../../js/utils";
  97. export default {
  98. components: {
  99. EditPlaylist: defineAsyncComponent(() =>
  100. import("@/components/modals/EditPlaylist")
  101. ),
  102. UserIdToUsername,
  103. Report: defineAsyncComponent(() =>
  104. import("@/components/modals/Report.vue")
  105. ),
  106. EditSong: defineAsyncComponent(() =>
  107. import("@/components/modals/EditSong")
  108. )
  109. },
  110. data() {
  111. return {
  112. utils
  113. };
  114. },
  115. computed: {
  116. ...mapState("modalVisibility", {
  117. modals: state => state.modals
  118. }),
  119. ...mapState("admin/playlists", {
  120. playlists: state => state.playlists
  121. }),
  122. ...mapGetters({
  123. socket: "websockets/getSocket"
  124. })
  125. },
  126. mounted() {
  127. this.socket.on("event:admin.playlist.created", res =>
  128. this.addPlaylist(res.data.playlist)
  129. );
  130. this.socket.on("event:admin.playlist.deleted", res =>
  131. this.removePlaylist(res.data.playlistId)
  132. );
  133. this.socket.on("event:admin.playlist.song.added", res =>
  134. this.addPlaylistSong({
  135. playlistId: res.data.playlistId,
  136. song: res.data.song
  137. })
  138. );
  139. this.socket.on("event:admin.playlist.song.removed", res =>
  140. this.removePlaylistSong({
  141. playlistId: res.data.playlistId,
  142. youtubeId: res.data.youtubeId
  143. })
  144. );
  145. this.socket.on("event:admin.playlist.displayName.updated", res =>
  146. this.updatePlaylistDisplayName({
  147. playlistId: res.data.playlistId,
  148. displayName: res.data.displayName
  149. })
  150. );
  151. this.socket.on("event:admin.playlist.privacy.updated", res =>
  152. this.updatePlaylistPrivacy({
  153. playlistId: res.data.playlistId,
  154. privacy: res.data.privacy
  155. })
  156. );
  157. ws.onConnect(this.init);
  158. },
  159. methods: {
  160. edit(playlistId) {
  161. this.editPlaylist(playlistId);
  162. this.openModal("editPlaylist");
  163. },
  164. init() {
  165. this.socket.dispatch("playlists.index", res => {
  166. if (res.status === "success") {
  167. this.setPlaylists(res.data.playlists);
  168. if (this.$route.query.playlistId) {
  169. const playlist = this.playlists.find(
  170. playlist =>
  171. playlist._id === this.$route.query.playlistId
  172. );
  173. if (playlist) this.edit(playlist._id);
  174. }
  175. }
  176. });
  177. this.socket.dispatch("apis.joinAdminRoom", "playlists", () => {});
  178. },
  179. getDateFormatted(createdAt) {
  180. const date = new Date(createdAt);
  181. const year = date.getFullYear();
  182. const month = `${date.getMonth() + 1}`.padStart(2, 0);
  183. const day = `${date.getDate()}`.padStart(2, 0);
  184. const hour = `${date.getHours()}`.padStart(2, 0);
  185. const minute = `${date.getMinutes()}`.padStart(2, 0);
  186. return `${year}-${month}-${day} ${hour}:${minute}`;
  187. },
  188. totalLengthForPlaylist(songs) {
  189. let length = 0;
  190. songs.forEach(song => {
  191. length += song.duration;
  192. });
  193. return this.utils.formatTimeLong(length);
  194. },
  195. deleteOrphanedStationPlaylists() {
  196. this.socket.dispatch(
  197. "playlists.deleteOrphanedStationPlaylists",
  198. res => {
  199. if (res.status === "success") new Toast(res.message);
  200. else new Toast(`Error: ${res.message}`);
  201. }
  202. );
  203. },
  204. deleteOrphanedGenrePlaylists() {
  205. this.socket.dispatch(
  206. "playlists.deleteOrphanedGenrePlaylists",
  207. res => {
  208. if (res.status === "success") new Toast(res.message);
  209. else new Toast(`Error: ${res.message}`);
  210. }
  211. );
  212. },
  213. requestOrphanedPlaylistSongs() {
  214. this.socket.dispatch(
  215. "playlists.requestOrphanedPlaylistSongs",
  216. res => {
  217. if (res.status === "success") new Toast(res.message);
  218. else new Toast(`Error: ${res.message}`);
  219. }
  220. );
  221. },
  222. clearAndRefillAllStationPlaylists() {
  223. this.socket.dispatch(
  224. "playlists.clearAndRefillAllStationPlaylists",
  225. res => {
  226. if (res.status === "success")
  227. new Toast({ content: res.message, timeout: 4000 });
  228. else
  229. new Toast({
  230. content: `Error: ${res.message}`,
  231. timeout: 4000
  232. });
  233. }
  234. );
  235. },
  236. clearAndRefillAllGenrePlaylists() {
  237. this.socket.dispatch(
  238. "playlists.clearAndRefillAllGenrePlaylists",
  239. res => {
  240. if (res.status === "success")
  241. new Toast({ content: res.message, timeout: 4000 });
  242. else
  243. new Toast({
  244. content: `Error: ${res.message}`,
  245. timeout: 4000
  246. });
  247. }
  248. );
  249. },
  250. ...mapActions("modalVisibility", ["openModal"]),
  251. ...mapActions("user/playlists", ["editPlaylist"]),
  252. ...mapActions("admin/playlists", [
  253. "addPlaylist",
  254. "setPlaylists",
  255. "removePlaylist",
  256. "addPlaylistSong",
  257. "removePlaylistSong",
  258. "updatePlaylistDisplayName",
  259. "updatePlaylistPrivacy"
  260. ])
  261. }
  262. };
  263. </script>
  264. <style lang="scss" scoped>
  265. .night-mode {
  266. .table {
  267. color: var(--light-grey-2);
  268. background-color: var(--dark-grey-3);
  269. thead tr {
  270. background: var(--dark-grey-3);
  271. td {
  272. color: var(--white);
  273. }
  274. }
  275. tbody tr:hover {
  276. background-color: var(--dark-grey-4) !important;
  277. }
  278. tbody tr:nth-child(even) {
  279. background-color: var(--dark-grey-2);
  280. }
  281. strong {
  282. color: var(--light-grey-2);
  283. }
  284. }
  285. }
  286. body {
  287. font-family: "Hind", sans-serif;
  288. }
  289. td {
  290. vertical-align: middle;
  291. }
  292. .is-primary:focus {
  293. background-color: var(--primary-color) !important;
  294. }
  295. </style>