sort post based on postHotness function
This commit is contained in:
parent
37c7dc02bc
commit
0c81aba202
@ -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) {
|
||||
|
||||
const pb = opts.pb;
|
||||
@ -26,6 +33,22 @@ module.exports = function (fastify, opts, done) {
|
||||
if (data?.filterTicker) {
|
||||
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, {
|
||||
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"
|
||||
|
||||
}))?.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 {
|
||||
|
||||
@ -61,15 +93,42 @@ module.exports = function (fastify, opts, done) {
|
||||
}
|
||||
|
||||
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, {
|
||||
sort: sort,
|
||||
filter: `pinned=false`,
|
||||
filter: filter,
|
||||
expand: 'user, comments(post), alreadyVoted(post)',
|
||||
fields: "*,expand.user,expand.comments(post), expand.alreadyVoted(post).user,expand.alreadyVoted(post).type"
|
||||
|
||||
});
|
||||
|
||||
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({
|
||||
filter: `pinned=true`,
|
||||
|
||||
@ -9,7 +9,7 @@ module.exports = function (fastify, opts, done) {
|
||||
const commentId = data?.commentId;
|
||||
const postId = data?.postId;
|
||||
const userId = data?.userId;
|
||||
|
||||
console.log(data)
|
||||
let output = 'failure';
|
||||
|
||||
try {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user