main.js 7.0 KB

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