about summary refs log tree commit diff
path: root/src/lib/api/feed-manip.ts
blob: 67ca8f952da47ceb91f8182116b9bc8eadbe0779 (plain) (blame)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import {AppBskyFeedFeedViewPost} from '@atproto/api'
import lande from 'lande'
type FeedViewPost = AppBskyFeedFeedViewPost.Main
import {hasProp} from '@atproto/lexicon'

export type FeedTunerFn = (
  tuner: FeedTuner,
  slices: FeedViewPostsSlice[],
) => void

export class FeedViewPostsSlice {
  constructor(public items: FeedViewPost[] = []) {}

  get uri() {
    if (this.isReply) {
      return this.items[1].post.uri
    }
    return this.items[0].post.uri
  }

  get ts() {
    if (this.items[0].reason?.indexedAt) {
      return this.items[0].reason.indexedAt as string
    }
    return this.items[0].post.indexedAt
  }

  get isThread() {
    return (
      this.items.length > 1 &&
      this.items.every(
        item => item.post.author.did === this.items[0].post.author.did,
      )
    )
  }

  get isReply() {
    return this.items.length === 2 && !this.isThread
  }

  get rootItem() {
    if (this.isReply) {
      return this.items[1]
    }
    return this.items[0]
  }

  containsUri(uri: string) {
    return !!this.items.find(item => item.post.uri === uri)
  }

  insert(item: FeedViewPost) {
    const selfReplyUri = getSelfReplyUri(item)
    const i = this.items.findIndex(item2 => item2.post.uri === selfReplyUri)
    if (i !== -1) {
      this.items.splice(i + 1, 0, item)
    } else {
      this.items.push(item)
    }
  }

  flattenReplyParent() {
    if (this.items[0].reply?.parent) {
      this.items.splice(0, 0, {post: this.items[0].reply?.parent})
    }
  }

  logSelf() {
    console.log(
      `- Slice ${this.items.length}${this.isThread ? ' (thread)' : ''} -`,
    )
    for (const item of this.items) {
      console.log(
        `  ${item.reason ? `RP by ${item.reason.by.handle}: ` : ''}${
          item.post.author.handle
        }: ${item.reply ? `(Reply ${item.reply.parent.author.handle}) ` : ''}${
          item.post.record.text
        }`,
      )
    }
  }
}

export class FeedTuner {
  seenUris: Set<string> = new Set()

  constructor() {}

  reset() {
    this.seenUris.clear()
  }

  tune(
    feed: FeedViewPost[],
    tunerFns: FeedTunerFn[] = [],
  ): FeedViewPostsSlice[] {
    const slices: FeedViewPostsSlice[] = []

    // arrange the posts into thread slices
    for (let i = feed.length - 1; i >= 0; i--) {
      const item = feed[i]

      const selfReplyUri = getSelfReplyUri(item)
      if (selfReplyUri) {
        const parent = slices.find(item2 => item2.containsUri(selfReplyUri))
        if (parent) {
          parent.insert(item)
          continue
        }
      }
      slices.unshift(new FeedViewPostsSlice([item]))
    }

    // remove any items already "seen"
    for (let i = slices.length - 1; i >= 0; i--) {
      if (this.seenUris.has(slices[i].uri)) {
        slices.splice(i, 1)
      }
    }

    // turn non-threads with reply parents into threads
    for (const slice of slices) {
      if (
        !slice.isThread &&
        !slice.items[0].reason &&
        slice.items[0].reply?.parent &&
        !this.seenUris.has(slice.items[0].reply?.parent.uri)
      ) {
        slice.flattenReplyParent()
      }
    }

    // sort by slice roots' timestamps
    slices.sort((a, b) => b.ts.localeCompare(a.ts))

    // run the custom tuners
    for (const tunerFn of tunerFns) {
      tunerFn(this, slices)
    }

    for (const slice of slices) {
      for (const item of slice.items) {
        this.seenUris.add(item.post.uri)
      }
      // DEBUG uncomment to get a quick view of the data
      // slice.logSelf()
    }

    return slices
  }

  static dedupReposts(tuner: FeedTuner, slices: FeedViewPostsSlice[]) {
    // remove duplicates caused by reposts
    for (let i = 0; i < slices.length; i++) {
      const item1 = slices[i]
      for (let j = i + 1; j < slices.length; j++) {
        const item2 = slices[j]
        if (item2.isThread) {
          // dont dedup items that are rendering in a thread as this can cause rendering errors
          continue
        }
        if (item1.containsUri(item2.items[0].post.uri)) {
          slices.splice(j, 1)
          j--
        }
      }
    }
  }

  static likedRepliesOnly(tuner: FeedTuner, slices: FeedViewPostsSlice[]) {
    // remove any replies without any likes
    for (let i = slices.length - 1; i >= 0; i--) {
      if (slices[i].isThread) {
        continue
      }
      const item = slices[i].rootItem
      const isRepost = Boolean(item.reason)
      if (item.reply && !isRepost && item.post.upvoteCount === 0) {
        slices.splice(i, 1)
      }
    }
  }

  static englishOnly(tuner: FeedTuner, slices: FeedViewPostsSlice[]) {
    // TEMP
    // remove slices with no english in them
    // we very soon need to get the local user's language and filter
    // according to their preferences, but for the moment
    // we're just rolling with english
    // -prf
    for (let i = slices.length - 1; i >= 0; i--) {
      let hasEnglish = false
      for (const item of slices[i].items) {
        if (
          hasProp(item.post.record, 'text') &&
          typeof item.post.record.text === 'string'
        ) {
          const res = lande(item.post.record.text)
          if (res[0][0] === 'eng') {
            hasEnglish = true
            break
          }
        }
      }
      if (!hasEnglish) {
        slices.splice(i, 1)
      }
    }
  }
}

function getSelfReplyUri(item: FeedViewPost): string | undefined {
  return item.reply?.parent.author.did === item.post.author.did
    ? item.reply?.parent.uri
    : undefined
}