main.js 7.5 KB

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