main.ts 8.9 KB

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