NotificationCard.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 <a class="font-weight-bold" v-bind:href="n.status.url">post</a>.
  24. </p>
  25. </div>
  26. <div v-else-if="n.type == 'comment'">
  27. <p class="my-0">
  28. <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>.
  29. </p>
  30. </div>
  31. <div v-else-if="n.type == 'mention'">
  32. <p class="my-0">
  33. <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.
  34. </p>
  35. </div>
  36. <div v-else-if="n.type == 'follow'">
  37. <p class="my-0">
  38. <a :href="n.account.url" class="font-weight-bold text-dark word-break" :title="n.account.username">{{truncate(n.account.username)}}</a> followed you.
  39. </p>
  40. </div>
  41. <div v-else-if="n.type == 'share'">
  42. <p class="my-0">
  43. <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>.
  44. </p>
  45. </div>
  46. <div v-else-if="n.type == 'modlog'">
  47. <p class="my-0">
  48. <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>.
  49. </p>
  50. </div>
  51. </div>
  52. <div class="small text-muted font-weight-bold" :title="n.created_at">{{timeAgo(n.created_at)}}</div>
  53. </div>
  54. <div v-if="notifications.length">
  55. <infinite-loading @infinite="infiniteNotifications">
  56. <div slot="no-results" class="font-weight-bold"></div>
  57. <div slot="no-more" class="font-weight-bold"></div>
  58. </infinite-loading>
  59. </div>
  60. <div v-if="notifications.length == 0" class="text-lighter text-center py-3">
  61. <p class="mb-0"><i class="fas fa-inbox fa-3x"></i></p>
  62. <p class="mb-0 small font-weight-bold">0 Notifications!</p>
  63. </div>
  64. </div>
  65. </div>
  66. </transition>
  67. </div>
  68. </template>
  69. <style type="text/css" scoped></style>
  70. <script type="text/javascript">
  71. export default {
  72. data() {
  73. return {
  74. notifications: {},
  75. notificationCursor: 2,
  76. notificationMaxId: 0,
  77. profile: {
  78. locked: false
  79. },
  80. followRequests: null
  81. };
  82. },
  83. mounted() {
  84. let self = this;
  85. this.fetchNotifications();
  86. setTimeout(function() {
  87. self.profile = window._sharedData.curUser;
  88. self.fetchFollowRequests();
  89. }, 500);
  90. },
  91. updated() {
  92. },
  93. methods: {
  94. fetchNotifications() {
  95. axios.get('/api/pixelfed/v1/notifications?pg=true')
  96. .then(res => {
  97. let data = res.data;
  98. let ids = res.data.map(n => n.id);
  99. this.notificationMaxId = Math.min(...ids);
  100. this.notifications = data;
  101. $('.notification-card .loader').addClass('d-none');
  102. $('.notification-card .contents').removeClass('d-none');
  103. //this.notificationPoll();
  104. });
  105. },
  106. infiniteNotifications($state) {
  107. if(this.notificationCursor > 5) {
  108. $state.complete();
  109. return;
  110. }
  111. axios.get('/api/pixelfed/v1/notifications', {
  112. params: {
  113. page: this.notificationCursor
  114. }
  115. }).then(res => {
  116. if(res.data.length) {
  117. let data = res.data.filter(n => {
  118. if(n.type == 'share' && !status) {
  119. return false;
  120. }
  121. if(_.find(this.notifications, {id: n.id})) {
  122. return false;
  123. }
  124. return true;
  125. });
  126. this.notifications.push(...data);
  127. this.notificationCursor++;
  128. $state.loaded();
  129. } else {
  130. $state.complete();
  131. }
  132. });
  133. },
  134. truncate(text) {
  135. if(text.length <= 15) {
  136. return text;
  137. }
  138. return text.slice(0,15) + '...'
  139. },
  140. timeAgo(ts) {
  141. return window.App.util.format.timeAgo(ts);
  142. },
  143. mentionUrl(status) {
  144. let username = status.account.username;
  145. let id = status.id;
  146. return '/p/' + username + '/' + id;
  147. },
  148. notificationPoll() {
  149. let interval = this.notifications.length > 5 ? 15000 : 120000;
  150. let self = this;
  151. setInterval(function() {
  152. axios.get('/api/pixelfed/v1/notifications')
  153. .then(res => {
  154. let data = res.data.filter(n => {
  155. if(n.type == 'share' || self.notificationMaxId >= n.id) {
  156. return false;
  157. }
  158. return true;
  159. });
  160. if(data.length) {
  161. let ids = data.map(n => n.id);
  162. self.notificationMaxId = Math.max(...ids);
  163. self.notifications.unshift(...data);
  164. let beep = new Audio('/static/beep.mp3');
  165. beep.volume = 0.7;
  166. beep.play();
  167. $('.notification-card .far.fa-bell').addClass('fas text-danger').removeClass('far text-muted');
  168. }
  169. });
  170. }, interval);
  171. },
  172. refreshNotifications() {
  173. let self = this;
  174. axios.get('/api/pixelfed/v1/notifications')
  175. .then(res => {
  176. let data = res.data.filter(n => {
  177. if(n.type == 'share' || self.notificationMaxId >= n.id) {
  178. return false;
  179. }
  180. return true;
  181. });
  182. if(data.length > 0) {
  183. let ids = data.map(n => n.id);
  184. let max = Math.max(ids);
  185. if(max <= self.notificationMaxId) {
  186. return;
  187. } else {
  188. self.notificationMaxId = max;
  189. self.notifications = data;
  190. let beep = new Audio('/static/beep.mp3');
  191. beep.volume = 0.7;
  192. beep.play();
  193. }
  194. }
  195. });
  196. },
  197. fetchFollowRequests() {
  198. if(window._sharedData.curUser.locked == true) {
  199. axios.get('/account/follow-requests.json')
  200. .then(res => {
  201. this.followRequests = res.data;
  202. })
  203. }
  204. },
  205. redirect(url) {
  206. window.location.href = url;
  207. }
  208. }
  209. }
  210. </script>