main.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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: "passwordResetEnabled"
  129. }
  130. },
  131. {
  132. path: "/admin",
  133. name: "admin",
  134. component: () => import("@/pages/Admin/index.vue"),
  135. children: [
  136. {
  137. path: "songs",
  138. component: () => import("@/pages/Admin/Songs/index.vue"),
  139. meta: { permissionRequired: "admin.view.songs" }
  140. },
  141. {
  142. path: "songs/import",
  143. component: () => import("@/pages/Admin/Songs/Import.vue"),
  144. meta: { permissionRequired: "admin.view.import" }
  145. },
  146. {
  147. path: "reports",
  148. component: () => import("@/pages/Admin/Reports.vue"),
  149. meta: { permissionRequired: "admin.view.reports" }
  150. },
  151. {
  152. path: "stations",
  153. component: () => import("@/pages/Admin/Stations.vue"),
  154. meta: { permissionRequired: "admin.view.stations" }
  155. },
  156. {
  157. path: "playlists",
  158. component: () => import("@/pages/Admin/Playlists.vue"),
  159. meta: { permissionRequired: "admin.view.playlists" }
  160. },
  161. {
  162. path: "users",
  163. component: () => import("@/pages/Admin/Users/index.vue"),
  164. meta: { permissionRequired: "admin.view.users" }
  165. },
  166. {
  167. path: "users/data-requests",
  168. component: () =>
  169. import("@/pages/Admin/Users/DataRequests.vue"),
  170. meta: { permissionRequired: "admin.view.dataRequests" }
  171. },
  172. {
  173. path: "users/punishments",
  174. component: () =>
  175. import("@/pages/Admin/Users/Punishments.vue"),
  176. meta: {
  177. permissionRequired: "admin.view.punishments"
  178. }
  179. },
  180. {
  181. path: "news",
  182. component: () => import("@/pages/Admin/News.vue"),
  183. meta: { permissionRequired: "admin.view.news" }
  184. },
  185. {
  186. path: "statistics",
  187. component: () => import("@/pages/Admin/Statistics.vue"),
  188. meta: {
  189. permissionRequired: "admin.view.statistics"
  190. }
  191. },
  192. {
  193. path: "youtube",
  194. component: () => import("@/pages/Admin/YouTube/index.vue"),
  195. meta: { permissionRequired: "admin.view.youtube" }
  196. },
  197. {
  198. path: "youtube/videos",
  199. component: () => import("@/pages/Admin/YouTube/Videos.vue"),
  200. meta: {
  201. permissionRequired: "admin.view.youtubeVideos"
  202. }
  203. },
  204. {
  205. path: "youtube/channels",
  206. component: () =>
  207. import("@/pages/Admin/YouTube/Channels.vue"),
  208. meta: {
  209. permissionRequired: "admin.view.youtubeChannels"
  210. }
  211. },
  212. {
  213. path: "soundcloud",
  214. component: () =>
  215. import("@/pages/Admin/SoundCloud/index.vue"),
  216. meta: { permissionRequired: "admin.view.soundcloud" }
  217. },
  218. {
  219. path: "soundcloud/tracks",
  220. component: () =>
  221. import("@/pages/Admin/SoundCloud/Tracks.vue"),
  222. meta: {
  223. permissionRequired: "admin.view.soundcloudTracks"
  224. }
  225. }
  226. ],
  227. meta: {
  228. permissionRequired: "admin.view"
  229. }
  230. },
  231. {
  232. name: "station",
  233. path: "/:id",
  234. component: () => import("@/pages//Station/index.vue")
  235. }
  236. ]
  237. });
  238. app.use(createPinia());
  239. const { createSocket } = useWebsocketsStore();
  240. createSocket().then(async socket => {
  241. const configStore = useConfigStore();
  242. const userAuthStore = useUserAuthStore();
  243. const modalsStore = useModalsStore();
  244. router.beforeEach((to, from, next) => {
  245. if (window.stationInterval) {
  246. clearInterval(window.stationInterval);
  247. window.stationInterval = 0;
  248. }
  249. // if (to.name === "station") {
  250. // modalsStore.closeModal("manageStation");
  251. // }
  252. modalsStore.closeAllModals();
  253. if (socket.ready && to.fullPath !== from.fullPath) {
  254. socket.clearCallbacks();
  255. socket.destroyListeners();
  256. }
  257. if (to.query.toast) {
  258. const toast =
  259. typeof to.query.toast === "string"
  260. ? { content: to.query.toast, timeout: 20000 }
  261. : { ...to.query.toast };
  262. new Toast(toast);
  263. const { query } = to;
  264. delete query.toast;
  265. next({ ...to, query });
  266. } else if (
  267. to.meta.configRequired ||
  268. to.meta.loginRequired ||
  269. to.meta.permissionRequired ||
  270. to.meta.guestsOnly
  271. ) {
  272. const gotData = () => {
  273. if (
  274. to.meta.configRequired &&
  275. !configStore.get(`${to.meta.configRequired}`)
  276. )
  277. next({ path: "/" });
  278. else if (to.meta.loginRequired && !userAuthStore.loggedIn)
  279. next({ path: "/login" });
  280. else if (
  281. to.meta.permissionRequired &&
  282. !userAuthStore.hasPermission(
  283. `${to.meta.permissionRequired}`
  284. )
  285. ) {
  286. if (
  287. to.path.startsWith("/admin") &&
  288. to.path !== "/admin/songs"
  289. )
  290. next({ path: "/admin/songs" });
  291. else next({ path: "/" });
  292. } else if (to.meta.guestsOnly && userAuthStore.loggedIn)
  293. next({ path: "/" });
  294. else next();
  295. };
  296. if (userAuthStore.gotData && userAuthStore.gotPermissions)
  297. gotData();
  298. else {
  299. const unsubscribe = userAuthStore.$onAction(
  300. ({ name, after, onError }) => {
  301. if (
  302. name === "authData" ||
  303. name === "updatePermissions"
  304. ) {
  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. socket.on("ready", res => {
  324. const { loggedIn, role, username, userId, email } = res.user;
  325. userAuthStore.authData({
  326. loggedIn,
  327. role,
  328. username,
  329. email,
  330. userId
  331. });
  332. if (loggedIn) {
  333. userAuthStore.resetCookieExpiration();
  334. }
  335. if (configStore.experimental.media_session) ms.initialize();
  336. else ms.uninitialize();
  337. });
  338. socket.on("keep.event:user.banned", res =>
  339. userAuthStore.banUser(res.data.ban)
  340. );
  341. socket.on("keep.event:user.username.updated", res =>
  342. userAuthStore.updateUsername(res.data.username)
  343. );
  344. socket.on("keep.event:user.preferences.updated", res => {
  345. const { preferences } = res.data;
  346. const {
  347. changeAutoSkipDisliked,
  348. changeNightmode,
  349. changeActivityLogPublic,
  350. changeAnonymousSongRequests,
  351. changeActivityWatch,
  352. changeDefaultStationPrivacy,
  353. changeDefaultPlaylistPrivacy
  354. } = useUserPreferencesStore();
  355. if (preferences.autoSkipDisliked !== undefined)
  356. changeAutoSkipDisliked(preferences.autoSkipDisliked);
  357. if (preferences.nightmode !== undefined) {
  358. changeNightmode(preferences.nightmode);
  359. }
  360. if (preferences.activityLogPublic !== undefined)
  361. changeActivityLogPublic(preferences.activityLogPublic);
  362. if (preferences.anonymousSongRequests !== undefined)
  363. changeAnonymousSongRequests(preferences.anonymousSongRequests);
  364. if (preferences.activityWatch !== undefined)
  365. changeActivityWatch(preferences.activityWatch);
  366. if (preferences.defaultStationPrivacy !== undefined)
  367. changeDefaultStationPrivacy(preferences.defaultStationPrivacy);
  368. if (preferences.defaultPlaylistPrivacy !== undefined)
  369. changeDefaultPlaylistPrivacy(preferences.defaultPlaylistPrivacy);
  370. });
  371. socket.on("keep.event:user.role.updated", res => {
  372. userAuthStore.updateRole(res.data.role);
  373. userAuthStore.updatePermissions().then(() => {
  374. const { meta } = router.currentRoute.value;
  375. if (
  376. meta &&
  377. meta.permissionRequired &&
  378. !userAuthStore.hasPermission(`${meta.permissionRequired}`)
  379. )
  380. router.push({
  381. path: "/",
  382. query: {
  383. toast: "You no longer have access to the page you were viewing."
  384. }
  385. });
  386. });
  387. });
  388. app.mount("#root");
  389. });