Timeline.vue 17 KB

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