main.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. const globalComponents = import.meta.glob("@/components/global/*.vue");
  60. Object.entries(globalComponents).forEach(
  61. async ([componentFilePath, definition]) => {
  62. const componentName = componentFilePath.split("/").pop().split(".")[0];
  63. const component = await definition();
  64. app.component(componentName, component.default);
  65. }
  66. );
  67. app.directive("scroll", {
  68. mounted(el, binding) {
  69. const f = evt => {
  70. clearTimeout(window.scrollDebounceId);
  71. window.scrollDebounceId = setTimeout(() => {
  72. if (binding.value(evt, el)) {
  73. window.removeEventListener("scroll", f);
  74. }
  75. }, 200);
  76. };
  77. window.addEventListener("scroll", f);
  78. }
  79. });
  80. app.directive("focus", {
  81. mounted(el) {
  82. window.focusedElementBefore = document.activeElement;
  83. el.focus();
  84. }
  85. });
  86. const router = createRouter({
  87. history: createWebHistory(),
  88. routes: [
  89. {
  90. name: "home",
  91. path: "/",
  92. component: () => import("@/pages/Home.vue")
  93. },
  94. {
  95. path: "/login",
  96. name: "login",
  97. redirect: "/"
  98. },
  99. {
  100. path: "/register",
  101. name: "register",
  102. redirect: "/"
  103. },
  104. {
  105. path: "/404",
  106. alias: ["/:pathMatch(.*)*"],
  107. component: () => import("@/pages/404.vue")
  108. },
  109. {
  110. path: "/terms",
  111. component: () => import("@/pages/Terms.vue")
  112. },
  113. {
  114. path: "/privacy",
  115. component: () => import("@/pages/Privacy.vue")
  116. },
  117. {
  118. path: "/team",
  119. component: () => import("@/pages/Team.vue")
  120. },
  121. {
  122. path: "/news",
  123. component: () => import("@/pages/News.vue")
  124. },
  125. {
  126. path: "/about",
  127. component: () => import("@/pages/About.vue")
  128. },
  129. {
  130. name: "profile",
  131. path: "/u/:username",
  132. component: () => import("@/pages/Profile/index.vue")
  133. },
  134. {
  135. path: "/settings",
  136. component: () => import("@/pages/Settings/index.vue"),
  137. meta: {
  138. loginRequired: true
  139. }
  140. },
  141. {
  142. path: "/reset_password",
  143. component: () => import("@/pages/ResetPassword.vue")
  144. },
  145. {
  146. path: "/set_password",
  147. props: { mode: "set" },
  148. component: () => import("@/pages/ResetPassword.vue"),
  149. meta: {
  150. loginRequired: true
  151. }
  152. },
  153. {
  154. path: "/admin",
  155. name: "admin",
  156. component: () => import("@/pages/Admin/index.vue"),
  157. children: [
  158. {
  159. path: "songs",
  160. component: () => import("@/pages/Admin/Songs/index.vue")
  161. },
  162. {
  163. path: "songs/import",
  164. component: () => import("@/pages/Admin/Songs/Import.vue")
  165. },
  166. {
  167. path: "reports",
  168. component: () => import("@/pages/Admin/Reports.vue")
  169. },
  170. {
  171. path: "stations",
  172. component: () => import("@/pages/Admin/Stations.vue")
  173. },
  174. {
  175. path: "playlists",
  176. component: () => import("@/pages/Admin/Playlists.vue")
  177. },
  178. {
  179. path: "users",
  180. component: () => import("@/pages/Admin/Users/index.vue")
  181. },
  182. {
  183. path: "users/data-requests",
  184. component: () =>
  185. import("@/pages/Admin/Users/DataRequests.vue")
  186. },
  187. {
  188. path: "users/punishments",
  189. component: () =>
  190. import("@/pages/Admin/Users/Punishments.vue")
  191. },
  192. {
  193. path: "news",
  194. component: () => import("@/pages/Admin/News.vue")
  195. },
  196. {
  197. path: "statistics",
  198. component: () => import("@/pages/Admin/Statistics.vue")
  199. },
  200. {
  201. path: "youtube",
  202. component: () => import("@/pages/Admin/YouTube/index.vue")
  203. },
  204. {
  205. path: "youtube/videos",
  206. component: () => import("@/pages/Admin/YouTube/Videos.vue")
  207. }
  208. ],
  209. meta: {
  210. adminRequired: true
  211. }
  212. },
  213. {
  214. name: "station",
  215. path: "/:id",
  216. component: () => import("@/pages//Station/index.vue")
  217. }
  218. ]
  219. });
  220. const userAuthStore = useUserAuthStore();
  221. const modalsStore = useModalsStore();
  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. // modalsStore.closeModal("manageStation");
  229. // }
  230. modalsStore.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. })();