NotificationCard.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <template>
  2. <div>
  3. <transition name="fade">
  4. <div class="card notification-card shadow-none border">
  5. <div class="card-body loader text-center" style="height: 200px;">
  6. <div class="spinner-border" role="status">
  7. <span class="sr-only">Loading...</span>
  8. </div>
  9. </div>
  10. <div v-if="notifications.length > 0" class="card-body px-0 py-0 contents" style="max-height: 240px; overflow-y: scroll;">
  11. <div v-if="profile.locked" class="media align-items-center mt-n2 px-3 py-2 border-bottom border-lighter bg-light cursor-pointer" @click="redirect('/account/follow-requests')">
  12. <div class="media-body font-weight-light pt-2 small d-flex align-items-center justify-content-between">
  13. <p class="mb-0 text-lighter"><i class="fas fa-cog text-light"></i></p>
  14. <p class="text-center pt-1 mb-1 text-dark font-weight-bold"><strong>{{followRequests.count}}</strong> Follow Requests</p>
  15. <p class="mb-0 text-lighter"><i class="fas fa-chevron-right"></i></p>
  16. </div>
  17. </div>
  18. <div v-if="notifications.length > 0" class="media align-items-center px-3 py-2 border-bottom border-light" v-for="(n, index) in notifications">
  19. <img class="mr-2 rounded-circle" style="border:1px solid #ccc" :src="n.account.avatar" alt="" width="32px" height="32px" onerror="this.onerror=null;this.src='/storage/avatars/default.png';">
  20. <div class="media-body font-weight-light small">
  21. <div v-if="n.type == 'favourite'">
  22. <p class="my-0">
  23. <a :href="n.account.url" class="font-weight-bold text-dark word-break" :title="n.account.username">{{truncate(n.account.username)}}</a> liked your
  24. <span v-if="n.status.hasOwnProperty('media_attachments')">
  25. <a class="font-weight-bold" v-bind:href="n.status.url" :id="'fvn-' + n.id">post</a>.
  26. <b-popover :target="'fvn-' + n.id" title="" triggers="hover" placement="top" boundary="window">
  27. <img :src="notificationPreview(n)" width="100px" height="100px" style="object-fit: cover;">
  28. </b-popover>
  29. </span>
  30. <span v-else>
  31. <a class="font-weight-bold" v-bind:href="n.status.url">post</a>.
  32. </span>
  33. </p>
  34. </div>
  35. <div v-else-if="n.type == 'comment'">
  36. <p class="my-0">
  37. <a :href="n.account.url" class="font-weight-bold text-dark word-break" :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>.
  38. </p>
  39. </div>
  40. <div v-else-if="n.type == 'mention'">
  41. <p class="my-0">
  42. <a :href="n.account.url" class="font-weight-bold text-dark word-break" :title="n.account.username">{{truncate(n.account.username)}}</a> <a class="font-weight-bold" v-bind:href="mentionUrl(n.status)">mentioned</a> you.
  43. </p>
  44. </div>
  45. <div v-else-if="n.type == 'follow'">
  46. <p class="my-0">
  47. <a :href="n.account.url" class="font-weight-bold text-dark word-break" :title="n.account.username">{{truncate(n.account.username)}}</a> followed you.
  48. </p>
  49. </div>
  50. <div v-else-if="n.type == 'share'">
  51. <p class="my-0">
  52. <a :href="n.account.url" class="font-weight-bold text-dark word-break" :title="n.account.username">{{truncate(n.account.username)}}</a> shared your <a class="font-weight-bold" v-bind:href="n.status.url">post</a>.
  53. </p>
  54. </div>
  55. <div v-else-if="n.type == 'modlog'">
  56. <p class="my-0">
  57. <a :href="n.account.url" 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>.
  58. </p>
  59. </div>
  60. <div v-else-if="n.type == 'tagged'">
  61. <p class="my-0">
  62. <a :href="n.account.url" class="font-weight-bold text-dark word-break" :title="n.account.username">{{truncate(n.account.username)}}</a> tagged you in a <a class="font-weight-bold" v-bind:href="n.tagged.post_url">post</a>.
  63. </p>
  64. </div>
  65. <div v-else-if="n.type == 'direct'">
  66. <p class="my-0">
  67. <a :href="n.account.url" class="font-weight-bold text-dark word-break" :title="n.account.username">{{truncate(n.account.username)}}</a> sent a <a class="font-weight-bold" v-bind:href="'/account/direct/t/'+n.account.id">dm</a>.
  68. </p>
  69. </div>
  70. <div v-else>
  71. <p class="my-0">
  72. We cannot display this notification at this time.
  73. </p>
  74. </div>
  75. </div>
  76. <div class="small text-muted font-weight-bold" :title="n.created_at">{{timeAgo(n.created_at)}}</div>
  77. </div>
  78. <div v-if="notifications.length">
  79. <infinite-loading @infinite="infiniteNotifications">
  80. <div slot="no-results" class="font-weight-bold"></div>
  81. <div slot="no-more" class="font-weight-bold"></div>
  82. </infinite-loading>
  83. </div>
  84. <div v-if="notifications.length == 0" class="text-lighter text-center py-3">
  85. <p class="mb-0"><i class="fas fa-inbox fa-3x"></i></p>
  86. <p class="mb-0 small font-weight-bold">0 Notifications!</p>
  87. </div>
  88. </div>
  89. </div>
  90. </transition>
  91. </div>
  92. </template>
  93. <style type="text/css" scoped></style>
  94. <script type="text/javascript">
  95. export default {
  96. data() {
  97. return {
  98. notifications: {},
  99. notificationCursor: 2,
  100. notificationMaxId: 0,
  101. profile: {
  102. locked: false
  103. },
  104. followRequests: null
  105. };
  106. },
  107. mounted() {
  108. let self = this;
  109. this.fetchNotifications();
  110. setTimeout(function() {
  111. self.profile = window._sharedData.curUser;
  112. self.fetchFollowRequests();
  113. }, 500);
  114. },
  115. updated() {
  116. },
  117. methods: {
  118. fetchNotifications() {
  119. axios.get('/api/pixelfed/v1/notifications?pg=true')
  120. .then(res => {
  121. let data = res.data;
  122. let ids = res.data.map(n => n.id);
  123. this.notificationMaxId = Math.min(...ids);
  124. this.notifications = data;
  125. $('.notification-card .loader').addClass('d-none');
  126. $('.notification-card .contents').removeClass('d-none');
  127. //this.notificationPoll();
  128. });
  129. },
  130. infiniteNotifications($state) {
  131. if(this.notificationCursor > 5) {
  132. $state.complete();
  133. return;
  134. }
  135. axios.get('/api/pixelfed/v1/notifications', {
  136. params: {
  137. page: this.notificationCursor
  138. }
  139. }).then(res => {
  140. if(res.data.length) {
  141. let data = res.data.filter(n => {
  142. if(n.type == 'share' && !status) {
  143. return false;
  144. }
  145. if(_.find(this.notifications, {id: n.id})) {
  146. return false;
  147. }
  148. return true;
  149. });
  150. this.notifications.push(...data);
  151. this.notificationCursor++;
  152. $state.loaded();
  153. } else {
  154. $state.complete();
  155. }
  156. });
  157. },
  158. truncate(text) {
  159. if(text.length <= 15) {
  160. return text;
  161. }
  162. return text.slice(0,15) + '...'
  163. },
  164. timeAgo(ts) {
  165. return window.App.util.format.timeAgo(ts);
  166. },
  167. mentionUrl(status) {
  168. let username = status.account.username;
  169. let id = status.id;
  170. return '/p/' + username + '/' + id;
  171. },
  172. notificationPoll() {
  173. let interval = this.notifications.length > 5 ? 15000 : 120000;
  174. let self = this;
  175. setInterval(function() {
  176. axios.get('/api/pixelfed/v1/notifications')
  177. .then(res => {
  178. let data = res.data.filter(n => {
  179. if(n.type == 'share' || self.notificationMaxId >= n.id) {
  180. return false;
  181. }
  182. return true;
  183. });
  184. if(data.length) {
  185. let ids = data.map(n => n.id);
  186. self.notificationMaxId = Math.max(...ids);
  187. self.notifications.unshift(...data);
  188. let beep = new Audio('/static/beep.mp3');
  189. beep.volume = 0.7;
  190. beep.play();
  191. $('.notification-card .far.fa-bell').addClass('fas text-danger').removeClass('far text-muted');
  192. }
  193. });
  194. }, interval);
  195. },
  196. refreshNotifications() {
  197. let self = this;
  198. axios.get('/api/pixelfed/v1/notifications')
  199. .then(res => {
  200. let data = res.data.filter(n => {
  201. if(n.type == 'share' || self.notificationMaxId >= n.id) {
  202. return false;
  203. }
  204. return true;
  205. });
  206. if(data.length > 0) {
  207. let ids = data.map(n => n.id);
  208. let max = Math.max(ids);
  209. if(max <= self.notificationMaxId) {
  210. return;
  211. } else {
  212. self.notificationMaxId = max;
  213. self.notifications = data;
  214. let beep = new Audio('/static/beep.mp3');
  215. beep.volume = 0.7;
  216. beep.play();
  217. }
  218. }
  219. });
  220. },
  221. fetchFollowRequests() {
  222. if(window._sharedData.curUser.locked == true) {
  223. axios.get('/account/follow-requests.json')
  224. .then(res => {
  225. this.followRequests = res.data;
  226. })
  227. }
  228. },
  229. redirect(url) {
  230. window.location.href = url;
  231. },
  232. notificationPreview(n) {
  233. if(!n.status.hasOwnProperty('media_attachments') || !n.status.media_attachments.length) {
  234. return '/storage/no-preview.png';
  235. }
  236. return n.status.media_attachments[0].preview_url;
  237. }
  238. }
  239. }
  240. </script>