sort post based on postHotness function

This commit is contained in:
MuslemRahimi 2024-06-03 21:42:04 +02:00
parent 37c7dc02bc
commit 0c81aba202
2 changed files with 62 additions and 3 deletions

View File

@ -1,4 +1,11 @@
// Declare a route // Optimized postHotness function
function postHotness(upvote, numOfComments, created) {
const ageInHours = (Date.now() - new Date(created).getTime()) / 36e5; // 36e5 is scientific notation for 3600000
const hotness = (upvote + numOfComments * 2) / Math.pow(ageInHours + 2, 1.5); // Example calculation
return hotness;
}
module.exports = function (fastify, opts, done) { module.exports = function (fastify, opts, done) {
const pb = opts.pb; const pb = opts.pb;
@ -26,6 +33,22 @@ module.exports = function (fastify, opts, done) {
if (data?.filterTicker) { if (data?.filterTicker) {
filter += `&& tagline="${data?.filterTicker}" && pinned=false`; filter += `&& tagline="${data?.filterTicker}" && pinned=false`;
} }
if(data?.sortingPosts === 'hot') {
//community page code space
// In case of sort === 'hot' show the most recent post up to 4 week by ranking them with the function postHotness
let endDate = new Date();
// Get the date one week earlier
let startDate = new Date();
startDate.setDate(endDate.getDate() - 30);
// Format the dates as needed (e.g., "YYYY-MM-DD")
let endDateStr = endDate.toISOString().split('T')[0];
let startDateStr = startDate.toISOString().split('T')[0];
filter += `&& created >= "${startDateStr}" && created <= "${endDateStr}" && pinned = false`
}
posts = (await pb.collection('posts').getList(data?.startPage, 10, { posts = (await pb.collection('posts').getList(data?.startPage, 10, {
sort: sort, sort: sort,
@ -34,6 +57,15 @@ module.exports = function (fastify, opts, done) {
fields: "*,expand.user,expand.comments(post), expand.alreadyVoted(post).user,expand.alreadyVoted(post).type" fields: "*,expand.user,expand.comments(post), expand.alreadyVoted(post).user,expand.alreadyVoted(post).type"
}))?.items; }))?.items;
if(data?.sortingPosts === 'hot') {
// Add hotness property to each post
posts?.forEach(post => {
post.hotness = postHotness(post?.upvote, post?.expand['comments(post)']?.length, post?.created);
});
posts?.sort((a, b) => b?.hotness - a?.hotness);
}
} }
else { else {
@ -61,15 +93,42 @@ module.exports = function (fastify, opts, done) {
} }
else { else {
//community page code space
// In case of sort === 'hot' show the most recent post up to 4 week by ranking them with the function postHotness
if(data?.sortingPosts === 'hot') {
let endDate = new Date();
// Get the date one week earlier
let startDate = new Date();
startDate.setDate(endDate.getDate() - 30);
// Format the dates as needed (e.g., "YYYY-MM-DD")
let endDateStr = endDate.toISOString().split('T')[0];
let startDateStr = startDate.toISOString().split('T')[0];
filter = `created >= "${startDateStr}" && created <= "${endDateStr}" && pinned = false`
}
else {
filter = `pinned=false`;
}
posts = await pb.collection('posts').getList(data?.startPage, 10, { posts = await pb.collection('posts').getList(data?.startPage, 10, {
sort: sort, sort: sort,
filter: `pinned=false`, filter: filter,
expand: 'user, comments(post), alreadyVoted(post)', expand: 'user, comments(post), alreadyVoted(post)',
fields: "*,expand.user,expand.comments(post), expand.alreadyVoted(post).user,expand.alreadyVoted(post).type" fields: "*,expand.user,expand.comments(post), expand.alreadyVoted(post).user,expand.alreadyVoted(post).type"
}); });
posts = posts.items posts = posts.items
// Add hotness property to each post
posts?.forEach(post => {
post.hotness = postHotness(post?.upvote, post?.expand['comments(post)']?.length, post?.created);
});
posts?.sort((a, b) => b?.hotness - a?.hotness);
pinnedPost = await pb.collection('posts').getFullList({ pinnedPost = await pb.collection('posts').getFullList({
filter: `pinned=true`, filter: `pinned=true`,

View File

@ -9,7 +9,7 @@ module.exports = function (fastify, opts, done) {
const commentId = data?.commentId; const commentId = data?.commentId;
const postId = data?.postId; const postId = data?.postId;
const userId = data?.userId; const userId = data?.userId;
console.log(data)
let output = 'failure'; let output = 'failure';
try { try {