main.js 5.7 KB

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