main.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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 "lofig";
  7. import Toast from "toasters";
  8. import { useUserAuthStore } from "@/stores/userAuth";
  9. import { useUserPreferencesStore } from "@/stores/userPreferences";
  10. import { useModalsStore } from "@/stores/modals";
  11. import ws from "@/ws";
  12. import ms from "@/ms";
  13. import AppComponent from "./App.vue";
  14. const defaultConfigURL = new URL(
  15. "/config/default.json",
  16. import.meta.url
  17. ).toString();
  18. const REQUIRED_CONFIG_VERSION = 13;
  19. lofig.folder = defaultConfigURL;
  20. const handleMetadata = attrs => {
  21. lofig.get("siteSettings.sitename").then(siteName => {
  22. if (siteName) {
  23. document.title = `${siteName} | ${attrs.title}`;
  24. } else {
  25. document.title = `Musare | ${attrs.title}`;
  26. }
  27. });
  28. };
  29. const app = createApp(AppComponent);
  30. app.use(VueTippy, {
  31. directive: "tippy", // => v-tippy
  32. flipDuration: 0,
  33. popperOptions: {
  34. modifiers: {
  35. preventOverflow: {
  36. enabled: true
  37. }
  38. }
  39. },
  40. allowHTML: true,
  41. defaultProps: { animation: "scale", touch: "hold" }
  42. });
  43. app.use(createPinia());
  44. app.component("Tippy", Tippy);
  45. app.component("PageMetadata", {
  46. watch: {
  47. $attrs: {
  48. // eslint-disable-next-line vue/no-arrow-functions-in-watch
  49. handler: attrs => {
  50. handleMetadata(attrs);
  51. },
  52. deep: true,
  53. immediate: true
  54. }
  55. },
  56. render() {
  57. return null;
  58. }
  59. });
  60. app.directive("scroll", {
  61. mounted(el, binding) {
  62. const f = evt => {
  63. clearTimeout(window.scrollDebounceId);
  64. window.scrollDebounceId = setTimeout(() => {
  65. if (binding.value(evt, el)) {
  66. window.removeEventListener("scroll", f);
  67. }
  68. }, 200);
  69. };
  70. window.addEventListener("scroll", f);
  71. }
  72. });
  73. app.directive("focus", {
  74. mounted(el) {
  75. window.focusedElementBefore = document.activeElement;
  76. el.focus();
  77. }
  78. });
  79. const router = createRouter({
  80. history: createWebHistory(),
  81. routes: [
  82. {
  83. name: "home",
  84. path: "/",
  85. component: () => import("@/pages/Home.vue")
  86. },
  87. {
  88. path: "/login",
  89. name: "login",
  90. redirect: "/"
  91. },
  92. {
  93. path: "/register",
  94. name: "register",
  95. redirect: "/"
  96. },
  97. {
  98. path: "/404",
  99. alias: ["/:pathMatch(.*)*"],
  100. component: () => import("@/pages/404.vue")
  101. },
  102. {
  103. path: "/terms",
  104. component: () => import("@/pages/Terms.vue")
  105. },
  106. {
  107. path: "/privacy",
  108. component: () => import("@/pages/Privacy.vue")
  109. },
  110. {
  111. path: "/team",
  112. component: () => import("@/pages/Team.vue")
  113. },
  114. {
  115. path: "/news",
  116. component: () => import("@/pages/News.vue")
  117. },
  118. {
  119. path: "/about",
  120. component: () => import("@/pages/About.vue")
  121. },
  122. {
  123. name: "profile",
  124. path: "/u/:username",
  125. component: () => import("@/pages/Profile/index.vue")
  126. },
  127. {
  128. path: "/settings",
  129. component: () => import("@/pages/Settings/index.vue"),
  130. meta: {
  131. loginRequired: true
  132. }
  133. },
  134. {
  135. path: "/reset_password",
  136. component: () => import("@/pages/ResetPassword.vue")
  137. },
  138. {
  139. path: "/set_password",
  140. props: { mode: "set" },
  141. component: () => import("@/pages/ResetPassword.vue"),
  142. meta: {
  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. meta: {
  221. permissionRequired: "admin.view"
  222. }
  223. },
  224. {
  225. name: "station",
  226. path: "/:id",
  227. component: () => import("@/pages//Station/index.vue")
  228. }
  229. ]
  230. });
  231. const userAuthStore = useUserAuthStore();
  232. const modalsStore = useModalsStore();
  233. router.beforeEach((to, from, next) => {
  234. if (window.stationInterval) {
  235. clearInterval(window.stationInterval);
  236. window.stationInterval = 0;
  237. }
  238. // if (to.name === "station") {
  239. // modalsStore.closeModal("manageStation");
  240. // }
  241. modalsStore.closeAllModals();
  242. if (ws.socket && to.fullPath !== from.fullPath) {
  243. ws.clearCallbacks();
  244. ws.destroyListeners();
  245. }
  246. if (to.query.toast) {
  247. const toast =
  248. typeof to.query.toast === "string"
  249. ? { content: to.query.toast, timeout: 20000 }
  250. : to.query.toast;
  251. new Toast(toast);
  252. const { query } = to;
  253. delete query.toast;
  254. next({ ...to, query });
  255. } else if (
  256. to.meta.loginRequired ||
  257. to.meta.permissionRequired ||
  258. to.meta.guestsOnly
  259. ) {
  260. const gotData = () => {
  261. if (to.meta.loginRequired && !userAuthStore.loggedIn)
  262. next({ path: "/login" });
  263. else if (
  264. to.meta.permissionRequired &&
  265. !userAuthStore.hasPermission(to.meta.permissionRequired)
  266. )
  267. next({ path: "/" });
  268. else if (to.meta.guestsOnly && userAuthStore.loggedIn)
  269. next({ path: "/" });
  270. else next();
  271. };
  272. if (userAuthStore.gotData && userAuthStore.gotPermissions) gotData();
  273. else {
  274. const unsubscribe = userAuthStore.$onAction(
  275. ({ name, after, onError }) => {
  276. if (name === "authData" || name === "updatePermissions") {
  277. after(() => {
  278. if (
  279. userAuthStore.gotData &&
  280. userAuthStore.gotPermissions
  281. )
  282. gotData();
  283. unsubscribe();
  284. });
  285. onError(() => {
  286. unsubscribe();
  287. });
  288. }
  289. }
  290. );
  291. }
  292. } else next();
  293. });
  294. app.use(router);
  295. lofig.folder = defaultConfigURL;
  296. (async () => {
  297. lofig.fetchConfig().then(config => {
  298. const { configVersion, skipConfigVersionCheck } = config;
  299. if (
  300. configVersion !== REQUIRED_CONFIG_VERSION &&
  301. !skipConfigVersionCheck
  302. ) {
  303. // eslint-disable-next-line no-alert
  304. alert(
  305. "CONFIG VERSION IS WRONG. PLEASE UPDATE YOUR CONFIG WITH THE HELP OF THE TEMPLATE FILE AND THE README FILE."
  306. );
  307. window.stop();
  308. }
  309. });
  310. const websocketsDomain = await lofig.get("backend.websocketsDomain");
  311. ws.init(websocketsDomain);
  312. if (await lofig.get("siteSettings.mediasession")) ms.init();
  313. ws.socket.on("ready", res => {
  314. const { loggedIn, role, username, userId, email } = res.data;
  315. userAuthStore.authData({
  316. loggedIn,
  317. role,
  318. username,
  319. email,
  320. userId
  321. });
  322. });
  323. ws.socket.on("keep.event:user.banned", res =>
  324. userAuthStore.banUser(res.data.ban)
  325. );
  326. ws.socket.on("keep.event:user.username.updated", res =>
  327. userAuthStore.updateUsername(res.data.username)
  328. );
  329. ws.socket.on("keep.event:user.preferences.updated", res => {
  330. const { preferences } = res.data;
  331. const {
  332. changeAutoSkipDisliked,
  333. changeNightmode,
  334. changeActivityLogPublic,
  335. changeAnonymousSongRequests,
  336. changeActivityWatch
  337. } = useUserPreferencesStore();
  338. if (preferences.autoSkipDisliked !== undefined)
  339. changeAutoSkipDisliked(preferences.autoSkipDisliked);
  340. if (preferences.nightmode !== undefined) {
  341. localStorage.setItem("nightmode", preferences.nightmode);
  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. ws.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. });
  368. app.mount("#root");
  369. })();