1
0

main.js 7.3 KB

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