Timeline.vue 18 KB

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