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) { Posts.addTopicEvents = function (events) {
if (config.topicPostSort === 'most_votes') {
return;
}
const html = helpers.renderEvents.call(ajaxify.data, events); const html = helpers.renderEvents.call(ajaxify.data, events);
translator.translate(html, (translated) => { 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(); $('[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 + '" />'; return '<img component="user/picture" data-uid="' + topicObj.user.uid + '" src="' + topicObj.user.picture + '" class="user-img" title="' + topicObj.user.username + '" />';
} }
function renderTopicEvents(index) { function renderTopicEvents(index, sort) {
const start = this.posts[index].timestamp; if (sort === 'most_votes') {
const end = this.posts[index].nextPostTimestamp; 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); const events = this.events.filter(event => event.timestamp >= start && event.timestamp < end);
if (!events.length) { if (!events.length) {
return ''; return '';

View File

@@ -209,7 +209,7 @@ Topics.getEvents = async (req, res) => {
return helpers.formatApiResponse(403, 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) => { Topics.deleteEvent = async (req, res) => {

View File

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

View File

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

View File

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