Files
2024-01-25 16:47:06 +01:00

150 lines
4.8 KiB
JavaScript

const db = require('./surreal');
async function SetTrending(){
try{
const posts = await db.query(`SELECT * FROM posts`);
const trending = [];
for(let i = 0; i < posts.length; i++){
trending.push(posts[i]);
}
trending.sort((a, b) => {
return b.viewcount - a.viewcount;
});
const trendingPosts = trending.slice(0, 20);
const trendingPostsDB = await db.query(`UPDATE homepage SET trending = "${trendingPosts}"`);
if(trendingPostsDB) {
console.log("Trending posts updated");
return true;
}
else {
console.log("Trending posts not updated");
return false;
}
}
catch(err){
console.log(err);
}
}
async function UpdatePostRanking(){
try{
const posts = await db.query(`SELECT * FROM posts`);
//find posts with most likes
const mostLiked = [];
for(let i = 0; i < posts.length; i++){
mostLiked.push(posts[i]);
}
mostLiked.sort((a, b) => {
return b.likes.length - a.likes.length;
});
const mostLikedPosts = mostLiked.slice(0, 50);
const mostLikedPostsDB = await db.query(`UPDATE homepage SET mostLiked = "${mostLikedPosts}"`);
//find posts with most comments
const mostCommented = [];
for(let i = 0; i < posts.length; i++){
mostCommented.push(posts[i]);
}
mostCommented.sort((a, b) => {
return b.comments.length - a.comments.length;
});
const mostCommentedPosts = mostCommented.slice(0, 50);
const mostCommentedPostsDB = await db.query(`UPDATE homepage SET mostCommented = "${mostCommentedPosts}"`);
//find posts with most saves
const mostSaved = [];
for(let i = 0; i < posts.length; i++){
mostSaved.push(posts[i]);
}
mostSaved.sort((a, b) => {
return b.saves.length - a.saves.length;
});
const mostSavedPosts = mostSaved.slice(0, 50);
const mostSavedPostsDB = await db.query(`UPDATE homepage SET mostSaved = "${mostSavedPosts}"`);
if(mostLikedPostsDB && mostCommentedPostsDB && mostSavedPostsDB) {
console.log("Post rankings updated");
return true;
}
else {
console.log("Post rankings not updated");
return false;
}
}
catch(err){
console.log(err);
}
}
async function UpdateUserRanking(){
try{
const users = await db.query(`SELECT activity FROM users`);
//calculate user activity points do this by looking at activity array and looking at the date of each activity than use formula to calculate points
//then sort users by activity points
//then update homepage with top 20 users
const MostPopularUsers = [];
for(i = 0; i < users.length; i++){
//calculate activity points
let tempPoints = 0;
for(j = 0; j < users[i].activity.length; j++){
//calculate points
const actionDate = new Date(users[i].activity[j].date);
const currentDate = new Date();
const timeDifference = currentDate - actionDate;
const timeDifferenceInDays = timeDifference / (1000 * 3600 * 24);
const points = Math.max(0, 100 - daysAgo);
tempPoints += points;
}
//update activity points
const user = await db.query(`UPDATE users SET activityPoints = "${tempPoints}" WHERE username = "${users[i].username}"`);
//check if user is has more points than the last user in the array
if(MostPopularUsers.length == 0){
MostPopularUsers.push(users[i]);
}
else if(MostPopularUsers[MostPopularUsers.length - 1].activityPoints < tempPoints){
MostPopularUsers.push(users[i]);
}
//sort array
MostPopularUsers.sort((a, b) => {
return b.activityPoints - a.activityPoints;
});
//remove last user in array if array is longer than 100
if(MostPopularUsers.length > 100){
MostPopularUsers.pop();
}
}
}
catch(err){
console.log(err);
}
}
async function UpdateAll(){
const trending = await SetTrending();
const postRanking = await UpdatePostRanking();
const userRanking = await UpdateUserRanking();
if(trending && postRanking && userRanking){
console.log("All rankings updated");
return true;
}
else{
console.log("All rankings not updated");
return false;
}
}
module.exports = {SetTrending, UpdatePostRanking, UpdateUserRanking, UpdateAll};