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 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: "/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. router.beforeEach((to, from, next) => {
  154. if (window.stationInterval) {
  155. clearInterval(window.stationInterval);
  156. window.stationInterval = 0;
  157. }
  158. if (ws.socket && to.fullPath !== from.fullPath) {
  159. ws.clearCallbacks();
  160. ws.destroyListeners();
  161. }
  162. if (to.meta.loginRequired || to.meta.adminRequired || to.meta.guestsOnly) {
  163. const gotData = () => {
  164. if (to.meta.loginRequired && !store.state.user.auth.loggedIn)
  165. next({ path: "/login" });
  166. else if (
  167. to.meta.adminRequired &&
  168. store.state.user.auth.role !== "admin"
  169. )
  170. next({ path: "/" });
  171. else if (to.meta.guestsOnly && store.state.user.auth.loggedIn)
  172. next({ path: "/" });
  173. else next();
  174. };
  175. if (store.state.user.auth.gotData) gotData();
  176. else {
  177. const watcher = store.watch(
  178. state => state.user.auth.gotData,
  179. () => {
  180. watcher();
  181. gotData();
  182. }
  183. );
  184. }
  185. } else next();
  186. });
  187. app.use(router);
  188. lofig.folder = "../config/default.json";
  189. (async () => {
  190. lofig.fetchConfig().then(config => {
  191. const { configVersion, skipConfigVersionCheck } = config;
  192. if (
  193. configVersion !== REQUIRED_CONFIG_VERSION &&
  194. !skipConfigVersionCheck
  195. ) {
  196. // eslint-disable-next-line no-alert
  197. alert(
  198. "CONFIG VERSION IS WRONG. PLEASE UPDATE YOUR CONFIG WITH THE HELP OF THE TEMPLATE FILE AND THE README FILE."
  199. );
  200. window.stop();
  201. }
  202. });
  203. const websocketsDomain = await lofig.get("backend.websocketsDomain");
  204. ws.init(websocketsDomain);
  205. ws.socket.on("ready", res => {
  206. const { loggedIn, role, username, userId, email } = res.data;
  207. store.dispatch("user/auth/authData", {
  208. loggedIn,
  209. role,
  210. username,
  211. email,
  212. userId
  213. });
  214. });
  215. ws.socket.on("keep.event:user.banned", res =>
  216. store.dispatch("user/auth/banUser", res.data.ban)
  217. );
  218. ws.socket.on("event:user.username.updated", res =>
  219. store.dispatch("user/auth/updateUsername", res.data.username)
  220. );
  221. ws.socket.on("keep.event:user.preferences.updated", res => {
  222. const { preferences } = res.data;
  223. if (preferences.autoSkipDisliked !== undefined)
  224. store.dispatch(
  225. "user/preferences/changeAutoSkipDisliked",
  226. preferences.autoSkipDisliked
  227. );
  228. if (preferences.nightmode !== undefined) {
  229. localStorage.setItem("nightmode", preferences.nightmode);
  230. store.dispatch(
  231. "user/preferences/changeNightmode",
  232. preferences.nightmode
  233. );
  234. }
  235. if (preferences.activityLogPublic !== undefined)
  236. store.dispatch(
  237. "user/preferences/changeActivityLogPublic",
  238. preferences.activityLogPublic
  239. );
  240. if (preferences.anonymousSongRequests !== undefined)
  241. store.dispatch(
  242. "user/preferences/changeAnonymousSongRequests",
  243. preferences.anonymousSongRequests
  244. );
  245. if (preferences.activityWatch !== undefined)
  246. store.dispatch(
  247. "user/preferences/changeActivityWatch",
  248. preferences.activityWatch
  249. );
  250. });
  251. app.mount("#root");
  252. })();