Display invite response buttons inline in notification panel
This commit is contained in:
parent
c66385f0f9
commit
f225039d36
19 changed files with 421 additions and 169 deletions
|
@ -161,7 +161,7 @@ impl Database {
|
|||
invitee_id: UserId,
|
||||
inviter_id: UserId,
|
||||
is_admin: bool,
|
||||
) -> Result<Option<proto::Notification>> {
|
||||
) -> Result<NotificationBatch> {
|
||||
self.transaction(move |tx| async move {
|
||||
self.check_user_is_channel_admin(channel_id, inviter_id, &*tx)
|
||||
.await?;
|
||||
|
@ -176,16 +176,18 @@ impl Database {
|
|||
.insert(&*tx)
|
||||
.await?;
|
||||
|
||||
self.create_notification(
|
||||
invitee_id,
|
||||
rpc::Notification::ChannelInvitation {
|
||||
actor_id: inviter_id.to_proto(),
|
||||
channel_id: channel_id.to_proto(),
|
||||
},
|
||||
true,
|
||||
&*tx,
|
||||
)
|
||||
.await
|
||||
Ok(self
|
||||
.create_notification(
|
||||
invitee_id,
|
||||
rpc::Notification::ChannelInvitation {
|
||||
channel_id: channel_id.to_proto(),
|
||||
},
|
||||
true,
|
||||
&*tx,
|
||||
)
|
||||
.await?
|
||||
.into_iter()
|
||||
.collect())
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
@ -228,7 +230,7 @@ impl Database {
|
|||
channel_id: ChannelId,
|
||||
user_id: UserId,
|
||||
accept: bool,
|
||||
) -> Result<()> {
|
||||
) -> Result<NotificationBatch> {
|
||||
self.transaction(move |tx| async move {
|
||||
let rows_affected = if accept {
|
||||
channel_member::Entity::update_many()
|
||||
|
@ -246,21 +248,34 @@ impl Database {
|
|||
.await?
|
||||
.rows_affected
|
||||
} else {
|
||||
channel_member::ActiveModel {
|
||||
channel_id: ActiveValue::Unchanged(channel_id),
|
||||
user_id: ActiveValue::Unchanged(user_id),
|
||||
..Default::default()
|
||||
}
|
||||
.delete(&*tx)
|
||||
.await?
|
||||
.rows_affected
|
||||
channel_member::Entity::delete_many()
|
||||
.filter(
|
||||
channel_member::Column::ChannelId
|
||||
.eq(channel_id)
|
||||
.and(channel_member::Column::UserId.eq(user_id))
|
||||
.and(channel_member::Column::Accepted.eq(false)),
|
||||
)
|
||||
.exec(&*tx)
|
||||
.await?
|
||||
.rows_affected
|
||||
};
|
||||
|
||||
if rows_affected == 0 {
|
||||
Err(anyhow!("no such invitation"))?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
Ok(self
|
||||
.respond_to_notification(
|
||||
user_id,
|
||||
&rpc::Notification::ChannelInvitation {
|
||||
channel_id: channel_id.to_proto(),
|
||||
},
|
||||
accept,
|
||||
&*tx,
|
||||
)
|
||||
.await?
|
||||
.into_iter()
|
||||
.collect())
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
|
|
@ -123,7 +123,7 @@ impl Database {
|
|||
&self,
|
||||
sender_id: UserId,
|
||||
receiver_id: UserId,
|
||||
) -> Result<Option<proto::Notification>> {
|
||||
) -> Result<NotificationBatch> {
|
||||
self.transaction(|tx| async move {
|
||||
let (id_a, id_b, a_to_b) = if sender_id < receiver_id {
|
||||
(sender_id, receiver_id, true)
|
||||
|
@ -164,15 +164,18 @@ impl Database {
|
|||
Err(anyhow!("contact already requested"))?;
|
||||
}
|
||||
|
||||
self.create_notification(
|
||||
receiver_id,
|
||||
rpc::Notification::ContactRequest {
|
||||
actor_id: sender_id.to_proto(),
|
||||
},
|
||||
true,
|
||||
&*tx,
|
||||
)
|
||||
.await
|
||||
Ok(self
|
||||
.create_notification(
|
||||
receiver_id,
|
||||
rpc::Notification::ContactRequest {
|
||||
actor_id: sender_id.to_proto(),
|
||||
},
|
||||
true,
|
||||
&*tx,
|
||||
)
|
||||
.await?
|
||||
.into_iter()
|
||||
.collect())
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
@ -274,7 +277,7 @@ impl Database {
|
|||
responder_id: UserId,
|
||||
requester_id: UserId,
|
||||
accept: bool,
|
||||
) -> Result<Option<proto::Notification>> {
|
||||
) -> Result<NotificationBatch> {
|
||||
self.transaction(|tx| async move {
|
||||
let (id_a, id_b, a_to_b) = if responder_id < requester_id {
|
||||
(responder_id, requester_id, false)
|
||||
|
@ -316,15 +319,34 @@ impl Database {
|
|||
Err(anyhow!("no such contact request"))?
|
||||
}
|
||||
|
||||
self.create_notification(
|
||||
requester_id,
|
||||
rpc::Notification::ContactRequestAccepted {
|
||||
actor_id: responder_id.to_proto(),
|
||||
},
|
||||
true,
|
||||
&*tx,
|
||||
)
|
||||
.await
|
||||
let mut notifications = Vec::new();
|
||||
notifications.extend(
|
||||
self.respond_to_notification(
|
||||
responder_id,
|
||||
&rpc::Notification::ContactRequest {
|
||||
actor_id: requester_id.to_proto(),
|
||||
},
|
||||
accept,
|
||||
&*tx,
|
||||
)
|
||||
.await?,
|
||||
);
|
||||
|
||||
if accept {
|
||||
notifications.extend(
|
||||
self.create_notification(
|
||||
requester_id,
|
||||
rpc::Notification::ContactRequestAccepted {
|
||||
actor_id: responder_id.to_proto(),
|
||||
},
|
||||
true,
|
||||
&*tx,
|
||||
)
|
||||
.await?,
|
||||
);
|
||||
}
|
||||
|
||||
Ok(notifications)
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
|
|
@ -52,7 +52,7 @@ impl Database {
|
|||
while let Some(row) = rows.next().await {
|
||||
let row = row?;
|
||||
let kind = row.kind;
|
||||
if let Some(proto) = self.model_to_proto(row) {
|
||||
if let Some(proto) = model_to_proto(self, row) {
|
||||
result.push(proto);
|
||||
} else {
|
||||
log::warn!("unknown notification kind {:?}", kind);
|
||||
|
@ -70,7 +70,7 @@ impl Database {
|
|||
notification: Notification,
|
||||
avoid_duplicates: bool,
|
||||
tx: &DatabaseTransaction,
|
||||
) -> Result<Option<proto::Notification>> {
|
||||
) -> Result<Option<(UserId, proto::Notification)>> {
|
||||
if avoid_duplicates {
|
||||
if self
|
||||
.find_notification(recipient_id, ¬ification, tx)
|
||||
|
@ -94,20 +94,25 @@ impl Database {
|
|||
content: ActiveValue::Set(notification_proto.content.clone()),
|
||||
actor_id: ActiveValue::Set(actor_id),
|
||||
is_read: ActiveValue::NotSet,
|
||||
response: ActiveValue::NotSet,
|
||||
created_at: ActiveValue::NotSet,
|
||||
id: ActiveValue::NotSet,
|
||||
}
|
||||
.save(&*tx)
|
||||
.await?;
|
||||
|
||||
Ok(Some(proto::Notification {
|
||||
id: model.id.as_ref().to_proto(),
|
||||
kind: notification_proto.kind,
|
||||
timestamp: model.created_at.as_ref().assume_utc().unix_timestamp() as u64,
|
||||
is_read: false,
|
||||
content: notification_proto.content,
|
||||
actor_id: notification_proto.actor_id,
|
||||
}))
|
||||
Ok(Some((
|
||||
recipient_id,
|
||||
proto::Notification {
|
||||
id: model.id.as_ref().to_proto(),
|
||||
kind: notification_proto.kind,
|
||||
timestamp: model.created_at.as_ref().assume_utc().unix_timestamp() as u64,
|
||||
is_read: false,
|
||||
response: None,
|
||||
content: notification_proto.content,
|
||||
actor_id: notification_proto.actor_id,
|
||||
},
|
||||
)))
|
||||
}
|
||||
|
||||
pub async fn remove_notification(
|
||||
|
@ -125,6 +130,32 @@ impl Database {
|
|||
Ok(id)
|
||||
}
|
||||
|
||||
pub async fn respond_to_notification(
|
||||
&self,
|
||||
recipient_id: UserId,
|
||||
notification: &Notification,
|
||||
response: bool,
|
||||
tx: &DatabaseTransaction,
|
||||
) -> Result<Option<(UserId, proto::Notification)>> {
|
||||
if let Some(id) = self
|
||||
.find_notification(recipient_id, notification, tx)
|
||||
.await?
|
||||
{
|
||||
let row = notification::Entity::update(notification::ActiveModel {
|
||||
id: ActiveValue::Unchanged(id),
|
||||
recipient_id: ActiveValue::Unchanged(recipient_id),
|
||||
response: ActiveValue::Set(Some(response)),
|
||||
is_read: ActiveValue::Set(true),
|
||||
..Default::default()
|
||||
})
|
||||
.exec(tx)
|
||||
.await?;
|
||||
Ok(model_to_proto(self, row).map(|notification| (recipient_id, notification)))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn find_notification(
|
||||
&self,
|
||||
recipient_id: UserId,
|
||||
|
@ -142,7 +173,11 @@ impl Database {
|
|||
.add(notification::Column::RecipientId.eq(recipient_id))
|
||||
.add(notification::Column::IsRead.eq(false))
|
||||
.add(notification::Column::Kind.eq(kind))
|
||||
.add(notification::Column::ActorId.eq(proto.actor_id)),
|
||||
.add(if proto.actor_id.is_some() {
|
||||
notification::Column::ActorId.eq(proto.actor_id)
|
||||
} else {
|
||||
notification::Column::ActorId.is_null()
|
||||
}),
|
||||
)
|
||||
.stream(&*tx)
|
||||
.await?;
|
||||
|
@ -152,7 +187,7 @@ impl Database {
|
|||
while let Some(row) = rows.next().await {
|
||||
let row = row?;
|
||||
let id = row.id;
|
||||
if let Some(proto) = self.model_to_proto(row) {
|
||||
if let Some(proto) = model_to_proto(self, row) {
|
||||
if let Some(existing) = Notification::from_proto(&proto) {
|
||||
if existing == *notification {
|
||||
return Ok(Some(id));
|
||||
|
@ -163,16 +198,17 @@ impl Database {
|
|||
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
fn model_to_proto(&self, row: notification::Model) -> Option<proto::Notification> {
|
||||
let kind = self.notification_kinds_by_id.get(&row.kind)?;
|
||||
Some(proto::Notification {
|
||||
id: row.id.to_proto(),
|
||||
kind: kind.to_string(),
|
||||
timestamp: row.created_at.assume_utc().unix_timestamp() as u64,
|
||||
is_read: row.is_read,
|
||||
content: row.content,
|
||||
actor_id: row.actor_id.map(|id| id.to_proto()),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn model_to_proto(this: &Database, row: notification::Model) -> Option<proto::Notification> {
|
||||
let kind = this.notification_kinds_by_id.get(&row.kind)?;
|
||||
Some(proto::Notification {
|
||||
id: row.id.to_proto(),
|
||||
kind: kind.to_string(),
|
||||
timestamp: row.created_at.assume_utc().unix_timestamp() as u64,
|
||||
is_read: row.is_read,
|
||||
response: row.response,
|
||||
content: row.content,
|
||||
actor_id: row.actor_id.map(|id| id.to_proto()),
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue