main.js 5.6 KB

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