Notifications.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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">{{ $t("notifications.title")}}</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. v-if="n.type === 'autospam.warning'"
  38. class="mr-2 rounded-circle shadow-sm p-1"
  39. style="border: 2px solid var(--danger)"
  40. src="/img/pixelfed-icon-color.svg"
  41. width="32"
  42. height="32"
  43. />
  44. <img
  45. v-else
  46. class="mr-2 rounded-circle shadow-sm"
  47. :src="n.account.avatar"
  48. width="32"
  49. height="32"
  50. onerror="this.onerror=null;this.src='/storage/avatars/default.png';">
  51. <div class="media-body font-weight-light small">
  52. <div v-if="n.type == 'favourite'">
  53. <p class="my-0">
  54. <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> {{ $t("notifications.liked")}}
  55. <span v-if="n.status && n.status.hasOwnProperty('media_attachments')">
  56. <a class="font-weight-bold" v-bind:href="getPostUrl(n.status)" :id="'fvn-' + n.id" @click.prevent="goToPost(n.status)">{{ $t("notifications.post")}}</a>.
  57. <b-popover :target="'fvn-' + n.id" title="" triggers="hover" placement="top" boundary="window">
  58. <img :src="notificationPreview(n)" width="100px" height="100px" style="object-fit: cover;">
  59. </b-popover>
  60. </span>
  61. <span v-else>
  62. <a class="font-weight-bold" :href="getPostUrl(n.status)" @click.prevent="goToPost(n.status)">{{ $t("notifications.post")}}</a>.
  63. </span>
  64. </p>
  65. </div>
  66. <div v-else-if="n.type == 'autospam.warning'">
  67. <p class="my-0">
  68. {{ $t("notifications.youRecent")}} <a :href="getPostUrl(n.status)" @click.prevent="goToPost(n.status)" class="font-weight-bold">{{ $t("notifications.post")}}</a> {{ $t("notifications.hasUnlisted")}}.
  69. </p>
  70. <p class="mt-n1 mb-0">
  71. <span class="small text-muted"><a href="#" class="font-weight-bold" @click.prevent="showAutospamInfo(n.status)">Click here</a> for more info.</span>
  72. </p>
  73. </div>
  74. <div v-else-if="n.type == 'comment'">
  75. <p class="my-0">
  76. <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> {{ $t("notifications.commented")}} <a class="font-weight-bold" :href="getPostUrl(n.status)" @click.prevent="goToPost(n.status)">{{ $t("notifications.post")}}</a>.
  77. </p>
  78. </div>
  79. <div v-else-if="n.type == 'group:comment'">
  80. <p class="my-0">
  81. <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> {{ $t("notifications.commented")}} <a class="font-weight-bold" :href="n.group_post_url">{{ $t("notifications.groupPost") }}</a>.
  82. </p>
  83. </div>
  84. <div v-else-if="n.type == 'story:react'">
  85. <p class="my-0">
  86. <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> {{ $t("notifications.reacted")}} <a class="font-weight-bold" v-bind:href="'/i/web/direct/thread/'+n.account.id">{{ $t("notifications.story")}}</a>.
  87. </p>
  88. </div>
  89. <div v-else-if="n.type == 'story:comment'">
  90. <p class="my-0">
  91. <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> {{ $t("notifications.commented")}} <a class="font-weight-bold" v-bind:href="'/i/web/direct/thread/'+n.account.id">{{ $t("notifications.story")}}</a>.
  92. </p>
  93. </div>
  94. <div v-else-if="n.type == 'mention'">
  95. <p class="my-0">
  96. <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)">{{ $t("notifications.mentioned")}}</a> {{ $t("notifications.you")}}.
  97. </p>
  98. </div>
  99. <div v-else-if="n.type == 'follow'">
  100. <p class="my-0">
  101. <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> {{ $t("notifications.followed")}} {{ $t("notifications.you")}}.
  102. </p>
  103. </div>
  104. <div v-else-if="n.type == 'share'">
  105. <p class="my-0">
  106. <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> {{ $t("notifications.shared")}}
  107. <span v-if="n.status && n.status.hasOwnProperty('media_attachments')">
  108. <a class="font-weight-bold" v-bind:href="getPostUrl(n.status)" :id="'fvn-' + n.id" @click.prevent="goToPost(n.status)">{{ $t("notifications.post")}}</a>.
  109. <b-popover :target="'fvn-' + n.id" title="" triggers="hover" placement="top" boundary="window">
  110. <img :src="notificationPreview(n)" width="100px" height="100px" style="object-fit: cover;">
  111. </b-popover>
  112. </span>
  113. </p>
  114. </div>
  115. <div v-else-if="n.type == 'modlog'">
  116. <p class="my-0">
  117. <a :href="getProfileUrl(n.account)" class="font-weight-bold text-dark word-break" :title="n.account.acct">{{truncate(n.account.username)}}</a> {{ $t("notifications.updatedA")}} <a class="font-weight-bold" v-bind:href="n.modlog.url">modlog</a>.
  118. </p>
  119. </div>
  120. <div v-else-if="n.type == 'tagged'">
  121. <p class="my-0">
  122. <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> {{ $t("notifications.tagged")}} <a class="font-weight-bold" v-bind:href="n.tagged.post_url">{{ $t("notifications.post")}}</a>.
  123. </p>
  124. </div>
  125. <div v-else-if="n.type == 'direct'">
  126. <p class="my-0">
  127. <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> {{ $t("notifications.sentA")}} <router-link class="font-weight-bold" :to="'/i/web/direct/thread/'+n.account.id">dm</router-link>.
  128. </p>
  129. </div>
  130. <div v-else-if="n.type == 'group.join.approved'">
  131. <p class="my-0">
  132. {{ $t("notifications.yourApplication")}} <a :href="n.group.url" class="font-weight-bold text-dark word-break" :title="n.group.name">{{truncate(n.group.name)}}</a> {{ $t("notifications.wasApproved")}}
  133. </p>
  134. </div>
  135. <div v-else-if="n.type == 'group.join.rejected'">
  136. <p class="my-0">
  137. {{ $t("notifications.yourApplication")}} <a :href="n.group.url" class="font-weight-bold text-dark word-break" :title="n.group.name">{{truncate(n.group.name)}}</a> {{ $t("notifications.wasRejected")}}
  138. </p>
  139. </div>
  140. <div v-else-if="n.type == 'group:invite'">
  141. <p class="my-0">
  142. <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>.
  143. </p>
  144. </div>
  145. <div v-else>
  146. <p class="my-0">
  147. {{ $t("notifications.cannotDisplay")}}
  148. </p>
  149. </div>
  150. </div>
  151. <div class="small text-muted font-weight-bold" style="font-size: 12px;" :title="n.created_at">{{timeAgo(n.created_at)}}</div>
  152. </div>
  153. </div>
  154. <div v-if="hasLoaded && feed.length == 0">
  155. <p class="small font-weight-bold text-center mb-0">{{ $t('notifications.noneFound') }}</p>
  156. </div>
  157. <div v-else>
  158. <intersect v-if="hasLoaded && canLoadMore" @enter="enterIntersect">
  159. <placeholder small style="margin-top: -6px" />
  160. <placeholder small/>
  161. <placeholder small/>
  162. <placeholder small/>
  163. </intersect>
  164. <div v-else class="d-block" style="height: 10px;">
  165. </div>
  166. </div>
  167. </template>
  168. </div>
  169. </div>
  170. </div>
  171. </div>
  172. </template>
  173. <script type="text/javascript">
  174. import Placeholder from './../partials/placeholders/NotificationPlaceholder.vue';
  175. import Intersect from 'vue-intersect';
  176. export default {
  177. props: {
  178. profile: {
  179. type: Object
  180. }
  181. },
  182. components: {
  183. "intersect": Intersect,
  184. "placeholder": Placeholder
  185. },
  186. data() {
  187. return {
  188. feed: {},
  189. maxId: undefined,
  190. isIntersecting: false,
  191. canLoadMore: false,
  192. isRefreshing: false,
  193. hasLoaded: false,
  194. isEmpty: false,
  195. retryTimeout: undefined,
  196. retryAttempts: 0
  197. }
  198. },
  199. mounted() {
  200. this.init();
  201. },
  202. destroyed() {
  203. clearTimeout(this.retryTimeout);
  204. },
  205. methods: {
  206. init() {
  207. if(this.retryAttempts == 1) {
  208. this.hasLoaded = true;
  209. this.isEmpty = true;
  210. clearTimeout(this.retryTimeout);
  211. return;
  212. }
  213. axios.get('/api/pixelfed/v1/notifications', {
  214. params: {
  215. limit: 9,
  216. }
  217. })
  218. .then(res => {
  219. if(!res || !res.data || !res.data.length) {
  220. this.retryAttempts = this.retryAttempts + 1;
  221. this.retryTimeout = setTimeout(() => this.init(), this.retryAttempts * 1500);
  222. return;
  223. }
  224. let data = res.data.filter(n => {
  225. if(n.type == 'share' && (!n.status || !n.account)) {
  226. return false;
  227. }
  228. if(n.type == 'comment' && (!n.status || !n.account)) {
  229. return false;
  230. }
  231. if(n.type == 'mention' && (!n.status || !n.account)) {
  232. return false;
  233. }
  234. if(n.type == 'favourite' && (!n.status || !n.account)) {
  235. return false;
  236. }
  237. if(n.type == 'follow' && !n.account) {
  238. return false;
  239. }
  240. if(n.type == 'modlog' && !n.modlog) {
  241. return false;
  242. }
  243. return true;
  244. });
  245. if(!res.data.length) {
  246. this.canLoadMore = false;
  247. } else {
  248. this.canLoadMore = true;
  249. }
  250. if(this.retryTimeout || this.retryAttempts) {
  251. this.retryAttempts = 0;
  252. clearTimeout(this.retryTimeout);
  253. }
  254. this.maxId = res.data[res.data.length - 1].id;
  255. this.feed = data;
  256. this.hasLoaded = true;
  257. setTimeout(() => {
  258. this.isRefreshing = false;
  259. }, 15000);
  260. });
  261. },
  262. refreshNotifications() {
  263. event.currentTarget.blur();
  264. this.isRefreshing = true;
  265. this.init();
  266. },
  267. enterIntersect() {
  268. if(this.isIntersecting || !this.canLoadMore) {
  269. return;
  270. }
  271. this.isIntersecting = true;
  272. axios.get('/api/pixelfed/v1/notifications', {
  273. params: {
  274. limit: 9,
  275. max_id: this.maxId
  276. }
  277. })
  278. .then(res => {
  279. if(!res.data || !res.data.length) {
  280. this.canLoadMore = false;
  281. this.isIntersecting = false;
  282. return;
  283. }
  284. let data = res.data.filter(n => {
  285. if(n.type == 'share' && (!n.status || !n.account)) {
  286. return false;
  287. }
  288. if(n.type == 'comment' && (!n.status || !n.account)) {
  289. return false;
  290. }
  291. if(n.type == 'mention' && (!n.status || !n.account)) {
  292. return false;
  293. }
  294. if(n.type == 'favourite' && (!n.status || !n.account)) {
  295. return false;
  296. }
  297. if(n.type == 'follow' && !n.account) {
  298. return false;
  299. }
  300. if(n.type == 'modlog' && !n.modlog) {
  301. return false;
  302. }
  303. return true;
  304. });
  305. if(!res.data.length) {
  306. this.canLoadMore = false;
  307. return;
  308. }
  309. this.maxId = res.data[res.data.length - 1].id;
  310. this.feed.push(...data);
  311. this.$nextTick(() => {
  312. this.isIntersecting = false;
  313. })
  314. });
  315. },
  316. truncate(text) {
  317. if(text.length <= 15) {
  318. return text;
  319. }
  320. return text.slice(0,15) + '...'
  321. },
  322. timeAgo(ts) {
  323. return window.App.util.format.timeAgo(ts);
  324. },
  325. mentionUrl(status) {
  326. let username = status.account.username;
  327. let id = status.id;
  328. return '/p/' + username + '/' + id;
  329. },
  330. redirect(url) {
  331. window.location.href = url;
  332. },
  333. notificationPreview(n) {
  334. if(!n.status || !n.status.hasOwnProperty('media_attachments') || !n.status.media_attachments.length) {
  335. return '/storage/no-preview.png';
  336. }
  337. return n.status.media_attachments[0].preview_url;
  338. },
  339. getProfileUrl(account) {
  340. return '/i/web/profile/' + account.id;
  341. },
  342. getPostUrl(status) {
  343. if(!status) {
  344. return;
  345. }
  346. return '/i/web/post/' + status.id;
  347. },
  348. goToPost(status) {
  349. this.$router.push({
  350. name: 'post',
  351. path: `/i/web/post/${status.id}`,
  352. params: {
  353. id: status.id,
  354. cachedStatus: status,
  355. cachedProfile: this.profile
  356. }
  357. })
  358. },
  359. goToProfile(account) {
  360. this.$router.push({
  361. name: 'profile',
  362. path: `/i/web/profile/${account.id}`,
  363. params: {
  364. id: account.id,
  365. cachedProfile: account,
  366. cachedUser: this.profile
  367. }
  368. })
  369. },
  370. showAutospamInfo(status) {
  371. let el = document.createElement('p');
  372. el.classList.add('text-left');
  373. el.classList.add('mb-0');
  374. el.innerHTML = '<p class="">We use automated systems to help detect potential abuse and spam. Your recent <a href="/i/web/post/' + status.id + '" class="font-weight-bold">post</a> was flagged for review. <br /> <p class=""><span class="font-weight-bold">Don\'t worry! Your post will be reviewed by a human</span>, and they will restore your post if they determine it appropriate.</p><p style="font-size:12px">Once a human approves your post, any posts you create after will not be marked as unlisted. If you delete this post and share more posts before a human can approve any of them, you will need to wait for at least one unlisted post to be reviewed by a human.';
  375. let wrapper = document.createElement('div');
  376. wrapper.appendChild(el);
  377. swal({
  378. title: 'Why was my post unlisted?',
  379. content: wrapper,
  380. icon: 'warning'
  381. })
  382. }
  383. }
  384. }
  385. </script>
  386. <style lang="scss">
  387. .notifications-component {
  388. &-feed {
  389. min-height: 50px;
  390. max-height: 300px;
  391. overflow-y: auto;
  392. -ms-overflow-style: none;
  393. scrollbar-width: none;
  394. overflow-y: scroll;
  395. &::-webkit-scrollbar {
  396. display: none;
  397. }
  398. }
  399. .card {
  400. width: 100%;
  401. position: relative;
  402. }
  403. .card-body {
  404. width: 100%;
  405. }
  406. }
  407. </style>