Timeline.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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 word-break">{{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 word-break">{{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 word-break">{{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 word-break">{{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. .word-break {
  200. word-break: break-all;
  201. }
  202. </style>
  203. <script type="text/javascript">
  204. export default {
  205. data() {
  206. return {
  207. page: 1,
  208. feed: [],
  209. profile: {},
  210. scope: window.location.pathname,
  211. min_id: 0,
  212. max_id: 0,
  213. notifications: {},
  214. stories: {},
  215. suggestions: {},
  216. }
  217. },
  218. beforeMount() {
  219. this.fetchTimelineApi();
  220. this.fetchProfile();
  221. },
  222. mounted() {
  223. },
  224. updated() {
  225. this.scroll();
  226. },
  227. methods: {
  228. fetchProfile() {
  229. axios.get('/api/v1/accounts/verify_credentials').then(res => {
  230. this.profile = res.data;
  231. $('.profile-card .loader').addClass('d-none');
  232. $('.profile-card .contents').removeClass('d-none');
  233. $('.profile-card .card-footer').removeClass('d-none');
  234. this.fetchNotifications();
  235. }).catch(err => {
  236. swal(
  237. 'Oops, something went wrong',
  238. 'Please reload the page.',
  239. 'error'
  240. );
  241. });
  242. },
  243. fetchTimelineApi() {
  244. let homeTimeline = '/api/v1/timelines/home?page=' + this.page;
  245. let localTimeline = '/api/v1/timelines/public?page=' + this.page;
  246. let apiUrl = this.scope == '/' ? homeTimeline : localTimeline;
  247. axios.get(apiUrl).then(res => {
  248. $('.timeline .loader').addClass('d-none');
  249. let data = res.data;
  250. this.feed.push(...data);
  251. let ids = data.map(status => status.id);
  252. this.min_id = Math.min(...ids);
  253. if(this.page == 1) {
  254. this.max_id = Math.max(...ids);
  255. }
  256. this.page++;
  257. }).catch(err => {
  258. });
  259. },
  260. fetchNotifications() {
  261. axios.get('/api/v1/notifications')
  262. .then(res => {
  263. this.notifications = res.data;
  264. $('.notification-card .loader').addClass('d-none');
  265. $('.notification-card .contents').removeClass('d-none');
  266. });
  267. },
  268. scroll() {
  269. window.onscroll = () => {
  270. let bottomOfWindow = document.documentElement.scrollTop + window.innerHeight == document.documentElement.offsetHeight;
  271. if (bottomOfWindow) {
  272. this.fetchTimelineApi();
  273. }
  274. };
  275. },
  276. reportUrl(status) {
  277. let type = status.in_reply_to ? 'comment' : 'post';
  278. let id = status.id;
  279. return '/i/report?type=' + type + '&id=' + id;
  280. },
  281. commentFocus(status, $event) {
  282. let el = event.target;
  283. let card = el.parentElement.parentElement.parentElement;
  284. let comments = card.getElementsByClassName('comments')[0];
  285. if(comments.children.length == 0) {
  286. comments.classList.add('mb-2');
  287. this.fetchStatusComments(status, card);
  288. }
  289. let footer = card.querySelectorAll('.card-footer')[0];
  290. let input = card.querySelectorAll('.status-reply-input')[0];
  291. if(footer.classList.contains('d-none') == true) {
  292. footer.classList.remove('d-none');
  293. input.focus();
  294. } else {
  295. footer.classList.add('d-none');
  296. input.blur();
  297. }
  298. },
  299. likeStatus(status, $event) {
  300. if($('body').hasClass('loggedIn') == false) {
  301. return;
  302. }
  303. axios.post('/i/like', {
  304. item: status.id
  305. }).then(res => {
  306. status.favourites_count = res.data.count;
  307. if(status.favourited == true) {
  308. status.favourited = false;
  309. } else {
  310. status.favourited = true;
  311. }
  312. }).catch(err => {
  313. swal('Error', 'Something went wrong, please try again later.', 'error');
  314. });
  315. },
  316. shareStatus(status, $event) {
  317. if($('body').hasClass('loggedIn') == false) {
  318. return;
  319. }
  320. axios.post('/i/share', {
  321. item: status.id
  322. }).then(res => {
  323. status.reblogs_count = res.data.count;
  324. if(status.reblogged == true) {
  325. status.reblogged = false;
  326. } else {
  327. status.reblogged = true;
  328. }
  329. }).catch(err => {
  330. swal('Error', 'Something went wrong, please try again later.', 'error');
  331. });
  332. },
  333. timestampFormat(timestamp) {
  334. let ts = new Date(timestamp);
  335. return ts.toDateString() + ' ' + ts.toLocaleTimeString();
  336. },
  337. editUrl(status) {
  338. return status.url + '/edit';
  339. },
  340. redirect(url) {
  341. window.location.href = url;
  342. return;
  343. },
  344. replyUrl(status) {
  345. let username = this.profile.username;
  346. let id = status.account.id == this.profile.id ? status.id : status.in_reply_to_id;
  347. return '/p/' + username + '/' + id;
  348. },
  349. mentionUrl(status) {
  350. let username = status.account.username;
  351. let id = status.id;
  352. return '/p/' + username + '/' + id;
  353. },
  354. statusOwner(status) {
  355. let sid = status.account.id;
  356. let uid = this.profile.id;
  357. if(sid == uid) {
  358. return true;
  359. } else {
  360. return false;
  361. }
  362. },
  363. fetchStatusComments(status, card) {
  364. axios.get('/api/v2/status/'+status.id+'/replies')
  365. .then(res => {
  366. let comments = card.querySelectorAll('.comments')[0];
  367. let data = res.data;
  368. data.forEach(function(i, k) {
  369. let username = document.createElement('a');
  370. username.classList.add('font-weight-bold');
  371. username.classList.add('text-dark');
  372. username.classList.add('mr-2');
  373. username.setAttribute('href', i.account.url);
  374. username.textContent = i.account.username;
  375. let text = document.createElement('span');
  376. text.innerHTML = i.content;
  377. let comment = document.createElement('p');
  378. comment.classList.add('read-more');
  379. comment.classList.add('mb-0');
  380. comment.appendChild(username);
  381. comment.appendChild(text);
  382. comments.appendChild(comment);
  383. });
  384. }).catch(err => {
  385. })
  386. },
  387. muteProfile(status) {
  388. if($('body').hasClass('loggedIn') == false) {
  389. return;
  390. }
  391. axios.post('/i/mute', {
  392. type: 'user',
  393. item: status.account.id
  394. }).then(res => {
  395. this.feed = this.feed.filter(s => s.account.id !== status.account.id);
  396. swal('Success', 'You have successfully muted ' + status.account.acct, 'success');
  397. }).catch(err => {
  398. swal('Error', 'Something went wrong. Please try again later.', 'error');
  399. });
  400. },
  401. blockProfile(status) {
  402. if($('body').hasClass('loggedIn') == false) {
  403. return;
  404. }
  405. axios.post('/i/block', {
  406. type: 'user',
  407. item: status.account.id
  408. }).then(res => {
  409. this.feed = this.feed.filter(s => s.account.id !== status.account.id);
  410. swal('Success', 'You have successfully blocked ' + status.account.acct, 'success');
  411. }).catch(err => {
  412. swal('Error', 'Something went wrong. Please try again later.', 'error');
  413. });
  414. },
  415. deletePost(status, index) {
  416. if($('body').hasClass('loggedIn') == false || status.account.id !== this.profile.id) {
  417. return;
  418. }
  419. axios.post('/i/delete', {
  420. type: 'status',
  421. item: status.id
  422. }).then(res => {
  423. this.feed.splice(index,1);
  424. swal('Success', 'You have successfully deleted this post', 'success');
  425. }).catch(err => {
  426. swal('Error', 'Something went wrong. Please try again later.', 'error');
  427. });
  428. },
  429. commentSubmit(status, $event) {
  430. let id = status.id;
  431. let form = $event.target;
  432. let input = $(form).find('input[name="comment"]');
  433. let comment = input.val();
  434. let comments = form.parentElement.parentElement.getElementsByClassName('comments')[0];
  435. axios.post('/i/comment', {
  436. item: id,
  437. comment: comment
  438. }).then(res => {
  439. input.val('');
  440. input.blur();
  441. let username = document.createElement('a');
  442. username.classList.add('font-weight-bold');
  443. username.classList.add('text-dark');
  444. username.classList.add('mr-2');
  445. username.setAttribute('href', this.profile.url);
  446. username.textContent = this.profile.username;
  447. let text = document.createElement('span');
  448. text.innerHTML = comment;
  449. let wrapper = document.createElement('p');
  450. wrapper.classList.add('read-more');
  451. wrapper.classList.add('mb-0');
  452. wrapper.appendChild(username);
  453. wrapper.appendChild(text);
  454. comments.insertBefore(wrapper, comments.firstChild);
  455. });
  456. }
  457. }
  458. }
  459. </script>