Remove contact notifications when cancelling a contact request
This commit is contained in:
parent
39e3ddb080
commit
83fb8d20b7
10 changed files with 224 additions and 87 deletions
|
@ -185,7 +185,11 @@ impl Database {
|
|||
///
|
||||
/// * `requester_id` - The user that initiates this request
|
||||
/// * `responder_id` - The user that will be removed
|
||||
pub async fn remove_contact(&self, requester_id: UserId, responder_id: UserId) -> Result<bool> {
|
||||
pub async fn remove_contact(
|
||||
&self,
|
||||
requester_id: UserId,
|
||||
responder_id: UserId,
|
||||
) -> Result<(bool, Option<NotificationId>)> {
|
||||
self.transaction(|tx| async move {
|
||||
let (id_a, id_b) = if responder_id < requester_id {
|
||||
(responder_id, requester_id)
|
||||
|
@ -204,7 +208,21 @@ impl Database {
|
|||
.ok_or_else(|| anyhow!("no such contact"))?;
|
||||
|
||||
contact::Entity::delete_by_id(contact.id).exec(&*tx).await?;
|
||||
Ok(contact.accepted)
|
||||
|
||||
let mut deleted_notification_id = None;
|
||||
if !contact.accepted {
|
||||
deleted_notification_id = self
|
||||
.delete_notification(
|
||||
responder_id,
|
||||
rpc::Notification::ContactRequest {
|
||||
actor_id: requester_id.to_proto(),
|
||||
},
|
||||
&*tx,
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
|
||||
Ok((contact.accepted, deleted_notification_id))
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
|
|
@ -3,12 +3,12 @@ use rpc::Notification;
|
|||
|
||||
impl Database {
|
||||
pub async fn initialize_notification_enum(&mut self) -> Result<()> {
|
||||
notification_kind::Entity::insert_many(Notification::all_kinds().iter().map(|kind| {
|
||||
notification_kind::ActiveModel {
|
||||
notification_kind::Entity::insert_many(Notification::all_variant_names().iter().map(
|
||||
|kind| notification_kind::ActiveModel {
|
||||
name: ActiveValue::Set(kind.to_string()),
|
||||
..Default::default()
|
||||
}
|
||||
}))
|
||||
},
|
||||
))
|
||||
.on_conflict(OnConflict::new().do_nothing().to_owned())
|
||||
.exec_without_returning(&self.pool)
|
||||
.await?;
|
||||
|
@ -19,6 +19,12 @@ impl Database {
|
|||
self.notification_kinds_by_name.insert(row.name, row.id);
|
||||
}
|
||||
|
||||
for name in Notification::all_variant_names() {
|
||||
if let Some(id) = self.notification_kinds_by_name.get(*name).copied() {
|
||||
self.notification_kinds_by_id.insert(id, name);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -46,6 +52,7 @@ impl Database {
|
|||
while let Some(row) = rows.next().await {
|
||||
let row = row?;
|
||||
let Some(kind) = self.notification_kinds_by_id.get(&row.kind) else {
|
||||
log::warn!("unknown notification kind {:?}", row.kind);
|
||||
continue;
|
||||
};
|
||||
result.push(proto::Notification {
|
||||
|
@ -96,4 +103,34 @@ impl Database {
|
|||
actor_id: notification.actor_id,
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn delete_notification(
|
||||
&self,
|
||||
recipient_id: UserId,
|
||||
notification: Notification,
|
||||
tx: &DatabaseTransaction,
|
||||
) -> Result<Option<NotificationId>> {
|
||||
let notification = notification.to_any();
|
||||
let kind = *self
|
||||
.notification_kinds_by_name
|
||||
.get(notification.kind.as_ref())
|
||||
.ok_or_else(|| anyhow!("invalid notification kind {:?}", notification.kind))?;
|
||||
let actor_id = notification.actor_id.map(|id| UserId::from_proto(id));
|
||||
let notification = notification::Entity::find()
|
||||
.filter(
|
||||
Condition::all()
|
||||
.add(notification::Column::RecipientId.eq(recipient_id))
|
||||
.add(notification::Column::Kind.eq(kind))
|
||||
.add(notification::Column::ActorId.eq(actor_id))
|
||||
.add(notification::Column::Content.eq(notification.content)),
|
||||
)
|
||||
.one(tx)
|
||||
.await?;
|
||||
if let Some(notification) = ¬ification {
|
||||
notification::Entity::delete_by_id(notification.id)
|
||||
.exec(tx)
|
||||
.await?;
|
||||
}
|
||||
Ok(notification.map(|notification| notification.id))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2177,7 +2177,8 @@ async fn remove_contact(
|
|||
let requester_id = session.user_id;
|
||||
let responder_id = UserId::from_proto(request.user_id);
|
||||
let db = session.db().await;
|
||||
let contact_accepted = db.remove_contact(requester_id, responder_id).await?;
|
||||
let (contact_accepted, deleted_notification_id) =
|
||||
db.remove_contact(requester_id, responder_id).await?;
|
||||
|
||||
let pool = session.connection_pool().await;
|
||||
// Update outgoing contact requests of requester
|
||||
|
@ -2204,6 +2205,14 @@ async fn remove_contact(
|
|||
}
|
||||
for connection_id in pool.user_connection_ids(responder_id) {
|
||||
session.peer.send(connection_id, update.clone())?;
|
||||
if let Some(notification_id) = deleted_notification_id {
|
||||
session.peer.send(
|
||||
connection_id,
|
||||
proto::DeleteNotification {
|
||||
notification_id: notification_id.to_proto(),
|
||||
},
|
||||
)?;
|
||||
}
|
||||
}
|
||||
|
||||
response.send(proto::Ack {})?;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue