main.js 7.3 KB

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