main.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 "lofig";
  6. import ws from "@/ws";
  7. import ms from "@/ms";
  8. import store from "./store";
  9. import AppComponent from "./App.vue";
  10. const defaultConfigURL = new URL(
  11. "/config/default.json",
  12. import.meta.url
  13. ).toString();
  14. const REQUIRED_CONFIG_VERSION = 13;
  15. lofig.folder = defaultConfigURL;
  16. const handleMetadata = attrs => {
  17. lofig.get("siteSettings.sitename").then(siteName => {
  18. if (siteName) {
  19. document.title = `${siteName} | ${attrs.title}`;
  20. } else {
  21. document.title = `Musare | ${attrs.title}`;
  22. }
  23. });
  24. };
  25. const app = createApp(AppComponent);
  26. app.use(store);
  27. app.use(VueTippy, {
  28. directive: "tippy", // => v-tippy
  29. flipDuration: 0,
  30. popperOptions: {
  31. modifiers: {
  32. preventOverflow: {
  33. enabled: true
  34. }
  35. }
  36. },
  37. allowHTML: true,
  38. defaultProps: { animation: "scale", touch: "hold" }
  39. });
  40. app.component("Tippy", Tippy);
  41. app.component("PageMetadata", {
  42. watch: {
  43. $attrs: {
  44. // eslint-disable-next-line vue/no-arrow-functions-in-watch
  45. handler: attrs => {
  46. handleMetadata(attrs);
  47. },
  48. deep: true,
  49. immediate: true
  50. }
  51. },
  52. render() {
  53. return null;
  54. }
  55. });
  56. const globalComponents = import.meta.glob("@/components/global/*.vue");
  57. Object.entries(globalComponents).forEach(
  58. async ([componentFilePath, definition]) => {
  59. const componentName = componentFilePath.split("/").pop().split(".")[0];
  60. const component = await definition();
  61. app.component(componentName, component.default);
  62. }
  63. );
  64. app.directive("scroll", {
  65. mounted(el, binding) {
  66. const f = evt => {
  67. clearTimeout(window.scrollDebounceId);
  68. window.scrollDebounceId = setTimeout(() => {
  69. if (binding.value(evt, el)) {
  70. window.removeEventListener("scroll", f);
  71. }
  72. }, 200);
  73. };
  74. window.addEventListener("scroll", f);
  75. }
  76. });
  77. app.directive("focus", {
  78. mounted(el) {
  79. window.focusedElementBefore = document.activeElement;
  80. el.focus();
  81. }
  82. });
  83. const router = createRouter({
  84. history: createWebHistory(),
  85. routes: [
  86. {
  87. name: "home",
  88. path: "/",
  89. component: () => import("@/pages/Home.vue")
  90. },
  91. {
  92. path: "/login",
  93. name: "login",
  94. redirect: "/"
  95. },
  96. {
  97. path: "/register",
  98. name: "register",
  99. redirect: "/"
  100. },
  101. {
  102. path: "/404",
  103. alias: ["/:pathMatch(.*)*"],
  104. component: () => import("@/pages/404.vue")
  105. },
  106. {
  107. path: "/terms",
  108. component: () => import("@/pages/Terms.vue")
  109. },
  110. {
  111. path: "/privacy",
  112. component: () => import("@/pages/Privacy.vue")
  113. },
  114. {
  115. path: "/team",
  116. component: () => import("@/pages/Team.vue")
  117. },
  118. {
  119. path: "/news",
  120. component: () => import("@/pages/News.vue")
  121. },
  122. {
  123. path: "/about",
  124. component: () => import("@/pages/About.vue")
  125. },
  126. {
  127. name: "profile",
  128. path: "/u/:username",
  129. component: () => import("@/pages/Profile/index.vue")
  130. },
  131. {
  132. path: "/settings",
  133. component: () => import("@/pages/Settings/index.vue"),
  134. meta: {
  135. loginRequired: true
  136. }
  137. },
  138. {
  139. path: "/reset_password",
  140. component: () => import("@/pages/ResetPassword.vue")
  141. },
  142. {
  143. path: "/set_password",
  144. props: { mode: "set" },
  145. component: () => import("@/pages/ResetPassword.vue"),
  146. meta: {
  147. loginRequired: true
  148. }
  149. },
  150. {
  151. path: "/admin",
  152. name: "admin",
  153. component: () => import("@/pages/Admin/index.vue"),
  154. children: [
  155. {
  156. path: "songs",
  157. component: () => import("@/pages/Admin/Songs/index.vue")
  158. },
  159. {
  160. path: "songs/import",
  161. component: () => import("@/pages/Admin/Songs/Import.vue")
  162. },
  163. {
  164. path: "reports",
  165. component: () => import("@/pages/Admin/Reports.vue")
  166. },
  167. {
  168. path: "stations",
  169. component: () => import("@/pages/Admin/Stations.vue")
  170. },
  171. {
  172. path: "playlists",
  173. component: () => import("@/pages/Admin/Playlists.vue")
  174. },
  175. {
  176. path: "users",
  177. component: () => import("@/pages/Admin/Users/index.vue")
  178. },
  179. {
  180. path: "users/data-requests",
  181. component: () =>
  182. import("@/pages/Admin/Users/DataRequests.vue")
  183. },
  184. {
  185. path: "users/punishments",
  186. component: () =>
  187. import("@/pages/Admin/Users/Punishments.vue")
  188. },
  189. {
  190. path: "news",
  191. component: () => import("@/pages/Admin/News.vue")
  192. },
  193. {
  194. path: "statistics",
  195. component: () => import("@/pages/Admin/Statistics.vue")
  196. },
  197. {
  198. path: "youtube",
  199. component: () => import("@/pages/Admin/YouTube/index.vue")
  200. },
  201. {
  202. path: "youtube/videos",
  203. component: () => import("@/pages/Admin/YouTube/Videos.vue")
  204. }
  205. ],
  206. meta: {
  207. adminRequired: true
  208. }
  209. },
  210. {
  211. name: "station",
  212. path: "/:id",
  213. component: () => import("@/pages//Station/index.vue")
  214. }
  215. ]
  216. });
  217. router.beforeEach((to, from, next) => {
  218. if (window.stationInterval) {
  219. clearInterval(window.stationInterval);
  220. window.stationInterval = 0;
  221. }
  222. // if (to.name === "station") {
  223. // store.dispatch("modalVisibility/closeModal", "manageStation");
  224. // }
  225. store.dispatch("modalVisibility/closeAllModals");
  226. if (ws.socket && to.fullPath !== from.fullPath) {
  227. ws.clearCallbacks();
  228. ws.destroyListeners();
  229. }
  230. if (to.meta.loginRequired || to.meta.adminRequired || to.meta.guestsOnly) {
  231. const gotData = () => {
  232. if (to.meta.loginRequired && !store.state.user.auth.loggedIn)
  233. next({ path: "/login", query: "" });
  234. else if (
  235. to.meta.adminRequired &&
  236. store.state.user.auth.role !== "admin"
  237. )
  238. next({ path: "/", query: "" });
  239. else if (to.meta.guestsOnly && store.state.user.auth.loggedIn)
  240. next({ path: "/", query: "" });
  241. else next();
  242. };
  243. if (store.state.user.auth.gotData) gotData();
  244. else {
  245. const watcher = store.watch(
  246. state => state.user.auth.gotData,
  247. () => {
  248. watcher();
  249. gotData();
  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. store.dispatch("user/auth/authData", {
  277. loggedIn,
  278. role,
  279. username,
  280. email,
  281. userId
  282. });
  283. });
  284. ws.socket.on("keep.event:user.banned", res =>
  285. store.dispatch("user/auth/banUser", res.data.ban)
  286. );
  287. ws.socket.on("keep.event:user.username.updated", res =>
  288. store.dispatch("user/auth/updateUsername", res.data.username)
  289. );
  290. ws.socket.on("keep.event:user.preferences.updated", res => {
  291. const { preferences } = res.data;
  292. if (preferences.autoSkipDisliked !== undefined)
  293. store.dispatch(
  294. "user/preferences/changeAutoSkipDisliked",
  295. preferences.autoSkipDisliked
  296. );
  297. if (preferences.nightmode !== undefined) {
  298. localStorage.setItem("nightmode", preferences.nightmode);
  299. store.dispatch(
  300. "user/preferences/changeNightmode",
  301. preferences.nightmode
  302. );
  303. }
  304. if (preferences.activityLogPublic !== undefined)
  305. store.dispatch(
  306. "user/preferences/changeActivityLogPublic",
  307. preferences.activityLogPublic
  308. );
  309. if (preferences.anonymousSongRequests !== undefined)
  310. store.dispatch(
  311. "user/preferences/changeAnonymousSongRequests",
  312. preferences.anonymousSongRequests
  313. );
  314. if (preferences.activityWatch !== undefined)
  315. store.dispatch(
  316. "user/preferences/changeActivityWatch",
  317. preferences.activityWatch
  318. );
  319. });
  320. app.mount("#root");
  321. })();