NotificationCard.vue 13 KB

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