main.js 5.5 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. touch: false,
  18. popperOptions: {
  19. modifiers: {
  20. preventOverflow: {
  21. enabled: true
  22. }
  23. }
  24. },
  25. allowHTML: true,
  26. defaultProps: { animation: "scale" }
  27. });
  28. app.component("Tippy", Tippy);
  29. app.component("Metadata", {
  30. watch: {
  31. $attrs: {
  32. // eslint-disable-next-line vue/no-arrow-functions-in-watch
  33. handler: attrs => {
  34. handleMetadata(attrs);
  35. },
  36. deep: true,
  37. immediate: true
  38. }
  39. },
  40. render() {
  41. return null;
  42. }
  43. });
  44. app.directive("scroll", {
  45. mounted(el, binding) {
  46. const f = evt => {
  47. clearTimeout(window.scrollDebounceId);
  48. window.scrollDebounceId = setTimeout(() => {
  49. if (binding.value(evt, el)) {
  50. window.removeEventListener("scroll", f);
  51. }
  52. }, 200);
  53. };
  54. window.addEventListener("scroll", f);
  55. }
  56. });
  57. app.directive("focus", {
  58. mounted(el) {
  59. window.focusedElementBefore = document.activeElement;
  60. el.focus();
  61. }
  62. });
  63. const router = createRouter({
  64. history: createWebHistory(),
  65. routes: [
  66. {
  67. path: "/",
  68. component: () => import("@/pages/Home.vue")
  69. },
  70. {
  71. path: "/404",
  72. alias: ["/:pathMatch(.*)*"],
  73. component: () => import("@/pages/404.vue")
  74. },
  75. {
  76. path: "/terms",
  77. component: () => import("@/pages/Terms.vue")
  78. },
  79. {
  80. path: "/privacy",
  81. component: () => import("@/pages/Privacy.vue")
  82. },
  83. {
  84. path: "/team",
  85. component: () => import("@/pages/Team.vue")
  86. },
  87. {
  88. path: "/news",
  89. component: () => import("@/pages/News.vue")
  90. },
  91. {
  92. path: "/about",
  93. component: () => import("@/pages/About.vue")
  94. },
  95. {
  96. name: "profile",
  97. path: "/u/:username",
  98. component: () => import("@/pages/Profile/index.vue")
  99. },
  100. {
  101. path: "/settings",
  102. component: () => import("@/pages/Settings/index.vue"),
  103. meta: {
  104. loginRequired: true
  105. }
  106. },
  107. {
  108. path: "/reset_password",
  109. component: () => import("@/pages/ResetPassword.vue")
  110. },
  111. {
  112. path: "/set_password",
  113. props: { mode: "set" },
  114. component: () => import("@/pages/ResetPassword.vue"),
  115. meta: {
  116. loginRequired: true
  117. }
  118. },
  119. {
  120. path: "/login",
  121. component: () => import("@/components/modals/Login.vue"),
  122. meta: {
  123. guestsOnly: true
  124. }
  125. },
  126. {
  127. path: "/register",
  128. component: () => import("@/components/modals/Register.vue"),
  129. meta: {
  130. guestsOnly: true
  131. }
  132. },
  133. {
  134. path: "/admin",
  135. component: () => import("@/pages/Admin/index.vue"),
  136. meta: {
  137. adminRequired: true
  138. }
  139. },
  140. {
  141. path: "/admin/:page",
  142. component: () => import("@/pages//Admin/index.vue"),
  143. meta: {
  144. adminRequired: true
  145. }
  146. },
  147. {
  148. name: "station",
  149. path: "/:id",
  150. component: () => import("@/pages//Station/index.vue")
  151. }
  152. ]
  153. });
  154. app.use(router);
  155. lofig.folder = "../config/default.json";
  156. (async () => {
  157. lofig.fetchConfig().then(config => {
  158. const { configVersion, skipConfigVersionCheck } = config;
  159. if (
  160. configVersion !== REQUIRED_CONFIG_VERSION &&
  161. !skipConfigVersionCheck
  162. ) {
  163. // eslint-disable-next-line no-alert
  164. alert(
  165. "CONFIG VERSION IS WRONG. PLEASE UPDATE YOUR CONFIG WITH THE HELP OF THE TEMPLATE FILE AND THE README FILE."
  166. );
  167. window.stop();
  168. }
  169. });
  170. const websocketsDomain = await lofig.get("websocketsDomain");
  171. ws.init(websocketsDomain);
  172. ws.socket.on("ready", res => {
  173. const { loggedIn, role, username, userId, email } = res.data;
  174. store.dispatch("user/auth/authData", {
  175. loggedIn,
  176. role,
  177. username,
  178. email,
  179. userId
  180. });
  181. });
  182. ws.socket.on("keep.event:user.banned", res =>
  183. store.dispatch("user/auth/banUser", res.data.ban)
  184. );
  185. ws.socket.on("event:user.username.updated", res =>
  186. store.dispatch("user/auth/updateUsername", res.data.username)
  187. );
  188. ws.socket.on("keep.event:user.preferences.updated", res => {
  189. const { preferences } = res.data;
  190. store.dispatch(
  191. "user/preferences/changeAutoSkipDisliked",
  192. preferences.autoSkipDisliked
  193. );
  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. })();