Skip to content

Commit

Permalink
Fix map
Browse files Browse the repository at this point in the history
  • Loading branch information
codergautam committed Oct 15, 2024
1 parent 00a2572 commit 7bd2ab7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 35 deletions.
2 changes: 1 addition & 1 deletion components/utils/sendableMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ export default function sendableMap(map, creator, hearted=false, staff=false, is
reject_reason: map.reject_reason,
resubmittable: map.resubmittable,
yours: isCreator||staff,
locations: map?.locationsCnt ?? map.data?.length,
locations: map?.locationsCnt ?? map.data?.length ?? JSON.parse(JSON.stringify(map))?.locationsCnt
}
}
90 changes: 56 additions & 34 deletions pages/api/map/mapHome.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let mapCache = {
popular: {
data: [],
timeStamp: 0,
persist: 4800000
persist: 9600000
},
recent: {
data: [],
Expand Down Expand Up @@ -52,35 +52,35 @@ export default async function handler(req, res) {
// sections
// [reviewQueue (if staff), myMaps (if exists), likedMaps, officialCountryMaps, recent, popular ]

if(user?.staff) {
// reviewQueue
console.time('findReviewQueue');
// let queueMaps = await Map.find({ in_review: true });
let queueMaps = [];
console.timeEnd('findReviewQueue');

console.time('findReviewQueueOwner');
let queueMapsSendable = await Promise.all(queueMaps.map(async (map) => {
let owner;
if(!map.map_creator_name) {
owner = await User.findById(map.created_by);
// save map creator name
console.log('updating map creator name', map._id, owner.username, map.name);
map.map_creator_name = owner.username;
await map.save();
} else {
owner = { username: map.map_creator_name };
}

const isCreator = map.created_by === user._id.toString();
return sendableMap(map, owner, hearted_maps?hearted_maps.has(map._id.toString()):false, true, isCreator);
}));
console.timeEnd('findReviewQueueOwner');

// oldest to newest
queueMapsSendable.sort((a,b) => b.created_at - a.created_at);
response.reviewQueue = queueMapsSendable;
}
// if(user?.staff) {
// // reviewQueue
// console.time('findReviewQueue');
// // let queueMaps = await Map.find({ in_review: true });
// let queueMaps = [];
// console.timeEnd('findReviewQueue');

// console.time('findReviewQueueOwner');
// let queueMapsSendable = await Promise.all(queueMaps.map(async (map) => {
// let owner;
// if(!map.map_creator_name) {
// owner = await User.findById(map.created_by);
// // save map creator name
// console.log('updating map creator name', map._id, owner.username, map.name);
// map.map_creator_name = owner.username;
// await map.save();
// } else {
// owner = { username: map.map_creator_name };
// }

// const isCreator = map.created_by === user._id.toString();
// return sendableMap(map, owner, hearted_maps?hearted_maps.has(map._id.toString()):false, true, isCreator);
// }));
// console.timeEnd('findReviewQueueOwner');

// // oldest to newest
// queueMapsSendable.sort((a,b) => b.created_at - a.created_at);
// response.reviewQueue = queueMapsSendable;
// }

// owned maps
// find maps made by user
Expand Down Expand Up @@ -155,17 +155,39 @@ export default async function handler(req, res) {
// retrieve from db
let maps = [];
if(method === "recent") {
maps = await Map.find({ accepted: true }).sort({ created_at: -1 }).limit(20);
console.time('findRecentMaps');
maps = await Map.find({ accepted: true }).sort({ created_at: -1 }).limit(100);
console.timeEnd('findRecentMaps');
} else if(method === "popular") {
maps = await Map.find({ accepted: true }).sort({ hearts: -1 }).limit(100);
console.time('findPopularMaps');
maps = await Map.find({ accepted: true }) .select({
locationsCnt: { $size: "$data" },
created_at: 1,
slug: 1,
name: 1,
hearts: 1,
plays: 1,
description_short: 1,
map_creator_name: 1,
in_review: 1,
official: 1,
accepted: 1,
reject_reason: 1,
resubmittable: 1
});

// sort and limit to 100
maps = maps.sort((a,b) => b.hearts - a.hearts).slice(0,100);

console.timeEnd('findPopularMaps');
} else if(method === "spotlight") {
maps = await Map.find({ accepted: true, spotlight: true });
maps = await Map.find({ accepted: true, spotlight: true }).limit(100).allowDiskUse(true);
console.log('spotlight maps', maps.length);
}

let sendableMaps = await Promise.all(maps.map(async (map) => {
let owner;
if(!map.map_creator_name) {
if(!map.map_creator_name && map.data) {
owner = await User.findById(map.created_by);
// save map creator name
console.log('updating map creator name', map._id, owner.username, map.name);
Expand Down

0 comments on commit 7bd2ab7

Please sign in to comment.