main.ts 9.4 KB

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