Activity.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <template>
  2. <div>
  3. <!-- <div class="bg-white py-4">
  4. <div class="container">
  5. <div class="d-flex justify-content-between align-items-center">
  6. <div></div>
  7. <a href="/account/activity" class="cursor-pointer font-weight-bold text-primary">Notifications</a>
  8. <a href="/account/direct" class="cursor-pointer font-weight-bold text-dark">Direct Messages</a>
  9. <a href="/account/following" class="cursor-pointer font-weight-bold text-dark">Following</a>
  10. <div></div>
  11. </div>
  12. </div>
  13. </div> -->
  14. <div class="container">
  15. <div class="row my-5">
  16. <div class="col-12 col-md-8 offset-md-2">
  17. <div v-if="notifications.length > 0" class="media mb-3 align-items-center px-3 border-bottom pb-3" v-for="(n, index) in notifications">
  18. <img class="mr-2 rounded-circle" style="border:1px solid #ccc" :src="n.account.avatar" alt="" width="32px" height="32px">
  19. <div class="media-body font-weight-light">
  20. <div v-if="n.type == 'favourite'">
  21. <p class="my-0">
  22. <a :href="n.account.url" class="font-weight-bold text-dark word-break" data-placement="bottom" data-toggle="tooltip" :title="n.account.username">{{truncate(n.account.username)}}</a> liked your <a class="font-weight-bold" v-bind:href="n.status.url">post</a>.
  23. </p>
  24. </div>
  25. <div v-else-if="n.type == 'comment'">
  26. <p class="my-0">
  27. <a :href="n.account.url" class="font-weight-bold text-dark word-break" data-placement="bottom" data-toggle="tooltip" :title="n.account.username">{{truncate(n.account.username)}}</a> commented on your <a class="font-weight-bold" v-bind:href="n.status.url">post</a>.
  28. </p>
  29. </div>
  30. <div v-else-if="n.type == 'mention'">
  31. <p class="my-0">
  32. <a :href="n.account.url" class="font-weight-bold text-dark word-break" data-placement="bottom" data-toggle="tooltip" :title="n.account.username">{{truncate(n.account.username)}}</a> <a class="font-weight-bold" v-bind:href="mentionUrl(n.status)">mentioned</a> you.
  33. </p>
  34. </div>
  35. <div v-else-if="n.type == 'follow'">
  36. <p class="my-0">
  37. <a :href="n.account.url" class="font-weight-bold text-dark word-break" data-placement="bottom" data-toggle="tooltip" :title="n.account.username">{{truncate(n.account.username)}}</a> followed you.
  38. </p>
  39. </div>
  40. <div v-else-if="n.type == 'share'">
  41. <p class="my-0">
  42. <a :href="n.account.url" class="font-weight-bold text-dark word-break" data-placement="bottom" data-toggle="tooltip" :title="n.account.username">{{truncate(n.account.username)}}</a> shared your <a class="font-weight-bold" v-bind:href="n.status.reblog.url">post</a>.
  43. </p>
  44. </div>
  45. <div v-else-if="n.type == 'modlog'">
  46. <p class="my-0">
  47. <a :href="n.account.url" class="font-weight-bold text-dark word-break" :title="n.account.username">{{truncate(n.account.username)}}</a> updated a <a class="font-weight-bold" v-bind:href="n.modlog.url">modlog</a>.
  48. </p>
  49. </div>
  50. <div v-else-if="n.type == 'tagged'">
  51. <p class="my-0">
  52. <a :href="n.account.url" class="font-weight-bold text-dark word-break" :title="n.account.username">{{truncate(n.account.username)}}</a> tagged you in a <a class="font-weight-bold" v-bind:href="n.tagged.post_url">post</a>.
  53. </p>
  54. </div>
  55. <div class="align-items-center">
  56. <span class="small text-muted" data-toggle="tooltip" data-placement="bottom" :title="n.created_at">{{timeAgo(n.created_at)}}</span>
  57. </div>
  58. </div>
  59. <div>
  60. <div v-if="n.status && n.status && n.status.media_attachments && n.status.media_attachments.length">
  61. <a :href="n.status.url">
  62. <img :src="n.status.media_attachments[0].preview_url" width="32px" height="32px">
  63. </a>
  64. </div>
  65. <div v-else-if="n.status && n.status.parent && n.status.parent.media_attachments && n.status.parent.media_attachments.length">
  66. <a :href="n.status.parent.url">
  67. <img :src="n.status.parent.media_attachments[0].preview_url" width="32px" height="32px">
  68. </a>
  69. </div>
  70. <!-- <div v-else-if="n.status && n.status.parent && n.status.parent.media_attachments && n.status.parent.media_attachments.length">
  71. <a :href="n.status.parent.url">
  72. <img :src="n.status.parent.media_attachments[0].preview_url" width="32px" height="32px">
  73. </a>
  74. </div> -->
  75. <!-- <div v-else-if="n.type == 'follow' && n.relationship.following == false">
  76. <a href="#" class="btn btn-primary py-0 font-weight-bold" @click.prevent="followProfile(n);">
  77. Follow
  78. </a>
  79. </div> -->
  80. <!-- <div v-else-if="n.status && n.status.parent && !n.status.parent.media_attachments && n.type == 'like' && n.relationship.following == false">
  81. <a href="#" class="btn btn-primary py-0 font-weight-bold">
  82. Follow
  83. </a>
  84. </div> -->
  85. <div v-else>
  86. <a class="btn btn-outline-primary py-0 font-weight-bold" :href="viewContext(n)">View</a>
  87. </div>
  88. </div>
  89. </div>
  90. <div v-if="notifications.length">
  91. <infinite-loading @infinite="infiniteNotifications">
  92. <div slot="no-results" class="font-weight-bold"></div>
  93. <div slot="no-more" class="font-weight-bold"></div>
  94. </infinite-loading>
  95. </div>
  96. <div v-if="notifications.length == 0" class="text-lighter text-center py-3">
  97. <p class="mb-0"><i class="fas fa-inbox fa-3x"></i></p>
  98. <p class="mb-0 small font-weight-bold">0 Notifications!</p>
  99. </div>
  100. </div>
  101. </div>
  102. </div>
  103. </div>
  104. </template>
  105. <script type="text/javascript">
  106. export default {
  107. data() {
  108. return {
  109. notifications: {},
  110. notificationCursor: 2,
  111. notificationMaxId: 0,
  112. };
  113. },
  114. mounted() {
  115. this.fetchNotifications();
  116. },
  117. updated() {
  118. $('[data-toggle="tooltip"]').tooltip()
  119. },
  120. methods: {
  121. fetchNotifications() {
  122. axios.get('/api/pixelfed/v1/notifications', {
  123. params: {
  124. pg: true
  125. }
  126. })
  127. .then(res => {
  128. let data = res.data.filter(n => {
  129. if(n.type == 'share' && !status) {
  130. return false;
  131. }
  132. return true;
  133. });
  134. let ids = res.data.map(n => n.id);
  135. this.notificationMaxId = Math.max(...ids);
  136. this.notifications = data;
  137. $('.notification-card .loader').addClass('d-none');
  138. $('.notification-card .contents').removeClass('d-none');
  139. });
  140. },
  141. infiniteNotifications($state) {
  142. if(this.notificationCursor > 10) {
  143. $state.complete();
  144. return;
  145. }
  146. axios.get('/api/pixelfed/v1/notifications', {
  147. params: {
  148. pg: true,
  149. page: this.notificationCursor
  150. }
  151. }).then(res => {
  152. if(res.data.length) {
  153. let data = res.data.filter(n => {
  154. if(n.type == 'share' && !status) {
  155. return false;
  156. }
  157. if(_.find(this.notifications, {id: n.id})) {
  158. return false;
  159. }
  160. return true;
  161. });
  162. this.notifications.push(...data);
  163. this.notificationCursor++;
  164. $state.loaded();
  165. } else {
  166. $state.complete();
  167. }
  168. });
  169. },
  170. truncate(text) {
  171. if(text.length <= 15) {
  172. return text;
  173. }
  174. return text.slice(0,15) + '...'
  175. },
  176. timeAgo(ts) {
  177. let date = Date.parse(ts);
  178. let seconds = Math.floor((new Date() - date) / 1000);
  179. let interval = Math.floor(seconds / 31536000);
  180. if (interval >= 1) {
  181. return interval + "y";
  182. }
  183. interval = Math.floor(seconds / 604800);
  184. if (interval >= 1) {
  185. return interval + "w";
  186. }
  187. interval = Math.floor(seconds / 86400);
  188. if (interval >= 1) {
  189. return interval + "d";
  190. }
  191. interval = Math.floor(seconds / 3600);
  192. if (interval >= 1) {
  193. return interval + "h";
  194. }
  195. interval = Math.floor(seconds / 60);
  196. if (interval >= 1) {
  197. return interval + "m";
  198. }
  199. return Math.floor(seconds) + "s";
  200. },
  201. mentionUrl(status) {
  202. let username = status.account.username;
  203. let id = status.id;
  204. return '/p/' + username + '/' + id;
  205. },
  206. followProfile(n) {
  207. let self = this;
  208. let id = n.account.id;
  209. axios.post('/i/follow', {
  210. item: id
  211. }).then(res => {
  212. self.notifications.map(notification => {
  213. if(notification.account.id === id) {
  214. notification.relationship.following = true;
  215. }
  216. });
  217. }).catch(err => {
  218. if(err.response.data.message) {
  219. swal('Error', err.response.data.message, 'error');
  220. }
  221. });
  222. },
  223. viewContext(n) {
  224. switch(n.type) {
  225. case 'follow':
  226. return n.account.url;
  227. break;
  228. case 'mention':
  229. return n.status.url;
  230. break;
  231. case 'like':
  232. case 'favourite':
  233. case 'comment':
  234. return n.status.url;
  235. break;
  236. case 'tagged':
  237. return n.tagged.post_url;
  238. break;
  239. }
  240. return '/';
  241. },
  242. }
  243. }
  244. </script>