main.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 ws from "@/ws";
  10. import ms from "@/ms";
  11. import store from "./store";
  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(store);
  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. const globalComponents = import.meta.glob("@/components/global/*.vue");
  61. Object.entries(globalComponents).forEach(
  62. async ([componentFilePath, definition]) => {
  63. const componentName = componentFilePath.split("/").pop().split(".")[0];
  64. const component = await definition();
  65. app.component(componentName, component.default);
  66. }
  67. );
  68. app.directive("scroll", {
  69. mounted(el, binding) {
  70. const f = evt => {
  71. clearTimeout(window.scrollDebounceId);
  72. window.scrollDebounceId = setTimeout(() => {
  73. if (binding.value(evt, el)) {
  74. window.removeEventListener("scroll", f);
  75. }
  76. }, 200);
  77. };
  78. window.addEventListener("scroll", f);
  79. }
  80. });
  81. app.directive("focus", {
  82. mounted(el) {
  83. window.focusedElementBefore = document.activeElement;
  84. el.focus();
  85. }
  86. });
  87. const router = createRouter({
  88. history: createWebHistory(),
  89. routes: [
  90. {
  91. name: "home",
  92. path: "/",
  93. component: () => import("@/pages/Home.vue")
  94. },
  95. {
  96. path: "/login",
  97. name: "login",
  98. redirect: "/"
  99. },
  100. {
  101. path: "/register",
  102. name: "register",
  103. redirect: "/"
  104. },
  105. {
  106. path: "/404",
  107. alias: ["/:pathMatch(.*)*"],
  108. component: () => import("@/pages/404.vue")
  109. },
  110. {
  111. path: "/terms",
  112. component: () => import("@/pages/Terms.vue")
  113. },
  114. {
  115. path: "/privacy",
  116. component: () => import("@/pages/Privacy.vue")
  117. },
  118. {
  119. path: "/team",
  120. component: () => import("@/pages/Team.vue")
  121. },
  122. {
  123. path: "/news",
  124. component: () => import("@/pages/News.vue")
  125. },
  126. {
  127. path: "/about",
  128. component: () => import("@/pages/About.vue")
  129. },
  130. {
  131. name: "profile",
  132. path: "/u/:username",
  133. component: () => import("@/pages/Profile/index.vue")
  134. },
  135. {
  136. path: "/settings",
  137. component: () => import("@/pages/Settings/index.vue"),
  138. meta: {
  139. loginRequired: true
  140. }
  141. },
  142. {
  143. path: "/reset_password",
  144. component: () => import("@/pages/ResetPassword.vue")
  145. },
  146. {
  147. path: "/set_password",
  148. props: { mode: "set" },
  149. component: () => import("@/pages/ResetPassword.vue"),
  150. meta: {
  151. loginRequired: true
  152. }
  153. },
  154. {
  155. path: "/admin",
  156. name: "admin",
  157. component: () => import("@/pages/Admin/index.vue"),
  158. children: [
  159. {
  160. path: "songs",
  161. component: () => import("@/pages/Admin/Songs/index.vue")
  162. },
  163. {
  164. path: "songs/import",
  165. component: () => import("@/pages/Admin/Songs/Import.vue")
  166. },
  167. {
  168. path: "reports",
  169. component: () => import("@/pages/Admin/Reports.vue")
  170. },
  171. {
  172. path: "stations",
  173. component: () => import("@/pages/Admin/Stations.vue")
  174. },
  175. {
  176. path: "playlists",
  177. component: () => import("@/pages/Admin/Playlists.vue")
  178. },
  179. {
  180. path: "users",
  181. component: () => import("@/pages/Admin/Users/index.vue")
  182. },
  183. {
  184. path: "users/data-requests",
  185. component: () =>
  186. import("@/pages/Admin/Users/DataRequests.vue")
  187. },
  188. {
  189. path: "users/punishments",
  190. component: () =>
  191. import("@/pages/Admin/Users/Punishments.vue")
  192. },
  193. {
  194. path: "news",
  195. component: () => import("@/pages/Admin/News.vue")
  196. },
  197. {
  198. path: "statistics",
  199. component: () => import("@/pages/Admin/Statistics.vue")
  200. },
  201. {
  202. path: "youtube",
  203. component: () => import("@/pages/Admin/YouTube/index.vue")
  204. },
  205. {
  206. path: "youtube/videos",
  207. component: () => import("@/pages/Admin/YouTube/Videos.vue")
  208. }
  209. ],
  210. meta: {
  211. adminRequired: true
  212. }
  213. },
  214. {
  215. name: "station",
  216. path: "/:id",
  217. component: () => import("@/pages//Station/index.vue")
  218. }
  219. ]
  220. });
  221. const userAuthStore = useUserAuthStore();
  222. router.beforeEach((to, from, next) => {
  223. if (window.stationInterval) {
  224. clearInterval(window.stationInterval);
  225. window.stationInterval = 0;
  226. }
  227. // if (to.name === "station") {
  228. // store.dispatch("modalVisibility/closeModal", "manageStation");
  229. // }
  230. store.dispatch("modalVisibility/closeAllModals");
  231. if (ws.socket && to.fullPath !== from.fullPath) {
  232. ws.clearCallbacks();
  233. ws.destroyListeners();
  234. }
  235. if (to.meta.loginRequired || to.meta.adminRequired || to.meta.guestsOnly) {
  236. const gotData = () => {
  237. if (to.meta.loginRequired && !userAuthStore.loggedIn)
  238. next({ path: "/login", query: "" });
  239. else if (to.meta.adminRequired && userAuthStore.role !== "admin")
  240. next({ path: "/", query: "" });
  241. else if (to.meta.guestsOnly && userAuthStore.loggedIn)
  242. next({ path: "/", query: "" });
  243. else next();
  244. };
  245. if (userAuthStore.gotData) gotData();
  246. else {
  247. const unsubscribe = userAuthStore.$onAction(
  248. ({ name, after, onError }) => {
  249. if (name === "authData") {
  250. after(() => {
  251. gotData();
  252. unsubscribe();
  253. });
  254. onError(() => {
  255. unsubscribe();
  256. });
  257. }
  258. }
  259. );
  260. }
  261. } else next();
  262. });
  263. app.use(router);
  264. lofig.folder = defaultConfigURL;
  265. (async () => {
  266. lofig.fetchConfig().then(config => {
  267. const { configVersion, skipConfigVersionCheck } = config;
  268. if (
  269. configVersion !== REQUIRED_CONFIG_VERSION &&
  270. !skipConfigVersionCheck
  271. ) {
  272. // eslint-disable-next-line no-alert
  273. alert(
  274. "CONFIG VERSION IS WRONG. PLEASE UPDATE YOUR CONFIG WITH THE HELP OF THE TEMPLATE FILE AND THE README FILE."
  275. );
  276. window.stop();
  277. }
  278. });
  279. const websocketsDomain = await lofig.get("backend.websocketsDomain");
  280. ws.init(websocketsDomain);
  281. if (await lofig.get("siteSettings.mediasession")) ms.init();
  282. ws.socket.on("ready", res => {
  283. const { loggedIn, role, username, userId, email } = res.data;
  284. userAuthStore.authData({
  285. loggedIn,
  286. role,
  287. username,
  288. email,
  289. userId
  290. });
  291. });
  292. ws.socket.on("keep.event:user.banned", res =>
  293. userAuthStore.banUser(res.data.ban)
  294. );
  295. ws.socket.on("keep.event:user.username.updated", res =>
  296. userAuthStore.updateUsername(res.data.username)
  297. );
  298. ws.socket.on("keep.event:user.preferences.updated", res => {
  299. const { preferences } = res.data;
  300. const {
  301. changeAutoSkipDisliked,
  302. changeNightmode,
  303. changeActivityLogPublic,
  304. changeAnonymousSongRequests,
  305. changeActivityWatch
  306. } = useUserPreferencesStore();
  307. if (preferences.autoSkipDisliked !== undefined)
  308. changeAutoSkipDisliked(preferences.autoSkipDisliked);
  309. if (preferences.nightmode !== undefined) {
  310. localStorage.setItem("nightmode", preferences.nightmode);
  311. changeNightmode(preferences.nightmode);
  312. }
  313. if (preferences.activityLogPublic !== undefined)
  314. changeActivityLogPublic(preferences.activityLogPublic);
  315. if (preferences.anonymousSongRequests !== undefined)
  316. changeAnonymousSongRequests(preferences.anonymousSongRequests);
  317. if (preferences.activityWatch !== undefined)
  318. changeActivityWatch(preferences.activityWatch);
  319. });
  320. app.mount("#root");
  321. })();