NotificationCard.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <template>
  2. <div>
  3. <transition name="fade">
  4. <div class="card notification-card shadow-none border">
  5. <div v-if="loading" class="card-body loader text-center" style="height: 240px;">
  6. <div class="spinner-border" role="status">
  7. <span class="sr-only">Loading...</span>
  8. </div>
  9. </div>
  10. <div v-if="!loading && notifications.length > 0" class="card-body px-0 py-0 contents" style="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 && followRequests.hasOwnProperty('count') ? followRequests.count : 0 }}</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="getProfileUrl(n.account)" class="font-weight-bold text-dark word-break" :title="n.account.username">{{n.account.local == false ? '@':''}}{{truncate(n.account.username)}}</a> liked your
  24. <span v-if="n.status && n.status.hasOwnProperty('media_attachments')">
  25. <a class="font-weight-bold" v-bind:href="getPostUrl(n.status)" :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="getPostUrl(n.status)">post</a>.
  32. </span>
  33. </p>
  34. </div>
  35. <div v-else-if="n.type == 'comment'">
  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> commented on your <a class="font-weight-bold" v-bind:href="getPostUrl(n.status)">post</a>.
  38. </p>
  39. </div>
  40. <div v-else-if="n.type == 'group: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="n.group_post_url">group post</a>.
  43. </p>
  44. </div>
  45. <div v-else-if="n.type == 'story:react'">
  46. <p class="my-0">
  47. <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>.
  48. </p>
  49. </div>
  50. <div v-else-if="n.type == 'story:comment'">
  51. <p class="my-0">
  52. <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>.
  53. </p>
  54. </div>
  55. <div v-else-if="n.type == 'mention'">
  56. <p class="my-0">
  57. <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> <a class="font-weight-bold" v-bind:href="mentionUrl(n.status)">mentioned</a> you.
  58. </p>
  59. </div>
  60. <div v-else-if="n.type == 'follow'">
  61. <p class="my-0">
  62. <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> followed you.
  63. </p>
  64. </div>
  65. <div v-else-if="n.type == 'share'">
  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> shared your <a class="font-weight-bold" v-bind:href="getPostUrl(n.status)">post</a>.
  68. </p>
  69. </div>
  70. <div v-else-if="n.type == 'modlog'">
  71. <p class="my-0">
  72. <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>.
  73. </p>
  74. </div>
  75. <div v-else-if="n.type == 'tagged'">
  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> tagged you in a <a class="font-weight-bold" v-bind:href="n.tagged.post_url">post</a>.
  78. </p>
  79. </div>
  80. <div v-else-if="n.type == 'direct'">
  81. <p class="my-0">
  82. <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>.
  83. </p>
  84. </div>
  85. <div v-else-if="n.type == 'group.join.approved'">
  86. <p class="my-0">
  87. Your application to join the <a :href="n.group.url" class="font-weight-bold text-dark word-break" :title="n.group.name">{{truncate(n.group.name)}}</a> group was approved!
  88. </p>
  89. </div>
  90. <div v-else-if="n.type == 'group.join.rejected'">
  91. <p class="my-0">
  92. 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.
  93. </p>
  94. </div>
  95. <div v-else-if="n.type == 'group:invite'">
  96. <p class="my-0">
  97. <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> invited you to join <a :href="n.group.url + '/invite/claim'" class="font-weight-bold text-dark word-break" :title="n.group.name">{{n.group.name}}</a>.
  98. </p>
  99. </div>
  100. <div v-else>
  101. <p class="my-0">
  102. We cannot display this notification at this time.
  103. </p>
  104. </div>
  105. </div>
  106. <div class="small text-muted font-weight-bold" :title="n.created_at">{{timeAgo(n.created_at)}}</div>
  107. </div>
  108. <div v-if="notifications.length">
  109. <infinite-loading @infinite="infiniteNotifications">
  110. <div slot="no-results" class="font-weight-bold"></div>
  111. <div slot="no-more" class="font-weight-bold"></div>
  112. </infinite-loading>
  113. </div>
  114. <div v-if="notifications.length == 0" class="text-lighter text-center py-3">
  115. <p class="mb-0"><i class="fas fa-inbox fa-3x"></i></p>
  116. <p class="mb-0 small font-weight-bold">0 Notifications!</p>
  117. </div>
  118. </div>
  119. <div v-if="!loading && !notifications.length" class="card-body px-0 py-0" style="height: 240px;">
  120. <div class="text-lighter text-center py-3">
  121. <p class="mb-0"><i class="fas fa-inbox fa-3x"></i></p>
  122. <p class="mb-0 small font-weight-bold">No notifications yet</p>
  123. <p v-if="showRefresh && !attemptedRefresh" class="mt-2 small font-weight-bold text-primary cursor-pointer" @click="refreshNotifications"><i class="fas fa-redo"></i> Refresh</p>
  124. </div>
  125. </div>
  126. </div>
  127. </transition>
  128. </div>
  129. </template>
  130. <style type="text/css" scoped></style>
  131. <script type="text/javascript">
  132. export default {
  133. data() {
  134. return {
  135. loading: true,
  136. notifications: {},
  137. notificationCursor: 2,
  138. notificationMaxId: 0,
  139. profile: {
  140. locked: false
  141. },
  142. followRequests: null,
  143. showRefresh: false,
  144. attemptedRefresh: false
  145. };
  146. },
  147. mounted() {
  148. let self = this;
  149. this.fetchNotifications();
  150. setTimeout(function() {
  151. self.profile = window._sharedData.curUser;
  152. self.fetchFollowRequests();
  153. }, 1500);
  154. },
  155. updated() {
  156. },
  157. methods: {
  158. fetchNotifications() {
  159. axios.get('/api/v1/notifications?pg=true')
  160. .then(res => {
  161. let data = res.data.filter(n => {
  162. if(n.type == 'share' && !n.status) {
  163. return false;
  164. }
  165. if(n.type == 'comment' && !n.status) {
  166. return false;
  167. }
  168. if(n.type == 'mention' && !n.status) {
  169. return false;
  170. }
  171. if(n.type == 'favourite' && !n.status) {
  172. return false;
  173. }
  174. if(n.type == 'follow' && !n.account) {
  175. return false;
  176. }
  177. return true;
  178. });
  179. let ids = data.map(n => n.id);
  180. this.notificationMaxId = Math.min(...ids);
  181. this.notifications = data;
  182. this.loading = false;
  183. if(data.length == 0 && !this.attemptedRefresh) {
  184. this.showRefresh = true;
  185. }
  186. //this.notificationPoll();
  187. });
  188. },
  189. infiniteNotifications($state) {
  190. if(this.notificationCursor > 5) {
  191. $state.complete();
  192. return;
  193. }
  194. axios.get('/api/pixelfed/v1/notifications', {
  195. params: {
  196. max_id: this.notificationMaxId
  197. }
  198. }).then(res => {
  199. if(res.data.length) {
  200. let data = res.data.filter(n => {
  201. if(n.type == 'share' && !n.status) {
  202. return false;
  203. }
  204. if(n.type == 'comment' && !n.status) {
  205. return false;
  206. }
  207. if(n.type == 'mention' && !n.status) {
  208. return false;
  209. }
  210. if(n.type == 'favourite' && !n.status) {
  211. return false;
  212. }
  213. if(n.type == 'follow' && !n.account) {
  214. return false;
  215. }
  216. if(_.find(this.notifications, {id: n.id})) {
  217. return false;
  218. }
  219. return true;
  220. });
  221. let ids = data.map(n => n.id);
  222. this.notificationMaxId = Math.min(...ids);
  223. this.notifications.push(...data);
  224. this.notificationCursor++;
  225. $state.loaded();
  226. } else {
  227. $state.complete();
  228. }
  229. });
  230. },
  231. truncate(text) {
  232. if(text.length <= 15) {
  233. return text;
  234. }
  235. return text.slice(0,15) + '...'
  236. },
  237. timeAgo(ts) {
  238. return window.App.util.format.timeAgo(ts);
  239. },
  240. mentionUrl(status) {
  241. let username = status.account.username;
  242. let id = status.id;
  243. return '/p/' + username + '/' + id;
  244. },
  245. notificationPoll() {
  246. let interval = this.notifications.length > 5 ? 15000 : 120000;
  247. let self = this;
  248. setInterval(function() {
  249. axios.get('/api/pixelfed/v1/notifications')
  250. .then(res => {
  251. let data = res.data.filter(n => {
  252. if(n.type == 'share' || self.notificationMaxId >= n.id) {
  253. return false;
  254. }
  255. return true;
  256. });
  257. if(data.length) {
  258. let ids = data.map(n => n.id);
  259. self.notificationMaxId = Math.max(...ids);
  260. self.notifications.unshift(...data);
  261. let beep = new Audio('/static/beep.mp3');
  262. beep.volume = 0.7;
  263. beep.play();
  264. $('.notification-card .far.fa-bell').addClass('fas text-danger').removeClass('far text-muted');
  265. }
  266. });
  267. }, interval);
  268. },
  269. refreshNotifications() {
  270. let self = this;
  271. axios.get('/api/pixelfed/v1/notifications')
  272. .then(res => {
  273. let data = res.data.filter(n => {
  274. if(n.type == 'share' || self.notificationMaxId >= n.id) {
  275. return false;
  276. }
  277. return true;
  278. });
  279. if(data.length > 0) {
  280. let ids = data.map(n => n.id);
  281. let max = Math.max(ids);
  282. if(max <= self.notificationMaxId) {
  283. return;
  284. } else {
  285. self.notificationMaxId = max;
  286. self.notifications = data;
  287. let beep = new Audio('/static/beep.mp3');
  288. beep.volume = 0.7;
  289. beep.play();
  290. }
  291. }
  292. });
  293. },
  294. fetchFollowRequests() {
  295. if(window._sharedData.curUser.locked == true) {
  296. axios.get('/account/follow-requests.json')
  297. .then(res => {
  298. this.followRequests = res.data;
  299. })
  300. }
  301. },
  302. redirect(url) {
  303. window.location.href = url;
  304. },
  305. notificationPreview(n) {
  306. if(!n.status || !n.status.hasOwnProperty('media_attachments') || !n.status.media_attachments.length) {
  307. return '/storage/no-preview.png';
  308. }
  309. return n.status.media_attachments[0].preview_url;
  310. },
  311. getProfileUrl(account) {
  312. if(account.local == true) {
  313. return account.url;
  314. }
  315. return '/i/web/profile/_/' + account.id;
  316. },
  317. getPostUrl(status) {
  318. if(!status) {
  319. return;
  320. }
  321. if(!status.hasOwnProperty('local') || status.local == true) {
  322. return status.url;
  323. }
  324. return '/i/web/post/_/' + status.account.id + '/' + status.id;
  325. },
  326. refreshNotifications() {
  327. this.loading = true;
  328. this.attemptedRefresh = true;
  329. this.fetchNotifications();
  330. }
  331. }
  332. }
  333. </script>