feat(api): add a route to update story views

don't worry, there are duplicate/spam checks in place
This commit is contained in:
parent da8b960004
commit 417fda7617
Signed by: tablet
GPG Key ID: 924A5F6AF051E87C

@ -0,0 +1,28 @@
import { isIdNan } from "@server/middlewareButNotReally";
import { Story } from "@models/stories";
export default eventHandler(async (ev) => {
const id = isIdNan(ev);
const ip = await getRequestIP(ev, { xForwardedFor: true });
const storage = useStorage(`/story/${id}/viewMap`);
if (ev.context.currentUser) {
if (!(await storage.hasItem(`loggedIn/${ev.context.currentUser._id}`))) {
await Story.findByIdAndUpdate(id, {
$inc: {
views: 1,
},
});
}
await storage.setItem(`loggedIn/${ev.context.currentUser._id}`, Date.now());
} else {
if (!(await storage.hasItem(`/loggedIn/${ip}`))) {
await Story.findByIdAndUpdate(id, {
$inc: {
views: 1,
},
});
}
await storage.setItem(ip, Date.now());
}
return {};
});