main.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. const globalComponents = require.context(
  53. "@/components/global/",
  54. true,
  55. /\.vue$/i
  56. );
  57. globalComponents.keys().forEach(componentFilePath => {
  58. const componentName = componentFilePath.split("/").pop().split(".")[0];
  59. const component = globalComponents(componentFilePath);
  60. app.component(componentName, component.default || component);
  61. });
  62. app.directive("scroll", {
  63. mounted(el, binding) {
  64. const f = evt => {
  65. clearTimeout(window.scrollDebounceId);
  66. window.scrollDebounceId = setTimeout(() => {
  67. if (binding.value(evt, el)) {
  68. window.removeEventListener("scroll", f);
  69. }
  70. }, 200);
  71. };
  72. window.addEventListener("scroll", f);
  73. }
  74. });
  75. app.directive("focus", {
  76. mounted(el) {
  77. window.focusedElementBefore = document.activeElement;
  78. el.focus();
  79. }
  80. });
  81. const router = createRouter({
  82. history: createWebHistory(),
  83. routes: [
  84. {
  85. name: "home",
  86. path: "/",
  87. component: () => import("@/pages/Home.vue")
  88. },
  89. {
  90. path: "/login",
  91. name: "login",
  92. redirect: "/"
  93. },
  94. {
  95. path: "/register",
  96. name: "register",
  97. redirect: "/"
  98. },
  99. {
  100. path: "/404",
  101. alias: ["/:pathMatch(.*)*"],
  102. component: () => import("@/pages/404.vue")
  103. },
  104. {
  105. path: "/terms",
  106. component: () => import("@/pages/Terms.vue")
  107. },
  108. {
  109. path: "/privacy",
  110. component: () => import("@/pages/Privacy.vue")
  111. },
  112. {
  113. path: "/team",
  114. component: () => import("@/pages/Team.vue")
  115. },
  116. {
  117. path: "/news",
  118. component: () => import("@/pages/News.vue")
  119. },
  120. {
  121. path: "/about",
  122. component: () => import("@/pages/About.vue")
  123. },
  124. {
  125. name: "profile",
  126. path: "/u/:username",
  127. component: () => import("@/pages/Profile/index.vue")
  128. },
  129. {
  130. path: "/settings",
  131. component: () => import("@/pages/Settings/index.vue"),
  132. meta: {
  133. loginRequired: true
  134. }
  135. },
  136. {
  137. path: "/reset_password",
  138. component: () => import("@/pages/ResetPassword.vue")
  139. },
  140. {
  141. path: "/set_password",
  142. props: { mode: "set" },
  143. component: () => import("@/pages/ResetPassword.vue"),
  144. meta: {
  145. loginRequired: true
  146. }
  147. },
  148. {
  149. path: "/admin",
  150. name: "admin",
  151. component: () => import("@/pages/Admin/index.vue"),
  152. children: [
  153. {
  154. path: "songs",
  155. component: () => import("@/pages/Admin/Songs/index.vue")
  156. },
  157. {
  158. path: "songs/import",
  159. component: () => import("@/pages/Admin/Songs/Import.vue")
  160. },
  161. {
  162. path: "reports",
  163. component: () => import("@/pages/Admin/Reports.vue")
  164. },
  165. {
  166. path: "stations",
  167. component: () => import("@/pages/Admin/Stations.vue")
  168. },
  169. {
  170. path: "playlists",
  171. component: () => import("@/pages/Admin/Playlists.vue")
  172. },
  173. {
  174. path: "users",
  175. component: () => import("@/pages/Admin/Users/index.vue")
  176. },
  177. {
  178. path: "users/data-requests",
  179. component: () =>
  180. import("@/pages/Admin/Users/DataRequests.vue")
  181. },
  182. {
  183. path: "users/punishments",
  184. component: () =>
  185. import("@/pages/Admin/Users/Punishments.vue")
  186. },
  187. {
  188. path: "news",
  189. component: () => import("@/pages/Admin/News.vue")
  190. },
  191. {
  192. path: "statistics",
  193. component: () => import("@/pages/Admin/Statistics.vue")
  194. },
  195. {
  196. path: "youtube",
  197. component: () => import("@/pages/Admin/YouTube/index.vue")
  198. },
  199. {
  200. path: "youtube/videos",
  201. component: () => import("@/pages/Admin/YouTube/Videos.vue")
  202. }
  203. ],
  204. meta: {
  205. adminRequired: true
  206. }
  207. },
  208. {
  209. name: "station",
  210. path: "/:id",
  211. component: () => import("@/pages//Station/index.vue")
  212. }
  213. ]
  214. });
  215. router.beforeEach((to, from, next) => {
  216. if (window.stationInterval) {
  217. clearInterval(window.stationInterval);
  218. window.stationInterval = 0;
  219. }
  220. // if (to.name === "station") {
  221. // store.dispatch("modalVisibility/closeModal", "manageStation");
  222. // }
  223. store.dispatch("modalVisibility/closeAllModals");
  224. if (ws.socket && to.fullPath !== from.fullPath) {
  225. ws.clearCallbacks();
  226. ws.destroyListeners();
  227. }
  228. if (to.meta.loginRequired || to.meta.adminRequired || to.meta.guestsOnly) {
  229. const gotData = () => {
  230. if (to.meta.loginRequired && !store.state.user.auth.loggedIn)
  231. next({ path: "/login", query: "" });
  232. else if (
  233. to.meta.adminRequired &&
  234. store.state.user.auth.role !== "admin"
  235. )
  236. next({ path: "/", query: "" });
  237. else if (to.meta.guestsOnly && store.state.user.auth.loggedIn)
  238. next({ path: "/", query: "" });
  239. else next();
  240. };
  241. if (store.state.user.auth.gotData) gotData();
  242. else {
  243. const watcher = store.watch(
  244. state => state.user.auth.gotData,
  245. () => {
  246. watcher();
  247. gotData();
  248. }
  249. );
  250. }
  251. } else next();
  252. });
  253. app.use(router);
  254. lofig.folder = "/config/default.json";
  255. (async () => {
  256. lofig.fetchConfig().then(config => {
  257. const { configVersion, skipConfigVersionCheck } = config;
  258. if (
  259. configVersion !== REQUIRED_CONFIG_VERSION &&
  260. !skipConfigVersionCheck
  261. ) {
  262. // eslint-disable-next-line no-alert
  263. alert(
  264. "CONFIG VERSION IS WRONG. PLEASE UPDATE YOUR CONFIG WITH THE HELP OF THE TEMPLATE FILE AND THE README FILE."
  265. );
  266. window.stop();
  267. }
  268. });
  269. const websocketsDomain = await lofig.get("backend.websocketsDomain");
  270. ws.init(websocketsDomain);
  271. if (await lofig.get("siteSettings.mediasession")) ms.init();
  272. ws.socket.on("ready", res => {
  273. const { loggedIn, role, username, userId, email } = res.data;
  274. store.dispatch("user/auth/authData", {
  275. loggedIn,
  276. role,
  277. username,
  278. email,
  279. userId
  280. });
  281. });
  282. ws.socket.on("keep.event:user.banned", res =>
  283. store.dispatch("user/auth/banUser", res.data.ban)
  284. );
  285. ws.socket.on("keep.event:user.username.updated", res =>
  286. store.dispatch("user/auth/updateUsername", res.data.username)
  287. );
  288. ws.socket.on("keep.event:user.preferences.updated", res => {
  289. const { preferences } = res.data;
  290. if (preferences.autoSkipDisliked !== undefined)
  291. store.dispatch(
  292. "user/preferences/changeAutoSkipDisliked",
  293. preferences.autoSkipDisliked
  294. );
  295. if (preferences.nightmode !== undefined) {
  296. localStorage.setItem("nightmode", preferences.nightmode);
  297. store.dispatch(
  298. "user/preferences/changeNightmode",
  299. preferences.nightmode
  300. );
  301. }
  302. if (preferences.activityLogPublic !== undefined)
  303. store.dispatch(
  304. "user/preferences/changeActivityLogPublic",
  305. preferences.activityLogPublic
  306. );
  307. if (preferences.anonymousSongRequests !== undefined)
  308. store.dispatch(
  309. "user/preferences/changeAnonymousSongRequests",
  310. preferences.anonymousSongRequests
  311. );
  312. if (preferences.activityWatch !== undefined)
  313. store.dispatch(
  314. "user/preferences/changeActivityWatch",
  315. preferences.activityWatch
  316. );
  317. });
  318. app.mount("#root");
  319. })();