main.ts 8.4 KB

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