main.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 = 7;
  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. 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. if (preferences.autoSkipDisliked !== undefined)
  190. store.dispatch(
  191. "user/preferences/changeAutoSkipDisliked",
  192. preferences.autoSkipDisliked
  193. );
  194. if (preferences.nightmode !== undefined) {
  195. localStorage.setItem("nightmode", preferences.nightmode);
  196. store.dispatch(
  197. "user/preferences/changeNightmode",
  198. preferences.nightmode
  199. );
  200. }
  201. if (preferences.activityLogPublic !== undefined)
  202. store.dispatch(
  203. "user/preferences/changeActivityLogPublic",
  204. preferences.activityLogPublic
  205. );
  206. if (preferences.anonymousSongRequests !== undefined)
  207. store.dispatch(
  208. "user/preferences/changeAnonymousSongRequests",
  209. preferences.anonymousSongRequests
  210. );
  211. if (preferences.activityWatch !== undefined)
  212. store.dispatch(
  213. "user/preferences/changeActivityWatch",
  214. preferences.activityWatch
  215. );
  216. });
  217. router.beforeEach((to, from, next) => {
  218. if (window.stationInterval) {
  219. clearInterval(window.stationInterval);
  220. window.stationInterval = 0;
  221. }
  222. if (ws.socket && to.fullPath !== from.fullPath) {
  223. ws.clearCallbacks();
  224. ws.destroyListeners();
  225. }
  226. if (
  227. to.meta.loginRequired ||
  228. to.meta.adminRequired ||
  229. to.meta.guestsOnly
  230. ) {
  231. const gotData = () => {
  232. if (to.meta.loginRequired && !store.state.user.auth.loggedIn)
  233. next({ path: "/login" });
  234. else if (
  235. to.meta.adminRequired &&
  236. store.state.user.auth.role !== "admin"
  237. )
  238. next({ path: "/" });
  239. else if (to.meta.guestsOnly && store.state.user.auth.loggedIn)
  240. next({ path: "/" });
  241. else next();
  242. };
  243. if (store.state.user.auth.gotData) gotData();
  244. else {
  245. const watcher = store.watch(
  246. state => state.user.auth.gotData,
  247. () => {
  248. watcher();
  249. gotData();
  250. }
  251. );
  252. }
  253. } else next();
  254. });
  255. app.mount("#root");
  256. })();