main.js 5.8 KB

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