main.js 7.1 KB

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