bsky-sdk 0.1.24

ATrium-based SDK for Bluesky
Documentation
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! Moderation behavior decision making.
use super::types::*;
use super::{Moderator, labels::KnownLabelValue, ui::ModerationUi};
use atrium_api::app::bsky::graph::defs::ListViewBasic;
use atrium_api::com::atproto::label::defs::Label;
use atrium_api::types::string::Did;

/// A moderation decision context.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DecisionContext {
    /// A profile being listed, e.g. in search or a follower list
    ProfileList,
    /// A profile being viewed directly
    ProfileView,
    /// The user's avatar in any context
    Avatar,
    /// The user's banner in any context
    Banner,
    /// The user's display name in any context
    DisplayName,
    /// Content being listed, e.g. posts in a feed, posts as replies, a user list list, a feed generator list, etc
    ContentList,
    /// Content being viewed direct, e.g. an opened post, the user list page, the feedgen page, etc
    ContentView,
    /// Media inside the content, e.g. a picture embedded in a post
    ContentMedia,
}

impl DecisionContext {
    pub const ALL: [DecisionContext; 8] = [
        DecisionContext::ProfileList,
        DecisionContext::ProfileView,
        DecisionContext::Avatar,
        DecisionContext::Banner,
        DecisionContext::DisplayName,
        DecisionContext::ContentList,
        DecisionContext::ContentView,
        DecisionContext::ContentMedia,
    ];
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ModerationBehaviorSeverity {
    High,
    Medium,
    Low,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) enum Priority {
    Priority1,
    Priority2,
    Priority3,
    Priority4,
    Priority5,
    Priority6,
    Priority7,
    Priority8,
}

impl AsRef<u8> for Priority {
    fn as_ref(&self) -> &u8 {
        match self {
            Priority::Priority1 => &1,
            Priority::Priority2 => &2,
            Priority::Priority3 => &3,
            Priority::Priority4 => &4,
            Priority::Priority5 => &5,
            Priority::Priority6 => &6,
            Priority::Priority7 => &7,
            Priority::Priority8 => &8,
        }
    }
}

/// A moderation decision.
#[derive(Debug)]
pub struct ModerationDecision {
    did: Option<Did>,
    is_me: bool,
    causes: Vec<ModerationCause>,
}

impl ModerationDecision {
    /// Calculate the [`ModerationUi`] for a given context.
    pub fn ui(&self, context: DecisionContext) -> ModerationUi {
        let mut ui = ModerationUi {
            no_override: false,
            filters: Vec::new(),
            blurs: Vec::new(),
            alerts: Vec::new(),
            informs: Vec::new(),
        };
        for cause in &self.causes {
            match cause {
                ModerationCause::Blocking(b)
                | ModerationCause::BlockedBy(b)
                /* | ModerationCause::BlockOther(b) */ => {
                    if self.is_me {
                        continue;
                    }
                    if matches!(
                        context,
                        DecisionContext::ProfileList | DecisionContext::ContentList
                    ) {
                        ui.filters.push(cause.clone())
                    }
                    if !b.downgraded {
                        match ModerationBehavior::BLOCK_BEHAVIOR.behavior_for(context) {
                            Some(BehaviorValue::Blur) => {
                                ui.no_override = true;
                                ui.blurs.push(cause.clone());
                            }
                            Some(BehaviorValue::Alert) => {
                                ui.alerts.push(cause.clone());
                            }
                            Some(BehaviorValue::Inform) => {
                                ui.informs.push(cause.clone());
                            }
                            _ => {}
                        }
                    }
                }
                ModerationCause::Label(label) => {
                    if matches!(
                        (context, label.target),
                        (DecisionContext::ProfileList, LabelTarget::Account)
                            | (
                                DecisionContext::ContentList,
                                LabelTarget::Account | LabelTarget::Content,
                            ),
                    ) && (label.setting == LabelPreference::Hide && !self.is_me)
                    {
                        ui.filters.push(cause.clone())
                    }
                    if !label.downgraded {
                        match label.behavior.behavior_for(context) {
                            Some(BehaviorValue::Blur) => {
                                ui.blurs.push(cause.clone());
                                if label.no_override && !self.is_me {
                                    ui.no_override = true;
                                }
                            }
                            Some(BehaviorValue::Alert) => {
                                ui.alerts.push(cause.clone());
                            }
                            Some(BehaviorValue::Inform) => {
                                ui.informs.push(cause.clone());
                            }
                            _ => {}
                        }
                    }
                }
                ModerationCause::Muted(muted) => {
                    if self.is_me {
                        continue;
                    }
                    if matches!(
                        context,
                        DecisionContext::ProfileList | DecisionContext::ContentList
                    ) {
                        ui.filters.push(cause.clone())
                    }
                    if !muted.downgraded {
                        match ModerationBehavior::MUTE_BEHAVIOR.behavior_for(context) {
                            Some(BehaviorValue::Blur) => {
                                ui.blurs.push(cause.clone());
                            }
                            Some(BehaviorValue::Alert) => {
                                ui.alerts.push(cause.clone());
                            }
                            Some(BehaviorValue::Inform) => {
                                ui.informs.push(cause.clone());
                            }
                            _ => {}
                        }
                    }
                }
                ModerationCause::MuteWord(mute_word) => {
                    if self.is_me {
                        continue;
                    }
                    if matches!(context, DecisionContext::ContentList) {
                        ui.filters.push(cause.clone())
                    }
                    if !mute_word.downgraded {
                        match ModerationBehavior::MUTEWORD_BEHAVIOR.behavior_for(context) {
                            Some(BehaviorValue::Blur) => {
                                ui.blurs.push(cause.clone());
                            }
                            Some(BehaviorValue::Alert) => {
                                ui.alerts.push(cause.clone());
                            }
                            Some(BehaviorValue::Inform) => {
                                ui.informs.push(cause.clone());
                            }
                            _ => {}
                        }
                    }
                }
                ModerationCause::Hidden(hidden) => {
                    if matches!(context, DecisionContext::ProfileList | DecisionContext::ContentList) {
                        ui.filters.push(cause.clone())
                    }
                    if !hidden.downgraded {
                        match ModerationBehavior::HIDE_BEHAVIOR.behavior_for(context) {
                            Some(BehaviorValue::Blur) => {
                                ui.blurs.push(cause.clone());
                            }
                            Some(BehaviorValue::Alert) => {
                                ui.alerts.push(cause.clone());
                            }
                            Some(BehaviorValue::Inform) => {
                                ui.informs.push(cause.clone());
                            }
                            _ => {}
                        }
                    }
                }
            }
        }
        ui.filters.sort_by_cached_key(|c| c.priority());
        ui.blurs.sort_by_cached_key(|c| c.priority());
        ui
    }
    /// Check if the decision is blocking or blocked by other user.
    pub fn is_blocked(&self) -> bool {
        self.causes.iter().any(|c| {
            matches!(
                c,
                ModerationCause::Blocking(_)
                    | ModerationCause::BlockedBy(_)
                    /* | ModerationCause::BlockOther(_) */
            )
        })
    }
    /// Check if the decision is by muted.
    pub fn is_muted(&self) -> bool {
        self.causes.iter().any(|c| matches!(c, ModerationCause::Muted(_)))
    }
    pub(crate) fn new() -> Self {
        Self { did: None, is_me: false, causes: Vec::new() }
    }
    pub(crate) fn merge(decisions: &[Self]) -> Self {
        assert!(!decisions.is_empty());
        // assert!(decisions
        //     .windows(2)
        //     .all(|w| w[0].did == w[1].did && w[0].is_me == w[1].is_me));
        Self {
            did: decisions[0].did.clone(),
            is_me: decisions[0].is_me,
            causes: decisions.iter().flat_map(|d| d.causes.iter().cloned()).collect(),
        }
    }
    pub(crate) fn set_did(&mut self, did: Did) {
        self.did = Some(did);
    }
    pub(crate) fn set_is_me(&mut self, is_me: bool) {
        self.is_me = is_me;
    }
    pub(crate) fn add_blocking(&mut self) {
        self.causes.push(ModerationCause::Blocking(Box::new(ModerationCauseOther {
            source: ModerationCauseSource::User,
            downgraded: false,
        })));
    }
    pub(crate) fn add_blocking_by_list(&mut self, list_view: &ListViewBasic) {
        self.causes.push(ModerationCause::Blocking(Box::new(ModerationCauseOther {
            source: ModerationCauseSource::List(Box::new(list_view.clone())),
            downgraded: false,
        })));
    }
    pub(crate) fn add_blocked_by(&mut self) {
        self.causes.push(ModerationCause::BlockedBy(Box::new(ModerationCauseOther {
            source: ModerationCauseSource::User,
            downgraded: false,
        })));
    }
    pub(crate) fn add_label(&mut self, target: LabelTarget, label: &Label, moderator: &Moderator) {
        let Some(label_def) = Self::lookup_label_def(label, moderator) else {
            return;
        };
        let is_self = Some(&label.src) == self.did.as_ref();
        let labeler = if is_self {
            None
        } else {
            moderator.prefs.labelers.iter().find(|l| l.did == label.src)
        };
        if !is_self && labeler.is_none() {
            return; // skip labelers not configured by the user
        }
        if is_self && label_def.flags.contains(&LabelValueDefinitionFlag::NoSelf) {
            return; // skip self-labels that arent supported
        }

        // establish the label preference for interpretation
        let mut label_pref = label_def.default_setting;
        if label_def.flags.contains(&LabelValueDefinitionFlag::Adult)
            && !moderator.prefs.adult_content_enabled
        {
            label_pref = LabelPreference::Hide;
        } else if let Some(pref) = labeler.and_then(|l| l.labels.get(&label_def.identifier)) {
            label_pref = *pref;
        } else if let Some(pref) = moderator.prefs.labels.get(&label_def.identifier) {
            label_pref = *pref;
        }

        // ignore labels the user has asked to ignore
        if label_pref == LabelPreference::Ignore {
            return;
        }

        // ignore 'unauthed' labels when the user is authed
        if label_def.flags.contains(&LabelValueDefinitionFlag::Unauthed)
            && moderator.user_did.is_some()
        {
            return;
        }

        let behavior = label_def.behaviors.behavior_for(target);
        // establish the priority of the label
        let severity = Self::measure_moderation_behavior_severity(&behavior);
        let priority = if label_def.flags.contains(&LabelValueDefinitionFlag::NoOverride)
            || (label_def.flags.contains(&LabelValueDefinitionFlag::Adult)
                && !moderator.prefs.adult_content_enabled)
        {
            Priority::Priority1
        } else if label_pref == LabelPreference::Hide {
            Priority::Priority2
        } else if severity == ModerationBehaviorSeverity::High {
            // blurring profile view or content view
            Priority::Priority5
        } else if severity == ModerationBehaviorSeverity::Medium {
            // blurring content list or content media
            Priority::Priority7
        } else {
            // blurring avatar, adding alerts
            Priority::Priority8
        };

        let no_override = label_def.flags.contains(&LabelValueDefinitionFlag::NoOverride)
            || (label_def.flags.contains(&LabelValueDefinitionFlag::Adult)
                && !moderator.prefs.adult_content_enabled);

        self.causes.push(ModerationCause::Label(Box::new(ModerationCauseLabel {
            source: if is_self || labeler.is_none() {
                ModerationCauseSource::User
            } else {
                ModerationCauseSource::Labeler(label.src.clone())
            },
            label: label.clone(),
            label_def,
            target,
            setting: label_pref,
            behavior,
            no_override,
            priority,
            downgraded: false,
        })));
    }
    pub(crate) fn add_muted(&mut self) {
        self.causes.push(ModerationCause::Muted(Box::new(ModerationCauseOther {
            source: ModerationCauseSource::User,
            downgraded: false,
        })));
    }
    pub(crate) fn add_muted_by_list(&mut self, list_view: &ListViewBasic) {
        self.causes.push(ModerationCause::Muted(Box::new(ModerationCauseOther {
            source: ModerationCauseSource::List(Box::new(list_view.clone())),
            downgraded: false,
        })));
    }
    pub(crate) fn add_muted_word(&mut self) {
        self.causes.push(ModerationCause::MuteWord(Box::new(ModerationCauseOther {
            source: ModerationCauseSource::User,
            downgraded: false,
        })));
    }
    pub(crate) fn add_hidden(&mut self) {
        self.causes.push(ModerationCause::Hidden(Box::new(ModerationCauseOther {
            source: ModerationCauseSource::User,
            downgraded: false,
        })));
    }
    pub(crate) fn downgrade(&mut self) {
        for cause in self.causes.iter_mut() {
            cause.downgrade()
        }
    }
    fn lookup_label_def(
        label: &Label,
        moderator: &Moderator,
    ) -> Option<InterpretedLabelValueDefinition> {
        if label.val.chars().all(|c| c.is_ascii_lowercase() || c == '-') {
            if let Some(def) = moderator
                .label_defs
                .get(&label.src)
                .and_then(|defs| defs.iter().find(|def| def.identifier == label.val))
            {
                return Some(def.clone());
            }
        }
        label.val.parse::<KnownLabelValue>().ok().map(|known_value| known_value.definition())
    }
    fn measure_moderation_behavior_severity(
        behavior: &ModerationBehavior,
    ) -> ModerationBehaviorSeverity {
        if behavior.profile_view == Some(ProfileViewBehavior::Blur)
            || behavior.content_view == Some(ContentViewBehavior::Blur)
        {
            return ModerationBehaviorSeverity::High;
        }
        if behavior.content_list == Some(ContentListBehavior::Blur)
            || behavior.content_media == Some(ContentMediaBehavior::Blur)
        {
            return ModerationBehaviorSeverity::Medium;
        }
        ModerationBehaviorSeverity::Low
    }
}