NotificationCard.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <template>
  2. <div>
  3. <transition name="fade">
  4. <div class="card notification-card shadow-none border">
  5. <div class="card-header bg-white">
  6. <p class="mb-0 d-flex align-items-center justify-content-between">
  7. <span data-toggle="tooltip" data-placement="bottom"><i class="fas fa-redo fa-lg text-white"></i></span>
  8. <span class="small text-dark text-uppercase font-weight-bold">Alerts</span>
  9. <a class="text-decoration-none text-muted" href="/account/activity"><i class="fas fa-inbox fa-lg"></i></a>
  10. </p>
  11. </div>
  12. <div class="card-body loader text-center" style="height: 200px;">
  13. <div class="spinner-border" role="status">
  14. <span class="sr-only">Loading...</span>
  15. </div>
  16. </div>
  17. <div class="card-body pt-2 px-0 py-0 contents" style="max-height: 200px; overflow-y: scroll;">
  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. };
  78. },
  79. mounted() {
  80. this.fetchNotifications();
  81. },
  82. updated() {
  83. },
  84. methods: {
  85. fetchNotifications() {
  86. axios.get('/api/pixelfed/v1/notifications')
  87. .then(res => {
  88. let data = res.data;
  89. let ids = res.data.map(n => n.id);
  90. this.notificationMaxId = Math.min(...ids);
  91. this.notifications = data;
  92. $('.notification-card .loader').addClass('d-none');
  93. $('.notification-card .contents').removeClass('d-none');
  94. //this.notificationPoll();
  95. });
  96. },
  97. infiniteNotifications($state) {
  98. if(this.notificationCursor > 5) {
  99. $state.complete();
  100. return;
  101. }
  102. axios.get('/api/pixelfed/v1/notifications', {
  103. params: {
  104. page: this.notificationCursor
  105. }
  106. }).then(res => {
  107. if(res.data.length) {
  108. let data = res.data.filter(n => {
  109. if(n.type == 'share' && !status) {
  110. return false;
  111. }
  112. if(_.find(this.notifications, {id: n.id})) {
  113. return false;
  114. }
  115. return true;
  116. });
  117. this.notifications.push(...data);
  118. this.notificationCursor++;
  119. $state.loaded();
  120. } else {
  121. $state.complete();
  122. }
  123. });
  124. },
  125. truncate(text) {
  126. if(text.length <= 15) {
  127. return text;
  128. }
  129. return text.slice(0,15) + '...'
  130. },
  131. timeAgo(ts) {
  132. let date = Date.parse(ts);
  133. let seconds = Math.floor((new Date() - date) / 1000);
  134. let interval = Math.floor(seconds / 31536000);
  135. if (interval >= 1) {
  136. return interval + "y";
  137. }
  138. interval = Math.floor(seconds / 604800);
  139. if (interval >= 1) {
  140. return interval + "w";
  141. }
  142. interval = Math.floor(seconds / 86400);
  143. if (interval >= 1) {
  144. return interval + "d";
  145. }
  146. interval = Math.floor(seconds / 3600);
  147. if (interval >= 1) {
  148. return interval + "h";
  149. }
  150. interval = Math.floor(seconds / 60);
  151. if (interval >= 1) {
  152. return interval + "m";
  153. }
  154. return Math.floor(seconds) + "s";
  155. },
  156. mentionUrl(status) {
  157. let username = status.account.username;
  158. let id = status.id;
  159. return '/p/' + username + '/' + id;
  160. },
  161. notificationPoll() {
  162. let interval = this.notifications.length > 5 ? 15000 : 120000;
  163. let self = this;
  164. setInterval(function() {
  165. axios.get('/api/pixelfed/v1/notifications')
  166. .then(res => {
  167. let data = res.data.filter(n => {
  168. if(n.type == 'share' || self.notificationMaxId >= n.id) {
  169. return false;
  170. }
  171. return true;
  172. });
  173. if(data.length) {
  174. let ids = data.map(n => n.id);
  175. self.notificationMaxId = Math.max(...ids);
  176. self.notifications.unshift(...data);
  177. let beep = new Audio('/static/beep.mp3');
  178. beep.volume = 0.7;
  179. beep.play();
  180. $('.notification-card .far.fa-bell').addClass('fas text-danger').removeClass('far text-muted');
  181. }
  182. });
  183. }, interval);
  184. },
  185. refreshNotifications() {
  186. let self = this;
  187. axios.get('/api/pixelfed/v1/notifications')
  188. .then(res => {
  189. let data = res.data.filter(n => {
  190. if(n.type == 'share' || self.notificationMaxId >= n.id) {
  191. return false;
  192. }
  193. return true;
  194. });
  195. if(data.length > 0) {
  196. let ids = data.map(n => n.id);
  197. let max = Math.max(ids);
  198. if(max <= self.notificationMaxId) {
  199. return;
  200. } else {
  201. self.notificationMaxId = max;
  202. self.notifications = data;
  203. let beep = new Audio('/static/beep.mp3');
  204. beep.volume = 0.7;
  205. beep.play();
  206. }
  207. }
  208. });
  209. }
  210. }
  211. }
  212. </script>