Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged Lue's changes from project 1 #24

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 46 additions & 15 deletions src/topics/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,34 +75,65 @@ module.exports = function (Topics) {
return result.posts;
};

// The following code was created with the assistance of ChatGPT.

async function addEventStartEnd(postData, set, reverse, topicData) {
console.log('Lucas Lin');

if (!postData.length) {
return;
}

postData.forEach((p, index) => {
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;
if (p) {
processPost(p, postData, index, reverse, topicData);
}
});

const lastPost = postData[postData.length - 1];
if (lastPost) {
lastPost.eventStart = reverse ? topicData.timestamp : lastPost.timestamp;
lastPost.eventEnd = reverse ? lastPost.timestamp : Date.now();
if (lastPost.index) {
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;
}
await processLastPost(lastPost, set, reverse, topicData);
}
}

function processPost(p, postData, index, reverse, topicData) {
if (p.index === 0 && reverse) {
setEventStartEnd(p, topicData.lastposttime, Date.now());
} else if (postData[index + 1]) {
setEventStartEnd(
p,
reverse ? postData[index + 1].timestamp : p.timestamp,
reverse ? p.timestamp : postData[index + 1].timestamp
);
}
}

async function processLastPost(lastPost, set, reverse, topicData) {
setEventStartEnd(
lastPost,
reverse ? topicData.timestamp : lastPost.timestamp,
reverse ? lastPost.timestamp : Date.now()
);
if (lastPost.index) {
const nextPost = await getNextPost(set, lastPost.index, reverse);
if (reverse) {
lastPost.eventStart = nextPost.length ? nextPost[0].score : lastPost.eventStart;
} else {
lastPost.eventEnd = nextPost.length ? nextPost[0].score : lastPost.eventEnd;
}
}
}

function setEventStartEnd(post, start, end) {
post.eventStart = start;
post.eventEnd = end;
}

async function getNextPost(set, index, reverse) {
const method = reverse ? 'getSortedSetRevRangeWithScores' : 'getSortedSetRangeWithScores';
return db[method](set, index, index);
}

Topics.addPostData = async function (postData, uid) {
if (!Array.isArray(postData) || !postData.length) {
return [];
Expand Down