Activity.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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="getProfileUrl(n.account)" class="font-weight-bold text-dark word-break" data-placement="bottom" data-toggle="tooltip" :title="n.account.username">{{n.account.local == false ? '@':''}}{{truncate(n.account.username)}}</a> liked your <a class="font-weight-bold" v-bind:href="getPostUrl(n.status)">post</a>.
  23. </p>
  24. </div>
  25. <div v-else-if="n.type == 'comment'">
  26. <p class="my-0">
  27. <a :href="getProfileUrl(n.account)" class="font-weight-bold text-dark word-break" data-placement="bottom" data-toggle="tooltip" :title="n.account.username">{{n.account.local == false ? '@':''}}{{truncate(n.account.username)}}</a> commented on your <a class="font-weight-bold" v-bind:href="getPostUrl(n.status)">post</a>.
  28. </p>
  29. </div>
  30. <div v-else-if="n.type == 'mention'">
  31. <p class="my-0">
  32. <a :href="getProfileUrl(n.account)" class="font-weight-bold text-dark word-break" data-placement="bottom" data-toggle="tooltip" :title="n.account.username">{{n.account.local == false ? '@':''}}{{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="getProfileUrl(n.account)" class="font-weight-bold text-dark word-break" data-placement="bottom" data-toggle="tooltip" :title="n.account.username">{{n.account.local == false ? '@':''}}{{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="getProfileUrl(n.account)" class="font-weight-bold text-dark word-break" data-placement="bottom" data-toggle="tooltip" :title="n.account.username">{{n.account.local == false ? '@':''}}{{truncate(n.account.username)}}</a> shared your <a class="font-weight-bold" v-bind:href="getPostUrl(n.status)">post</a>.
  43. </p>
  44. </div>
  45. <div v-else-if="n.type == 'modlog'">
  46. <p class="my-0">
  47. <a :href="getProfileUrl(n.account)" 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="getProfileUrl(n.account)" class="font-weight-bold text-dark word-break" :title="n.account.username">{{n.account.local == false ? '@':''}}{{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 v-else-if="n.type == 'direct'">
  56. <p class="my-0">
  57. <a :href="getProfileUrl(n.account)" class="font-weight-bold text-dark word-break" :title="n.account.username">{{n.account.local == false ? '@':''}}{{truncate(n.account.username)}}</a> sent a <a class="font-weight-bold" v-bind:href="'/account/direct/t/'+n.account.id">dm</a>.
  58. </p>
  59. </div>
  60. <div class="align-items-center">
  61. <span class="small text-muted" data-toggle="tooltip" data-placement="bottom" :title="n.created_at">{{timeAgo(n.created_at)}}</span>
  62. </div>
  63. </div>
  64. <div>
  65. <div v-if="n.status && n.status && n.status.media_attachments && n.status.media_attachments.length">
  66. <a :href="getPostUrl(n.status)">
  67. <img :src="n.status.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.status && n.status.parent && n.status.parent.media_attachments && n.status.parent.media_attachments.length">
  76. <a :href="n.status.parent.url">
  77. <img :src="n.status.parent.media_attachments[0].preview_url" width="32px" height="32px">
  78. </a>
  79. </div> -->
  80. <!-- <div v-else-if="n.type == 'follow' && n.relationship.following == false">
  81. <a href="#" class="btn btn-primary py-0 font-weight-bold" @click.prevent="followProfile(n);">
  82. Follow
  83. </a>
  84. </div> -->
  85. <!-- <div v-else-if="n.status && n.status.parent && !n.status.parent.media_attachments && n.type == 'like' && n.relationship.following == false">
  86. <a href="#" class="btn btn-primary py-0 font-weight-bold">
  87. Follow
  88. </a>
  89. </div> -->
  90. <div v-else>
  91. <a class="btn btn-outline-primary py-0 font-weight-bold" :href="viewContext(n)">View</a>
  92. </div>
  93. </div>
  94. </div>
  95. <div v-if="notifications.length">
  96. <infinite-loading @infinite="infiniteNotifications">
  97. <div slot="no-results" class="font-weight-bold"></div>
  98. <div slot="no-more" class="font-weight-bold"></div>
  99. </infinite-loading>
  100. </div>
  101. <div v-if="notifications.length == 0" class="text-lighter text-center py-3">
  102. <p class="mb-0"><i class="fas fa-inbox fa-3x"></i></p>
  103. <p class="mb-0 small font-weight-bold">0 Notifications!</p>
  104. </div>
  105. </div>
  106. </div>
  107. </div>
  108. </div>
  109. </template>
  110. <script type="text/javascript">
  111. export default {
  112. data() {
  113. return {
  114. notifications: {},
  115. notificationCursor: 2,
  116. notificationMaxId: 0,
  117. };
  118. },
  119. mounted() {
  120. this.fetchNotifications();
  121. },
  122. updated() {
  123. $('[data-toggle="tooltip"]').tooltip()
  124. },
  125. methods: {
  126. fetchNotifications() {
  127. axios.get('/api/pixelfed/v1/accounts/verify_credentials').then(res => {
  128. window._sharedData.curUser = res.data;
  129. window.App.util.navatar();
  130. });
  131. axios.get('/api/pixelfed/v1/notifications?pg=true')
  132. .then(res => {
  133. let data = res.data.filter(n => {
  134. if(n.type == 'share' && !n.status) {
  135. return false;
  136. }
  137. if(n.type == 'comment' && !n.status) {
  138. return false;
  139. }
  140. if(n.type == 'mention' && !n.status) {
  141. return false;
  142. }
  143. if(n.type == 'favourite' && !n.status) {
  144. return false;
  145. }
  146. if(n.type == 'follow' && !n.account) {
  147. return false;
  148. }
  149. return true;
  150. });
  151. let ids = res.data.map(n => n.id);
  152. this.notificationMaxId = Math.max(...ids);
  153. this.notifications = data;
  154. $('.notification-card .loader').addClass('d-none');
  155. $('.notification-card .contents').removeClass('d-none');
  156. });
  157. },
  158. infiniteNotifications($state) {
  159. if(this.notificationCursor > 10) {
  160. $state.complete();
  161. return;
  162. }
  163. axios.get('/api/pixelfed/v1/notifications', {
  164. params: {
  165. max_id: this.notificationMaxId
  166. }
  167. }).then(res => {
  168. if(res.data.length) {
  169. let data = res.data.filter(n => {
  170. if(n.type == 'share' && !n.status) {
  171. return false;
  172. }
  173. if(n.type == 'comment' && !n.status) {
  174. return false;
  175. }
  176. if(n.type == 'mention' && !n.status) {
  177. return false;
  178. }
  179. if(n.type == 'favourite' && !n.status) {
  180. return false;
  181. }
  182. if(n.type == 'follow' && !n.account) {
  183. return false;
  184. }
  185. if(_.find(this.notifications, {id: n.id})) {
  186. return false;
  187. }
  188. return true;
  189. });
  190. this.notifications.push(...data);
  191. this.notificationCursor++;
  192. $state.loaded();
  193. } else {
  194. $state.complete();
  195. }
  196. });
  197. },
  198. truncate(text) {
  199. if(text.length <= 15) {
  200. return text;
  201. }
  202. return text.slice(0,15) + '...'
  203. },
  204. timeAgo(ts) {
  205. let date = Date.parse(ts);
  206. let seconds = Math.floor((new Date() - date) / 1000);
  207. let interval = Math.floor(seconds / 31536000);
  208. if (interval >= 1) {
  209. return interval + "y";
  210. }
  211. interval = Math.floor(seconds / 604800);
  212. if (interval >= 1) {
  213. return interval + "w";
  214. }
  215. interval = Math.floor(seconds / 86400);
  216. if (interval >= 1) {
  217. return interval + "d";
  218. }
  219. interval = Math.floor(seconds / 3600);
  220. if (interval >= 1) {
  221. return interval + "h";
  222. }
  223. interval = Math.floor(seconds / 60);
  224. if (interval >= 1) {
  225. return interval + "m";
  226. }
  227. return Math.floor(seconds) + "s";
  228. },
  229. mentionUrl(status) {
  230. let username = status.account.username;
  231. let id = status.id;
  232. return '/p/' + username + '/' + id;
  233. },
  234. followProfile(n) {
  235. let self = this;
  236. let id = n.account.id;
  237. axios.post('/i/follow', {
  238. item: id
  239. }).then(res => {
  240. self.notifications.map(notification => {
  241. if(notification.account.id === id) {
  242. notification.relationship.following = true;
  243. }
  244. });
  245. }).catch(err => {
  246. if(err.response.data.message) {
  247. swal('Error', err.response.data.message, 'error');
  248. }
  249. });
  250. },
  251. viewContext(n) {
  252. switch(n.type) {
  253. case 'follow':
  254. return n.account.url;
  255. break;
  256. case 'mention':
  257. return n.status.url;
  258. break;
  259. case 'like':
  260. case 'favourite':
  261. case 'comment':
  262. return n.status.url;
  263. break;
  264. case 'tagged':
  265. return n.tagged.post_url;
  266. break;
  267. case 'direct':
  268. return '/account/direct/t/'+n.account.id;
  269. break
  270. }
  271. return '/';
  272. },
  273. getProfileUrl(account) {
  274. if(account.local == true) {
  275. return account.url;
  276. }
  277. return '/i/web/profile/_/' + account.id;
  278. },
  279. getPostUrl(status) {
  280. if(status.local == true) {
  281. return status.url;
  282. }
  283. return '/i/web/post/_/' + status.account.id + '/' + status.id;
  284. }
  285. }
  286. }
  287. </script>