From abed6af7f5e1fa54a93e7e03b8ea223ed0801022 Mon Sep 17 00:00:00 2001 From: Louis Chemineau Date: Tue, 21 Mar 2023 18:09:48 +0100 Subject: [PATCH] Show ancestors on single post timeline Signed-off-by: Louis Chemineau --- src/components/TimelineList.vue | 10 +++++++++- src/store/timeline.js | 21 ++++++++++++++++----- src/types/Mastodon.js | 6 ++++++ src/views/TimelineSinglePost.vue | 1 + 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/components/TimelineList.vue b/src/components/TimelineList.vue index daedd0e8..4a88fef6 100644 --- a/src/components/TimelineList.vue +++ b/src/components/TimelineList.vue @@ -65,6 +65,10 @@ export default { type: String, default: () => 'home', }, + showParents: { + type: Boolean, + default: false, + }, }, data() { return { @@ -139,7 +143,11 @@ export default { * @return {import('../store/timeline.js').APObject[]} */ timeline() { - return this.$store.getters.getTimeline + if (this.showParents) { + return this.$store.getters.getParentsTimeline + } else { + return this.$store.getters.getTimeline + } }, }, mounted() { diff --git a/src/store/timeline.js b/src/store/timeline.js index 1a3a9a73..af825ecb 100644 --- a/src/store/timeline.js +++ b/src/store/timeline.js @@ -36,6 +36,10 @@ const state = { * @type {Object} timeline - The posts' collection */ timeline: {}, + /** + * @type {Object} timeline - The parents posts' collection + */ + parentsTimeline: {}, /** * @type {string} type - Timeline's type: 'home', 'single-post',... */ @@ -65,14 +69,16 @@ const state = { const mutations = { /** * @param state - * @param {import('../types/Mastodon.js').Status[]} data + * @param {import ('../types/Mastodon.js').Status[]|import('../types/Mastodon.js').Context} data */ addToTimeline(state, data) { - // TODO: fix to handle ancestors - if (data.descendants) { - data = data.descendants + if (Array.isArray(data)) { + data.forEach((post) => Vue.set(state.timeline, post.id, post)) + state.parentsTimeline = {} + } else { + data.descendants.forEach((post) => Vue.set(state.timeline, post.id, post)) + data.ancestors.forEach((post) => Vue.set(state.parentsTimeline, post.id, post)) } - data.forEach((post) => Vue.set(state.timeline, post.id, post)) }, /** * @param state @@ -172,6 +178,11 @@ const getters = { return new Date(b.created_at).getTime() - new Date(a.created_at).getTime() }) }, + getParentsTimeline(state) { + return Object.values(state.parentsTimeline).sort(function(a, b) { + return new Date(b.created_at).getTime() - new Date(a.created_at).getTime() + }) + }, getPostFromTimeline(state) { return (postId) => { if (typeof state.timeline[postId] !== 'undefined') { diff --git a/src/types/Mastodon.js b/src/types/Mastodon.js index 9d1970b4..95cff702 100644 --- a/src/types/Mastodon.js +++ b/src/types/Mastodon.js @@ -157,6 +157,12 @@ * @property {Poll} [poll] - Ex: null */ +/** + * @typedef Context - https://docs.joinmastodon.org/entities/Context + * @property {Status[]} ancestors - Parents in the thread. + * @property {Status[]} descendants - Children in the thread. + */ + /** * @typedef Notification - https://docs.joinmastodon.org/entities/Notification * @property {string} id - Ex: "https://example.com/users/@tommy"" diff --git a/src/views/TimelineSinglePost.vue b/src/views/TimelineSinglePost.vue index 745dd0a1..5c0a3236 100644 --- a/src/views/TimelineSinglePost.vue +++ b/src/views/TimelineSinglePost.vue @@ -2,6 +2,7 @@