Profile.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. <template>
  2. <div v-if="isUser">
  3. <metadata :title="`Profile | ${user.username}`" />
  4. <edit-playlist v-if="modals.editPlaylist" />
  5. <create-playlist v-if="modals.createPlaylist" />
  6. <main-header />
  7. <div class="container">
  8. <div class="info-section">
  9. <div class="picture-name-row">
  10. <img
  11. class="profile-picture"
  12. :src="
  13. user.avatar.url && user.avatar.type === 'gravatar'
  14. ? `${user.avatar.url}?d=${notes}&s=250`
  15. : '/assets/notes.png'
  16. "
  17. onerror="this.src='/assets/notes.png'; this.onerror=''"
  18. />
  19. <div>
  20. <div class="name-role-row">
  21. <p class="name">{{ user.name }}</p>
  22. <span
  23. class="role admin"
  24. v-if="user.role === 'admin'"
  25. >admin</span
  26. >
  27. </div>
  28. <h2 class="username">@{{ user.username }}</h2>
  29. </div>
  30. </div>
  31. <div
  32. class="buttons"
  33. v-if="userId === user._id || role === 'admin'"
  34. >
  35. <router-link
  36. :to="`/admin/users?userId=${user._id}`"
  37. class="button is-primary"
  38. v-if="role === 'admin'"
  39. >
  40. Edit
  41. </router-link>
  42. <router-link
  43. to="/settings"
  44. class="button is-primary"
  45. v-if="userId === user._id"
  46. >
  47. Settings
  48. </router-link>
  49. </div>
  50. <div class="bio-row" v-if="user.bio">
  51. <i class="material-icons">notes</i>
  52. <p>{{ user.bio }}</p>
  53. </div>
  54. <div
  55. class="date-location-row"
  56. v-if="user.createdAt || user.location"
  57. >
  58. <div class="date" v-if="user.createdAt">
  59. <i class="material-icons">calendar_today</i>
  60. <p>{{ user.createdAt }}</p>
  61. </div>
  62. <div class="location" v-if="user.location">
  63. <i class="material-icons">location_on</i>
  64. <p>{{ user.location }}</p>
  65. </div>
  66. </div>
  67. </div>
  68. <div class="bottom-section">
  69. <div class="buttons">
  70. <button
  71. :class="{ active: activeTab === 'recentActivity' }"
  72. @click="switchTab('recentActivity')"
  73. >
  74. Recent activity
  75. </button>
  76. <button
  77. :class="{ active: activeTab === 'playlists' }"
  78. @click="switchTab('playlists')"
  79. >
  80. Playlists
  81. </button>
  82. </div>
  83. <div
  84. class="content recent-activity-tab"
  85. v-if="activeTab === 'recentActivity'"
  86. >
  87. <div v-if="activities.length > 0">
  88. <div
  89. class="item activity"
  90. v-for="activity in sortedActivities"
  91. :key="activity._id"
  92. >
  93. <div class="thumbnail">
  94. <img :src="activity.thumbnail" alt="" />
  95. <i class="material-icons activity-type-icon">{{
  96. activity.icon
  97. }}</i>
  98. </div>
  99. <div class="left-part">
  100. <p
  101. class="top-text"
  102. v-html="activity.message"
  103. ></p>
  104. <p class="bottom-text">
  105. {{
  106. formatDistance(
  107. parseISO(activity.createdAt),
  108. new Date(),
  109. { addSuffix: true }
  110. )
  111. }}
  112. </p>
  113. </div>
  114. <div class="actions">
  115. <a
  116. class="hide-icon"
  117. href="#"
  118. @click="hideActivity(activity._id)"
  119. >
  120. <i class="material-icons">visibility_off</i>
  121. </a>
  122. </div>
  123. </div>
  124. </div>
  125. <div v-else>
  126. <h2>No recent activity.</h2>
  127. </div>
  128. </div>
  129. <div
  130. class="content playlists-tab"
  131. v-if="activeTab === 'playlists'"
  132. >
  133. <div v-if="playlists.length > 0">
  134. <div
  135. class="item playlist"
  136. v-for="playlist in playlists"
  137. :key="playlist._id"
  138. >
  139. <playlist-item
  140. v-if="
  141. playlist.privacy === 'public' ||
  142. (playlist.privacy === 'private' &&
  143. playlist.createdBy === userId)
  144. "
  145. :playlist="playlist"
  146. >
  147. <div v-if="user._id === userId" slot="actions">
  148. <button
  149. class="button is-primary"
  150. @click="editPlaylistClick(playlist._id)"
  151. >
  152. <i
  153. class="material-icons icon-with-button"
  154. >create</i
  155. >Edit
  156. </button>
  157. </div>
  158. </playlist-item>
  159. </div>
  160. <button
  161. v-if="user._id === userId"
  162. class="button is-primary"
  163. @click="
  164. openModal({
  165. sector: 'station',
  166. modal: 'createPlaylist'
  167. })
  168. "
  169. >
  170. Create new playlist
  171. </button>
  172. </div>
  173. <div v-else>
  174. <h2>No playlists here.</h2>
  175. </div>
  176. </div>
  177. </div>
  178. </div>
  179. <main-footer />
  180. </div>
  181. </template>
  182. <script>
  183. import { mapState, mapActions } from "vuex";
  184. import { format, formatDistance, parseISO } from "date-fns";
  185. import Toast from "toasters";
  186. import PlaylistItem from "../components/ui/PlaylistItem.vue";
  187. import MainHeader from "../components/layout/MainHeader.vue";
  188. import MainFooter from "../components/layout/MainFooter.vue";
  189. import io from "../io";
  190. export default {
  191. components: {
  192. MainHeader,
  193. MainFooter,
  194. PlaylistItem,
  195. CreatePlaylist: () => import("../components/modals/CreatePlaylist.vue"),
  196. EditPlaylist: () => import("../components/modals/EditPlaylist.vue")
  197. },
  198. data() {
  199. return {
  200. user: {},
  201. notes: "",
  202. isUser: false,
  203. activeTab: "recentActivity",
  204. playlists: [],
  205. activities: []
  206. };
  207. },
  208. computed: {
  209. ...mapState({
  210. role: state => state.user.auth.role,
  211. userId: state => state.user.auth.userId,
  212. ...mapState("modalVisibility", {
  213. modals: state => state.modals.station
  214. })
  215. }),
  216. sortedActivities() {
  217. const { activities } = this;
  218. return activities.sort(
  219. (x, y) => new Date(y.createdAt) - new Date(x.createdAt)
  220. );
  221. }
  222. },
  223. mounted() {
  224. lofig.get("frontendDomain").then(frontendDomain => {
  225. this.frontendDomain = frontendDomain;
  226. this.notes = encodeURI(`${this.frontendDomain}/assets/notes.png`);
  227. });
  228. io.getSocket(socket => {
  229. this.socket = socket;
  230. this.socket.emit(
  231. "users.findByUsername",
  232. this.$route.params.username,
  233. res => {
  234. if (res.status === "error") this.$router.go("/404");
  235. else {
  236. this.user = res.data;
  237. this.user.createdAt = format(
  238. parseISO(this.user.createdAt),
  239. "MMMM do yyyy"
  240. );
  241. this.isUser = true;
  242. if (this.user._id !== this.userId) {
  243. this.socket.emit(
  244. "apis.joinRoom",
  245. `profile-${res.data._id}`,
  246. () => {}
  247. );
  248. }
  249. this.socket.emit(
  250. "playlists.indexForUser",
  251. this.user._id,
  252. res => {
  253. if (res.status === "success")
  254. this.playlists = res.data;
  255. }
  256. );
  257. this.socket.on("event:playlist.create", playlist => {
  258. this.playlists.push(playlist);
  259. });
  260. this.socket.on("event:playlist.delete", playlistId => {
  261. this.playlists.forEach((playlist, index) => {
  262. if (playlist._id === playlistId) {
  263. this.playlists.splice(index, 1);
  264. }
  265. });
  266. });
  267. this.socket.on("event:playlist.addSong", data => {
  268. this.playlists.forEach((playlist, index) => {
  269. if (playlist._id === data.playlistId) {
  270. this.playlists[index].songs.push(data.song);
  271. }
  272. });
  273. });
  274. this.socket.on("event:playlist.removeSong", data => {
  275. this.playlists.forEach((playlist, index) => {
  276. if (playlist._id === data.playlistId) {
  277. this.playlists[index].songs.forEach(
  278. (song, index2) => {
  279. if (song.songId === data.songId) {
  280. this.playlists[
  281. index
  282. ].songs.splice(index2, 1);
  283. }
  284. }
  285. );
  286. }
  287. });
  288. });
  289. this.socket.on(
  290. "event:playlist.updateDisplayName",
  291. data => {
  292. this.playlists.forEach((playlist, index) => {
  293. if (playlist._id === data.playlistId) {
  294. this.playlists[index].displayName =
  295. data.displayName;
  296. }
  297. });
  298. }
  299. );
  300. this.socket.on("event:playlist.updatePrivacy", data => {
  301. this.playlists.forEach((playlist, index) => {
  302. if (playlist._id === data.playlist._id) {
  303. this.playlists[index].privacy =
  304. data.playlist.privacy;
  305. }
  306. });
  307. });
  308. if (this.user._id === this.userId) {
  309. this.socket.emit(
  310. "activities.getSet",
  311. this.userId,
  312. 1,
  313. res => {
  314. if (res.status === "success") {
  315. for (
  316. let a = 0;
  317. a < res.data.length;
  318. a += 1
  319. ) {
  320. this.formatActivity(
  321. res.data[a],
  322. activity => {
  323. this.activities.unshift(
  324. activity
  325. );
  326. }
  327. );
  328. }
  329. }
  330. }
  331. );
  332. this.socket.on(
  333. "event:activity.create",
  334. activity => {
  335. console.log(activity);
  336. this.formatActivity(activity, activity => {
  337. this.activities.unshift(activity);
  338. });
  339. }
  340. );
  341. }
  342. }
  343. }
  344. );
  345. });
  346. },
  347. methods: {
  348. formatDistance,
  349. parseISO,
  350. switchTab(tabName) {
  351. this.activeTab = tabName;
  352. },
  353. editPlaylistClick(playlistId) {
  354. console.log(playlistId);
  355. this.editPlaylist(playlistId);
  356. this.openModal({ sector: "station", modal: "editPlaylist" });
  357. },
  358. hideActivity(activityId) {
  359. this.socket.emit("activities.hideActivity", activityId, res => {
  360. if (res.status === "success") {
  361. this.activities = this.activities.filter(
  362. activity => activity._id !== activityId
  363. );
  364. } else {
  365. new Toast({ content: res.message, timeout: 3000 });
  366. }
  367. });
  368. },
  369. formatActivity(res, cb) {
  370. console.log("activity", res);
  371. const icons = {
  372. created_account: "account_circle",
  373. created_station: "radio",
  374. deleted_station: "delete",
  375. created_playlist: "playlist_add_check",
  376. deleted_playlist: "delete_sweep",
  377. liked_song: "favorite",
  378. added_song_to_playlist: "playlist_add",
  379. added_songs_to_playlist: "playlist_add"
  380. };
  381. const activity = {
  382. ...res,
  383. thumbnail: "",
  384. message: "",
  385. icon: ""
  386. };
  387. const plural = activity.payload.length > 1;
  388. activity.icon = icons[activity.activityType];
  389. if (activity.activityType === "created_account") {
  390. activity.message = "Welcome to Musare!";
  391. return cb(activity);
  392. }
  393. if (activity.activityType === "created_station") {
  394. this.socket.emit(
  395. "stations.getStationForActivity",
  396. activity.payload[0],
  397. res => {
  398. if (res.status === "success") {
  399. activity.message = `Created the station <strong>${res.data.title}</strong>`;
  400. activity.thumbnail = res.data.thumbnail;
  401. return cb(activity);
  402. }
  403. activity.message = "Created a station";
  404. return cb(activity);
  405. }
  406. );
  407. }
  408. if (activity.activityType === "deleted_station") {
  409. activity.message = `Deleted a station`;
  410. return cb(activity);
  411. }
  412. if (activity.activityType === "created_playlist") {
  413. this.socket.emit(
  414. "playlists.getPlaylistForActivity",
  415. activity.payload[0],
  416. res => {
  417. if (res.status === "success") {
  418. activity.message = `Created the playlist <strong>${res.data.title}</strong>`;
  419. // activity.thumbnail = res.data.thumbnail;
  420. return cb(activity);
  421. }
  422. activity.message = "Created a playlist";
  423. return cb(activity);
  424. }
  425. );
  426. }
  427. if (activity.activityType === "deleted_playlist") {
  428. activity.message = `Deleted a playlist`;
  429. return cb(activity);
  430. }
  431. if (activity.activityType === "liked_song") {
  432. if (plural) {
  433. activity.message = `Liked ${activity.payload.length} songs.`;
  434. return cb(activity);
  435. }
  436. this.socket.emit(
  437. "songs.getSongForActivity",
  438. activity.payload[0],
  439. res => {
  440. if (res.status === "success") {
  441. activity.message = `Liked the song <strong>${res.data.title}</strong>`;
  442. activity.thumbnail = res.data.thumbnail;
  443. return cb(activity);
  444. }
  445. activity.message = "Liked a song";
  446. return cb(activity);
  447. }
  448. );
  449. }
  450. if (activity.activityType === "added_song_to_playlist") {
  451. this.socket.emit(
  452. "songs.getSongForActivity",
  453. activity.payload[0].songId,
  454. song => {
  455. console.log(song);
  456. this.socket.emit(
  457. "playlists.getPlaylistForActivity",
  458. activity.payload[0].playlistId,
  459. playlist => {
  460. if (song.status === "success") {
  461. if (playlist.status === "success")
  462. activity.message = `Added the song <strong>${song.data.title}</strong> to the playlist <strong>${playlist.data.title}</strong>`;
  463. else
  464. activity.message = `Added the song <strong>${song.data.title}</strong> to a playlist`;
  465. activity.thumbnail = song.data.thumbnail;
  466. return cb(activity);
  467. }
  468. if (playlist.status === "success") {
  469. activity.message = `Added a song to the playlist <strong>${playlist.data.title}</strong>`;
  470. return cb(activity);
  471. }
  472. activity.message = "Added a song to a playlist";
  473. return cb(activity);
  474. }
  475. );
  476. }
  477. );
  478. }
  479. if (activity.activityType === "added_songs_to_playlist") {
  480. activity.message = `Added ${activity.payload.length} songs to a playlist`;
  481. return cb(activity);
  482. }
  483. return false;
  484. },
  485. ...mapActions("modalVisibility", ["openModal"]),
  486. ...mapActions("user/playlists", ["editPlaylist"])
  487. }
  488. };
  489. </script>
  490. <style lang="scss" scoped>
  491. @import "../styles/global.scss";
  492. @media only screen and (max-width: 1000px) {
  493. // #page-title {
  494. // margin: 0;
  495. // font-size: 40px;
  496. // }
  497. // #sidebar-with-content {
  498. // width: 962px;
  499. // margin: 0 auto;
  500. // margin-top: 30px;
  501. // flex-direction: row;
  502. // .content {
  503. // width: 600px;
  504. // margin-top: 0px !important;
  505. // }
  506. // }
  507. .info-section {
  508. margin-top: 0 !important;
  509. .picture-name-row {
  510. flex-direction: column !important;
  511. }
  512. .name-role-row {
  513. margin-top: 24px;
  514. }
  515. .buttons .button:not(last-of-type) {
  516. margin-bottom: 10px;
  517. margin-right: 5px;
  518. }
  519. .date-location-row {
  520. flex-direction: column;
  521. width: auto !important;
  522. }
  523. .date-location-row > div:nth-child(2),
  524. .buttons .button:nth-child(2) {
  525. margin-left: 0 !important;
  526. }
  527. }
  528. .bottom-section {
  529. flex-direction: column;
  530. }
  531. .content {
  532. margin: 24px 0;
  533. }
  534. }
  535. .info-section {
  536. width: 912px;
  537. max-width: 100%;
  538. margin-left: auto;
  539. margin-right: auto;
  540. margin-top: 32px;
  541. padding: 24px;
  542. .picture-name-row {
  543. display: flex;
  544. flex-direction: row;
  545. align-items: center;
  546. justify-content: center;
  547. margin-bottom: 24px;
  548. }
  549. .profile-picture {
  550. width: 100px;
  551. height: 100px;
  552. border-radius: 100%;
  553. margin-right: 32px;
  554. }
  555. .name-role-row {
  556. display: flex;
  557. flex-direction: row;
  558. align-items: center;
  559. }
  560. .name {
  561. font-size: 34px;
  562. line-height: 40px;
  563. color: $dark-grey-3;
  564. }
  565. .role {
  566. padding: 2px 24px;
  567. color: $white;
  568. text-transform: uppercase;
  569. font-size: 12px;
  570. line-height: 14px;
  571. height: 18px;
  572. border-radius: 5px;
  573. margin-left: 12px;
  574. &.admin {
  575. background-color: $red;
  576. }
  577. }
  578. .username {
  579. font-size: 24px;
  580. line-height: 28px;
  581. color: $dark-grey;
  582. margin: 0;
  583. }
  584. .buttons {
  585. width: 388px;
  586. max-width: 100%;
  587. display: flex;
  588. flex-direction: row;
  589. margin-left: auto;
  590. margin-right: auto;
  591. margin-bottom: 24px;
  592. .button {
  593. flex: 1;
  594. font-size: 17px;
  595. line-height: 20px;
  596. &:nth-child(2) {
  597. margin-left: 20px;
  598. }
  599. }
  600. }
  601. .bio-row,
  602. .date-location-row {
  603. i {
  604. font-size: 24px;
  605. color: $dark-grey-2;
  606. margin-right: 12px;
  607. }
  608. p {
  609. font-size: 17px;
  610. line-height: 20px;
  611. color: $dark-grey-2;
  612. word-break: break-word;
  613. }
  614. }
  615. .bio-row {
  616. max-width: 608px;
  617. margin-bottom: 24px;
  618. margin-left: auto;
  619. margin-right: auto;
  620. display: flex;
  621. width: max-content;
  622. }
  623. .date-location-row {
  624. max-width: 608px;
  625. margin-left: auto;
  626. margin-right: auto;
  627. margin-bottom: 24px;
  628. display: flex;
  629. width: max-content;
  630. margin-bottom: 24px;
  631. > div:nth-child(2) {
  632. margin-left: 48px;
  633. }
  634. }
  635. .date,
  636. .location {
  637. display: flex;
  638. }
  639. }
  640. .bottom-section {
  641. width: 962px;
  642. max-width: 100%;
  643. margin-left: auto;
  644. margin-right: auto;
  645. margin-top: 32px;
  646. padding: 24px;
  647. display: flex;
  648. .buttons {
  649. height: 100%;
  650. width: 250px;
  651. margin-right: 64px;
  652. button {
  653. outline: none;
  654. border: none;
  655. box-shadow: none;
  656. color: $musare-blue;
  657. font-size: 22px;
  658. line-height: 26px;
  659. padding: 7px 0 7px 12px;
  660. width: 100%;
  661. text-align: left;
  662. cursor: pointer;
  663. border-radius: 5px;
  664. background-color: transparent;
  665. &.active {
  666. color: $white;
  667. background-color: $musare-blue;
  668. }
  669. }
  670. }
  671. .content {
  672. width: 600px;
  673. max-width: 100%;
  674. .item {
  675. height: 72px;
  676. border: 0.5px $light-grey-2 solid;
  677. margin-bottom: 12px;
  678. border-radius: 5px;
  679. display: flex;
  680. overflow: hidden;
  681. .top-text {
  682. color: $dark-grey-2;
  683. font-size: 20px;
  684. line-height: 23px;
  685. margin-bottom: 0;
  686. }
  687. .bottom-text {
  688. color: $dark-grey-2;
  689. font-size: 16px;
  690. line-height: 19px;
  691. margin-bottom: 0;
  692. margin-top: 6px;
  693. &:first-letter {
  694. text-transform: uppercase;
  695. }
  696. }
  697. .thumbnail {
  698. position: relative;
  699. display: flex;
  700. align-items: center;
  701. justify-content: center;
  702. width: 70.5px;
  703. height: 70.5px;
  704. background-color: #000;
  705. z-index: -1;
  706. img {
  707. opacity: 0.4;
  708. }
  709. .activity-type-icon {
  710. position: absolute;
  711. color: #fff;
  712. }
  713. }
  714. .left-part {
  715. flex: 1;
  716. padding: 12px;
  717. }
  718. .actions {
  719. display: flex;
  720. align-items: center;
  721. padding: 12px;
  722. .hide-icon {
  723. border-bottom: 0;
  724. display: flex;
  725. i {
  726. color: #bdbdbd;
  727. }
  728. }
  729. }
  730. button {
  731. font-size: 17px;
  732. }
  733. }
  734. }
  735. }
  736. .night-mode {
  737. .name,
  738. .username,
  739. .bio-row i,
  740. .bio-row p,
  741. .date-location-row i,
  742. .date-location-row p,
  743. .item .left-part .top-text,
  744. .item .left-part .bottom-text {
  745. color: $light-grey;
  746. }
  747. }
  748. </style>