main.ts 9.3 KB

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