Activity.vue 11 KB

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