main.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /* eslint-disable vue/one-component-per-file */
  2. import { createApp } from "vue";
  3. import VueTippy, { Tippy } from "vue-tippy";
  4. import { createRouter, createWebHistory } from "vue-router";
  5. import { createPinia } from "pinia";
  6. import Toast from "toasters";
  7. import { useConfigStore } from "@/stores/config";
  8. import { useUserAuthStore } from "@/stores/userAuth";
  9. import { useUserPreferencesStore } from "@/stores/userPreferences";
  10. import { useModalsStore } from "@/stores/modals";
  11. import { useWebsocketsStore } from "@/stores/websockets";
  12. import ms from "@/ms";
  13. import i18n from "@/i18n";
  14. import AppComponent from "./App.vue";
  15. const handleMetadata = attrs => {
  16. const configStore = useConfigStore();
  17. document.title = `${configStore.sitename} | ${attrs.title}`;
  18. };
  19. const app = createApp(AppComponent);
  20. app.use(i18n);
  21. app.use(VueTippy, {
  22. directive: "tippy", // => v-tippy
  23. flipDuration: 0,
  24. popperOptions: {
  25. modifiers: {
  26. preventOverflow: {
  27. enabled: true
  28. }
  29. }
  30. },
  31. allowHTML: true,
  32. defaultProps: { animation: "scale", touch: "hold" }
  33. });
  34. app.component("Tippy", Tippy);
  35. app.component("PageMetadata", {
  36. watch: {
  37. $attrs: {
  38. // eslint-disable-next-line vue/no-arrow-functions-in-watch
  39. handler: attrs => {
  40. handleMetadata(attrs);
  41. },
  42. deep: true,
  43. immediate: true
  44. }
  45. },
  46. render() {
  47. return null;
  48. }
  49. });
  50. app.directive("scroll", {
  51. mounted(el, binding) {
  52. const f = (evt: Event) => {
  53. clearTimeout(window.scrollDebounceId);
  54. window.scrollDebounceId = setTimeout(() => {
  55. if (binding.value(evt, el)) {
  56. document.body.removeEventListener("scroll", f);
  57. }
  58. }, 200);
  59. };
  60. document.body.addEventListener("scroll", f);
  61. }
  62. });
  63. app.directive("focus", {
  64. mounted(el) {
  65. window.focusedElementBefore = document.activeElement;
  66. el.focus();
  67. }
  68. });
  69. const router = createRouter({
  70. history: createWebHistory(),
  71. routes: [
  72. {
  73. name: "home",
  74. path: "/",
  75. component: () => import("@/pages/Home.vue")
  76. },
  77. {
  78. path: "/login",
  79. name: "login",
  80. redirect: "/"
  81. },
  82. {
  83. path: "/register",
  84. name: "register",
  85. redirect: "/"
  86. },
  87. {
  88. path: "/404",
  89. alias: ["/:pathMatch(.*)*"],
  90. component: () => import("@/pages/404.vue")
  91. },
  92. {
  93. path: "/terms",
  94. component: () => import("@/pages/Terms.vue")
  95. },
  96. {
  97. path: "/privacy",
  98. component: () => import("@/pages/Privacy.vue")
  99. },
  100. {
  101. path: "/team",
  102. component: () => import("@/pages/Team.vue")
  103. },
  104. {
  105. path: "/news",
  106. component: () => import("@/pages/News.vue")
  107. },
  108. {
  109. path: "/about",
  110. component: () => import("@/pages/About.vue")
  111. },
  112. {
  113. name: "profile",
  114. path: "/u/:username",
  115. component: () => import("@/pages/Profile/index.vue")
  116. },
  117. {
  118. path: "/settings",
  119. component: () => import("@/pages/Settings/index.vue"),
  120. meta: {
  121. loginRequired: true
  122. }
  123. },
  124. {
  125. path: "/reset_password",
  126. component: () => import("@/pages/ResetPassword.vue")
  127. },
  128. {
  129. path: "/set_password",
  130. props: { mode: "set" },
  131. component: () => import("@/pages/ResetPassword.vue"),
  132. meta: {
  133. loginRequired: true
  134. }
  135. },
  136. {
  137. path: "/admin",
  138. name: "admin",
  139. component: () => import("@/pages/Admin/index.vue"),
  140. children: [
  141. {
  142. path: "songs",
  143. component: () => import("@/pages/Admin/Songs/index.vue"),
  144. meta: { permissionRequired: "admin.view.songs" }
  145. },
  146. {
  147. path: "songs/import",
  148. component: () => import("@/pages/Admin/Songs/Import.vue"),
  149. meta: { permissionRequired: "admin.view.import" }
  150. },
  151. {
  152. path: "reports",
  153. component: () => import("@/pages/Admin/Reports.vue"),
  154. meta: { permissionRequired: "admin.view.reports" }
  155. },
  156. {
  157. path: "stations",
  158. component: () => import("@/pages/Admin/Stations.vue"),
  159. meta: { permissionRequired: "admin.view.stations" }
  160. },
  161. {
  162. path: "playlists",
  163. component: () => import("@/pages/Admin/Playlists.vue"),
  164. meta: { permissionRequired: "admin.view.playlists" }
  165. },
  166. {
  167. path: "users",
  168. component: () => import("@/pages/Admin/Users/index.vue"),
  169. meta: { permissionRequired: "admin.view.users" }
  170. },
  171. {
  172. path: "users/data-requests",
  173. component: () =>
  174. import("@/pages/Admin/Users/DataRequests.vue"),
  175. meta: { permissionRequired: "admin.view.dataRequests" }
  176. },
  177. {
  178. path: "users/punishments",
  179. component: () =>
  180. import("@/pages/Admin/Users/Punishments.vue"),
  181. meta: {
  182. permissionRequired: "admin.view.punishments"
  183. }
  184. },
  185. {
  186. path: "news",
  187. component: () => import("@/pages/Admin/News.vue"),
  188. meta: { permissionRequired: "admin.view.news" }
  189. },
  190. {
  191. path: "statistics",
  192. component: () => import("@/pages/Admin/Statistics.vue"),
  193. meta: {
  194. permissionRequired: "admin.view.statistics"
  195. }
  196. },
  197. {
  198. path: "youtube",
  199. component: () => import("@/pages/Admin/YouTube/index.vue"),
  200. meta: { permissionRequired: "admin.view.youtube" }
  201. },
  202. {
  203. path: "youtube/videos",
  204. component: () => import("@/pages/Admin/YouTube/Videos.vue"),
  205. meta: {
  206. permissionRequired: "admin.view.youtubeVideos"
  207. }
  208. },
  209. {
  210. path: "youtube/channels",
  211. component: () =>
  212. import("@/pages/Admin/YouTube/Channels.vue"),
  213. meta: {
  214. permissionRequired: "admin.view.youtubeChannels"
  215. }
  216. },
  217. {
  218. path: "soundcloud",
  219. component: () =>
  220. import("@/pages/Admin/SoundCloud/index.vue"),
  221. meta: { permissionRequired: "admin.view.soundcloud" }
  222. },
  223. {
  224. path: "soundcloud/tracks",
  225. component: () =>
  226. import("@/pages/Admin/SoundCloud/Tracks.vue"),
  227. meta: {
  228. permissionRequired: "admin.view.soundcloudTracks"
  229. }
  230. }
  231. ],
  232. meta: {
  233. permissionRequired: "admin.view"
  234. }
  235. },
  236. {
  237. name: "station",
  238. path: "/:id",
  239. component: () => import("@/pages//Station/index.vue")
  240. }
  241. ]
  242. });
  243. app.use(createPinia());
  244. const { createSocket } = useWebsocketsStore();
  245. createSocket().then(async socket => {
  246. const configStore = useConfigStore();
  247. const userAuthStore = useUserAuthStore();
  248. const modalsStore = useModalsStore();
  249. router.beforeEach((to, from, next) => {
  250. if (window.stationInterval) {
  251. clearInterval(window.stationInterval);
  252. window.stationInterval = 0;
  253. }
  254. // if (to.name === "station") {
  255. // modalsStore.closeModal("manageStation");
  256. // }
  257. modalsStore.closeAllModals();
  258. if (socket.ready && to.fullPath !== from.fullPath) {
  259. socket.clearCallbacks();
  260. socket.destroyListeners();
  261. }
  262. if (to.query.toast) {
  263. const toast =
  264. typeof to.query.toast === "string"
  265. ? { content: to.query.toast, timeout: 20000 }
  266. : { ...to.query.toast };
  267. new Toast(toast);
  268. const { query } = to;
  269. delete query.toast;
  270. next({ ...to, query });
  271. } else if (
  272. to.meta.loginRequired ||
  273. to.meta.permissionRequired ||
  274. to.meta.guestsOnly
  275. ) {
  276. const gotData = () => {
  277. if (to.meta.loginRequired && !userAuthStore.loggedIn)
  278. next({ path: "/login" });
  279. else if (
  280. to.meta.permissionRequired &&
  281. !userAuthStore.hasPermission(
  282. `${to.meta.permissionRequired}`
  283. )
  284. ) {
  285. if (
  286. to.path.startsWith("/admin") &&
  287. to.path !== "/admin/songs"
  288. )
  289. next({ path: "/admin/songs" });
  290. else next({ path: "/" });
  291. } else if (to.meta.guestsOnly && userAuthStore.loggedIn)
  292. next({ path: "/" });
  293. else next();
  294. };
  295. if (userAuthStore.gotData && userAuthStore.gotPermissions)
  296. gotData();
  297. else {
  298. const unsubscribe = userAuthStore.$onAction(
  299. ({ name, after, onError }) => {
  300. if (
  301. name === "authData" ||
  302. name === "updatePermissions"
  303. ) {
  304. after(() => {
  305. if (
  306. userAuthStore.gotData &&
  307. userAuthStore.gotPermissions
  308. )
  309. gotData();
  310. unsubscribe();
  311. });
  312. onError(() => {
  313. unsubscribe();
  314. });
  315. }
  316. }
  317. );
  318. }
  319. } else next();
  320. });
  321. app.use(router);
  322. socket.on("ready", res => {
  323. const { loggedIn, role, username, userId, email } = res.user;
  324. userAuthStore.authData({
  325. loggedIn,
  326. role,
  327. username,
  328. email,
  329. userId
  330. });
  331. if (configStore.experimental.media_session) ms.init();
  332. });
  333. socket.on("keep.event:user.banned", res =>
  334. userAuthStore.banUser(res.data.ban)
  335. );
  336. socket.on("keep.event:user.username.updated", res =>
  337. userAuthStore.updateUsername(res.data.username)
  338. );
  339. socket.on("keep.event:user.preferences.updated", res => {
  340. const { preferences } = res.data;
  341. const {
  342. changeAutoSkipDisliked,
  343. changeNightmode,
  344. changeActivityLogPublic,
  345. changeAnonymousSongRequests,
  346. changeActivityWatch
  347. } = useUserPreferencesStore();
  348. if (preferences.autoSkipDisliked !== undefined)
  349. changeAutoSkipDisliked(preferences.autoSkipDisliked);
  350. if (preferences.nightmode !== undefined) {
  351. changeNightmode(preferences.nightmode);
  352. }
  353. if (preferences.activityLogPublic !== undefined)
  354. changeActivityLogPublic(preferences.activityLogPublic);
  355. if (preferences.anonymousSongRequests !== undefined)
  356. changeAnonymousSongRequests(preferences.anonymousSongRequests);
  357. if (preferences.activityWatch !== undefined)
  358. changeActivityWatch(preferences.activityWatch);
  359. });
  360. socket.on("keep.event:user.role.updated", res => {
  361. userAuthStore.updateRole(res.data.role);
  362. userAuthStore.updatePermissions().then(() => {
  363. const { meta } = router.currentRoute.value;
  364. if (
  365. meta &&
  366. meta.permissionRequired &&
  367. !userAuthStore.hasPermission(`${meta.permissionRequired}`)
  368. )
  369. router.push({
  370. path: "/",
  371. query: {
  372. toast: "You no longer have access to the page you were viewing."
  373. }
  374. });
  375. });
  376. });
  377. app.mount("#root");
  378. });