Add simple count of views to share links (#4036)

* Add simple count of views to share links

* Remove no longer applicable tests

* Avoid incrementing view count for known bots
This commit is contained in:
Tom Moor
2022-08-31 08:16:40 +02:00
committed by GitHub
parent 212985e18f
commit 32b76303e5
9 changed files with 61 additions and 17 deletions

View File

@@ -3,6 +3,7 @@ import path from "path";
import util from "util";
import { Context, Next } from "koa";
import { escape } from "lodash";
import { Sequelize } from "sequelize";
import documentLoader from "@server/commands/documentLoader";
import env from "@server/env";
import presentEnv from "@server/presenters/env";
@@ -81,6 +82,13 @@ export const renderShare = async (ctx: Context, next: Next) => {
});
share = result.share;
document = result.document;
if (share && !ctx.userAgent.isBot) {
await share.update({
lastAccessedAt: new Date(),
views: Sequelize.literal("views + 1"),
});
}
} catch (err) {
// If the share or document does not exist, return a 404.
ctx.status = 404;