magnetar/src/model/processing/emoji.rs

52 lines
1.6 KiB
Rust

use crate::model::processing::PackResult;
use crate::model::{PackType, PackingContext};
use itertools::Itertools;
use magnetar_calckey_model::ck;
use magnetar_calckey_model::emoji::EmojiTag;
use magnetar_sdk::types::emoji::{EmojiBase, PackEmojiBase};
use magnetar_sdk::types::Id;
use magnetar_sdk::{Packed, Required};
pub struct EmojiModel;
impl EmojiModel {
pub fn pack_existing(&self, ctx: &PackingContext, emoji: &ck::emoji::Model) -> PackEmojiBase {
PackEmojiBase::pack_from((
Required(Id::from(&emoji.id)),
Required(EmojiBase::extract(ctx, &emoji)),
))
}
pub async fn fetch_many_emojis(
&self,
ctx: &PackingContext,
shortcodes: &[String],
host: Option<&str>,
) -> PackResult<Vec<PackEmojiBase>> {
let emojis = ctx.service.emoji_cache.get_many(shortcodes, host).await?;
let packed_emojis = emojis.iter().map(|e| self.pack_existing(ctx, &e)).collect();
Ok(packed_emojis)
}
pub async fn fetch_many_tag_emojis(
&self,
ctx: &PackingContext,
tags: &[EmojiTag<'_>],
) -> PackResult<Vec<PackEmojiBase>> {
let emojis = ctx.service.emoji_cache.get_many_tagged(tags).await?;
let packed_emojis = emojis.iter().map(|e| self.pack_existing(ctx, &e)).collect();
Ok(packed_emojis)
}
pub fn deduplicate_emoji(&self, ctx: &PackingContext, emoji_list: Vec<String>) -> Vec<String> {
emoji_list
.into_iter()
.sorted()
.dedup()
.take(ctx.limits.max_emojis)
.collect::<Vec<_>>()
}
}