From 0a64034ea974fc3651a85f3925b300d3ed9394a1 Mon Sep 17 00:00:00 2001 From: asonix Date: Thu, 14 Jan 2021 23:50:26 -0600 Subject: [PATCH] Profiles: Expose notifications for profile - Expose notification counts for profile - Expose Accept & Reject Follow Request constructors - Don't error on missing activitypub for RejectFollowRequest --- profiles/src/apub/actions/follow_request.rs | 26 +++++++++--------- profiles/src/apub/actions/mod.rs | 16 +++++++++++ profiles/src/store/mod.rs | 2 +- profiles/src/store/view/notif.rs | 30 +++++++++++++++++++-- 4 files changed, 58 insertions(+), 16 deletions(-) diff --git a/profiles/src/apub/actions/follow_request.rs b/profiles/src/apub/actions/follow_request.rs index 140784f..012cb35 100644 --- a/profiles/src/apub/actions/follow_request.rs +++ b/profiles/src/apub/actions/follow_request.rs @@ -87,25 +87,25 @@ impl Action for RejectFollowRequest { .remove(self.follow_request_id)?; if let Some(undo_follow_request) = opt { - let follow_apub_id = ctx - .apub - .apub_for_follow_request(self.follow_request_id)? - .req()?; - ctx.apub.delete_object(&follow_apub_id)?; - ctx.store .view .follow_request_notifs .remove(undo_follow_request.0.id); - if ctx.is_local(undo_follow_request.0.left)? - && !ctx.is_local(undo_follow_request.0.right)? + if let Some(follow_apub_id) = + ctx.apub.apub_for_follow_request(self.follow_request_id)? { - return Ok(Some(Box::new(crate::apub::results::RejectFollow { - follow_apub_id, - profile_id: undo_follow_request.0.left, - requester_id: undo_follow_request.0.right, - }))); + ctx.apub.delete_object(&follow_apub_id)?; + + if ctx.is_local(undo_follow_request.0.left)? + && !ctx.is_local(undo_follow_request.0.right)? + { + return Ok(Some(Box::new(crate::apub::results::RejectFollow { + follow_apub_id, + profile_id: undo_follow_request.0.left, + requester_id: undo_follow_request.0.right, + }))); + } } } diff --git a/profiles/src/apub/actions/mod.rs b/profiles/src/apub/actions/mod.rs index 553f2d4..8d63292 100644 --- a/profiles/src/apub/actions/mod.rs +++ b/profiles/src/apub/actions/mod.rs @@ -394,10 +394,26 @@ pub struct AcceptFollowRequest { published: DateTime, } +impl AcceptFollowRequest { + pub fn from_id(follow_request_id: Uuid) -> Self { + AcceptFollowRequest { + accept_apub_id: None, + follow_request_id, + published: Utc::now(), + } + } +} + pub struct RejectFollowRequest { follow_request_id: Uuid, } +impl RejectFollowRequest { + pub fn from_id(follow_request_id: Uuid) -> Self { + RejectFollowRequest { follow_request_id } + } +} + pub struct UndoFollowRequest { follow_request_id: Uuid, } diff --git a/profiles/src/store/mod.rs b/profiles/src/store/mod.rs index 7ff1980..7811fd9 100644 --- a/profiles/src/store/mod.rs +++ b/profiles/src/store/mod.rs @@ -750,7 +750,7 @@ fn count( let count = match tree.get(key.as_bytes())? { Some(ivec) => { let s = String::from_utf8_lossy(&ivec); - let count: u64 = s.parse().expect("Count is valid"); + let count: u64 = s.parse().unwrap_or(0); count } None => 0, diff --git a/profiles/src/store/view/notif.rs b/profiles/src/store/view/notif.rs index 4e37791..555c6dd 100644 --- a/profiles/src/store/view/notif.rs +++ b/profiles/src/store/view/notif.rs @@ -134,7 +134,17 @@ impl NotificationStore { } } - pub fn remove(&self, notification_id: Uuid) { + pub fn count(&self, profile_id: Uuid) -> Result { + let keys = self.keys(); + if let Some(count) = self.counts.get(keys.counts_key(profile_id))? { + let count: u64 = String::from_utf8_lossy(&count).parse().unwrap_or(0); + Ok(count) + } else { + Ok(0) + } + } + + pub(crate) fn remove(&self, notification_id: Uuid) { for profile_id in self.profiles(notification_id) { let keys = self.keys(); @@ -283,6 +293,16 @@ impl NotificationStore { Ok(()) } + pub fn for_profile(&self, profile_id: Uuid) -> impl DoubleEndedIterator { + let keys = self.keys(); + + self.notifications + .scan_prefix(keys.notification_prefix(profile_id)) + .values() + .filter_map(|res| res.ok()) + .filter_map(uuid_from_ivec) + } + fn notification_keys(&self, profile_id: Uuid) -> impl DoubleEndedIterator { let keys = self.keys(); @@ -308,6 +328,12 @@ impl NotificationStore { .scan_prefix(keys.profile_prefix(notification_id)) .values() .filter_map(|res| res.ok()) - .filter_map(|ivec| Uuid::from_slice(&ivec).ok()) + .filter_map(uuid_from_ivec) } } + +fn uuid_from_ivec(ivec: sled::IVec) -> Option { + Uuid::from_slice(&ivec) + .map_err(|e| log::warn!("Failed to parse ivec {}", e)) + .ok() +}