barbed-ffz 0.0.3

FrankerFaceZ provider helpers for the barbed workspace
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
use std::collections::HashMap;

use async_trait::async_trait;
use barbed_core::emotes::Emote;
#[cfg(any(test, feature = "reqwest-client"))]
use barbed_core::emotes::{
    EmoteId, EmoteImage, EmoteImageFormat, EmoteImageScale, EmoteModifier, EmoteModifierFlags,
    EmoteProvider, EmoteThemeMode,
};
#[cfg(any(test, feature = "reqwest-client"))]
use serde::Deserialize;
use thiserror::Error;

#[cfg(feature = "reqwest-client")]
const API_BASE: &str = "https://api.frankerfacez.com/v1";

#[derive(Debug, Error)]
pub enum FfzError {
    #[error("FFZ response failed to decode: {0}")]
    Json(#[from] serde_json::Error),
    #[cfg(feature = "reqwest-client")]
    #[error("FFZ request failed: {0}")]
    Http(#[from] reqwest::Error),
    #[error("FFZ global emote sets were not seeded in the in-memory client")]
    MissingGlobal,
    #[error("FFZ room `{0}` was not seeded in the in-memory client")]
    MissingRoom(String),
    #[error("FFZ room response missing room payload")]
    MissingRoomPayload,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FfzEmoteSet {
    pub id: String,
    pub title: Option<String>,
    pub emotes: Vec<Emote>,
}

impl FfzEmoteSet {
    pub fn is_empty(&self) -> bool {
        self.emotes.is_empty()
    }
}

#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct FfzGlobalEmoteSets {
    sets: HashMap<String, FfzEmoteSet>,
    default_set_ids: Vec<String>,
    user_scoped: HashMap<String, Vec<String>>,
}

impl FfzGlobalEmoteSets {
    pub fn is_empty(&self) -> bool {
        self.default_set_ids.is_empty() && self.user_scoped.is_empty()
    }

    pub fn default_sets(&self) -> Vec<FfzEmoteSet> {
        self.default_set_ids
            .iter()
            .filter_map(|id| self.sets.get(id).cloned())
            .collect()
    }

    pub fn sets_for_user(&self, user_id: &str) -> Vec<FfzEmoteSet> {
        self.user_scoped
            .iter()
            .filter_map(|(set_id, users)| {
                if users.iter().any(|candidate| candidate == user_id) {
                    self.sets.get(set_id).cloned()
                } else {
                    None
                }
            })
            .collect()
    }

    pub fn user_scoped_summary(&self) -> Vec<(FfzEmoteSet, usize)> {
        self.user_scoped
            .iter()
            .filter_map(|(set_id, users)| {
                self.sets.get(set_id).cloned().map(|set| (set, users.len()))
            })
            .collect()
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FfzRoomEmoteSets {
    pub room_id: String,
    pub twitch_id: String,
    pub sets: Vec<FfzEmoteSet>,
}

impl FfzRoomEmoteSets {
    pub fn is_empty(&self) -> bool {
        self.sets.is_empty()
    }
}

#[async_trait]
pub trait FfzApi: Send + Sync {
    async fn global_emote_sets(&self) -> Result<FfzGlobalEmoteSets, FfzError>;

    async fn room_emote_sets_by_twitch_id(
        &self,
        twitch_id: &str,
    ) -> Result<FfzRoomEmoteSets, FfzError>;
}

#[derive(Clone, Default)]
pub struct InMemoryFfzApi {
    global: Option<FfzGlobalEmoteSets>,
    rooms: HashMap<String, FfzRoomEmoteSets>,
}

impl InMemoryFfzApi {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn with_global(mut self, sets: FfzGlobalEmoteSets) -> Self {
        self.global = Some(sets);
        self
    }

    pub fn insert_room(&mut self, twitch_id: impl Into<String>, room: FfzRoomEmoteSets) {
        self.rooms.insert(twitch_id.into(), room);
    }
}

#[async_trait]
impl FfzApi for InMemoryFfzApi {
    async fn global_emote_sets(&self) -> Result<FfzGlobalEmoteSets, FfzError> {
        self.global.clone().ok_or(FfzError::MissingGlobal)
    }

    async fn room_emote_sets_by_twitch_id(
        &self,
        twitch_id: &str,
    ) -> Result<FfzRoomEmoteSets, FfzError> {
        self.rooms
            .get(twitch_id)
            .cloned()
            .ok_or_else(|| FfzError::MissingRoom(twitch_id.to_string()))
    }
}

#[cfg(feature = "reqwest-client")]
#[derive(Clone)]
pub struct FfzClient {
    http: reqwest::Client,
}

#[cfg(feature = "reqwest-client")]
impl FfzClient {
    pub fn new() -> Result<Self, FfzError> {
        Ok(Self {
            http: reqwest::Client::builder()
                .user_agent("barbed/0.0.2")
                .build()?,
        })
    }
}

#[cfg(feature = "reqwest-client")]
#[async_trait]
impl FfzApi for FfzClient {
    async fn global_emote_sets(&self) -> Result<FfzGlobalEmoteSets, FfzError> {
        let body = self
            .http
            .get(format!("{API_BASE}/set/global"))
            .send()
            .await?
            .error_for_status()?
            .text()
            .await?;
        parse_global_sets_json(&body)
    }

    async fn room_emote_sets_by_twitch_id(
        &self,
        twitch_id: &str,
    ) -> Result<FfzRoomEmoteSets, FfzError> {
        let body = self
            .http
            .get(format!("{API_BASE}/room/id/{twitch_id}"))
            .send()
            .await?
            .error_for_status()?
            .text()
            .await?;
        parse_room_sets_json(&body)
    }
}

#[cfg(any(test, feature = "reqwest-client"))]
fn parse_global_sets_json(body: &str) -> Result<FfzGlobalEmoteSets, FfzError> {
    let response: GlobalSetsResponse = serde_json::from_str(body)?;
    let sets = response
        .sets
        .into_iter()
        .map(|(id, model)| (id, emote_set_from_model(model)))
        .collect();
    Ok(FfzGlobalEmoteSets {
        sets,
        default_set_ids: response
            .default_sets
            .into_iter()
            .map(|id| id.to_string())
            .collect(),
        user_scoped: response.users,
    })
}

#[cfg(any(test, feature = "reqwest-client"))]
fn parse_room_sets_json(body: &str) -> Result<FfzRoomEmoteSets, FfzError> {
    let response: RoomResponse = serde_json::from_str(body)?;
    let room = response.room.ok_or(FfzError::MissingRoomPayload)?;
    Ok(FfzRoomEmoteSets {
        room_id: room.id,
        twitch_id: room.twitch_id.to_string(),
        sets: response
            .sets
            .into_values()
            .map(emote_set_from_model)
            .collect(),
    })
}

#[derive(Deserialize)]
#[cfg(any(test, feature = "reqwest-client"))]
struct GlobalSetsResponse {
    #[serde(default)]
    default_sets: Vec<u64>,
    #[serde(default)]
    sets: HashMap<String, EmoteSetModel>,
    #[serde(default)]
    users: HashMap<String, Vec<String>>,
}

#[derive(Deserialize)]
#[cfg(any(test, feature = "reqwest-client"))]
struct RoomResponse {
    room: Option<RoomModel>,
    #[serde(default)]
    sets: HashMap<String, EmoteSetModel>,
}

#[derive(Deserialize)]
#[cfg(any(test, feature = "reqwest-client"))]
struct RoomModel {
    id: String,
    #[serde(rename = "twitch_id")]
    twitch_id: u64,
}

#[derive(Deserialize)]
#[cfg(any(test, feature = "reqwest-client"))]
struct EmoteSetModel {
    id: u64,
    title: Option<String>,
    #[serde(default)]
    emoticons: Vec<EmoteModel>,
}

#[derive(Deserialize)]
#[cfg(any(test, feature = "reqwest-client"))]
struct EmoteModel {
    id: u64,
    name: String,
    #[serde(default)]
    hidden: bool,
    #[serde(default)]
    modifier: bool,
    #[serde(default)]
    modifier_flags: u32,
    #[serde(default)]
    urls: HashMap<String, Option<String>>,
    animated: Option<HashMap<String, Option<String>>>,
    mask: Option<HashMap<String, Option<String>>>,
    mask_animated: Option<HashMap<String, Option<String>>>,
}

#[cfg(any(test, feature = "reqwest-client"))]
fn emote_set_from_model(model: EmoteSetModel) -> FfzEmoteSet {
    FfzEmoteSet {
        id: model.id.to_string(),
        title: model.title,
        emotes: model.emoticons.into_iter().map(emote_from_model).collect(),
    }
}

#[cfg(any(test, feature = "reqwest-client"))]
fn emote_from_model(model: EmoteModel) -> Emote {
    let mut images = image_variants(&model.urls, EmoteImageFormat::Static);
    if let Some(animated) = &model.animated {
        images.extend(image_variants(animated, EmoteImageFormat::Animated));
    }
    let is_animated = images
        .iter()
        .any(|image| matches!(image.format, EmoteImageFormat::Animated));

    let mut emote = Emote::new(
        EmoteId::new(EmoteProvider::FrankerFaceZ, model.id.to_string()),
        model.name,
        is_animated,
        images,
    );

    if model.modifier {
        let flags = EmoteModifierFlags::from_bits_retain(model.modifier_flags);
        let mut mask_images = Vec::new();
        if let Some(mask) = &model.mask {
            mask_images.extend(image_variants(mask, EmoteImageFormat::Static));
        }
        if let Some(mask) = &model.mask_animated {
            mask_images.extend(image_variants(mask, EmoteImageFormat::Animated));
        }
        emote = emote.with_modifier(EmoteModifier {
            flags,
            raw_flags: model.modifier_flags,
            is_hidden: model.hidden || flags.contains(EmoteModifierFlags::HIDDEN),
            mask_images,
        });
    }

    emote
}

#[cfg(any(test, feature = "reqwest-client"))]
fn image_variants(
    images: &HashMap<String, Option<String>>,
    format: EmoteImageFormat,
) -> Vec<EmoteImage> {
    images
        .iter()
        .filter_map(|(scale_key, url)| {
            let url = url.clone()?;
            Some(EmoteImage {
                format: format.clone(),
                theme_mode: EmoteThemeMode::Light,
                scale: parse_scale(scale_key),
                url,
            })
        })
        .collect()
}

#[cfg(any(test, feature = "reqwest-client"))]
fn parse_scale(value: &str) -> EmoteImageScale {
    match value {
        "1" | "1.0" => EmoteImageScale::One,
        "2" | "2.0" => EmoteImageScale::Two,
        "3" | "3.0" => EmoteImageScale::Three,
        other => EmoteImageScale::Other(other.to_string()),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn global_fixture_preserves_default_and_user_scoped_sets() {
        let sets = parse_global_sets_json(include_str!("../tests/fixtures/global_sets.json"))
            .expect("global fixture should parse");
        assert_eq!(sets.default_sets().len(), 1);
        assert_eq!(sets.sets_for_user("42").len(), 1);
        assert_eq!(sets.user_scoped_summary().len(), 1);
    }

    #[test]
    fn room_fixture_preserves_modifier_metadata() {
        let room = parse_room_sets_json(include_str!("../tests/fixtures/room_sets.json"))
            .expect("room fixture should parse");
        let modifier = room.sets[0].emotes[0]
            .modifier()
            .expect("modifier metadata should exist");
        assert!(modifier.is_hidden);
        assert!(modifier.flags.contains(EmoteModifierFlags::RAINBOW));
    }

    #[tokio::test(flavor = "current_thread")]
    async fn in_memory_api_returns_seeded_global_and_room_sets() {
        let mut api = InMemoryFfzApi::new().with_global(
            parse_global_sets_json(include_str!("../tests/fixtures/global_sets.json"))
                .expect("global fixture should parse"),
        );
        api.insert_room(
            "42",
            parse_room_sets_json(include_str!("../tests/fixtures/room_sets.json"))
                .expect("room fixture should parse"),
        );

        assert_eq!(
            api.global_emote_sets()
                .await
                .expect("global sets should exist")
                .default_sets()
                .len(),
            1
        );
        assert_eq!(
            api.room_emote_sets_by_twitch_id("42")
                .await
                .expect("room sets should exist")
                .sets
                .len(),
            1
        );
    }
}