-
Notifications
You must be signed in to change notification settings - Fork 306
/
fetch.ts
96 lines (92 loc) · 3.58 KB
/
fetch.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
import { bioCard, postsRootSelector, postsSelectors, selfInfoSelectors } from '../utils/selector'
import { MutationObserverWatcher } from '@holoflows/kit'
import { PersonIdentifier } from '../../../database/type'
import { getEmptyPostInfo, SocialNetworkUI, SocialNetworkUIInformationCollector } from '../../../social-network/ui'
import { deconstructPayload } from '../../../utils/type-transform/Payload'
import { instanceOfTwitterUI } from './index'
import { postParser, resolveInfoFromBioCard } from '../utils/fetch'
import { uploadToService } from '../utils/user'
import { isNil } from 'lodash-es'
const resolveLastRecognizedIdentity = (self: SocialNetworkUI) => {
const selfSelector = selfInfoSelectors().screenName
const assign = () => {
const ref = self.lastRecognizedIdentity
const screenName = selfInfoSelectors().screenName.evaluate()
const nickname = selfInfoSelectors().name.evaluate()
const avatar = selfInfoSelectors().userAvatar.evaluate()
if (!isNil(screenName)) {
ref.value = {
identifier: new PersonIdentifier(self.networkIdentifier, screenName),
nickname,
avatar,
}
}
}
new MutationObserverWatcher(selfSelector)
.addListener('onAdd', () => assign())
.addListener('onChange', () => assign())
.startWatch({
childList: true,
subtree: true,
})
.then()
}
const registerUserCollector = () => {
new MutationObserverWatcher(bioCard())
.useForeach(() => {
const resolve = () => {
const r = resolveInfoFromBioCard()
uploadToService(r)
}
resolve()
return {
onNodeMutation: resolve,
onTargetChanged: resolve,
}
})
.startWatch({
childList: true,
subtree: true,
})
.then()
}
const registerPostCollector = (self: SocialNetworkUI) => {
new MutationObserverWatcher(postsSelectors())
.useForeach((node, _, proxy) => {
const info = getEmptyPostInfo(postsRootSelector())
const collectPostInfo = () => {
const r = postParser(node)
if (!r) return
info.postContent.value = r.content
const postBy = new PersonIdentifier(self.networkIdentifier, r.handle)
if (!info.postBy.value.equals(postBy)) {
info.postBy.value = postBy
}
info.postID.value = r.pid
uploadToService(r)
}
collectPostInfo()
info.postPayload.value = deconstructPayload(info.postContent.value, self.payloadDecoder)
info.postContent.addListener(newValue => {
info.postPayload.value = deconstructPayload(newValue, self.payloadDecoder)
})
// push to map
self.posts.set(proxy, info)
return {
onNodeMutation: collectPostInfo,
onTargetChanged: collectPostInfo,
onRemove: () => self.posts.delete(proxy),
}
})
.setDOMProxyOption({ afterShadowRootInit: { mode: 'closed' } })
.startWatch({
childList: true,
subtree: true,
})
.then()
}
export const twitterUIFetch: SocialNetworkUIInformationCollector = {
resolveLastRecognizedIdentity: () => resolveLastRecognizedIdentity(instanceOfTwitterUI),
collectPeople: registerUserCollector,
collectPosts: () => registerPostCollector(instanceOfTwitterUI),
}