main.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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 { createPinia } from "pinia";
  6. import "lofig";
  7. import Toast from "toasters";
  8. import { useUserAuthStore } from "@/stores/userAuth";
  9. import { useUserPreferencesStore } from "@/stores/userPreferences";
  10. import { useModalsStore } from "@/stores/modals";
  11. import { useWebsocketsStore } from "@/stores/websockets";
  12. import ms from "@/ms";
  13. import i18n from "@/i18n";
  14. import AppComponent from "./App.vue";
  15. const defaultConfigURL = new URL(
  16. "/config/default.json",
  17. import.meta.url
  18. ).toString();
  19. const REQUIRED_CONFIG_VERSION = 13;
  20. lofig.folder = defaultConfigURL;
  21. const handleMetadata = attrs => {
  22. lofig.get("siteSettings.sitename").then(siteName => {
  23. if (siteName) {
  24. document.title = `${siteName} | ${attrs.title}`;
  25. } else {
  26. document.title = `Musare | ${attrs.title}`;
  27. }
  28. });
  29. };
  30. const app = createApp(AppComponent);
  31. app.use(i18n);
  32. app.use(VueTippy, {
  33. directive: "tippy", // => v-tippy
  34. flipDuration: 0,
  35. popperOptions: {
  36. modifiers: {
  37. preventOverflow: {
  38. enabled: true
  39. }
  40. }
  41. },
  42. allowHTML: true,
  43. defaultProps: { animation: "scale", touch: "hold" }
  44. });
  45. app.component("Tippy", Tippy);
  46. app.component("PageMetadata", {
  47. watch: {
  48. $attrs: {
  49. // eslint-disable-next-line vue/no-arrow-functions-in-watch
  50. handler: attrs => {
  51. handleMetadata(attrs);
  52. },
  53. deep: true,
  54. immediate: true
  55. }
  56. },
  57. render() {
  58. return null;
  59. }
  60. });
  61. app.directive("scroll", {
  62. mounted(el, binding) {
  63. const f = (evt: Event) => {
  64. clearTimeout(window.scrollDebounceId);
  65. window.scrollDebounceId = setTimeout(() => {
  66. if (binding.value(evt, el)) {
  67. document.body.removeEventListener("scroll", f);
  68. }
  69. }, 200);
  70. };
  71. document.body.addEventListener("scroll", f);
  72. }
  73. });
  74. app.directive("focus", {
  75. mounted(el) {
  76. window.focusedElementBefore = document.activeElement;
  77. el.focus();
  78. }
  79. });
  80. const router = createRouter({
  81. history: createWebHistory(),
  82. routes: [
  83. {
  84. name: "home",
  85. path: "/",
  86. component: () => import("@/pages/Home.vue")
  87. },
  88. {
  89. path: "/login",
  90. name: "login",
  91. redirect: "/"
  92. },
  93. {
  94. path: "/register",
  95. name: "register",
  96. redirect: "/"
  97. },
  98. {
  99. path: "/404",
  100. alias: ["/:pathMatch(.*)*"],
  101. component: () => import("@/pages/404.vue")
  102. },
  103. {
  104. path: "/terms",
  105. component: () => import("@/pages/Terms.vue")
  106. },
  107. {
  108. path: "/privacy",
  109. component: () => import("@/pages/Privacy.vue")
  110. },
  111. {
  112. path: "/team",
  113. component: () => import("@/pages/Team.vue")
  114. },
  115. {
  116. path: "/news",
  117. component: () => import("@/pages/News.vue")
  118. },
  119. {
  120. path: "/about",
  121. component: () => import("@/pages/About.vue")
  122. },
  123. {
  124. name: "profile",
  125. path: "/u/:username",
  126. component: () => import("@/pages/Profile/index.vue")
  127. },
  128. {
  129. path: "/settings",
  130. component: () => import("@/pages/Settings/index.vue"),
  131. meta: {
  132. loginRequired: true
  133. }
  134. },
  135. {
  136. path: "/reset_password",
  137. component: () => import("@/pages/ResetPassword.vue")
  138. },
  139. {
  140. path: "/set_password",
  141. props: { mode: "set" },
  142. component: () => import("@/pages/ResetPassword.vue"),
  143. meta: {
  144. loginRequired: true
  145. }
  146. },
  147. {
  148. path: "/admin",
  149. name: "admin",
  150. component: () => import("@/pages/Admin/index.vue"),
  151. children: [
  152. {
  153. path: "songs",
  154. component: () => import("@/pages/Admin/Songs/index.vue"),
  155. meta: { permissionRequired: "admin.view.songs" }
  156. },
  157. {
  158. path: "songs/import",
  159. component: () => import("@/pages/Admin/Songs/Import.vue"),
  160. meta: { permissionRequired: "admin.view.import" }
  161. },
  162. {
  163. path: "reports",
  164. component: () => import("@/pages/Admin/Reports.vue"),
  165. meta: { permissionRequired: "admin.view.reports" }
  166. },
  167. {
  168. path: "stations",
  169. component: () => import("@/pages/Admin/Stations.vue"),
  170. meta: { permissionRequired: "admin.view.stations" }
  171. },
  172. {
  173. path: "playlists",
  174. component: () => import("@/pages/Admin/Playlists.vue"),
  175. meta: { permissionRequired: "admin.view.playlists" }
  176. },
  177. {
  178. path: "users",
  179. component: () => import("@/pages/Admin/Users/index.vue"),
  180. meta: { permissionRequired: "admin.view.users" }
  181. },
  182. {
  183. path: "users/data-requests",
  184. component: () =>
  185. import("@/pages/Admin/Users/DataRequests.vue"),
  186. meta: { permissionRequired: "admin.view.dataRequests" }
  187. },
  188. {
  189. path: "users/punishments",
  190. component: () =>
  191. import("@/pages/Admin/Users/Punishments.vue"),
  192. meta: {
  193. permissionRequired: "admin.view.punishments"
  194. }
  195. },
  196. {
  197. path: "news",
  198. component: () => import("@/pages/Admin/News.vue"),
  199. meta: { permissionRequired: "admin.view.news" }
  200. },
  201. {
  202. path: "statistics",
  203. component: () => import("@/pages/Admin/Statistics.vue"),
  204. meta: {
  205. permissionRequired: "admin.view.statistics"
  206. }
  207. },
  208. {
  209. path: "youtube",
  210. component: () => import("@/pages/Admin/YouTube/index.vue"),
  211. meta: { permissionRequired: "admin.view.youtube" }
  212. },
  213. {
  214. path: "youtube/videos",
  215. component: () => import("@/pages/Admin/YouTube/Videos.vue"),
  216. meta: {
  217. permissionRequired: "admin.view.youtubeVideos"
  218. }
  219. }
  220. ],
  221. meta: {
  222. permissionRequired: "admin.view"
  223. }
  224. },
  225. {
  226. name: "station",
  227. path: "/:id",
  228. component: () => import("@/pages//Station/index.vue")
  229. }
  230. ]
  231. });
  232. app.use(createPinia());
  233. const { createSocket } = useWebsocketsStore();
  234. createSocket().then(async socket => {
  235. const userAuthStore = useUserAuthStore();
  236. const modalsStore = useModalsStore();
  237. router.beforeEach((to, from, next) => {
  238. if (window.stationInterval) {
  239. clearInterval(window.stationInterval);
  240. window.stationInterval = 0;
  241. }
  242. // if (to.name === "station") {
  243. // modalsStore.closeModal("manageStation");
  244. // }
  245. modalsStore.closeAllModals();
  246. if (socket.ready && to.fullPath !== from.fullPath) {
  247. socket.clearCallbacks();
  248. socket.destroyListeners();
  249. }
  250. if (to.query.toast) {
  251. const toast =
  252. typeof to.query.toast === "string"
  253. ? { content: to.query.toast, timeout: 20000 }
  254. : { ...to.query.toast };
  255. new Toast(toast);
  256. const { query } = to;
  257. delete query.toast;
  258. next({ ...to, query });
  259. } else if (
  260. to.meta.loginRequired ||
  261. to.meta.permissionRequired ||
  262. to.meta.guestsOnly
  263. ) {
  264. const gotData = () => {
  265. if (to.meta.loginRequired && !userAuthStore.loggedIn)
  266. next({ path: "/login" });
  267. else if (
  268. to.meta.permissionRequired &&
  269. !userAuthStore.hasPermission(
  270. `${to.meta.permissionRequired}`
  271. )
  272. )
  273. next({ path: "/" });
  274. else if (to.meta.guestsOnly && userAuthStore.loggedIn)
  275. next({ path: "/" });
  276. else next();
  277. };
  278. if (userAuthStore.gotData && userAuthStore.gotPermissions)
  279. gotData();
  280. else {
  281. const unsubscribe = userAuthStore.$onAction(
  282. ({ name, after, onError }) => {
  283. if (
  284. name === "authData" ||
  285. name === "updatePermissions"
  286. ) {
  287. after(() => {
  288. if (
  289. userAuthStore.gotData &&
  290. userAuthStore.gotPermissions
  291. )
  292. gotData();
  293. unsubscribe();
  294. });
  295. onError(() => {
  296. unsubscribe();
  297. });
  298. }
  299. }
  300. );
  301. }
  302. } else next();
  303. });
  304. app.use(router);
  305. lofig.fetchConfig().then(config => {
  306. const { configVersion, skipConfigVersionCheck } = config;
  307. if (
  308. configVersion !== REQUIRED_CONFIG_VERSION &&
  309. !skipConfigVersionCheck
  310. ) {
  311. // eslint-disable-next-line no-alert
  312. alert(
  313. "CONFIG VERSION IS WRONG. PLEASE UPDATE YOUR CONFIG WITH THE HELP OF THE TEMPLATE FILE AND THE README FILE."
  314. );
  315. window.stop();
  316. }
  317. });
  318. socket.on("ready", res => {
  319. const { loggedIn, role, username, userId, email } = res.data;
  320. userAuthStore.authData({
  321. loggedIn,
  322. role,
  323. username,
  324. email,
  325. userId
  326. });
  327. });
  328. socket.on("keep.event:user.banned", res =>
  329. userAuthStore.banUser(res.data.ban)
  330. );
  331. socket.on("keep.event:user.username.updated", res =>
  332. userAuthStore.updateUsername(res.data.username)
  333. );
  334. socket.on("keep.event:user.preferences.updated", res => {
  335. const { preferences } = res.data;
  336. const {
  337. changeAutoSkipDisliked,
  338. changeNightmode,
  339. changeActivityLogPublic,
  340. changeAnonymousSongRequests,
  341. changeActivityWatch
  342. } = useUserPreferencesStore();
  343. if (preferences.autoSkipDisliked !== undefined)
  344. changeAutoSkipDisliked(preferences.autoSkipDisliked);
  345. if (preferences.nightmode !== undefined) {
  346. changeNightmode(preferences.nightmode);
  347. }
  348. if (preferences.activityLogPublic !== undefined)
  349. changeActivityLogPublic(preferences.activityLogPublic);
  350. if (preferences.anonymousSongRequests !== undefined)
  351. changeAnonymousSongRequests(preferences.anonymousSongRequests);
  352. if (preferences.activityWatch !== undefined)
  353. changeActivityWatch(preferences.activityWatch);
  354. });
  355. socket.on("keep.event:user.role.updated", res => {
  356. userAuthStore.updateRole(res.data.role);
  357. userAuthStore.updatePermissions().then(() => {
  358. const { meta } = router.currentRoute.value;
  359. if (
  360. meta &&
  361. meta.permissionRequired &&
  362. !userAuthStore.hasPermission(`${meta.permissionRequired}`)
  363. )
  364. router.push({
  365. path: "/",
  366. query: {
  367. toast: "You no longer have access to the page you were viewing."
  368. }
  369. });
  370. });
  371. });
  372. lofig.get("experimental").then(experimental => {
  373. if (
  374. experimental &&
  375. Object.hasOwn(experimental, "media_session") &&
  376. experimental.media_session
  377. )
  378. ms.init();
  379. });
  380. app.mount("#root");
  381. });