| 1 | use chrono::{Duration, Utc}; |
| 2 | use store::AvatarRecord; |
| 3 | |
| 4 | use crate::engine::SyncEngine; |
| 5 | use crate::error::Result; |
| 6 | use crate::events::CoreEvent; |
| 7 | use crate::remote::Remote; |
| 8 | |
| 9 | const AVATAR_MAX_AGE_DAYS: i64 = 7; |
| 10 | const AVATAR_BATCH: usize = 60; |
| 11 | |
| 12 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 13 | pub struct Avatar { |
| 14 | pub bytes: Vec<u8>, |
| 15 | pub content_type: String, |
| 16 | } |
| 17 | |
| 18 | impl<R: Remote> SyncEngine<R> { |
| 19 | pub fn avatar(&self, user_id: &str) -> Option<Avatar> { |
| 20 | let record = self.store.avatar(user_id).ok().flatten()?; |
| 21 | Some(Avatar { |
| 22 | bytes: record.bytes?, |
| 23 | content_type: record.content_type, |
| 24 | }) |
| 25 | } |
| 26 | |
| 27 | pub async fn fetch_avatars(&self, user_ids: &[String]) -> Result<()> { |
| 28 | let cutoff = Utc::now() - Duration::days(AVATAR_MAX_AGE_DAYS); |
| 29 | let mut unique = user_ids.to_vec(); |
| 30 | unique.sort(); |
| 31 | unique.dedup(); |
| 32 | let wanted = self.store.avatar_ids_needing_fetch(&unique, cutoff)?; |
| 33 | let claimed = self.claim_avatar_ids(wanted); |
| 34 | let outcome = self.fetch_claimed_avatars(&claimed).await; |
| 35 | self.release_avatar_ids(&claimed); |
| 36 | outcome |
| 37 | } |
| 38 | |
| 39 | async fn fetch_claimed_avatars(&self, claimed: &[String]) -> Result<()> { |
| 40 | let mut first_error = None; |
| 41 | let mut fetched_any = false; |
| 42 | for chunk in claimed.chunks(AVATAR_BATCH) { |
| 43 | let photos = match self.remote.user_photos(chunk).await { |
| 44 | Ok(photos) => photos, |
| 45 | Err(error) => { |
| 46 | first_error.get_or_insert(error); |
| 47 | continue; |
| 48 | } |
| 49 | }; |
| 50 | let mut changed = Vec::new(); |
| 51 | for (user_id, photo) in chunk.iter().zip(photos) { |
| 52 | let (bytes, content_type) = match photo { |
| 53 | Ok(Some(photo)) => (Some(photo.bytes), photo.content_type), |
| 54 | Ok(None) => (None, String::new()), |
| 55 | Err(error) => { |
| 56 | first_error.get_or_insert(error); |
| 57 | continue; |
| 58 | } |
| 59 | }; |
| 60 | fetched_any = true; |
| 61 | if bytes.is_some() { |
| 62 | changed.push(user_id.clone()); |
| 63 | } |
| 64 | self.store.upsert_avatar(&AvatarRecord { |
| 65 | user_id: user_id.clone(), |
| 66 | bytes, |
| 67 | content_type, |
| 68 | fetched_at: Utc::now(), |
| 69 | })?; |
| 70 | } |
| 71 | if !changed.is_empty() { |
| 72 | let _ = self |
| 73 | .events |
| 74 | .send(CoreEvent::AvatarsChanged { user_ids: changed }); |
| 75 | } |
| 76 | } |
| 77 | match first_error { |
| 78 | Some(error) if !fetched_any => Err(error), |
| 79 | Some(error) => { |
| 80 | self.report(format!("some avatars could not be fetched: {error}")); |
| 81 | Ok(()) |
| 82 | } |
| 83 | None => Ok(()), |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | fn claim_avatar_ids(&self, wanted: Vec<String>) -> Vec<String> { |
| 88 | let mut in_flight = self |
| 89 | .avatars_in_flight |
| 90 | .lock() |
| 91 | .unwrap_or_else(|poisoned| poisoned.into_inner()); |
| 92 | wanted |
| 93 | .into_iter() |
| 94 | .filter(|user_id| in_flight.insert(user_id.clone())) |
| 95 | .collect() |
| 96 | } |
| 97 | |
| 98 | fn release_avatar_ids(&self, claimed: &[String]) { |
| 99 | let mut in_flight = self |
| 100 | .avatars_in_flight |
| 101 | .lock() |
| 102 | .unwrap_or_else(|poisoned| poisoned.into_inner()); |
| 103 | for user_id in claimed { |
| 104 | in_flight.remove(user_id); |
| 105 | } |
| 106 | } |
| 107 | } |