Users.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. <script setup lang="ts">
  2. import { useRoute } from "vue-router";
  3. import {
  4. defineAsyncComponent,
  5. ref,
  6. reactive,
  7. computed,
  8. watch,
  9. onMounted
  10. } from "vue";
  11. import Toast from "toasters";
  12. import { storeToRefs } from "pinia";
  13. import { useWebsocketsStore } from "@/stores/websockets";
  14. import { useStationStore } from "@/stores/station";
  15. const ProfilePicture = defineAsyncComponent(
  16. () => import("@/components/ProfilePicture.vue")
  17. );
  18. const stationStore = useStationStore();
  19. const route = useRoute();
  20. const notesUri = ref("");
  21. const frontendDomain = ref("");
  22. const tab = ref("active");
  23. const tabs = ref([]);
  24. const search = reactive({
  25. query: "",
  26. searchedQuery: "",
  27. page: 0,
  28. count: 0,
  29. resultsLeft: 0,
  30. pageSize: 0,
  31. results: []
  32. });
  33. const { socket } = useWebsocketsStore();
  34. const { station, users, userCount } = storeToRefs(stationStore);
  35. const isOwner = userId => station.value.owner === userId;
  36. const isDj = userId => !!station.value.djs.find(dj => dj._id === userId);
  37. const sortedUsers = computed(() =>
  38. users.value && users.value.loggedIn
  39. ? users.value.loggedIn
  40. .slice()
  41. .sort(
  42. (a, b) =>
  43. Number(isOwner(b._id)) - Number(isOwner(a._id)) ||
  44. Number(!isOwner(a._id)) - Number(!isOwner(b._id))
  45. )
  46. : []
  47. );
  48. const resultsLeftCount = computed(() => search.count - search.results.length);
  49. const nextPageResultsCount = computed(() =>
  50. Math.min(search.pageSize, resultsLeftCount.value)
  51. );
  52. const { hasPermission } = stationStore;
  53. const copyToClipboard = async () => {
  54. try {
  55. await navigator.clipboard.writeText(
  56. frontendDomain.value + route.fullPath
  57. );
  58. } catch (err) {
  59. new Toast("Failed to copy to clipboard.");
  60. }
  61. };
  62. const showTab = _tab => {
  63. if (tabs.value[`${_tab}-tab`])
  64. tabs.value[`${_tab}-tab`].scrollIntoView({ block: "nearest" });
  65. tab.value = _tab;
  66. };
  67. const addDj = userId => {
  68. socket.dispatch("stations.addDj", station.value._id, userId, res => {
  69. new Toast(res.message);
  70. });
  71. };
  72. const removeDj = userId => {
  73. socket.dispatch("stations.removeDj", station.value._id, userId, res => {
  74. new Toast(res.message);
  75. });
  76. };
  77. const searchForUser = page => {
  78. if (search.page >= page || search.searchedQuery !== search.query) {
  79. search.results = [];
  80. search.page = 0;
  81. search.count = 0;
  82. search.resultsLeft = 0;
  83. search.pageSize = 0;
  84. }
  85. search.searchedQuery = search.query;
  86. socket.dispatch("users.search", search.query, page, res => {
  87. const { data } = res;
  88. if (res.status === "success") {
  89. const { count, pageSize, users } = data;
  90. search.results = [...search.results, ...users];
  91. search.page = page;
  92. search.count = count;
  93. search.resultsLeft = count - search.results.length;
  94. search.pageSize = pageSize;
  95. } else if (res.status === "error") {
  96. search.results = [];
  97. search.page = 0;
  98. search.count = 0;
  99. search.resultsLeft = 0;
  100. search.pageSize = 0;
  101. new Toast(res.message);
  102. }
  103. });
  104. };
  105. watch(
  106. () => hasPermission("stations.update"),
  107. value => {
  108. if (!value && (tab.value === "djs" || tab.value === "add-dj"))
  109. showTab("active");
  110. }
  111. );
  112. onMounted(async () => {
  113. frontendDomain.value = await lofig.get("frontendDomain");
  114. notesUri.value = encodeURI(`${frontendDomain.value}/assets/notes.png`);
  115. });
  116. </script>
  117. <template>
  118. <div id="users">
  119. <div class="tabs-container">
  120. <div v-if="hasPermission('stations.update')" class="tab-selection">
  121. <button
  122. class="button is-default"
  123. :ref="el => (tabs['active-tab'] = el)"
  124. :class="{ selected: tab === 'active' }"
  125. @click="showTab('active')"
  126. >
  127. Active
  128. </button>
  129. <button
  130. class="button is-default"
  131. :ref="el => (tabs['djs-tab'] = el)"
  132. :class="{ selected: tab === 'djs' }"
  133. @click="showTab('djs')"
  134. >
  135. DJs
  136. </button>
  137. <button
  138. class="button is-default"
  139. :ref="el => (tabs['add-dj-tab'] = el)"
  140. :class="{ selected: tab === 'add-dj' }"
  141. @click="showTab('add-dj')"
  142. >
  143. Add DJ
  144. </button>
  145. </div>
  146. <div class="tab" v-show="tab === 'active'">
  147. <h5 class="has-text-centered">Total users: {{ userCount }}</h5>
  148. <transition-group name="notification-box">
  149. <h6
  150. class="has-text-centered"
  151. v-if="
  152. users.loggedIn &&
  153. users.loggedOut &&
  154. ((users.loggedIn.length === 1 &&
  155. users.loggedOut.length === 0) ||
  156. (users.loggedIn.length === 0 &&
  157. users.loggedOut.length === 1))
  158. "
  159. key="only-me"
  160. >
  161. It's just you in the station!
  162. </h6>
  163. <h6
  164. class="has-text-centered"
  165. v-else-if="
  166. users.loggedIn &&
  167. users.loggedOut &&
  168. users.loggedOut.length > 0
  169. "
  170. key="logged-out-users"
  171. >
  172. {{ users.loggedOut.length }}
  173. {{
  174. users.loggedOut.length > 1 ? "users are" : "user is"
  175. }}
  176. logged-out.
  177. </h6>
  178. </transition-group>
  179. <aside class="menu">
  180. <ul class="menu-list scrollable-list">
  181. <li v-for="user in sortedUsers" :key="user.username">
  182. <router-link
  183. :to="{
  184. name: 'profile',
  185. params: { username: user.username }
  186. }"
  187. target="_blank"
  188. >
  189. <div class="left">
  190. <profile-picture
  191. :avatar="user.avatar"
  192. :name="user.name || user.username"
  193. />
  194. {{ user.name || user.username }}
  195. <span
  196. v-if="isOwner(user._id)"
  197. class="material-icons user-rank"
  198. content="Station Owner"
  199. v-tippy="{ theme: 'info' }"
  200. >local_police</span
  201. >
  202. <span
  203. v-else-if="isDj(user._id)"
  204. class="material-icons user-rank"
  205. content="Station DJ"
  206. v-tippy="{ theme: 'info' }"
  207. >shield</span
  208. >
  209. <button
  210. v-if="
  211. hasPermission('stations.djs.add') &&
  212. !isDj(user._id) &&
  213. !isOwner(user._id)
  214. "
  215. class="button is-primary material-icons"
  216. @click.prevent="addDj(user._id)"
  217. content="Promote user to DJ"
  218. v-tippy
  219. >
  220. add_moderator
  221. </button>
  222. <button
  223. v-else-if="
  224. hasPermission(
  225. 'stations.djs.remove'
  226. ) && isDj(user._id)
  227. "
  228. class="button is-danger material-icons"
  229. @click.prevent="removeDj(user._id)"
  230. content="Demote user from DJ"
  231. v-tippy
  232. >
  233. remove_moderator
  234. </button>
  235. </div>
  236. <div class="user-state">
  237. <i
  238. class="material-icons"
  239. v-if="user.state === 'participate'"
  240. v-tippy
  241. content="Participating"
  242. >group</i
  243. >
  244. <i
  245. class="material-icons"
  246. v-if="user.state === 'local_paused'"
  247. v-tippy
  248. content="Paused"
  249. >pause</i
  250. >
  251. <i
  252. class="material-icons"
  253. v-if="user.state === 'muted'"
  254. v-tippy
  255. content="Muted"
  256. >volume_mute</i
  257. >
  258. <i
  259. class="material-icons"
  260. v-if="user.state === 'playing'"
  261. v-tippy
  262. content="Listening to music"
  263. >play_arrow</i
  264. >
  265. <i
  266. class="material-icons"
  267. v-if="user.state === 'buffering'"
  268. v-tippy
  269. content="Buffering"
  270. >warning</i
  271. >
  272. </div>
  273. </router-link>
  274. </li>
  275. </ul>
  276. </aside>
  277. </div>
  278. <div
  279. v-if="hasPermission('stations.update')"
  280. class="tab"
  281. v-show="tab === 'djs'"
  282. >
  283. <h5 class="has-text-centered">Station DJs</h5>
  284. <h6 v-if="station.djs.length === 0" class="has-text-centered">
  285. There are currently no DJs.
  286. </h6>
  287. <aside class="menu">
  288. <ul class="menu-list scrollable-list">
  289. <li v-for="dj in station.djs" :key="dj._id">
  290. <router-link
  291. :to="{
  292. name: 'profile',
  293. params: { username: dj.username }
  294. }"
  295. target="_blank"
  296. >
  297. <profile-picture
  298. :avatar="dj.avatar"
  299. :name="dj.name || dj.username"
  300. />
  301. {{ dj.name || dj.username }}
  302. <span
  303. class="material-icons user-rank"
  304. content="Station DJ"
  305. v-tippy="{ theme: 'info' }"
  306. >shield</span
  307. >
  308. <button
  309. v-if="hasPermission('stations.djs.remove')"
  310. class="button is-danger material-icons"
  311. @click.prevent="removeDj(dj._id)"
  312. content="Demote user from DJ"
  313. v-tippy
  314. >
  315. remove_moderator
  316. </button>
  317. </router-link>
  318. </li>
  319. </ul>
  320. </aside>
  321. </div>
  322. <div
  323. v-if="hasPermission('stations.update')"
  324. class="tab add-dj-tab"
  325. v-show="tab === 'add-dj'"
  326. >
  327. <h5 class="has-text-centered">Add Station DJ</h5>
  328. <h6 class="has-text-centered">
  329. Search for users to promote to DJ.
  330. </h6>
  331. <div class="control is-grouped input-with-button">
  332. <p class="control is-expanded">
  333. <input
  334. class="input"
  335. type="text"
  336. placeholder="Enter your user query here..."
  337. v-model="search.query"
  338. @keyup.enter="searchForUser(1)"
  339. />
  340. </p>
  341. <p class="control">
  342. <button
  343. class="button is-primary"
  344. @click="searchForUser(1)"
  345. >
  346. <i class="material-icons icon-with-button">search</i
  347. >Search
  348. </button>
  349. </p>
  350. </div>
  351. <aside class="menu">
  352. <ul class="menu-list scrollable-list">
  353. <li v-for="user in search.results" :key="user.username">
  354. <router-link
  355. :to="{
  356. name: 'profile',
  357. params: { username: user.username }
  358. }"
  359. target="_blank"
  360. >
  361. <profile-picture
  362. :avatar="user.avatar"
  363. :name="user.name || user.username"
  364. />
  365. {{ user.name || user.username }}
  366. <span
  367. v-if="isOwner(user._id)"
  368. class="material-icons user-rank"
  369. content="Station Owner"
  370. v-tippy="{ theme: 'info' }"
  371. >local_police</span
  372. >
  373. <span
  374. v-else-if="isDj(user._id)"
  375. class="material-icons user-rank"
  376. content="Station DJ"
  377. v-tippy="{ theme: 'info' }"
  378. >shield</span
  379. >
  380. <button
  381. v-if="
  382. hasPermission('stations.djs.add') &&
  383. !isDj(user._id) &&
  384. !isOwner(user._id)
  385. "
  386. class="button is-primary material-icons"
  387. @click.prevent="addDj(user._id)"
  388. content="Promote user to DJ"
  389. v-tippy
  390. >
  391. add_moderator
  392. </button>
  393. <button
  394. v-else-if="
  395. hasPermission('stations.djs.remove') &&
  396. isDj(user._id)
  397. "
  398. class="button is-danger material-icons"
  399. @click.prevent="removeDj(user._id)"
  400. content="Demote user from DJ"
  401. v-tippy
  402. >
  403. remove_moderator
  404. </button>
  405. </router-link>
  406. </li>
  407. <button
  408. v-if="resultsLeftCount > 0"
  409. class="button is-primary load-more-button"
  410. @click="searchForUser(search.page + 1)"
  411. >
  412. Load {{ nextPageResultsCount }} more results
  413. </button>
  414. </ul>
  415. </aside>
  416. </div>
  417. </div>
  418. <button
  419. class="button is-primary tab-actionable-button"
  420. @click="copyToClipboard()"
  421. >
  422. <i class="material-icons icon-with-button">share</i>
  423. <span> Share (copy to clipboard) </span>
  424. </button>
  425. </div>
  426. </template>
  427. <style lang="less" scoped>
  428. .night-mode {
  429. #users {
  430. background-color: var(--dark-grey-3) !important;
  431. border: 0 !important;
  432. }
  433. a {
  434. color: var(--light-grey-2);
  435. background-color: var(--dark-grey-2) !important;
  436. border: 0 !important;
  437. &:hover {
  438. color: var(--light-grey) !important;
  439. }
  440. }
  441. .tabs-container .tab-selection .button {
  442. background: var(--dark-grey) !important;
  443. color: var(--white) !important;
  444. }
  445. }
  446. .notification-box-enter-active,
  447. .fade-leave-active {
  448. transition: opacity 0.5s;
  449. }
  450. .notification-box-enter,
  451. .notification-box-leave-to {
  452. opacity: 0;
  453. }
  454. #users {
  455. background-color: var(--white);
  456. margin-bottom: 20px;
  457. border-radius: 0 0 @border-radius @border-radius;
  458. max-height: 100%;
  459. .tabs-container {
  460. padding: 10px;
  461. .tab-selection {
  462. display: flex;
  463. overflow-x: auto;
  464. margin-bottom: 10px;
  465. .button {
  466. border-radius: 0;
  467. border: 0;
  468. text-transform: uppercase;
  469. font-size: 14px;
  470. color: var(--dark-grey-3);
  471. background-color: var(--light-grey-2);
  472. flex-grow: 1;
  473. height: 32px;
  474. &:not(:first-of-type) {
  475. margin-left: 5px;
  476. }
  477. }
  478. .selected {
  479. background-color: var(--primary-color) !important;
  480. color: var(--white) !important;
  481. font-weight: 600;
  482. }
  483. }
  484. .tab {
  485. position: absolute;
  486. height: calc(100% - 120px);
  487. width: calc(100% - 20px);
  488. overflow-y: auto;
  489. .menu {
  490. margin-top: 20px;
  491. width: 100%;
  492. .menu-list {
  493. margin-left: 0;
  494. padding: 0;
  495. &.scrollable-list {
  496. max-height: unset;
  497. }
  498. }
  499. li {
  500. &:not(:first-of-type) {
  501. margin-top: 10px;
  502. }
  503. a {
  504. display: flex;
  505. flex-direction: row;
  506. align-items: center;
  507. padding: 5px 10px;
  508. border: 0.5px var(--light-grey-3) solid;
  509. border-radius: @border-radius;
  510. cursor: pointer;
  511. &:hover {
  512. background-color: var(--light-grey);
  513. color: var(--black);
  514. }
  515. .profile-picture {
  516. margin-right: 10px;
  517. width: 36px;
  518. height: 36px;
  519. }
  520. :deep(.profile-picture.using-initials span) {
  521. font-size: calc(
  522. 36px / 5 * 2
  523. ); // 2/5th of .profile-picture height/width
  524. }
  525. .user-rank {
  526. color: var(--primary-color);
  527. font-size: 18px;
  528. margin: 0 5px;
  529. }
  530. .button {
  531. margin-left: auto;
  532. font-size: 18px;
  533. width: 36px;
  534. }
  535. .left {
  536. display: flex;
  537. align-items: center;
  538. flex: 1;
  539. }
  540. .user-state {
  541. display: flex;
  542. }
  543. }
  544. }
  545. }
  546. h5 {
  547. font-size: 20px;
  548. }
  549. &.add-dj-tab {
  550. .control.is-grouped.input-with-button {
  551. margin: 20px 0 0 0 !important;
  552. & > .control {
  553. margin-bottom: 0 !important;
  554. }
  555. }
  556. .menu {
  557. margin-top: 10px;
  558. }
  559. .load-more-button {
  560. width: 100%;
  561. margin-top: 10px;
  562. }
  563. }
  564. }
  565. }
  566. }
  567. </style>