Activity.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 class="align-items-center">
  46. <span class="small text-muted" data-toggle="tooltip" data-placement="bottom" :title="n.created_at">{{timeAgo(n.created_at)}}</span>
  47. </div>
  48. </div>
  49. <div>
  50. <div v-if="n.status && n.status && n.status.media_attachments && n.status.media_attachments.length">
  51. <a :href="n.status.url">
  52. <img :src="n.status.media_attachments[0].preview_url" width="32px" height="32px">
  53. </a>
  54. </div>
  55. <div v-else-if="n.status && n.status.parent && n.status.parent.media_attachments && n.status.parent.media_attachments.length">
  56. <a :href="n.status.parent.url">
  57. <img :src="n.status.parent.media_attachments[0].preview_url" width="32px" height="32px">
  58. </a>
  59. </div>
  60. <!-- <div v-else-if="n.status && n.status.parent && n.status.parent.media_attachments && n.status.parent.media_attachments.length">
  61. <a :href="n.status.parent.url">
  62. <img :src="n.status.parent.media_attachments[0].preview_url" width="32px" height="32px">
  63. </a>
  64. </div> -->
  65. <!-- <div v-else-if="n.type == 'follow' && n.relationship.following == false">
  66. <a href="#" class="btn btn-primary py-0 font-weight-bold" @click.prevent="followProfile(n);">
  67. Follow
  68. </a>
  69. </div> -->
  70. <!-- <div v-else-if="n.status && n.status.parent && !n.status.parent.media_attachments && n.type == 'like' && n.relationship.following == false">
  71. <a href="#" class="btn btn-primary py-0 font-weight-bold">
  72. Follow
  73. </a>
  74. </div> -->
  75. <div v-else>
  76. <a class="btn btn-outline-primary py-0 font-weight-bold" :href="viewContext(n)">View</a>
  77. </div>
  78. </div>
  79. </div>
  80. <div v-if="notifications.length">
  81. <infinite-loading @infinite="infiniteNotifications">
  82. <div slot="no-results" class="font-weight-bold"></div>
  83. <div slot="no-more" class="font-weight-bold"></div>
  84. </infinite-loading>
  85. </div>
  86. <div v-if="notifications.length == 0" class="text-lighter text-center py-3">
  87. <p class="mb-0"><i class="fas fa-inbox fa-3x"></i></p>
  88. <p class="mb-0 small font-weight-bold">0 Notifications!</p>
  89. </div>
  90. </div>
  91. </div>
  92. </div>
  93. </div>
  94. </template>
  95. <script type="text/javascript">
  96. export default {
  97. data() {
  98. return {
  99. notifications: {},
  100. notificationCursor: 2,
  101. notificationMaxId: 0,
  102. };
  103. },
  104. mounted() {
  105. this.fetchNotifications();
  106. },
  107. updated() {
  108. $('[data-toggle="tooltip"]').tooltip()
  109. },
  110. methods: {
  111. fetchNotifications() {
  112. axios.get('/api/pixelfed/v1/notifications', {
  113. params: {
  114. pg: true
  115. }
  116. })
  117. .then(res => {
  118. let data = res.data.filter(n => {
  119. if(n.type == 'share' && !status) {
  120. return false;
  121. }
  122. return true;
  123. });
  124. let ids = res.data.map(n => n.id);
  125. this.notificationMaxId = Math.max(...ids);
  126. this.notifications = data;
  127. $('.notification-card .loader').addClass('d-none');
  128. $('.notification-card .contents').removeClass('d-none');
  129. });
  130. },
  131. infiniteNotifications($state) {
  132. if(this.notificationCursor > 10) {
  133. $state.complete();
  134. return;
  135. }
  136. axios.get('/api/pixelfed/v1/notifications', {
  137. params: {
  138. pg: true,
  139. page: this.notificationCursor
  140. }
  141. }).then(res => {
  142. if(res.data.length) {
  143. let data = res.data.filter(n => {
  144. if(n.type == 'share' && !status) {
  145. return false;
  146. }
  147. if(_.find(this.notifications, {id: n.id})) {
  148. return false;
  149. }
  150. return true;
  151. });
  152. this.notifications.push(...data);
  153. this.notificationCursor++;
  154. $state.loaded();
  155. } else {
  156. $state.complete();
  157. }
  158. });
  159. },
  160. truncate(text) {
  161. if(text.length <= 15) {
  162. return text;
  163. }
  164. return text.slice(0,15) + '...'
  165. },
  166. timeAgo(ts) {
  167. let date = Date.parse(ts);
  168. let seconds = Math.floor((new Date() - date) / 1000);
  169. let interval = Math.floor(seconds / 31536000);
  170. if (interval >= 1) {
  171. return interval + "y";
  172. }
  173. interval = Math.floor(seconds / 604800);
  174. if (interval >= 1) {
  175. return interval + "w";
  176. }
  177. interval = Math.floor(seconds / 86400);
  178. if (interval >= 1) {
  179. return interval + "d";
  180. }
  181. interval = Math.floor(seconds / 3600);
  182. if (interval >= 1) {
  183. return interval + "h";
  184. }
  185. interval = Math.floor(seconds / 60);
  186. if (interval >= 1) {
  187. return interval + "m";
  188. }
  189. return Math.floor(seconds) + "s";
  190. },
  191. mentionUrl(status) {
  192. let username = status.account.username;
  193. let id = status.id;
  194. return '/p/' + username + '/' + id;
  195. },
  196. followProfile(n) {
  197. let self = this;
  198. let id = n.account.id;
  199. axios.post('/i/follow', {
  200. item: id
  201. }).then(res => {
  202. self.notifications.map(notification => {
  203. if(notification.account.id === id) {
  204. notification.relationship.following = true;
  205. }
  206. });
  207. }).catch(err => {
  208. if(err.response.data.message) {
  209. swal('Error', err.response.data.message, 'error');
  210. }
  211. });
  212. },
  213. viewContext(n) {
  214. switch(n.type) {
  215. case 'follow':
  216. return n.account.url;
  217. break;
  218. case 'mention':
  219. return n.status.url;
  220. break;
  221. case 'like':
  222. case 'favourite':
  223. case 'comment':
  224. return n.status.url;
  225. break;
  226. }
  227. return '/';
  228. },
  229. }
  230. }
  231. </script>