1
0

Activity.vue 12 KB

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