main.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. meta: {
  110. guestsOnly: true
  111. }
  112. },
  113. {
  114. path: "/set_password",
  115. props: { mode: "set" },
  116. component: () => import("@/pages/ResetPassword.vue"),
  117. meta: {
  118. loginRequired: true
  119. }
  120. },
  121. {
  122. path: "/login",
  123. component: () => import("@/components/modals/Login.vue"),
  124. meta: {
  125. guestsOnly: true
  126. }
  127. },
  128. {
  129. path: "/register",
  130. component: () => import("@/components/modals/Register.vue"),
  131. meta: {
  132. guestsOnly: true
  133. }
  134. },
  135. {
  136. path: "/admin",
  137. component: () => import("@/pages/Admin/index.vue"),
  138. meta: {
  139. adminRequired: true
  140. }
  141. },
  142. {
  143. path: "/admin/:page",
  144. component: () => import("@/pages//Admin/index.vue"),
  145. meta: {
  146. adminRequired: true
  147. }
  148. },
  149. {
  150. name: "station",
  151. path: "/:id",
  152. component: () => import("@/pages//Station/index.vue")
  153. }
  154. ]
  155. });
  156. router.beforeEach((to, from, next) => {
  157. if (window.stationInterval) {
  158. clearInterval(window.stationInterval);
  159. window.stationInterval = 0;
  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" });
  169. else if (
  170. to.meta.adminRequired &&
  171. store.state.user.auth.role !== "admin"
  172. )
  173. next({ path: "/" });
  174. else if (to.meta.guestsOnly && store.state.user.auth.loggedIn)
  175. next({ path: "/" });
  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. ws.socket.on("ready", res => {
  209. const { loggedIn, role, username, userId, email } = res.data;
  210. store.dispatch("user/auth/authData", {
  211. loggedIn,
  212. role,
  213. username,
  214. email,
  215. userId
  216. });
  217. });
  218. ws.socket.on("keep.event:user.banned", res =>
  219. store.dispatch("user/auth/banUser", res.data.ban)
  220. );
  221. ws.socket.on("event:user.username.updated", res =>
  222. store.dispatch("user/auth/updateUsername", res.data.username)
  223. );
  224. ws.socket.on("keep.event:user.preferences.updated", res => {
  225. const { preferences } = res.data;
  226. if (preferences.autoSkipDisliked !== undefined)
  227. store.dispatch(
  228. "user/preferences/changeAutoSkipDisliked",
  229. preferences.autoSkipDisliked
  230. );
  231. if (preferences.nightmode !== undefined) {
  232. localStorage.setItem("nightmode", preferences.nightmode);
  233. store.dispatch(
  234. "user/preferences/changeNightmode",
  235. preferences.nightmode
  236. );
  237. }
  238. if (preferences.activityLogPublic !== undefined)
  239. store.dispatch(
  240. "user/preferences/changeActivityLogPublic",
  241. preferences.activityLogPublic
  242. );
  243. if (preferences.anonymousSongRequests !== undefined)
  244. store.dispatch(
  245. "user/preferences/changeAnonymousSongRequests",
  246. preferences.anonymousSongRequests
  247. );
  248. if (preferences.activityWatch !== undefined)
  249. store.dispatch(
  250. "user/preferences/changeActivityWatch",
  251. preferences.activityWatch
  252. );
  253. });
  254. app.mount("#root");
  255. })();