Timeline.vue 17 KB

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