RecentActivity.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <template>
  2. <div class="content recent-activity-tab">
  3. <div v-if="activities.length > 0">
  4. <h4 class="section-title">Recent activity</h4>
  5. <p class="section-description">
  6. This is a log of all actions
  7. {{ userId === myUserId ? "you have" : `${username} has` }}
  8. taken recently.
  9. </p>
  10. <hr class="section-horizontal-rule" />
  11. <activity-item
  12. class="item activity-item universal-item"
  13. v-for="activity in sortedActivities"
  14. :key="activity._id"
  15. :activity="activity"
  16. >
  17. <div slot="actions">
  18. <a
  19. v-if="userId === myUserId"
  20. href="#"
  21. @click.prevent="hideActivity(activity._id)"
  22. >
  23. <i class="material-icons hide-icon">visibility_off</i>
  24. </a>
  25. </div>
  26. </activity-item>
  27. </div>
  28. <div v-else>
  29. <h3>No recent activity.</h3>
  30. </div>
  31. </div>
  32. </template>
  33. <script>
  34. import { mapState, mapActions } from "vuex";
  35. import Toast from "toasters";
  36. import io from "../../../io";
  37. import ActivityItem from "../../../components/ui/ActivityItem.vue";
  38. export default {
  39. components: { ActivityItem },
  40. props: {
  41. userId: {
  42. type: String,
  43. default: ""
  44. }
  45. },
  46. computed: {
  47. sortedActivities() {
  48. const { activities } = this;
  49. return activities.sort(
  50. (x, y) => new Date(y.createdAt) - new Date(x.createdAt)
  51. );
  52. },
  53. ...mapState({
  54. activities: state => state.user.activities.activities,
  55. ...mapState("modalVisibility", {
  56. modals: state => state.modals.station
  57. }),
  58. myUserId: state => state.user.auth.userId,
  59. username: state => state.user.auth.username
  60. })
  61. },
  62. mounted() {
  63. io.getSocket(socket => {
  64. this.socket = socket;
  65. if (this.myUserId !== this.userId) {
  66. this.socket.emit(
  67. "apis.joinRoom",
  68. `profile-${this.userId}-activities`,
  69. () => {}
  70. );
  71. }
  72. this.socket.emit("activities.getSet", this.userId, 1, res => {
  73. if (res.status === "success") {
  74. // for (let a = 0; a < res.data.length; a += 1) {
  75. // this.formatActivity(res.data[a], activity => {
  76. // this.activities.unshift(activity);
  77. // });
  78. // }
  79. this.getSetOfActivities({
  80. activities: res.data,
  81. set: 1
  82. });
  83. }
  84. });
  85. this.socket.on("event:activity.create", activity => {
  86. console.log("activity created (socket event): ", activity);
  87. this.formatActivity(activity, activity => {
  88. this.activities.unshift(activity);
  89. });
  90. });
  91. this.socket.on("event:activity.hide", activityId => {
  92. this.removeActivity(activityId);
  93. });
  94. });
  95. },
  96. methods: {
  97. hideActivity(activityId) {
  98. this.socket.emit("activities.hideActivity", activityId, res => {
  99. if (res.status !== "success")
  100. new Toast({ content: res.message, timeout: 3000 });
  101. });
  102. },
  103. formatActivity(res, cb) {
  104. console.log("activity", res);
  105. const icons = {
  106. created_account: "account_circle",
  107. created_station: "radio",
  108. deleted_station: "delete",
  109. created_playlist: "playlist_add_check",
  110. deleted_playlist: "delete_sweep",
  111. liked_song: "favorite",
  112. added_song_to_playlist: "playlist_add",
  113. added_songs_to_playlist: "playlist_add"
  114. };
  115. const activity = {
  116. ...res,
  117. thumbnail: "",
  118. message: "",
  119. icon: ""
  120. };
  121. const plural = activity.payload.length > 1;
  122. activity.icon = icons[activity.activityType];
  123. if (activity.activityType === "created_account") {
  124. activity.message = "Welcome to Musare!";
  125. return cb(activity);
  126. }
  127. if (activity.activityType === "created_station") {
  128. this.socket.emit(
  129. "stations.getStationForActivity",
  130. activity.payload[0],
  131. res => {
  132. if (res.status === "success") {
  133. activity.message = `Created the station <strong>${res.data.title}</strong>`;
  134. activity.thumbnail = res.data.thumbnail;
  135. return cb(activity);
  136. }
  137. activity.message = "Created a station";
  138. return cb(activity);
  139. }
  140. );
  141. }
  142. if (activity.activityType === "deleted_station") {
  143. activity.message = `Deleted a station`;
  144. return cb(activity);
  145. }
  146. if (activity.activityType === "created_playlist") {
  147. this.socket.emit(
  148. "playlists.getPlaylistForActivity",
  149. activity.payload[0],
  150. res => {
  151. if (res.status === "success") {
  152. activity.message = `Created the playlist <strong>${res.data.title}</strong>`;
  153. // activity.thumbnail = res.data.thumbnail;
  154. return cb(activity);
  155. }
  156. activity.message = "Created a playlist";
  157. return cb(activity);
  158. }
  159. );
  160. }
  161. if (activity.activityType === "deleted_playlist") {
  162. activity.message = `Deleted a playlist`;
  163. return cb(activity);
  164. }
  165. if (activity.activityType === "liked_song") {
  166. if (plural) {
  167. activity.message = `Liked ${activity.payload.length} songs.`;
  168. return cb(activity);
  169. }
  170. this.socket.emit(
  171. "songs.getSongForActivity",
  172. activity.payload[0],
  173. res => {
  174. if (res.status === "success") {
  175. activity.message = `Liked the song <strong>${res.data.title}</strong>`;
  176. activity.thumbnail = res.data.thumbnail;
  177. return cb(activity);
  178. }
  179. activity.message = "Liked a song";
  180. return cb(activity);
  181. }
  182. );
  183. }
  184. if (activity.activityType === "added_song_to_playlist") {
  185. this.socket.emit(
  186. "songs.getSongForActivity",
  187. activity.payload[0].songId,
  188. song => {
  189. console.log(song);
  190. this.socket.emit(
  191. "playlists.getPlaylistForActivity",
  192. activity.payload[0].playlistId,
  193. playlist => {
  194. if (song.status === "success") {
  195. if (playlist.status === "success")
  196. activity.message = `Added the song <strong>${song.data.title}</strong> to the playlist <strong>${playlist.data.title}</strong>`;
  197. else
  198. activity.message = `Added the song <strong>${song.data.title}</strong> to a playlist`;
  199. activity.thumbnail = song.data.thumbnail;
  200. return cb(activity);
  201. }
  202. if (playlist.status === "success") {
  203. activity.message = `Added a song to the playlist <strong>${playlist.data.title}</strong>`;
  204. return cb(activity);
  205. }
  206. activity.message = "Added a song to a playlist";
  207. return cb(activity);
  208. }
  209. );
  210. }
  211. );
  212. }
  213. if (activity.activityType === "added_songs_to_playlist") {
  214. activity.message = `Added ${activity.payload.length} songs to a playlist`;
  215. return cb(activity);
  216. }
  217. return false;
  218. },
  219. ...mapActions("user/activities", [
  220. "getSetOfActivities",
  221. "removeActivity"
  222. ])
  223. }
  224. };
  225. </script>