let comments influence ranking

This commit is contained in:
keyan 2023-05-09 13:52:35 -05:00
parent d948a653c9
commit 80968d7b35
4 changed files with 60 additions and 6 deletions

View File

@ -88,14 +88,15 @@ export async function orderByNumerator (me, models) {
if (me) {
const user = await models.user.findUnique({ where: { id: me.id } })
if (user.wildWestMode) {
return 'GREATEST("Item"."weightedVotes", POWER("Item"."weightedVotes", 1.2))'
return 'GREATEST("Item"."weightedVotes", POWER("Item"."weightedVotes", 1.2)) + "Item"."weightedComments"/2'
}
}
return `(CASE WHEN "Item"."weightedVotes" > "Item"."weightedDownVotes"
THEN 1
ELSE -1 END
* GREATEST(ABS("Item"."weightedVotes" - "Item"."weightedDownVotes"), POWER(ABS("Item"."weightedVotes" - "Item"."weightedDownVotes"), 1.2)))`
* GREATEST(ABS("Item"."weightedVotes" - "Item"."weightedDownVotes"), POWER(ABS("Item"."weightedVotes" - "Item"."weightedDownVotes"), 1.2))
+ "Item"."weightedComments"/2)`
}
export async function filterClause (me, models) {
@ -1246,7 +1247,7 @@ export const SELECT =
"Item"."subName", "Item".status, "Item"."uploadId", "Item"."pollCost", "Item".boost, "Item".msats,
"Item".ncomments, "Item"."commentMsats", "Item"."lastCommentAt", "Item"."weightedVotes",
"Item"."weightedDownVotes", "Item".freebie, "Item"."otsHash", "Item"."bountyPaidTo",
ltree2text("Item"."path") AS "path"`
ltree2text("Item"."path") AS "path", "Item"."weightedComments"`
async function newTimedOrderByWeightedSats (me, models, num) {
return `

View File

@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "Item" ADD COLUMN "weightedComments" DOUBLE PRECISION NOT NULL DEFAULT 0;

View File

@ -0,0 +1,50 @@
UPDATE "Item"
SET "weightedComments" = sub."weightedComments"
FROM (
SELECT subsub."itemId", SUM(subsub."userTrust") AS "weightedComments"
FROM
(SELECT p.id AS "itemId", u.id AS "userId", u.trust AS "userTrust"
FROM "Item" p
-- all decendants of p that aren't from the same author
JOIN "Item" c ON c.path <@ p.path AND c.id <> p.id AND c."userId" <> p."userId"
JOIN users u ON u.id = c."userId"
WHERE u.trust > 0
GROUP BY p.id, u.id, u.trust) subsub
GROUP BY subsub."itemId") sub
WHERE "Item".id = sub."itemId";
-- TODO: this could be better, e.g. testing for comment length or using comment votes instead
-- of just comment presence ... this is just an mvp
CREATE OR REPLACE FUNCTION ncomments_after_comment() RETURNS TRIGGER AS $$
DECLARE
user_trust DOUBLE PRECISION;
BEGIN
-- grab user's trust who is commenting
SELECT trust INTO user_trust FROM users WHERE id = NEW."userId";
UPDATE "Item"
SET "lastCommentAt" = now_utc(), "ncomments" = "ncomments" + 1
WHERE id <> NEW.id and path @> NEW.path;
-- we only want to add the user's trust to weightedComments if they aren't
-- already the author of a descendant comment
UPDATE "Item"
SET "weightedComments" = "weightedComments" + user_trust
FROM (
-- for every ancestor of the new comment, return the ones that don't have
-- the same author in their descendants
SELECT p.id
FROM "Item" p
-- all decendants of p that aren't the new comment
JOIN "Item" c ON c.path <@ p.path AND c.id <> NEW.id
-- p is an ancestor of this comment, it isn't itself, and it doesn't have the same author
WHERE p.path @> NEW.path AND p.id <> NEW.id AND p."userId" <> NEW."userId"
GROUP BY p.id
-- only return p if it doesn't have any descendants with the same author as the comment
HAVING bool_and(c."userId" <> NEW."userId")
) fresh
WHERE "Item".id = fresh.id;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

View File

@ -276,9 +276,10 @@ model Item {
upvotes Int @default(0)
// denormalized comment stats
ncomments Int @default(0)
commentMsats BigInt @default(0)
lastCommentAt DateTime?
ncomments Int @default(0)
commentMsats BigInt @default(0)
lastCommentAt DateTime?
weightedComments Float @default(0)
// if sub is null, this is the main sub
sub Sub? @relation(fields: [subName], references: [name])