1
0

main.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. if (ws.socket && to.fullPath !== from.fullPath) {
  211. ws.clearCallbacks();
  212. ws.destroyListeners();
  213. }
  214. if (to.meta.loginRequired || to.meta.adminRequired || to.meta.guestsOnly) {
  215. const gotData = () => {
  216. if (to.meta.loginRequired && !store.state.user.auth.loggedIn)
  217. next({ path: "/login", query: "" });
  218. else if (
  219. to.meta.adminRequired &&
  220. store.state.user.auth.role !== "admin"
  221. )
  222. next({ path: "/", query: "" });
  223. else if (to.meta.guestsOnly && store.state.user.auth.loggedIn)
  224. next({ path: "/", query: "" });
  225. else next();
  226. };
  227. if (store.state.user.auth.gotData) gotData();
  228. else {
  229. const watcher = store.watch(
  230. state => state.user.auth.gotData,
  231. () => {
  232. watcher();
  233. gotData();
  234. }
  235. );
  236. }
  237. } else next();
  238. });
  239. app.use(router);
  240. lofig.folder = "/config/default.json";
  241. (async () => {
  242. lofig.fetchConfig().then(config => {
  243. const { configVersion, skipConfigVersionCheck } = config;
  244. if (
  245. configVersion !== REQUIRED_CONFIG_VERSION &&
  246. !skipConfigVersionCheck
  247. ) {
  248. // eslint-disable-next-line no-alert
  249. alert(
  250. "CONFIG VERSION IS WRONG. PLEASE UPDATE YOUR CONFIG WITH THE HELP OF THE TEMPLATE FILE AND THE README FILE."
  251. );
  252. window.stop();
  253. }
  254. });
  255. const websocketsDomain = await lofig.get("backend.websocketsDomain");
  256. ws.init(websocketsDomain);
  257. if (await lofig.get("siteSettings.mediasession")) ms.init();
  258. ws.socket.on("ready", res => {
  259. const { loggedIn, role, username, userId, email } = res.data;
  260. store.dispatch("user/auth/authData", {
  261. loggedIn,
  262. role,
  263. username,
  264. email,
  265. userId
  266. });
  267. });
  268. ws.socket.on("keep.event:user.banned", res =>
  269. store.dispatch("user/auth/banUser", res.data.ban)
  270. );
  271. ws.socket.on("keep.event:user.username.updated", res =>
  272. store.dispatch("user/auth/updateUsername", res.data.username)
  273. );
  274. ws.socket.on("keep.event:user.preferences.updated", res => {
  275. const { preferences } = res.data;
  276. if (preferences.autoSkipDisliked !== undefined)
  277. store.dispatch(
  278. "user/preferences/changeAutoSkipDisliked",
  279. preferences.autoSkipDisliked
  280. );
  281. if (preferences.nightmode !== undefined) {
  282. localStorage.setItem("nightmode", preferences.nightmode);
  283. store.dispatch(
  284. "user/preferences/changeNightmode",
  285. preferences.nightmode
  286. );
  287. }
  288. if (preferences.activityLogPublic !== undefined)
  289. store.dispatch(
  290. "user/preferences/changeActivityLogPublic",
  291. preferences.activityLogPublic
  292. );
  293. if (preferences.anonymousSongRequests !== undefined)
  294. store.dispatch(
  295. "user/preferences/changeAnonymousSongRequests",
  296. preferences.anonymousSongRequests
  297. );
  298. if (preferences.activityWatch !== undefined)
  299. store.dispatch(
  300. "user/preferences/changeActivityWatch",
  301. preferences.activityWatch
  302. );
  303. });
  304. app.mount("#root");
  305. })();