Repository navigation
Expand file tree
/
Copy pathtopicAffinity.ts
More file actions
169 lines (153 loc) · 4.66 KB
/
Copy pathtopicAffinity.ts
File metadata and controls
169 lines (153 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { desc, eq, gt, inArray } from "drizzle-orm";
import type { db as defaultDb } from "@/server/db";
import {
post_votes,
bookmarks,
comments,
post_topic,
user_topic_affinity,
} from "@/server/db/schema";
// Implicit topic affinity: how much a user has engaged with each topic, derived
// from their votes/bookmarks/comments via post_topic edges with time decay.
type Db = typeof defaultDb;
export const HALF_LIFE_DAYS = 60;
const MS_PER_DAY = 86_400_000;
const PER_SOURCE_LIMIT = 300;
export const INTERACTION_WEIGHT = {
upvote: 3,
downvote: -1,
bookmark: 4,
comment: 2,
} as const;
export function decayedWeight(
base: number,
ageDays: number,
halfLifeDays: number = HALF_LIFE_DAYS,
): number {
if (ageDays <= 0) return base;
return base * Math.pow(0.5, ageDays / halfLifeDays);
}
interface Interaction {
postId: string;
base: number;
createdAt: string;
}
// Recompute and persist one user's topic affinity, replacing their existing
// rows. Returns the number of topics with positive affinity.
export async function recomputeUserAffinity(
db: Db,
userId: string,
nowMs: number,
): Promise<number> {
const [votes, marks, cmts] = await Promise.all([
db
.select({
postId: post_votes.postId,
voteType: post_votes.voteType,
createdAt: post_votes.createdAt,
})
.from(post_votes)
.where(eq(post_votes.userId, userId))
.orderBy(desc(post_votes.createdAt))
.limit(PER_SOURCE_LIMIT),
db
.select({ postId: bookmarks.postId, createdAt: bookmarks.createdAt })
.from(bookmarks)
.where(eq(bookmarks.userId, userId))
.orderBy(desc(bookmarks.createdAt))
.limit(PER_SOURCE_LIMIT),
db
.select({ postId: comments.postId, createdAt: comments.createdAt })
.from(comments)
.where(eq(comments.authorId, userId))
.orderBy(desc(comments.createdAt))
.limit(PER_SOURCE_LIMIT),
]);
const interactions: Interaction[] = [
...votes.map((v) => ({
postId: v.postId,
base:
v.voteType === "up"
? INTERACTION_WEIGHT.upvote
: INTERACTION_WEIGHT.downvote,
createdAt: v.createdAt,
})),
...marks.map((m) => ({
postId: m.postId,
base: INTERACTION_WEIGHT.bookmark,
createdAt: m.createdAt,
})),
...cmts.map((c) => ({
postId: c.postId,
base: INTERACTION_WEIGHT.comment,
createdAt: c.createdAt,
})),
];
if (interactions.length === 0) {
await db
.delete(user_topic_affinity)
.where(eq(user_topic_affinity.userId, userId));
return 0;
}
const postIds = Array.from(new Set(interactions.map((i) => i.postId)));
const edges = await db
.select({ postId: post_topic.postId, topicId: post_topic.topicId })
.from(post_topic)
.where(inArray(post_topic.postId, postIds));
const topicsByPost = new Map<string, number[]>();
for (const edge of edges) {
const arr = topicsByPost.get(edge.postId) ?? [];
arr.push(edge.topicId);
topicsByPost.set(edge.postId, arr);
}
const scores = new Map<number, number>();
for (const it of interactions) {
const topics = topicsByPost.get(it.postId);
if (!topics?.length) continue;
const ageDays = Math.max(
0,
(nowMs - Date.parse(it.createdAt)) / MS_PER_DAY,
);
const weight = decayedWeight(it.base, ageDays);
for (const topicId of topics) {
scores.set(topicId, (scores.get(topicId) ?? 0) + weight);
}
}
const updatedAt = new Date(nowMs).toISOString();
const rows = [...scores.entries()]
.filter(([, score]) => score > 0)
.map(([topicId, score]) => ({ userId, topicId, score, updatedAt }));
await db
.delete(user_topic_affinity)
.where(eq(user_topic_affinity.userId, userId));
if (rows.length > 0) {
await db.insert(user_topic_affinity).values(rows);
}
return rows.length;
}
// User ids with at least one interaction since `sinceIso`, capped.
export async function findRecentlyActiveUsers(
db: Db,
sinceIso: string,
limit: number,
): Promise<string[]> {
const [voters, markers, commenters] = await Promise.all([
db
.selectDistinct({ userId: post_votes.userId })
.from(post_votes)
.where(gt(post_votes.createdAt, sinceIso)),
db
.selectDistinct({ userId: bookmarks.userId })
.from(bookmarks)
.where(gt(bookmarks.createdAt, sinceIso)),
db
.selectDistinct({ userId: comments.authorId })
.from(comments)
.where(gt(comments.createdAt, sinceIso)),
]);
const ids = new Set<string>();
for (const row of voters) ids.add(row.userId);
for (const row of markers) ids.add(row.userId);
for (const row of commenters) ids.add(row.userId);
return Array.from(ids).slice(0, limit);
}