fix: #10010, handle reverse sorting for topic events

dont add events to dom if sort is most votes
if sorting is reverse add new events after the main post or at the top instead of bottom
This commit is contained in:
Barış Soner Uşaklı
2021-11-17 23:34:01 -05:00
parent e368feef51
commit d5bfd51267
6 changed files with 43 additions and 16 deletions

View File

@@ -289,9 +289,22 @@ define('forum/topic/posts', [
};
Posts.addTopicEvents = function (events) {
if (config.topicPostSort === 'most_votes') {
return;
}
const html = helpers.renderEvents.call(ajaxify.data, events);
translator.translate(html, (translated) => {
document.querySelector('[component="topic"]').insertAdjacentHTML('beforeend', translated);
if (config.topicPostSort === 'oldest_to_newest') {
$('[component="topic"]').append(translated);
} else if (config.topicPostSort === 'newest_to_oldest') {
const mainPost = $('[component="topic"] [component="post"][data-index="0"]');
if (mainPost.length) {
$(translated).insertAfter(mainPost);
} else {
$('[component="topic"]').prepend(translated);
}
}
$('[component="topic/event"] .timeago').timeago();
});
};

View File

@@ -210,9 +210,12 @@
return '<img component="user/picture" data-uid="' + topicObj.user.uid + '" src="' + topicObj.user.picture + '" class="user-img" title="' + topicObj.user.username + '" />';
}
function renderTopicEvents(index) {
const start = this.posts[index].timestamp;
const end = this.posts[index].nextPostTimestamp;
function renderTopicEvents(index, sort) {
if (sort === 'most_votes') {
return '';
}
const start = this.posts[index].eventStart;
const end = this.posts[index].eventEnd;
const events = this.events.filter(event => event.timestamp >= start && event.timestamp < end);
if (!events.length) {
return '';

View File

@@ -209,7 +209,7 @@ Topics.getEvents = async (req, res) => {
return helpers.formatApiResponse(403, res);
}
helpers.formatApiResponse(200, res, await topics.events.get(req.params.tid));
helpers.formatApiResponse(200, res, await topics.events.get(req.params.tid, req.uid));
};
Topics.deleteEvent = async (req, res) => {

View File

@@ -66,7 +66,7 @@ Events.init = async () => {
Events._types = types;
};
Events.get = async (tid, uid) => {
Events.get = async (tid, uid, reverse = false) => {
const topics = require('.');
if (!await topics.exists(tid)) {
@@ -79,7 +79,9 @@ Events.get = async (tid, uid) => {
eventIds = eventIds.map(obj => obj.value);
let events = await db.getObjects(keys);
events = await modifyEvent({ tid, uid, eventIds, timestamps, events });
if (reverse) {
events.reverse();
}
return events;
};

View File

@@ -179,7 +179,7 @@ Topics.getTopicWithPosts = async function (topicData, set, uid, start, stop, rev
getMerger(topicData),
Topics.getRelatedTopics(topicData, uid),
Topics.thumbs.load([topicData]),
Topics.events.get(topicData.tid, uid),
Topics.events.get(topicData.tid, uid, reverse),
]);
topicData.thumbs = thumbs[0];

View File

@@ -56,7 +56,7 @@ module.exports = function (Topics) {
Topics.calculatePostIndices(replies, repliesStart);
await Topics.addNextPostTimestamp(postData, set, reverse);
await addEventStartEnd(postData, set, reverse, topicOrTid);
const result = await plugins.hooks.fire('filter:topic.getPosts', {
topic: topicOrTid,
uid: uid,
@@ -65,24 +65,33 @@ module.exports = function (Topics) {
return result.posts;
};
Topics.addNextPostTimestamp = async function (postData, set, reverse) {
async function addEventStartEnd(postData, set, reverse, topicData) {
if (!postData.length) {
return;
}
postData.forEach((p, index) => {
if (p && postData[index + 1]) {
p.nextPostTimestamp = postData[index + 1].timestamp;
if (p && p.index === 0 && reverse) {
p.eventStart = topicData.lastposttime;
p.eventEnd = Date.now();
} else if (p && postData[index + 1]) {
p.eventStart = reverse ? postData[index + 1].timestamp : p.timestamp;
p.eventEnd = reverse ? p.timestamp : postData[index + 1].timestamp;
}
});
const lastPost = postData[postData.length - 1];
if (lastPost) {
lastPost.nextPostTimestamp = Date.now();
lastPost.eventStart = reverse ? topicData.timestamp : lastPost.timestamp;
lastPost.eventEnd = reverse ? lastPost.timestamp : Date.now();
if (lastPost.index) {
const data = await db[reverse ? 'getSortedSetRevRangeWithScores' : 'getSortedSetRangeWithScores'](set, lastPost.index, lastPost.index);
lastPost.nextPostTimestamp = data.length ? data[0].score : lastPost.nextPostTimestamp;
const nextPost = await db[reverse ? 'getSortedSetRevRangeWithScores' : 'getSortedSetRangeWithScores'](set, lastPost.index, lastPost.index);
if (reverse) {
lastPost.eventStart = nextPost.length ? nextPost[0].score : lastPost.eventStart;
} else {
lastPost.eventEnd = nextPost.length ? nextPost[0].score : lastPost.eventEnd;
}
}
}
};
}
Topics.addPostData = async function (postData, uid) {
if (!Array.isArray(postData) || !postData.length) {