main.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. },
  154. {
  155. path: "songs/import",
  156. component: () => import("@/pages/Admin/Songs/Import.vue")
  157. },
  158. {
  159. path: "reports",
  160. component: () => import("@/pages/Admin/Reports.vue")
  161. },
  162. {
  163. path: "stations",
  164. component: () => import("@/pages/Admin/Stations.vue")
  165. },
  166. {
  167. path: "playlists",
  168. component: () => import("@/pages/Admin/Playlists.vue")
  169. },
  170. {
  171. path: "users",
  172. component: () => import("@/pages/Admin/Users/index.vue")
  173. },
  174. {
  175. path: "users/data-requests",
  176. component: () =>
  177. import("@/pages/Admin/Users/DataRequests.vue")
  178. },
  179. {
  180. path: "users/punishments",
  181. component: () =>
  182. import("@/pages/Admin/Users/Punishments.vue")
  183. },
  184. {
  185. path: "news",
  186. component: () => import("@/pages/Admin/News.vue")
  187. },
  188. {
  189. path: "statistics",
  190. component: () => import("@/pages/Admin/Statistics.vue")
  191. },
  192. {
  193. path: "youtube",
  194. component: () => import("@/pages/Admin/YouTube/index.vue")
  195. },
  196. {
  197. path: "youtube/videos",
  198. component: () => import("@/pages/Admin/YouTube/Videos.vue")
  199. }
  200. ],
  201. meta: {
  202. adminRequired: true
  203. }
  204. },
  205. {
  206. name: "station",
  207. path: "/:id",
  208. component: () => import("@/pages//Station/index.vue")
  209. }
  210. ]
  211. });
  212. const userAuthStore = useUserAuthStore();
  213. const modalsStore = useModalsStore();
  214. router.beforeEach((to, from, next) => {
  215. if (window.stationInterval) {
  216. clearInterval(window.stationInterval);
  217. window.stationInterval = 0;
  218. }
  219. // if (to.name === "station") {
  220. // modalsStore.closeModal("manageStation");
  221. // }
  222. modalsStore.closeAllModals();
  223. if (ws.socket && to.fullPath !== from.fullPath) {
  224. ws.clearCallbacks();
  225. ws.destroyListeners();
  226. }
  227. if (to.meta.loginRequired || to.meta.adminRequired || to.meta.guestsOnly) {
  228. const gotData = () => {
  229. if (to.meta.loginRequired && !userAuthStore.loggedIn)
  230. next({ path: "/login" });
  231. else if (to.meta.adminRequired && userAuthStore.role !== "admin")
  232. next({ path: "/" });
  233. else if (to.meta.guestsOnly && userAuthStore.loggedIn)
  234. next({ path: "/" });
  235. else next();
  236. };
  237. if (userAuthStore.gotData) gotData();
  238. else {
  239. const unsubscribe = userAuthStore.$onAction(
  240. ({ name, after, onError }) => {
  241. if (name === "authData") {
  242. after(() => {
  243. gotData();
  244. unsubscribe();
  245. });
  246. onError(() => {
  247. unsubscribe();
  248. });
  249. }
  250. }
  251. );
  252. }
  253. } else next();
  254. });
  255. app.use(router);
  256. lofig.folder = defaultConfigURL;
  257. (async () => {
  258. lofig.fetchConfig().then(config => {
  259. const { configVersion, skipConfigVersionCheck } = config;
  260. if (
  261. configVersion !== REQUIRED_CONFIG_VERSION &&
  262. !skipConfigVersionCheck
  263. ) {
  264. // eslint-disable-next-line no-alert
  265. alert(
  266. "CONFIG VERSION IS WRONG. PLEASE UPDATE YOUR CONFIG WITH THE HELP OF THE TEMPLATE FILE AND THE README FILE."
  267. );
  268. window.stop();
  269. }
  270. });
  271. const websocketsDomain = await lofig.get("backend.websocketsDomain");
  272. ws.init(websocketsDomain);
  273. if (await lofig.get("siteSettings.mediasession")) ms.init();
  274. ws.socket.on("ready", res => {
  275. const { loggedIn, role, username, userId, email } = res.data;
  276. userAuthStore.authData({
  277. loggedIn,
  278. role,
  279. username,
  280. email,
  281. userId
  282. });
  283. });
  284. ws.socket.on("keep.event:user.banned", res =>
  285. userAuthStore.banUser(res.data.ban)
  286. );
  287. ws.socket.on("keep.event:user.username.updated", res =>
  288. userAuthStore.updateUsername(res.data.username)
  289. );
  290. ws.socket.on("keep.event:user.preferences.updated", res => {
  291. const { preferences } = res.data;
  292. const {
  293. changeAutoSkipDisliked,
  294. changeNightmode,
  295. changeActivityLogPublic,
  296. changeAnonymousSongRequests,
  297. changeActivityWatch
  298. } = useUserPreferencesStore();
  299. if (preferences.autoSkipDisliked !== undefined)
  300. changeAutoSkipDisliked(preferences.autoSkipDisliked);
  301. if (preferences.nightmode !== undefined) {
  302. localStorage.setItem("nightmode", preferences.nightmode);
  303. changeNightmode(preferences.nightmode);
  304. }
  305. if (preferences.activityLogPublic !== undefined)
  306. changeActivityLogPublic(preferences.activityLogPublic);
  307. if (preferences.anonymousSongRequests !== undefined)
  308. changeAnonymousSongRequests(preferences.anonymousSongRequests);
  309. if (preferences.activityWatch !== undefined)
  310. changeActivityWatch(preferences.activityWatch);
  311. });
  312. app.mount("#root");
  313. })();