main.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 "lofig";
  6. import ws from "@/ws";
  7. import ms from "@/ms";
  8. import store from "./store";
  9. import AppComponent from "./App.vue";
  10. const REQUIRED_CONFIG_VERSION = 11;
  11. const handleMetadata = attrs => {
  12. document.title = `Musare | ${attrs.title}`;
  13. };
  14. const app = createApp(AppComponent);
  15. app.use(store);
  16. app.use(VueTippy, {
  17. directive: "tippy", // => v-tippy
  18. flipDuration: 0,
  19. popperOptions: {
  20. modifiers: {
  21. preventOverflow: {
  22. enabled: true
  23. }
  24. }
  25. },
  26. allowHTML: true,
  27. defaultProps: { animation: "scale", touch: "hold" }
  28. });
  29. app.component("Tippy", Tippy);
  30. app.component("PageMetadata", {
  31. watch: {
  32. $attrs: {
  33. // eslint-disable-next-line vue/no-arrow-functions-in-watch
  34. handler: attrs => {
  35. handleMetadata(attrs);
  36. },
  37. deep: true,
  38. immediate: true
  39. }
  40. },
  41. render() {
  42. return null;
  43. }
  44. });
  45. app.directive("scroll", {
  46. mounted(el, binding) {
  47. const f = evt => {
  48. clearTimeout(window.scrollDebounceId);
  49. window.scrollDebounceId = setTimeout(() => {
  50. if (binding.value(evt, el)) {
  51. window.removeEventListener("scroll", f);
  52. }
  53. }, 200);
  54. };
  55. window.addEventListener("scroll", f);
  56. }
  57. });
  58. app.directive("focus", {
  59. mounted(el) {
  60. window.focusedElementBefore = document.activeElement;
  61. el.focus();
  62. }
  63. });
  64. const router = createRouter({
  65. history: createWebHistory(),
  66. routes: [
  67. {
  68. name: "home",
  69. path: "/",
  70. component: () => import("@/pages/Home.vue")
  71. },
  72. {
  73. path: "/login",
  74. name: "login",
  75. redirect: "/"
  76. },
  77. {
  78. path: "/register",
  79. name: "register",
  80. redirect: "/"
  81. },
  82. {
  83. path: "/404",
  84. alias: ["/:pathMatch(.*)*"],
  85. component: () => import("@/pages/404.vue")
  86. },
  87. {
  88. path: "/terms",
  89. component: () => import("@/pages/Terms.vue")
  90. },
  91. {
  92. path: "/privacy",
  93. component: () => import("@/pages/Privacy.vue")
  94. },
  95. {
  96. path: "/team",
  97. component: () => import("@/pages/Team.vue")
  98. },
  99. {
  100. path: "/news",
  101. component: () => import("@/pages/News.vue")
  102. },
  103. {
  104. path: "/about",
  105. component: () => import("@/pages/About.vue")
  106. },
  107. {
  108. name: "profile",
  109. path: "/u/:username",
  110. component: () => import("@/pages/Profile/index.vue")
  111. },
  112. {
  113. path: "/settings",
  114. component: () => import("@/pages/Settings/index.vue"),
  115. meta: {
  116. loginRequired: true
  117. }
  118. },
  119. {
  120. path: "/reset_password",
  121. component: () => import("@/pages/ResetPassword.vue")
  122. },
  123. {
  124. path: "/set_password",
  125. props: { mode: "set" },
  126. component: () => import("@/pages/ResetPassword.vue"),
  127. meta: {
  128. loginRequired: true
  129. }
  130. },
  131. {
  132. path: "/admin",
  133. component: () => import("@/pages/Admin/index.vue"),
  134. children: [
  135. {
  136. path: "songs",
  137. component: () => import("@/pages/Admin/Songs.vue")
  138. },
  139. {
  140. path: "reports",
  141. component: () => import("@/pages/Admin/Reports.vue")
  142. },
  143. {
  144. path: "stations",
  145. component: () => import("@/pages/Admin/Stations.vue")
  146. },
  147. {
  148. path: "playlists",
  149. component: () => import("@/pages/Admin/Playlists.vue")
  150. },
  151. {
  152. path: "users",
  153. component: () => import("@/pages/Admin/Users/index.vue")
  154. },
  155. {
  156. path: "users/data-requests",
  157. component: () =>
  158. import("@/pages/Admin/Users/DataRequests.vue")
  159. },
  160. {
  161. path: "punishments",
  162. component: () => import("@/pages/Admin/Punishments.vue")
  163. },
  164. {
  165. path: "news",
  166. component: () => import("@/pages/Admin/News.vue")
  167. },
  168. {
  169. path: "statistics",
  170. component: () => import("@/pages/Admin/Statistics.vue")
  171. }
  172. ],
  173. meta: {
  174. adminRequired: true
  175. }
  176. },
  177. {
  178. name: "station",
  179. path: "/:id",
  180. component: () => import("@/pages//Station/index.vue")
  181. }
  182. ]
  183. });
  184. router.beforeEach((to, from, next) => {
  185. if (window.stationInterval) {
  186. clearInterval(window.stationInterval);
  187. window.stationInterval = 0;
  188. }
  189. if (from.name === "home" && to.name === "station") {
  190. if (store.state.modalVisibility.modals.manageStation)
  191. store.dispatch("modalVisibility/closeModal", "manageStation");
  192. }
  193. if (ws.socket && to.fullPath !== from.fullPath) {
  194. ws.clearCallbacks();
  195. ws.destroyListeners();
  196. }
  197. if (to.meta.loginRequired || to.meta.adminRequired || to.meta.guestsOnly) {
  198. const gotData = () => {
  199. if (to.meta.loginRequired && !store.state.user.auth.loggedIn)
  200. next({ path: "/login", query: "" });
  201. else if (
  202. to.meta.adminRequired &&
  203. store.state.user.auth.role !== "admin"
  204. )
  205. next({ path: "/", query: "" });
  206. else if (to.meta.guestsOnly && store.state.user.auth.loggedIn)
  207. next({ path: "/", query: "" });
  208. else next();
  209. };
  210. if (store.state.user.auth.gotData) gotData();
  211. else {
  212. const watcher = store.watch(
  213. state => state.user.auth.gotData,
  214. () => {
  215. watcher();
  216. gotData();
  217. }
  218. );
  219. }
  220. } else next();
  221. });
  222. app.use(router);
  223. lofig.folder = "/config/default.json";
  224. (async () => {
  225. lofig.fetchConfig().then(config => {
  226. const { configVersion, skipConfigVersionCheck } = config;
  227. if (
  228. configVersion !== REQUIRED_CONFIG_VERSION &&
  229. !skipConfigVersionCheck
  230. ) {
  231. // eslint-disable-next-line no-alert
  232. alert(
  233. "CONFIG VERSION IS WRONG. PLEASE UPDATE YOUR CONFIG WITH THE HELP OF THE TEMPLATE FILE AND THE README FILE."
  234. );
  235. window.stop();
  236. }
  237. });
  238. const websocketsDomain = await lofig.get("backend.websocketsDomain");
  239. ws.init(websocketsDomain);
  240. if (await lofig.get("siteSettings.mediasession")) ms.init();
  241. ws.socket.on("ready", res => {
  242. const { loggedIn, role, username, userId, email } = res.data;
  243. store.dispatch("user/auth/authData", {
  244. loggedIn,
  245. role,
  246. username,
  247. email,
  248. userId
  249. });
  250. });
  251. ws.socket.on("keep.event:user.banned", res =>
  252. store.dispatch("user/auth/banUser", res.data.ban)
  253. );
  254. ws.socket.on("keep.event:user.username.updated", res =>
  255. store.dispatch("user/auth/updateUsername", res.data.username)
  256. );
  257. ws.socket.on("keep.event:user.preferences.updated", res => {
  258. const { preferences } = res.data;
  259. if (preferences.autoSkipDisliked !== undefined)
  260. store.dispatch(
  261. "user/preferences/changeAutoSkipDisliked",
  262. preferences.autoSkipDisliked
  263. );
  264. if (preferences.nightmode !== undefined) {
  265. localStorage.setItem("nightmode", preferences.nightmode);
  266. store.dispatch(
  267. "user/preferences/changeNightmode",
  268. preferences.nightmode
  269. );
  270. }
  271. if (preferences.activityLogPublic !== undefined)
  272. store.dispatch(
  273. "user/preferences/changeActivityLogPublic",
  274. preferences.activityLogPublic
  275. );
  276. if (preferences.anonymousSongRequests !== undefined)
  277. store.dispatch(
  278. "user/preferences/changeAnonymousSongRequests",
  279. preferences.anonymousSongRequests
  280. );
  281. if (preferences.activityWatch !== undefined)
  282. store.dispatch(
  283. "user/preferences/changeActivityWatch",
  284. preferences.activityWatch
  285. );
  286. });
  287. app.mount("#root");
  288. })();