Activity.vue 11 KB

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