Timeline.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. <template>
  2. <div class="container" style="">
  3. <div class="row">
  4. <div class="col-md-8 col-lg-8 pt-2 px-0 my-3 timeline">
  5. <div class="loader text-center">
  6. <div class="lds-ring"><div></div><div></div><div></div><div></div></div>
  7. </div>
  8. <div class="card mb-4 status-card card-md-rounded-0" :data-status-id="status.id" v-for="(status, index) in feed" :key="status.id">
  9. <div class="card-header d-inline-flex align-items-center bg-white">
  10. <img v-bind:src="status.account.avatar" width="32px" height="32px" style="border-radius: 32px;">
  11. <a class="username font-weight-bold pl-2 text-dark" v-bind:href="status.account.url">
  12. {{status.account.username}}
  13. </a>
  14. <div class="text-right" style="flex-grow:1;">
  15. <div class="dropdown">
  16. <button class="btn btn-link text-dark no-caret dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" title="Post options">
  17. <span class="fas fa-ellipsis-v fa-lg text-muted"></span>
  18. </button>
  19. <div class="dropdown-menu dropdown-menu-right" aria-labelledby="dropdownMenuButton">
  20. <a class="dropdown-item font-weight-bold" :href="status.url">Go to post</a>
  21. <span v-bind:class="[statusOwner(status) ? 'd-none' : '']">
  22. <a class="dropdown-item font-weight-bold" :href="reportUrl(status)">Report</a>
  23. <a class="dropdown-item font-weight-bold" v-on:click="muteProfile(status)">Mute Profile</a>
  24. <a class="dropdown-item font-weight-bold" v-on:click="blockProfile(status)">Block Profile</a>
  25. </span>
  26. <span v-bind:class="[statusOwner(status) ? '' : 'd-none']">
  27. <a class="dropdown-item font-weight-bold" :href="editUrl(status)">Edit</a>
  28. <a class="dropdown-item font-weight-bold text-danger" v-on:click="deletePost(status)">Delete</a>
  29. </span>
  30. </div>
  31. </div>
  32. </div>
  33. </div>
  34. <div class="postPresenterContainer">
  35. <div v-if="status.pf_type === 'photo'" class="w-100">
  36. <photo-presenter :status="status"></photo-presenter>
  37. </div>
  38. <div v-else-if="status.pf_type === 'video'" class="w-100">
  39. <video-presenter :status="status"></video-presenter>
  40. </div>
  41. <div v-else-if="status.pf_type === 'photo:album'" class="w-100">
  42. <photo-album-presenter :status="status"></photo-album-presenter>
  43. </div>
  44. <div v-else-if="status.pf_type === 'video:album'" class="w-100">
  45. <video-album-presenter :status="status"></video-album-presenter>
  46. </div>
  47. <div v-else-if="status.pf_type === 'photo:video:album'" class="w-100">
  48. <mixed-album-presenter :status="status"></mixed-album-presenter>
  49. </div>
  50. <div v-else class="w-100">
  51. <p class="text-center p-0 font-weight-bold text-white">Error: Problem rendering preview.</p>
  52. </div>
  53. </div>
  54. <div class="card-body">
  55. <div class="reactions my-1">
  56. <h3 v-bind:class="[status.favourited ? 'fas fa-heart text-danger pr-3 m-0 cursor-pointer' : 'far fa-heart pr-3 m-0 like-btn cursor-pointer']" title="Like" v-on:click="likeStatus(status, $event)"></h3>
  57. <h3 class="far fa-comment pr-3 m-0 cursor-pointer" title="Comment" v-on:click="commentFocus(status, $event)"></h3>
  58. <h3 v-bind:class="[status.reblogged ? 'far fa-share-square pr-3 m-0 text-primary cursor-pointer' : 'far fa-share-square pr-3 m-0 share-btn cursor-pointer']" title="Share" v-on:click="shareStatus(status, $event)"></h3>
  59. </div>
  60. <div class="likes font-weight-bold">
  61. <span class="like-count">{{status.favourites_count}}</span> {{status.favourites_count == 1 ? 'like' : 'likes'}}
  62. </div>
  63. <div class="caption">
  64. <p class="mb-2 read-more" style="overflow: hidden;">
  65. <span class="username font-weight-bold">
  66. <bdi><a class="text-dark" :href="status.account.url">{{status.account.username}}</a></bdi>
  67. </span>
  68. <span v-html="status.content"></span>
  69. </p>
  70. </div>
  71. <div class="comments">
  72. </div>
  73. <div class="timestamp pt-1">
  74. <p class="small text-uppercase mb-0">
  75. <a :href="status.url" class="text-muted">
  76. <timeago :datetime="status.created_at" :auto-update="60" :converter-options="{includeSeconds:true}" :title="timestampFormat(status.created_at)" v-b-tooltip.hover.bottom></timeago>
  77. </a>
  78. </p>
  79. </div>
  80. </div>
  81. <div class="card-footer bg-white d-none">
  82. <form class="" v-on:submit.prevent="commentSubmit(status, $event)">
  83. <input type="hidden" name="item" value="">
  84. <input class="form-control status-reply-input" name="comment" placeholder="Add a comment…" autocomplete="off">
  85. </form>
  86. </div>
  87. </div>
  88. </div>
  89. <div class="col-md-4 col-lg-4 pt-2 my-3">
  90. <div class="mb-4">
  91. <div class="card profile-card">
  92. <div class="card-body loader text-center">
  93. <div class="lds-ring"><div></div><div></div><div></div><div></div></div>
  94. </div>
  95. <div class="card-body contents d-none">
  96. <div class="media d-flex align-items-center">
  97. <a :href="profile.url">
  98. <img class="mr-3 rounded-circle box-shadow" :src="profile.avatar || '/storage/avatars/default.png'" alt="avatar" width="64px" height="64px">
  99. </a>
  100. <div class="media-body">
  101. <p class="mb-0 px-0 font-weight-bold"><a :href="profile.url" class="text-dark">&commat;{{profile.username}}</a></p>
  102. <p class="my-0 text-muted text-truncate pb-0">{{profile.display_name}}</p>
  103. </div>
  104. </div>
  105. </div>
  106. <div class="card-footer bg-white py-1 d-none">
  107. <div class="d-flex justify-content-between text-center">
  108. <span class="pl-3">
  109. <p class="mb-0 font-weight-bold">{{profile.statuses_count}}</p>
  110. <p class="mb-0 small text-muted">Posts</p>
  111. </span>
  112. <span>
  113. <p class="mb-0 font-weight-bold">{{profile.followers_count}}</p>
  114. <p class="mb-0 small text-muted">Followers</p>
  115. </span>
  116. <span class="pr-3">
  117. <p class="mb-0 font-weight-bold">{{profile.following_count}}</p>
  118. <p class="mb-0 small text-muted">Following</p>
  119. </span>
  120. </div>
  121. </div>
  122. </div>
  123. </div>
  124. <div class="mb-4">
  125. <div class="card notification-card">
  126. <div class="card-header bg-white">
  127. <p class="mb-0 d-flex align-items-center justify-content-between">
  128. <span class="text-muted font-weight-bold">Notifications</span>
  129. <a class="text-dark small" href="/account/activity">See All</a>
  130. </p>
  131. </div>
  132. <div class="card-body loader text-center" style="height: 300px;">
  133. <div class="lds-ring"><div></div><div></div><div></div><div></div></div>
  134. </div>
  135. <div class="card-body pt-2 contents" style="max-height: 300px; overflow-y: scroll;">
  136. <div class="media mb-3 align-items-center" v-for="(n, index) in notifications">
  137. <img class="mr-2 rounded-circle img-thumbnail" :src="n.account.avatar" alt="" width="32px" height="32px">
  138. <div class="media-body font-weight-light small">
  139. <div v-if="n.type == 'favourite'">
  140. <p class="my-0">
  141. <span class="font-weight-bold">{{n.account.username}}</span> liked your post.
  142. </p>
  143. </div>
  144. <div v-else-if="n.type == 'comment'">
  145. <p class="my-0">
  146. <span class="font-weight-bold">{{n.account.username}}</span> commented on your post.
  147. </p>
  148. </div>
  149. <div v-else-if="n.type == 'mention'">
  150. <p class="my-0">
  151. <span class="font-weight-bold">{{n.account.username}}</span> mentioned you.
  152. </p>
  153. </div>
  154. <div v-else-if="n.type == 'follow'">
  155. <p class="my-0">
  156. <span class="font-weight-bold">{{n.account.username}}</span> followed you.
  157. </p>
  158. </div>
  159. </div>
  160. </div>
  161. </div>
  162. </div>
  163. </div>
  164. <footer>
  165. <div class="container pb-5">
  166. <p class="mb-0 text-uppercase font-weight-bold text-muted small">
  167. <a href="/site/about" class="text-dark pr-2">About Us</a>
  168. <a href="/site/help" class="text-dark pr-2">Support</a>
  169. <a href="/site/open-source" class="text-dark pr-2">Open Source</a>
  170. <a href="/site/language" class="text-dark pr-2">Language</a>
  171. <a href="/site/terms" class="text-dark pr-2">Terms</a>
  172. <a href="/site/privacy" class="text-dark pr-2">Privacy</a>
  173. <a href="/site/platform" class="text-dark pr-2">API</a>
  174. </p>
  175. <p class="mb-0 text-uppercase font-weight-bold text-muted small">
  176. <a href="http://pixelfed.org" class="text-muted" rel="noopener" title="" data-toggle="tooltip">Powered by PixelFed</a>
  177. </p>
  178. </div>
  179. </footer>
  180. </div>
  181. </div>
  182. </div>
  183. </template>
  184. <style type="text/css">
  185. .postPresenterContainer {
  186. display: flex;
  187. align-items: center;
  188. background: #fff;
  189. }
  190. .cursor-pointer {
  191. cursor: pointer;
  192. }
  193. </style>
  194. <script type="text/javascript">
  195. export default {
  196. data() {
  197. return {
  198. page: 1,
  199. feed: [],
  200. profile: {},
  201. scope: window.location.pathname,
  202. min_id: 0,
  203. max_id: 0,
  204. notifications: {},
  205. stories: {},
  206. suggestions: {},
  207. }
  208. },
  209. beforeMount() {
  210. this.fetchTimelineApi();
  211. this.fetchProfile();
  212. },
  213. mounted() {
  214. },
  215. updated() {
  216. this.scroll();
  217. },
  218. methods: {
  219. fetchProfile() {
  220. axios.get('/api/v1/accounts/verify_credentials').then(res => {
  221. this.profile = res.data;
  222. $('.profile-card .loader').addClass('d-none');
  223. $('.profile-card .contents').removeClass('d-none');
  224. $('.profile-card .card-footer').removeClass('d-none');
  225. this.fetchNotifications();
  226. }).catch(err => {
  227. swal(
  228. 'Oops, something went wrong',
  229. 'Please reload the page.',
  230. 'error'
  231. );
  232. });
  233. },
  234. fetchTimelineApi() {
  235. let homeTimeline = '/api/v1/timelines/home?page=' + this.page;
  236. let localTimeline = '/api/v1/timelines/public?page=' + this.page;
  237. let apiUrl = this.scope == '/' ? homeTimeline : localTimeline;
  238. axios.get(apiUrl).then(res => {
  239. $('.timeline .loader').addClass('d-none');
  240. let data = res.data;
  241. this.feed.push(...data);
  242. let ids = data.map(status => status.id);
  243. this.min_id = Math.min(...ids);
  244. if(this.page == 1) {
  245. this.max_id = Math.max(...ids);
  246. }
  247. this.page++;
  248. }).catch(err => {
  249. });
  250. },
  251. fetchNotifications() {
  252. axios.get('/api/v1/notifications')
  253. .then(res => {
  254. this.notifications = res.data;
  255. $('.notification-card .loader').addClass('d-none');
  256. $('.notification-card .contents').removeClass('d-none');
  257. });
  258. },
  259. scroll() {
  260. window.onscroll = () => {
  261. let bottomOfWindow = document.documentElement.scrollTop + window.innerHeight == document.documentElement.offsetHeight;
  262. if (bottomOfWindow) {
  263. this.fetchTimelineApi();
  264. }
  265. };
  266. },
  267. reportUrl(status) {
  268. let type = status.in_reply_to ? 'comment' : 'post';
  269. let id = status.id;
  270. return '/i/report?type=' + type + '&id=' + id;
  271. },
  272. commentFocus(status, $event) {
  273. let el = event.target;
  274. let card = el.parentElement.parentElement.parentElement;
  275. let comments = card.getElementsByClassName('comments')[0];
  276. if(comments.children.length == 0) {
  277. comments.classList.add('mb-2');
  278. this.fetchStatusComments(status, card);
  279. }
  280. let footer = card.querySelectorAll('.card-footer')[0];
  281. let input = card.querySelectorAll('.status-reply-input')[0];
  282. if(footer.classList.contains('d-none') == true) {
  283. footer.classList.remove('d-none');
  284. input.focus();
  285. } else {
  286. footer.classList.add('d-none');
  287. input.blur();
  288. }
  289. },
  290. likeStatus(status, $event) {
  291. if($('body').hasClass('loggedIn') == false) {
  292. return;
  293. }
  294. axios.post('/i/like', {
  295. item: status.id
  296. }).then(res => {
  297. status.favourites_count = res.data.count;
  298. if(status.favourited == true) {
  299. status.favourited = false;
  300. } else {
  301. status.favourited = true;
  302. }
  303. }).catch(err => {
  304. swal('Error', 'Something went wrong, please try again later.', 'error');
  305. });
  306. },
  307. shareStatus(status, $event) {
  308. if($('body').hasClass('loggedIn') == false) {
  309. return;
  310. }
  311. axios.post('/i/share', {
  312. item: status.id
  313. }).then(res => {
  314. status.reblogs_count = res.data.count;
  315. if(status.reblogged == true) {
  316. status.reblogged = false;
  317. } else {
  318. status.reblogged = true;
  319. }
  320. }).catch(err => {
  321. swal('Error', 'Something went wrong, please try again later.', 'error');
  322. });
  323. },
  324. timestampFormat(timestamp) {
  325. let ts = new Date(timestamp);
  326. return ts.toDateString() + ' ' + ts.toLocaleTimeString();
  327. },
  328. editUrl(status) {
  329. return status.url + '/edit';
  330. },
  331. statusOwner(status) {
  332. let sid = status.account.id;
  333. let uid = this.profile.id;
  334. if(sid == uid) {
  335. return true;
  336. } else {
  337. return false;
  338. }
  339. },
  340. fetchStatusComments(status, card) {
  341. axios.get('/api/v2/status/'+status.id+'/replies')
  342. .then(res => {
  343. let comments = card.querySelectorAll('.comments')[0];
  344. let data = res.data;
  345. data.forEach(function(i, k) {
  346. let username = document.createElement('a');
  347. username.classList.add('font-weight-bold');
  348. username.classList.add('text-dark');
  349. username.classList.add('mr-2');
  350. username.setAttribute('href', i.account.url);
  351. username.textContent = i.account.username;
  352. let text = document.createElement('span');
  353. text.innerHTML = i.content;
  354. let comment = document.createElement('p');
  355. comment.classList.add('read-more');
  356. comment.classList.add('mb-0');
  357. comment.appendChild(username);
  358. comment.appendChild(text);
  359. comments.appendChild(comment);
  360. });
  361. }).catch(err => {
  362. })
  363. },
  364. muteProfile(status) {
  365. if($('body').hasClass('loggedIn') == false) {
  366. return;
  367. }
  368. axios.post('/i/mute', {
  369. type: 'user',
  370. item: status.account.id
  371. }).then(res => {
  372. this.feed = this.feed.filter(s => s.account.id !== status.account.id);
  373. swal('Success', 'You have successfully muted ' + status.account.acct, 'success');
  374. }).catch(err => {
  375. swal('Error', 'Something went wrong. Please try again later.', 'error');
  376. });
  377. },
  378. blockProfile(status) {
  379. if($('body').hasClass('loggedIn') == false) {
  380. return;
  381. }
  382. axios.post('/i/block', {
  383. type: 'user',
  384. item: status.account.id
  385. }).then(res => {
  386. this.feed = this.feed.filter(s => s.account.id !== status.account.id);
  387. swal('Success', 'You have successfully blocked ' + status.account.acct, 'success');
  388. }).catch(err => {
  389. swal('Error', 'Something went wrong. Please try again later.', 'error');
  390. });
  391. },
  392. deletePost(status, index) {
  393. if($('body').hasClass('loggedIn') == false || status.account.id !== this.profile.id) {
  394. return;
  395. }
  396. axios.post('/i/delete', {
  397. type: 'status',
  398. item: status.id
  399. }).then(res => {
  400. this.feed.splice(index,1);
  401. swal('Success', 'You have successfully deleted this post', 'success');
  402. }).catch(err => {
  403. swal('Error', 'Something went wrong. Please try again later.', 'error');
  404. });
  405. },
  406. commentSubmit(status, $event) {
  407. let id = status.id;
  408. let form = $event.target;
  409. let input = $(form).find('input[name="comment"]');
  410. let comment = input.val();
  411. let comments = form.parentElement.parentElement.getElementsByClassName('comments')[0];
  412. axios.post('/i/comment', {
  413. item: id,
  414. comment: comment
  415. }).then(res => {
  416. input.val('');
  417. input.blur();
  418. let username = document.createElement('a');
  419. username.classList.add('font-weight-bold');
  420. username.classList.add('text-dark');
  421. username.classList.add('mr-2');
  422. username.setAttribute('href', this.profile.url);
  423. username.textContent = this.profile.username;
  424. let text = document.createElement('span');
  425. text.innerHTML = comment;
  426. let wrapper = document.createElement('p');
  427. wrapper.classList.add('read-more');
  428. wrapper.classList.add('mb-0');
  429. wrapper.appendChild(username);
  430. wrapper.appendChild(text);
  431. comments.insertBefore(wrapper, comments.firstChild);
  432. });
  433. }
  434. }
  435. }
  436. </script>