-
Notifications
You must be signed in to change notification settings - Fork 42
/
discourse.ts
126 lines (105 loc) · 3.12 KB
/
discourse.ts
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
import { QueryParams } from '../lexicon/types/app/bsky/feed/getFeedSkeleton'
import { AppContext } from '../config'
import { AlgoManager } from '../addn/algoManager'
import { Post } from '../db/schema'
import dbClient from '../db/dbClient'
import limit from '../addn/rateLimit'
// max 15 chars
export const shortname = 'discourse'
export const handler = async (ctx: AppContext, params: QueryParams) => {
const builder = await dbClient.getPostBySortWeight(
'discourse_posts',
params.limit,
params.cursor,
)
const feed = builder.map((row) => ({
post: row._id.toString(),
}))
let cursor: string | undefined
let last = builder.length
if (params.cursor) last += Number.parseInt(params.cursor)
if (builder.length > 0) {
cursor = `${last}`
}
return {
cursor,
feed,
}
}
export class manager extends AlgoManager {
public name: string = shortname
public threshold = 19
public static cacheAge(params): Number {
return 600 // feed does not move fast
}
public async periodicTask() {
await this.db.removeTagFromOldPosts(
this.name,
new Date().getTime() - 1.5 * 24 * 60 * 60 * 1000,
)
await dbClient.aggregatePostsByRepliesToCollection(
'post',
shortname,
this.threshold,
'discourse_posts',
100, // top 100 only
)
const discourse_posts = await dbClient.getCollection('discourse_posts')
let updated = 0
console.log(`${this.name}: ${discourse_posts.length} posts updating...`)
for (let i = 0; i < discourse_posts.length; i++) {
let cursor: string | undefined = ''
let likes: number = Number.isInteger(discourse_posts[i].likes)
? discourse_posts[i].likes
: 0
// only check when previous likes are less than current count
if (likes < discourse_posts[i].count) {
updated++
try {
const post = await limit(() =>
this.agent.getPostThread({
uri: discourse_posts[i]._id.toString(),
depth: 1,
}),
)
const likeCount = Number.parseInt(
(<any>post.data.thread.post)?.likeCount,
)
likes = likeCount
} catch (err) {
console.log(
`${this.name}: Cannot retrieve ${discourse_posts[
i
]._id.toString()}`,
)
likes = 0
}
}
const record = {
_id: discourse_posts[i]._id,
count: discourse_posts[i].count,
indexedAt: discourse_posts[i].indexedAt,
likes: likes,
sort_weight:
likes > discourse_posts[i].count ? discourse_posts[i].count : likes,
}
await dbClient.insertOrReplaceRecord(
{ _id: record._id },
record,
'discourse_posts',
)
}
console.log(
`${this.name}: ${discourse_posts.length} updated (${updated} from server)`,
)
}
public async filter_post(post: Post): Promise<Boolean> {
if (post.text.length <= 100) return false
if (post.replyRoot !== null) {
if (post.replyRoot.split('/')[2] != post.author) return true
else return false
} else {
return false
}
}
}