-
Notifications
You must be signed in to change notification settings - Fork 42
/
discourse.ts
109 lines (90 loc) · 2.76 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
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'
// 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 async periodicTask() {
await this.db.removeTagFromOldPosts(
this.name,
new Date().getTime() - 3 * 24 * 60 * 60 * 1000,
)
await dbClient.aggregatePostsByRepliesToCollection(
'post',
shortname,
this.threshold,
'discourse_posts',
1000, // top 1000 only
)
const discourse_posts = await dbClient.getCollection('discourse_posts')
let updated = 0
console.log(`${this.name}: ${discourse_posts.length} post 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) {
likes = 0 // reset to count
updated++
while (cursor !== undefined) {
const likes_query = await this.agent.app.bsky.feed.getLikes({
uri: discourse_posts[i]._id.toString(),
cursor: cursor,
})
cursor = likes_query.data.cursor
likes += likes_query.data.likes.length
}
}
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.replyRoot !== null) {
if (post.replyRoot.split('/')[2] != post.author) return true
else return false
} else {
return false
}
}
}