Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 71 additions & 45 deletions crates/chatd/src/adapters/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1517,6 +1517,16 @@ impl Account {
fn live(&self) -> MutexGuard<'_, Live> {
self.live.lock().unwrap_or_else(|poisoned| poisoned.into_inner())
}

/// The picker's sticker packs, read from this account's file the first time they are asked for.
fn sticker_packs(&self) -> Vec<wire::StickerPackInfo> {
let mut live = self.live();
if !live.stickers_loaded {
live.sticker_packs = read_sticker_packs(&live.stickers_path);
live.stickers_loaded = true;
}
live.sticker_packs.clone()
}
}

pub fn start(ctx: Context, session: &str, inbox: Receiver<Command>) -> Result<(), String> {
Expand Down Expand Up @@ -2671,43 +2681,7 @@ fn apply(account: &Account, event: Event) {
write_sticker_packs(&live.stickers_path, &live.sticker_packs);
account.ctx.hub.catalogs.forget_matching(|(acct, kind, ..)| *acct == account.ctx.account && *kind == CatalogKind::Stickers);
}
Event::StickerPackOp { pack_id, pack_key, install } => {
if install {
let mut pack = account
.wire
.preview_sticker_pack(&pack_id, &pack_key)
.unwrap_or_else(|_| wire::StickerPackInfo {
id: pack_id.clone(),
key: pack_key.clone(),
title: "Sticker pack".to_owned(),
author: String::new(),
cover_id: Some(0),
stickers: Vec::new(),
installed: true,
});
pack.installed = true;
let mut live = account.live();
if !live.stickers_loaded {
live.sticker_packs = read_sticker_packs(&live.stickers_path);
live.stickers_loaded = true;
}
if let Some(existing) = live.sticker_packs.iter_mut().find(|p| p.id == pack.id) {
*existing = pack;
} else if live.sticker_packs.len() < 128 {
live.sticker_packs.push(pack);
}
write_sticker_packs(&live.stickers_path, &live.sticker_packs);
} else {
let mut live = account.live();
if !live.stickers_loaded {
live.sticker_packs = read_sticker_packs(&live.stickers_path);
live.stickers_loaded = true;
}
live.sticker_packs.retain(|p| p.id != pack_id);
write_sticker_packs(&live.stickers_path, &live.sticker_packs);
}
account.ctx.hub.catalogs.forget_matching(|(acct, kind, ..)| *acct == account.ctx.account && *kind == CatalogKind::Stickers);
}
Event::StickerPackOp { pack_id, pack_key, install } => apply_sticker_pack_op(account, &pack_id, &pack_key, install),
Event::Story(story) => apply_story(account, story),
Event::StoryReaction { by, story_author, story_sent, emoji, removed, .. } => {
if account.live().stories_disabled {
Expand Down Expand Up @@ -2980,6 +2954,65 @@ fn apply_storage_sync(account: &Account, snapshot: wire::StorageSnapshot) {
if disabled_stories {
let _ = ctx.forget_conversation(STORIES_CONVERSATION);
}
apply_storage_sticker_packs(account, snapshot.sticker_packs);
}

/// Puts a pack another device installed into the picker's list, or takes a removed one out. The
/// pack's manifest is fetched as for any install, so the list holds its title and stickers.
fn apply_sticker_pack_op(account: &Account, pack_id: &str, pack_key: &str, install: bool) {
if install {
// Asked of the wire before the list is touched, so no lock is held on the way to Signal.
let mut pack = account
.wire
.preview_sticker_pack(pack_id, pack_key)
.unwrap_or_else(|_| wire::StickerPackInfo {
id: pack_id.to_owned(),
key: pack_key.to_owned(),
title: "Sticker pack".to_owned(),
author: String::new(),
cover_id: Some(0),
stickers: Vec::new(),
installed: true,
});
pack.installed = true;
let mut live = account.live();
if !live.stickers_loaded {
live.sticker_packs = read_sticker_packs(&live.stickers_path);
live.stickers_loaded = true;
}
if let Some(existing) = live.sticker_packs.iter_mut().find(|p| p.id == pack.id) {
*existing = pack;
} else if live.sticker_packs.len() < 128 {
live.sticker_packs.push(pack);
}
write_sticker_packs(&live.stickers_path, &live.sticker_packs);
} else {
let mut live = account.live();
if !live.stickers_loaded {
live.sticker_packs = read_sticker_packs(&live.stickers_path);
live.stickers_loaded = true;
}
live.sticker_packs.retain(|p| p.id != pack_id);
write_sticker_packs(&live.stickers_path, &live.sticker_packs);
}
account.ctx.hub.catalogs.forget_matching(|(acct, kind, ..)| *acct == account.ctx.account && *kind == CatalogKind::Stickers);
}

/// Applies the sticker pack records of a Storage Service snapshot, which is read on every storage
/// change: a pack this device already holds, stickers and all, is not fetched from Signal again,
/// and a removal is always carried out.
fn apply_storage_sticker_packs(account: &Account, packs: Vec<wire::StorageStickerPack>) {
let held: Vec<String> = account
.sticker_packs()
.into_iter()
.filter(|pack| pack.installed && !pack.stickers.is_empty())
.map(|pack| pack.id)
.collect();
for pack in packs {
if pack.removed || !held.contains(&pack.id) {
apply_sticker_pack_op(account, &pack.id, &pack.key, !pack.removed);
}
}
}

fn ensure_stories_row(account: &Account) {
Expand Down Expand Up @@ -5830,14 +5863,7 @@ fn pin_message(account: &Account, conversation_id: &str, message_id: &str, pinne
fn catalog(account: &Account, kind: CatalogKind, query: &str, cursor: Option<&str>) -> Result<CatalogPage, Refusal> {
if kind == CatalogKind::Stickers {
let q = query.trim().to_ascii_lowercase();
let packs = {
let mut live = account.live();
if !live.stickers_loaded {
live.sticker_packs = read_sticker_packs(&live.stickers_path);
live.stickers_loaded = true;
}
live.sticker_packs.clone()
};
let packs = account.sticker_packs();
let mut items = Vec::new();
let mut media = Vec::new();
{
Expand Down
Loading
Loading