Profiles: Expose notifications for profile

- Expose notification counts for profile
- Expose Accept & Reject Follow Request constructors
- Don't error on missing activitypub for RejectFollowRequest
This commit is contained in:
asonix 2021-01-14 23:50:26 -06:00
parent f350d718ac
commit 0a64034ea9
4 changed files with 58 additions and 16 deletions

View file

@ -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,
})));
}
}
}

View file

@ -394,10 +394,26 @@ pub struct AcceptFollowRequest {
published: DateTime<Utc>,
}
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,
}

View file

@ -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,

View file

@ -134,7 +134,17 @@ impl NotificationStore {
}
}
pub fn remove(&self, notification_id: Uuid) {
pub fn count(&self, profile_id: Uuid) -> Result<u64, StoreError> {
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<Item = Uuid> {
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<Item = sled::IVec> {
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> {
Uuid::from_slice(&ivec)
.map_err(|e| log::warn!("Failed to parse ivec {}", e))
.ok()
}