main.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. meta: {
  128. configRequired: "mailEnabled"
  129. }
  130. },
  131. {
  132. path: "/set_password",
  133. props: { mode: "set" },
  134. component: () => import("@/pages/ResetPassword.vue"),
  135. meta: {
  136. configRequired: "mailEnabled",
  137. loginRequired: true
  138. }
  139. },
  140. {
  141. path: "/admin",
  142. name: "admin",
  143. component: () => import("@/pages/Admin/index.vue"),
  144. children: [
  145. {
  146. path: "songs",
  147. component: () => import("@/pages/Admin/Songs/index.vue"),
  148. meta: { permissionRequired: "admin.view.songs" }
  149. },
  150. {
  151. path: "songs/import",
  152. component: () => import("@/pages/Admin/Songs/Import.vue"),
  153. meta: { permissionRequired: "admin.view.import" }
  154. },
  155. {
  156. path: "reports",
  157. component: () => import("@/pages/Admin/Reports.vue"),
  158. meta: { permissionRequired: "admin.view.reports" }
  159. },
  160. {
  161. path: "stations",
  162. component: () => import("@/pages/Admin/Stations.vue"),
  163. meta: { permissionRequired: "admin.view.stations" }
  164. },
  165. {
  166. path: "playlists",
  167. component: () => import("@/pages/Admin/Playlists.vue"),
  168. meta: { permissionRequired: "admin.view.playlists" }
  169. },
  170. {
  171. path: "users",
  172. component: () => import("@/pages/Admin/Users/index.vue"),
  173. meta: { permissionRequired: "admin.view.users" }
  174. },
  175. {
  176. path: "users/data-requests",
  177. component: () =>
  178. import("@/pages/Admin/Users/DataRequests.vue"),
  179. meta: { permissionRequired: "admin.view.dataRequests" }
  180. },
  181. {
  182. path: "users/punishments",
  183. component: () =>
  184. import("@/pages/Admin/Users/Punishments.vue"),
  185. meta: {
  186. permissionRequired: "admin.view.punishments"
  187. }
  188. },
  189. {
  190. path: "news",
  191. component: () => import("@/pages/Admin/News.vue"),
  192. meta: { permissionRequired: "admin.view.news" }
  193. },
  194. {
  195. path: "statistics",
  196. component: () => import("@/pages/Admin/Statistics.vue"),
  197. meta: {
  198. permissionRequired: "admin.view.statistics"
  199. }
  200. },
  201. {
  202. path: "youtube",
  203. component: () => import("@/pages/Admin/YouTube/index.vue"),
  204. meta: { permissionRequired: "admin.view.youtube" }
  205. },
  206. {
  207. path: "youtube/videos",
  208. component: () => import("@/pages/Admin/YouTube/Videos.vue"),
  209. meta: {
  210. permissionRequired: "admin.view.youtubeVideos"
  211. }
  212. },
  213. {
  214. path: "youtube/channels",
  215. component: () =>
  216. import("@/pages/Admin/YouTube/Channels.vue"),
  217. meta: {
  218. permissionRequired: "admin.view.youtubeChannels"
  219. }
  220. },
  221. {
  222. path: "soundcloud",
  223. component: () =>
  224. import("@/pages/Admin/SoundCloud/index.vue"),
  225. meta: { permissionRequired: "admin.view.soundcloud" }
  226. },
  227. {
  228. path: "soundcloud/tracks",
  229. component: () =>
  230. import("@/pages/Admin/SoundCloud/Tracks.vue"),
  231. meta: {
  232. permissionRequired: "admin.view.soundcloudTracks"
  233. }
  234. }
  235. ],
  236. meta: {
  237. permissionRequired: "admin.view"
  238. }
  239. },
  240. {
  241. name: "station",
  242. path: "/:id",
  243. component: () => import("@/pages//Station/index.vue")
  244. }
  245. ]
  246. });
  247. app.use(createPinia());
  248. const { createSocket } = useWebsocketsStore();
  249. createSocket().then(async socket => {
  250. const configStore = useConfigStore();
  251. const userAuthStore = useUserAuthStore();
  252. const modalsStore = useModalsStore();
  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(
  292. `${to.meta.permissionRequired}`
  293. )
  294. ) {
  295. if (
  296. to.path.startsWith("/admin") &&
  297. to.path !== "/admin/songs"
  298. )
  299. next({ path: "/admin/songs" });
  300. else next({ path: "/" });
  301. } else if (to.meta.guestsOnly && userAuthStore.loggedIn)
  302. next({ path: "/" });
  303. else next();
  304. };
  305. if (userAuthStore.gotData && userAuthStore.gotPermissions)
  306. gotData();
  307. else {
  308. const unsubscribe = userAuthStore.$onAction(
  309. ({ name, after, onError }) => {
  310. if (
  311. name === "authData" ||
  312. name === "updatePermissions"
  313. ) {
  314. after(() => {
  315. if (
  316. userAuthStore.gotData &&
  317. userAuthStore.gotPermissions
  318. )
  319. gotData();
  320. unsubscribe();
  321. });
  322. onError(() => {
  323. unsubscribe();
  324. });
  325. }
  326. }
  327. );
  328. }
  329. } else next();
  330. });
  331. app.use(router);
  332. socket.on("ready", res => {
  333. const { loggedIn, role, username, userId, email } = res.user;
  334. userAuthStore.authData({
  335. loggedIn,
  336. role,
  337. username,
  338. email,
  339. userId
  340. });
  341. if (loggedIn) {
  342. userAuthStore.resetCookieExpiration();
  343. }
  344. if (configStore.experimental.media_session) ms.initialize();
  345. else ms.uninitialize();
  346. });
  347. socket.on("keep.event:user.banned", res =>
  348. userAuthStore.banUser(res.data.ban)
  349. );
  350. socket.on("keep.event:user.username.updated", res =>
  351. userAuthStore.updateUsername(res.data.username)
  352. );
  353. socket.on("keep.event:user.preferences.updated", res => {
  354. const { preferences } = res.data;
  355. const {
  356. changeAutoSkipDisliked,
  357. changeNightmode,
  358. changeActivityLogPublic,
  359. changeAnonymousSongRequests,
  360. changeActivityWatch
  361. } = useUserPreferencesStore();
  362. if (preferences.autoSkipDisliked !== undefined)
  363. changeAutoSkipDisliked(preferences.autoSkipDisliked);
  364. if (preferences.nightmode !== undefined) {
  365. changeNightmode(preferences.nightmode);
  366. }
  367. if (preferences.activityLogPublic !== undefined)
  368. changeActivityLogPublic(preferences.activityLogPublic);
  369. if (preferences.anonymousSongRequests !== undefined)
  370. changeAnonymousSongRequests(preferences.anonymousSongRequests);
  371. if (preferences.activityWatch !== undefined)
  372. changeActivityWatch(preferences.activityWatch);
  373. });
  374. socket.on("keep.event:user.role.updated", res => {
  375. userAuthStore.updateRole(res.data.role);
  376. userAuthStore.updatePermissions().then(() => {
  377. const { meta } = router.currentRoute.value;
  378. if (
  379. meta &&
  380. meta.permissionRequired &&
  381. !userAuthStore.hasPermission(`${meta.permissionRequired}`)
  382. )
  383. router.push({
  384. path: "/",
  385. query: {
  386. toast: "You no longer have access to the page you were viewing."
  387. }
  388. });
  389. });
  390. });
  391. app.mount("#root");
  392. });