about summary refs log tree commit diff
path: root/src/lib/labeling/helpers.ts
blob: 0092b99e4fd76a933a4e65ffac63a3f1a201d3d0 (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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import {
  AppBskyActorDefs,
  AppBskyEmbedRecordWithMedia,
  AppBskyEmbedRecord,
  AppBskyEmbedImages,
  AppBskyEmbedExternal,
} from '@atproto/api'
import {
  CONFIGURABLE_LABEL_GROUPS,
  ILLEGAL_LABEL_GROUP,
  UNKNOWN_LABEL_GROUP,
} from './const'
import {
  Label,
  LabelValGroup,
  ModerationBehaviorCode,
  PostModeration,
  ProfileModeration,
  PostLabelInfo,
  ProfileLabelInfo,
} from './types'
import {RootStoreModel} from 'state/index'

type Embed =
  | AppBskyEmbedRecord.View
  | AppBskyEmbedImages.View
  | AppBskyEmbedExternal.View
  | AppBskyEmbedRecordWithMedia.View
  | {$type: string; [k: string]: unknown}

export function getLabelValueGroup(labelVal: string): LabelValGroup {
  let id: keyof typeof CONFIGURABLE_LABEL_GROUPS
  for (id in CONFIGURABLE_LABEL_GROUPS) {
    if (ILLEGAL_LABEL_GROUP.values.includes(labelVal)) {
      return ILLEGAL_LABEL_GROUP
    }
    if (CONFIGURABLE_LABEL_GROUPS[id].values.includes(labelVal)) {
      return CONFIGURABLE_LABEL_GROUPS[id]
    }
  }
  return UNKNOWN_LABEL_GROUP
}

export function getPostModeration(
  store: RootStoreModel,
  postInfo: PostLabelInfo,
): PostModeration {
  const accountPref = store.preferences.getLabelPreference(
    postInfo.accountLabels,
  )
  const profilePref = store.preferences.getLabelPreference(
    postInfo.profileLabels,
  )
  const postPref = store.preferences.getLabelPreference(postInfo.postLabels)

  // avatar
  let avatar = {
    warn: accountPref.pref === 'hide' || accountPref.pref === 'warn',
    blur:
      accountPref.pref === 'hide' ||
      accountPref.pref === 'warn' ||
      profilePref.pref === 'hide' ||
      profilePref.pref === 'warn',
  }

  // hide no-override cases
  if (accountPref.pref === 'hide' && accountPref.desc.id === 'illegal') {
    return hidePostNoOverride(accountPref.desc.warning)
  }
  if (profilePref.pref === 'hide' && profilePref.desc.id === 'illegal') {
    return hidePostNoOverride(profilePref.desc.warning)
  }
  if (postPref.pref === 'hide' && postPref.desc.id === 'illegal') {
    return hidePostNoOverride(postPref.desc.warning)
  }

  // hide cases
  if (accountPref.pref === 'hide') {
    return {
      avatar,
      list: hide(accountPref.desc.warning),
      thread: hide(accountPref.desc.warning),
      view: warn(accountPref.desc.warning),
    }
  }
  if (profilePref.pref === 'hide') {
    return {
      avatar,
      list: hide(profilePref.desc.warning),
      thread: hide(profilePref.desc.warning),
      view: warn(profilePref.desc.warning),
    }
  }
  if (postPref.pref === 'hide') {
    return {
      avatar,
      list: hide(postPref.desc.warning),
      thread: hide(postPref.desc.warning),
      view: warn(postPref.desc.warning),
    }
  }

  // muting
  if (postInfo.isMuted) {
    return {
      avatar,
      list: hide('Post from an account you muted.'),
      thread: warn('Post from an account you muted.'),
      view: warn('Post from an account you muted.'),
    }
  }

  // warning cases
  if (postPref.pref === 'warn') {
    if (postPref.desc.imagesOnly) {
      return {
        avatar,
        list: warnContent(postPref.desc.warning), // TODO make warnImages when there's time
        thread: warnContent(postPref.desc.warning), // TODO make warnImages when there's time
        view: warnContent(postPref.desc.warning), // TODO make warnImages when there's time
      }
    }
    return {
      avatar,
      list: warnContent(postPref.desc.warning),
      thread: warnContent(postPref.desc.warning),
      view: warnContent(postPref.desc.warning),
    }
  }
  if (accountPref.pref === 'warn') {
    return {
      avatar,
      list: warnContent(accountPref.desc.warning),
      thread: warnContent(accountPref.desc.warning),
      view: warnContent(accountPref.desc.warning),
    }
  }

  return {
    avatar,
    list: show(),
    thread: show(),
    view: show(),
  }
}

export function getProfileModeration(
  store: RootStoreModel,
  profileLabels: ProfileLabelInfo,
): ProfileModeration {
  const accountPref = store.preferences.getLabelPreference(
    profileLabels.accountLabels,
  )
  const profilePref = store.preferences.getLabelPreference(
    profileLabels.profileLabels,
  )

  // avatar
  let avatar = {
    warn: accountPref.pref === 'hide' || accountPref.pref === 'warn',
    blur:
      accountPref.pref === 'hide' ||
      accountPref.pref === 'warn' ||
      profilePref.pref === 'hide' ||
      profilePref.pref === 'warn',
  }

  // hide no-override cases
  if (accountPref.pref === 'hide' && accountPref.desc.id === 'illegal') {
    return hideProfileNoOverride(accountPref.desc.warning)
  }
  if (profilePref.pref === 'hide' && profilePref.desc.id === 'illegal') {
    return hideProfileNoOverride(profilePref.desc.warning)
  }

  // hide cases
  if (accountPref.pref === 'hide') {
    return {
      avatar,
      list: hide(accountPref.desc.warning),
      view: hide(accountPref.desc.warning),
    }
  }
  if (profilePref.pref === 'hide') {
    return {
      avatar,
      list: hide(profilePref.desc.warning),
      view: hide(profilePref.desc.warning),
    }
  }

  // warn cases
  if (accountPref.pref === 'warn') {
    return {
      avatar,
      list: warn(accountPref.desc.warning),
      view: warn(accountPref.desc.warning),
    }
  }
  // we don't warn for this
  // if (profilePref.pref === 'warn') {
  //   return {
  //     avatar,
  //     list: warn(profilePref.desc.warning),
  //     view: warn(profilePref.desc.warning),
  //   }
  // }

  return {
    avatar,
    list: show(),
    view: show(),
  }
}

export function getProfileViewBasicLabelInfo(
  profile: AppBskyActorDefs.ProfileViewBasic,
): ProfileLabelInfo {
  return {
    accountLabels: filterAccountLabels(profile.labels),
    profileLabels: filterProfileLabels(profile.labels),
    isMuted: profile.viewer?.muted || false,
  }
}

export function getEmbedLabels(embed?: Embed): Label[] {
  if (!embed) {
    return []
  }
  if (
    AppBskyEmbedRecord.isView(embed) &&
    AppBskyEmbedRecord.isViewRecord(embed.record)
  ) {
    return embed.record.labels || []
  }
  return []
}

export function filterAccountLabels(labels?: Label[]): Label[] {
  if (!labels) {
    return []
  }
  return labels.filter(
    label => !label.uri.endsWith('/app.bsky.actor.profile/self'),
  )
}

export function filterProfileLabels(labels?: Label[]): Label[] {
  if (!labels) {
    return []
  }
  return labels.filter(label =>
    label.uri.endsWith('/app.bsky.actor.profile/self'),
  )
}

// internal methods
// =

function show() {
  return {
    behavior: ModerationBehaviorCode.Show,
  }
}

function hidePostNoOverride(reason: string) {
  return {
    avatar: {warn: true, blur: true},
    list: hideNoOverride(reason),
    thread: hideNoOverride(reason),
    view: hideNoOverride(reason),
  }
}

function hideProfileNoOverride(reason: string) {
  return {
    avatar: {warn: true, blur: true},
    list: hideNoOverride(reason),
    view: hideNoOverride(reason),
  }
}

function hideNoOverride(reason: string) {
  return {
    behavior: ModerationBehaviorCode.Hide,
    reason,
    noOverride: true,
  }
}

function hide(reason: string) {
  return {
    behavior: ModerationBehaviorCode.Hide,
    reason,
  }
}

function warn(reason: string) {
  return {
    behavior: ModerationBehaviorCode.Warn,
    reason,
  }
}

function warnContent(reason: string) {
  return {
    behavior: ModerationBehaviorCode.WarnContent,
    reason,
  }
}

// TODO
// function warnImages(reason: string) {
//   return {
//     behavior: ModerationBehaviorCode.WarnImages,
//     reason,
//   }
// }