main.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. app.directive("scroll", {
  46. mounted(el, binding) {
  47. const f = evt => {
  48. clearTimeout(window.scrollDebounceId);
  49. window.scrollDebounceId = setTimeout(() => {
  50. if (binding.value(evt, el)) {
  51. window.removeEventListener("scroll", f);
  52. }
  53. }, 200);
  54. };
  55. window.addEventListener("scroll", f);
  56. }
  57. });
  58. app.directive("focus", {
  59. mounted(el) {
  60. window.focusedElementBefore = document.activeElement;
  61. el.focus();
  62. }
  63. });
  64. const router = createRouter({
  65. history: createWebHistory(),
  66. routes: [
  67. {
  68. name: "home",
  69. path: "/",
  70. component: () => import("@/pages/Home.vue")
  71. },
  72. {
  73. path: "/login",
  74. name: "login",
  75. redirect: "/"
  76. },
  77. {
  78. path: "/register",
  79. name: "register",
  80. redirect: "/"
  81. },
  82. {
  83. path: "/404",
  84. alias: ["/:pathMatch(.*)*"],
  85. component: () => import("@/pages/404.vue")
  86. },
  87. {
  88. path: "/terms",
  89. component: () => import("@/pages/Terms.vue")
  90. },
  91. {
  92. path: "/privacy",
  93. component: () => import("@/pages/Privacy.vue")
  94. },
  95. {
  96. path: "/team",
  97. component: () => import("@/pages/Team.vue")
  98. },
  99. {
  100. path: "/news",
  101. component: () => import("@/pages/News.vue")
  102. },
  103. {
  104. path: "/about",
  105. component: () => import("@/pages/About.vue")
  106. },
  107. {
  108. name: "profile",
  109. path: "/u/:username",
  110. component: () => import("@/pages/Profile/index.vue")
  111. },
  112. {
  113. path: "/settings",
  114. component: () => import("@/pages/Settings/index.vue"),
  115. meta: {
  116. loginRequired: true
  117. }
  118. },
  119. {
  120. path: "/reset_password",
  121. component: () => import("@/pages/ResetPassword.vue")
  122. },
  123. {
  124. path: "/set_password",
  125. props: { mode: "set" },
  126. component: () => import("@/pages/ResetPassword.vue"),
  127. meta: {
  128. loginRequired: true
  129. }
  130. },
  131. {
  132. path: "/admin",
  133. component: () => import("@/pages/Admin/index.vue"),
  134. meta: {
  135. adminRequired: true
  136. }
  137. },
  138. {
  139. path: "/admin/:page(.*)",
  140. component: () => import("@/pages//Admin/index.vue"),
  141. meta: {
  142. adminRequired: true
  143. }
  144. },
  145. {
  146. name: "station",
  147. path: "/:id",
  148. component: () => import("@/pages//Station/index.vue")
  149. }
  150. ]
  151. });
  152. router.beforeEach((to, from, next) => {
  153. if (window.stationInterval) {
  154. clearInterval(window.stationInterval);
  155. window.stationInterval = 0;
  156. }
  157. if (from.name === "home" && to.name === "station") {
  158. if (store.state.modalVisibility.modals.manageStation)
  159. store.dispatch("modalVisibility/closeModal", "manageStation");
  160. }
  161. if (ws.socket && to.fullPath !== from.fullPath) {
  162. ws.clearCallbacks();
  163. ws.destroyListeners();
  164. }
  165. if (to.meta.loginRequired || to.meta.adminRequired || to.meta.guestsOnly) {
  166. const gotData = () => {
  167. if (to.meta.loginRequired && !store.state.user.auth.loggedIn)
  168. next({ path: "/login", query: "" });
  169. else if (
  170. to.meta.adminRequired &&
  171. store.state.user.auth.role !== "admin"
  172. )
  173. next({ path: "/", query: "" });
  174. else if (to.meta.guestsOnly && store.state.user.auth.loggedIn)
  175. next({ path: "/", query: "" });
  176. else next();
  177. };
  178. if (store.state.user.auth.gotData) gotData();
  179. else {
  180. const watcher = store.watch(
  181. state => state.user.auth.gotData,
  182. () => {
  183. watcher();
  184. gotData();
  185. }
  186. );
  187. }
  188. } else next();
  189. });
  190. app.use(router);
  191. lofig.folder = "/config/default.json";
  192. (async () => {
  193. lofig.fetchConfig().then(config => {
  194. const { configVersion, skipConfigVersionCheck } = config;
  195. if (
  196. configVersion !== REQUIRED_CONFIG_VERSION &&
  197. !skipConfigVersionCheck
  198. ) {
  199. // eslint-disable-next-line no-alert
  200. alert(
  201. "CONFIG VERSION IS WRONG. PLEASE UPDATE YOUR CONFIG WITH THE HELP OF THE TEMPLATE FILE AND THE README FILE."
  202. );
  203. window.stop();
  204. }
  205. });
  206. const websocketsDomain = await lofig.get("backend.websocketsDomain");
  207. ws.init(websocketsDomain);
  208. if (await lofig.get("siteSettings.mediasession")) ms.init();
  209. ws.socket.on("ready", res => {
  210. const { loggedIn, role, username, userId, email } = res.data;
  211. store.dispatch("user/auth/authData", {
  212. loggedIn,
  213. role,
  214. username,
  215. email,
  216. userId
  217. });
  218. });
  219. ws.socket.on("keep.event:user.banned", res =>
  220. store.dispatch("user/auth/banUser", res.data.ban)
  221. );
  222. ws.socket.on("keep.event:user.username.updated", res =>
  223. store.dispatch("user/auth/updateUsername", res.data.username)
  224. );
  225. ws.socket.on("keep.event:user.preferences.updated", res => {
  226. const { preferences } = res.data;
  227. if (preferences.autoSkipDisliked !== undefined)
  228. store.dispatch(
  229. "user/preferences/changeAutoSkipDisliked",
  230. preferences.autoSkipDisliked
  231. );
  232. if (preferences.nightmode !== undefined) {
  233. localStorage.setItem("nightmode", preferences.nightmode);
  234. store.dispatch(
  235. "user/preferences/changeNightmode",
  236. preferences.nightmode
  237. );
  238. }
  239. if (preferences.activityLogPublic !== undefined)
  240. store.dispatch(
  241. "user/preferences/changeActivityLogPublic",
  242. preferences.activityLogPublic
  243. );
  244. if (preferences.anonymousSongRequests !== undefined)
  245. store.dispatch(
  246. "user/preferences/changeAnonymousSongRequests",
  247. preferences.anonymousSongRequests
  248. );
  249. if (preferences.activityWatch !== undefined)
  250. store.dispatch(
  251. "user/preferences/changeActivityWatch",
  252. preferences.activityWatch
  253. );
  254. });
  255. app.mount("#root");
  256. })();