Add title and thumbnails to user stats

This commit is contained in:
Ajay 2023-05-09 23:53:18 -04:00
parent 6f0abddd3e
commit 8d1025e17d
2 changed files with 47 additions and 2 deletions

View file

@ -115,6 +115,24 @@ async function getPermissions(userID: HashedUserID): Promise<Record<string, bool
return result;
}
async function getTitleSubmissionCount(userID: HashedUserID): Promise<number> {
try {
const row = await db.prepare("get", `SELECT COUNT(*) as "titleSubmissionCount" FROM "titles" JOIN "titleVotes" ON "titles"."UUID" = "titleVotes"."UUID" WHERE "titles"."userID" = ? AND "titleVotes"."votes" >= 0`, [userID], { useReplica: true });
return row?.titleSubmissionCount ?? 0;
} catch (err) /* istanbul ignore next */ {
return null;
}
}
async function getThumbnailSubmissionCount(userID: HashedUserID): Promise<number> {
try {
const row = await db.prepare("get", `SELECT COUNT(*) as "thumbnailSubmissionCount" FROM "thumbnails" JOIN "thumbnailVotes" ON "thumbnails"."UUID" = "thumbnailVotes"."UUID" WHERE "thumbnails"."userID" = ? AND "thumbnailVotes"."votes" >= 0`, [userID], { useReplica: true });
return row?.thumbnailSubmissionCount ?? 0;
} catch (err) /* istanbul ignore next */ {
return null;
}
}
type cases = Record<string, any>
const executeIfFunction = (f: any) =>
@ -140,7 +158,9 @@ const dbGetValue = (userID: HashedUserID, property: string): Promise<string|Segm
vip: () => isUserVIP(userID),
lastSegmentID: () => dbGetLastSegmentForUser(userID),
permissions: () => getPermissions(userID),
freeChaptersAccess: () => true
freeChaptersAccess: () => true,
titleSubmissionCount: () => getTitleSubmissionCount(userID),
thumbnailSubmissionCount: () => getThumbnailSubmissionCount(userID),
})("")(property);
};
@ -150,7 +170,8 @@ async function getUserInfo(req: Request, res: Response): Promise<Response> {
const defaultProperties: string[] = ["userID", "userName", "minutesSaved", "segmentCount", "ignoredSegmentCount",
"viewCount", "ignoredViewCount", "warnings", "warningReason", "reputation",
"vip", "lastSegmentID"];
const allProperties: string[] = [...defaultProperties, "banned", "permissions", "freeChaptersAccess"];
const allProperties: string[] = [...defaultProperties, "banned", "permissions", "freeChaptersAccess",
"ignoredSegmentCount", "titleSubmissionCount", "thumbnailSubmissionCount"];
let paramValues: string[] = req.query.values
? JSON.parse(req.query.values as string)
: req.query.value

View file

@ -23,6 +23,16 @@ describe("getUserInfo", () => {
await db.prepare("run", sponsorTimesQuery, ["getUserInfo4", 1, 11, 2, "uuid000010", getHash("getuserinfo_user_04"), 9, 10, "chapter", "chapter", 0]);
await db.prepare("run", sponsorTimesQuery, ["getUserInfo5", 1, 11, 2, "uuid000011", getHash("getuserinfo_user_05"), 9, 10, "sponsor", "skip", 0]);
const titlesQuery = 'INSERT INTO "titles" ("videoID", "title", "original", "userID", "service", "hashedVideoID", "timeSubmitted", "UUID") VALUES (?, ?, ?, ?, ?, ?, ?, ?)';
const titleVotesQuery = 'INSERT INTO "titleVotes" ("UUID", "votes", "locked", "shadowHidden") VALUES (?, ?, ?, 0);';
const thumbnailsQuery = 'INSERT INTO "thumbnails" ("videoID", "original", "userID", "service", "hashedVideoID", "timeSubmitted", "UUID") VALUES (?, ?, ?, ?, ?, ?, ?)';
const thumbnailVotesQuery = 'INSERT INTO "thumbnailVotes" ("UUID", "votes", "locked", "shadowHidden") VALUES (?, ?, ?, 0)';
await db.prepare("run", titlesQuery, ["getUserInfo6", "title0", 1, getHash("getuserinfo_user_01"), "YouTube", getHash("getUserInfo0"), 1, "uuid000001"]);
await db.prepare("run", titleVotesQuery, ["uuid000001", 0, 0]);
await db.prepare("run", thumbnailsQuery, ["getUserInfo6", "thumbnail0", getHash("getuserinfo_user_01"), "YouTube", getHash("getUserInfo0"), 1, "uuid000002"]);
await db.prepare("run", thumbnailVotesQuery, ["uuid000002", 0, 0]);
await db.prepare("run", titlesQuery, ["getUserInfo6", "title1", 0, getHash("getuserinfo_user_01"), "YouTube", getHash("getUserInfo0"), 2, "uuid000003"]);
await db.prepare("run", titleVotesQuery, ["uuid000003", -1, 0]);
const insertWarningQuery = 'INSERT INTO warnings ("userID", "issueTime", "issuerUserID", "enabled", "reason") VALUES (?, ?, ?, ?, ?)';
await db.prepare("run", insertWarningQuery, [getHash("getuserinfo_warning_0"), 10, "getuserinfo_vip", 1, "warning0-0"]);
@ -366,4 +376,18 @@ describe("getUserInfo", () => {
})
.catch(err => done(err));
});
it("Should get title and vote submission counts", (done) => {
client.get(endpoint, { params: { userID: "getuserinfo_user_01", value: ["titleSubmissionCount", "thumbnailSubmissionCount"] } })
.then(res => {
assert.strictEqual(res.status, 200);
const expected = {
titleSubmissionCount: 1,
thumbnailSubmissionCount: 1
};
assert.ok(partialDeepEquals(res.data, expected));
done();
})
.catch(err => done(err));
});
});