Notifications.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. <template>
  2. <div class="notifications-component">
  3. <div class="card shadow-sm mb-3" style="overflow: hidden;border-radius: 15px !important;">
  4. <div class="card-body pb-0">
  5. <div class="d-flex justify-content-between align-items-center mb-3">
  6. <span class="text-muted font-weight-bold">Notifications</span>
  7. <div v-if="feed && feed.length">
  8. <router-link to="/i/web/notifications" class="btn btn-outline-light btn-sm mr-2" style="color: #B8C2CC !important">
  9. <i class="far fa-filter"></i>
  10. </router-link>
  11. <button
  12. v-if="hasLoaded && feed.length"
  13. class="btn btn-light btn-sm"
  14. :class="{ 'text-lighter': isRefreshing }"
  15. :disabled="isRefreshing"
  16. @click="refreshNotifications">
  17. <i class="fal fa-redo"></i>
  18. </button>
  19. </div>
  20. </div>
  21. <div v-if="!hasLoaded" class="notifications-component-feed">
  22. <div class="d-flex align-items-center justify-content-center flex-column bg-light rounded-lg p-3 mb-3" style="min-height: 100px;">
  23. <b-spinner variant="grow" />
  24. </div>
  25. </div>
  26. <div v-else class="notifications-component-feed">
  27. <template v-if="isEmpty">
  28. <div class="d-flex align-items-center justify-content-center flex-column bg-light rounded-lg p-3 mb-3" style="min-height: 100px;">
  29. <i class="fal fa-bell fa-2x text-lighter"></i>
  30. <p class="mt-2 small font-weight-bold text-center mb-0">{{ $t('notifications.noneFound') }}</p>
  31. </div>
  32. </template>
  33. <template v-else>
  34. <div v-for="(n, index) in feed" class="mb-2">
  35. <div class="media align-items-center">
  36. <img
  37. class="mr-2 rounded-circle shadow-sm"
  38. :src="n.account.avatar"
  39. width="32"
  40. height="32"
  41. onerror="this.onerror=null;this.src='/storage/avatars/default.png';">
  42. <div class="media-body font-weight-light small">
  43. <div v-if="n.type == 'favourite'">
  44. <p class="my-0">
  45. <a :href="getProfileUrl(n.account)" class="font-weight-bold text-dark word-break" :title="n.account.acct">{{n.account.local == false ? '@':''}}{{truncate(n.account.username)}}</a> liked your
  46. <span v-if="n.status && n.status.hasOwnProperty('media_attachments')">
  47. <a class="font-weight-bold" v-bind:href="getPostUrl(n.status)" :id="'fvn-' + n.id" @click.prevent="goToPost(n.status)">post</a>.
  48. <b-popover :target="'fvn-' + n.id" title="" triggers="hover" placement="top" boundary="window">
  49. <img :src="notificationPreview(n)" width="100px" height="100px" style="object-fit: cover;">
  50. </b-popover>
  51. </span>
  52. <span v-else>
  53. <a class="font-weight-bold" :href="getPostUrl(n.status)" @click.prevent="goToPost(n.status)">post</a>.
  54. </span>
  55. </p>
  56. </div>
  57. <div v-else-if="n.type == 'comment'">
  58. <p class="my-0">
  59. <a :href="getProfileUrl(n.account)" class="font-weight-bold text-dark word-break" :title="n.account.acct">{{n.account.local == false ? '@':''}}{{truncate(n.account.username)}}</a> commented on your <a class="font-weight-bold" :href="getPostUrl(n.status)" @click.prevent="goToPost(n.status)">post</a>.
  60. </p>
  61. </div>
  62. <div v-else-if="n.type == 'group:comment'">
  63. <p class="my-0">
  64. <a :href="getProfileUrl(n.account)" class="font-weight-bold text-dark word-break" :title="n.account.acct">{{n.account.local == false ? '@':''}}{{truncate(n.account.username)}}</a> commented on your <a class="font-weight-bold" :href="n.group_post_url">group post</a>.
  65. </p>
  66. </div>
  67. <div v-else-if="n.type == 'story:react'">
  68. <p class="my-0">
  69. <a :href="getProfileUrl(n.account)" class="font-weight-bold text-dark word-break" :title="n.account.acct">{{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>.
  70. </p>
  71. </div>
  72. <div v-else-if="n.type == 'story:comment'">
  73. <p class="my-0">
  74. <a :href="getProfileUrl(n.account)" class="font-weight-bold text-dark word-break" :title="n.account.acct">{{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>.
  75. </p>
  76. </div>
  77. <div v-else-if="n.type == 'mention'">
  78. <p class="my-0">
  79. <a :href="getProfileUrl(n.account)" class="font-weight-bold text-dark word-break" :title="n.account.acct">{{n.account.local == false ? '@':''}}{{truncate(n.account.username)}}</a> <a class="font-weight-bold" v-bind:href="mentionUrl(n.status)" @click.prevent="goToPost(n.status)">mentioned</a> you.
  80. </p>
  81. </div>
  82. <div v-else-if="n.type == 'follow'">
  83. <p class="my-0">
  84. <a :href="getProfileUrl(n.account)" class="font-weight-bold text-dark word-break" :title="n.account.acct">{{n.account.local == false ? '@':''}}{{truncate(n.account.username)}}</a> followed you.
  85. </p>
  86. </div>
  87. <div v-else-if="n.type == 'share'">
  88. <p class="my-0">
  89. <a :href="getProfileUrl(n.account)" class="font-weight-bold text-dark word-break" :title="n.account.acct">{{n.account.local == false ? '@':''}}{{truncate(n.account.username)}}</a> shared your <a class="font-weight-bold" :href="getPostUrl(n.status)" @click.prevent="goToPost(n.status)">post</a>.
  90. </p>
  91. </div>
  92. <div v-else-if="n.type == 'modlog'">
  93. <p class="my-0">
  94. <a :href="getProfileUrl(n.account)" class="font-weight-bold text-dark word-break" :title="n.account.acct">{{truncate(n.account.username)}}</a> updated a <a class="font-weight-bold" v-bind:href="n.modlog.url">modlog</a>.
  95. </p>
  96. </div>
  97. <div v-else-if="n.type == 'tagged'">
  98. <p class="my-0">
  99. <a :href="getProfileUrl(n.account)" class="font-weight-bold text-dark word-break" :title="n.account.acct">{{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>.
  100. </p>
  101. </div>
  102. <div v-else-if="n.type == 'direct'">
  103. <p class="my-0">
  104. <a :href="getProfileUrl(n.account)" class="font-weight-bold text-dark word-break" :title="n.account.acct">{{n.account.local == false ? '@':''}}{{truncate(n.account.username)}}</a> sent a <router-link class="font-weight-bold" :to="'/i/web/direct/thread/'+n.account.id">dm</router-link>.
  105. </p>
  106. </div>
  107. <div v-else-if="n.type == 'group.join.approved'">
  108. <p class="my-0">
  109. 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!
  110. </p>
  111. </div>
  112. <div v-else-if="n.type == 'group.join.rejected'">
  113. <p class="my-0">
  114. 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.
  115. </p>
  116. </div>
  117. <div v-else-if="n.type == 'group:invite'">
  118. <p class="my-0">
  119. <a :href="getProfileUrl(n.account)" class="font-weight-bold text-dark word-break" :title="n.account.acct">{{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>.
  120. </p>
  121. </div>
  122. <div v-else>
  123. <p class="my-0">
  124. We cannot display this notification at this time.
  125. </p>
  126. </div>
  127. </div>
  128. <div class="small text-muted font-weight-bold" :title="n.created_at">{{timeAgo(n.created_at)}}</div>
  129. </div>
  130. </div>
  131. <div v-if="hasLoaded && feed.length == 0">
  132. <p class="small font-weight-bold text-center mb-0">{{ $t('notifications.noneFound') }}</p>
  133. </div>
  134. <div v-else>
  135. <intersect v-if="hasLoaded && canLoadMore" @enter="enterIntersect">
  136. <placeholder small style="margin-top: -6px" />
  137. <placeholder small/>
  138. <placeholder small/>
  139. <placeholder small/>
  140. </intersect>
  141. <div v-else class="d-block" style="height: 10px;">
  142. </div>
  143. </div>
  144. </template>
  145. </div>
  146. </div>
  147. </div>
  148. </div>
  149. </template>
  150. <script type="text/javascript">
  151. import Placeholder from './../partials/placeholders/NotificationPlaceholder.vue';
  152. import Intersect from 'vue-intersect';
  153. export default {
  154. props: {
  155. profile: {
  156. type: Object
  157. }
  158. },
  159. components: {
  160. "intersect": Intersect,
  161. "placeholder": Placeholder
  162. },
  163. data() {
  164. return {
  165. feed: {},
  166. maxId: undefined,
  167. isIntersecting: false,
  168. canLoadMore: false,
  169. isRefreshing: false,
  170. hasLoaded: false,
  171. isEmpty: false,
  172. retryTimeout: undefined,
  173. retryAttempts: 0
  174. }
  175. },
  176. mounted() {
  177. this.init();
  178. },
  179. destroyed() {
  180. clearTimeout(this.retryTimeout);
  181. },
  182. methods: {
  183. init() {
  184. if(this.retryAttempts == 3) {
  185. this.hasLoaded = true;
  186. this.isEmpty = true;
  187. clearTimeout(this.retryTimeout);
  188. return;
  189. }
  190. axios.get('/api/pixelfed/v1/notifications', {
  191. params: {
  192. limit: 9,
  193. }
  194. })
  195. .then(res => {
  196. if(!res || !res.data || !res.data.length) {
  197. this.retryAttempts = this.retryAttempts + 1;
  198. this.retryTimeout = setTimeout(() => this.init(), this.retryAttempts * 1500);
  199. return;
  200. }
  201. let data = res.data.filter(n => {
  202. if(n.type == 'share' && !n.status) {
  203. return false;
  204. }
  205. if(n.type == 'comment' && !n.status) {
  206. return false;
  207. }
  208. if(n.type == 'mention' && !n.status) {
  209. return false;
  210. }
  211. if(n.type == 'favourite' && !n.status) {
  212. return false;
  213. }
  214. if(n.type == 'follow' && !n.account) {
  215. return false;
  216. }
  217. if(n.type == 'modlog' && !n.modlog) {
  218. return false;
  219. }
  220. return true;
  221. });
  222. if(!res.data.length) {
  223. this.canLoadMore = false;
  224. } else {
  225. this.canLoadMore = true;
  226. }
  227. if(this.retryTimeout || this.retryAttempts) {
  228. this.retryAttempts = 0;
  229. clearTimeout(this.retryTimeout);
  230. }
  231. this.maxId = res.data[res.data.length - 1].id;
  232. this.feed = data;
  233. this.hasLoaded = true;
  234. setTimeout(() => {
  235. this.isRefreshing = false;
  236. }, 15000);
  237. });
  238. },
  239. refreshNotifications() {
  240. event.currentTarget.blur();
  241. this.isRefreshing = true;
  242. this.init();
  243. },
  244. enterIntersect() {
  245. if(this.isIntersecting || !this.canLoadMore) {
  246. return;
  247. }
  248. this.isIntersecting = true;
  249. axios.get('/api/pixelfed/v1/notifications', {
  250. params: {
  251. limit: 9,
  252. max_id: this.maxId
  253. }
  254. })
  255. .then(res => {
  256. if(!res.data || !res.data.length) {
  257. this.canLoadMore = false;
  258. this.isIntersecting = false;
  259. return;
  260. }
  261. let data = res.data.filter(n => {
  262. if(n.type == 'share' && !n.status) {
  263. return false;
  264. }
  265. if(n.type == 'comment' && !n.status) {
  266. return false;
  267. }
  268. if(n.type == 'mention' && !n.status) {
  269. return false;
  270. }
  271. if(n.type == 'favourite' && !n.status) {
  272. return false;
  273. }
  274. if(n.type == 'follow' && !n.account) {
  275. return false;
  276. }
  277. if(n.type == 'modlog' && !n.modlog) {
  278. return false;
  279. }
  280. return true;
  281. });
  282. if(!res.data.length) {
  283. this.canLoadMore = false;
  284. return;
  285. }
  286. this.maxId = res.data[res.data.length - 1].id;
  287. this.feed.push(...data);
  288. this.$nextTick(() => {
  289. this.isIntersecting = false;
  290. })
  291. });
  292. },
  293. truncate(text) {
  294. if(text.length <= 15) {
  295. return text;
  296. }
  297. return text.slice(0,15) + '...'
  298. },
  299. timeAgo(ts) {
  300. return window.App.util.format.timeAgo(ts);
  301. },
  302. mentionUrl(status) {
  303. let username = status.account.username;
  304. let id = status.id;
  305. return '/p/' + username + '/' + id;
  306. },
  307. redirect(url) {
  308. window.location.href = url;
  309. },
  310. notificationPreview(n) {
  311. if(!n.status || !n.status.hasOwnProperty('media_attachments') || !n.status.media_attachments.length) {
  312. return '/storage/no-preview.png';
  313. }
  314. return n.status.media_attachments[0].preview_url;
  315. },
  316. getProfileUrl(account) {
  317. return '/i/web/profile/' + account.id;
  318. },
  319. getPostUrl(status) {
  320. if(!status) {
  321. return;
  322. }
  323. return '/i/web/post/' + status.id;
  324. },
  325. goToPost(status) {
  326. this.$router.push({
  327. name: 'post',
  328. path: `/i/web/post/${status.id}`,
  329. params: {
  330. id: status.id,
  331. cachedStatus: status,
  332. cachedProfile: this.profile
  333. }
  334. })
  335. },
  336. goToProfile(account) {
  337. this.$router.push({
  338. name: 'profile',
  339. path: `/i/web/profile/${account.id}`,
  340. params: {
  341. id: account.id,
  342. cachedProfile: account,
  343. cachedUser: this.profile
  344. }
  345. })
  346. },
  347. }
  348. }
  349. </script>
  350. <style lang="scss">
  351. .notifications-component {
  352. &-feed {
  353. min-height: 50px;
  354. max-height: 300px;
  355. overflow-y: auto;
  356. -ms-overflow-style: none;
  357. scrollbar-width: none;
  358. overflow-y: scroll;
  359. &::-webkit-scrollbar {
  360. display: none;
  361. }
  362. }
  363. .card {
  364. width: 100%;
  365. position: relative;
  366. }
  367. .card-body {
  368. width: 100%;
  369. }
  370. }
  371. </style>