forge
Explore
Log in

Don't you dare go Hollow

Presence for new relations and deletions reach former members

Browse files at this commit

63d5a7fbe59a4b30fed745eb8029799166fbc6d1Ahmet Kaan GümüşParent: 22b1bb7
  • fix
    • presence for new relations and deletions reach former members
Show full message
🐛 fix: presence for new relations and deletions reach former members

Opening a DM, accepting a friend or an invitation now sends each side the
other's presence, so new contacts no longer look offline until a
reconnect. delete_community resolves members before deleting and sends
CommunityDeleted itself, so deleting a user who is alone in a DM call
clears the ring and the DM on the peer instead of leaving them stale.
Adds tests for kicking a non member and a builder leaving an owned group.
crates/server/src/call/admin.rsmodified+0 -3
@@ -293,10 +293,7 @@ impl Handle for DeleteUser {
293 let departed = leave_user_voice(&cx.sh.services.rooms, user_id, cx.db).await?;293 let departed = leave_user_voice(&cx.sh.services.rooms, user_id, cx.db).await?;
294 cx.out.extend(departed);294 cx.out.extend(departed);
295 for community_id in member::owned(user_id, cx.db).await? {295 for community_id in member::owned(user_id, cx.db).await? {
296- let member_ids = member::member_ids(community_id, cx.db).await?;
297 delete_community(community_id, cx).await?;296 delete_community(community_id, cx).await?;
298- let event = Event::CommunityDeleted { community_id };
299- cx.out.push(Broadcast::new(To::Users(member_ids), event));
300 }297 }
301 for community in community::communities_of(user_id, cx.db).await? {298 for community in community::communities_of(user_id, cx.db).await? {
302 let member_ids = member::member_ids(community.id, cx.db).await?;299 let member_ids = member::member_ids(community.id, cx.db).await?;
crates/server/src/call/community.rsmodified+0 -7
@@ -151,7 +151,6 @@ impl Handle for DeleteCommunity {
151 let db = cx.db;151 let db = cx.db;
152 ensure_editable(self.community_id, db).await?;152 ensure_editable(self.community_id, db).await?;
153 allowed(&me, self.community_id, db, permission::can_delete_community).await?;153 allowed(&me, self.community_id, db, permission::can_delete_community).await?;
154- let member_ids = db::member::member_ids(self.community_id, db).await?;
155 let owned = db::member::rank(me.id, self.community_id, db).await? == Some(Rank::Owner);154 let owned = db::member::rank(me.id, self.community_id, db).await? == Some(Rank::Owner);
156 let deleted = db::community::read(self.community_id, me.id, db).await?;155 let deleted = db::community::read(self.community_id, me.id, db).await?;
157 if permission::is_builder(me.role) && !owned {156 if permission::is_builder(me.role) && !owned {
@@ -165,12 +164,6 @@ impl Handle for DeleteCommunity {
165 .await?;164 .await?;
166 }165 }
167 delete_community(self.community_id, cx).await?;166 delete_community(self.community_id, cx).await?;
168- cx.emit(
169- To::Users(member_ids),
170- Event::CommunityDeleted {
171- community_id: deleted.id,
172- },
173- );
174 167
175 Ok(())168 Ok(())
176 }169 }
crates/server/src/call/social.rsmodified+9 -2
@@ -18,7 +18,7 @@ use crate::{
18 db::{self, direct::Opening, friend::FriendState},18 db::{self, direct::Opening, friend::FriendState},
19 error::{ServerError, ensure, ensure_with},19 error::{ServerError, ensure, ensure_with},
20 publish::To,20 publish::To,
21- request::{actor_rank, allowed, ensure_editable},21+ request::{actor_rank, allowed, ensure_editable, introduce},
22 };22 };
23 23
24 async fn friends_updated(user_ids: [Id; 2], cx: &mut Cx<'_>) -> Result<(), ServerError> {24 async fn friends_updated(user_ids: [Id; 2], cx: &mut Cx<'_>) -> Result<(), ServerError> {
@@ -56,6 +56,7 @@ impl Handle for OpenDirect {
56 Opening::Created(community) => {56 Opening::Created(community) => {
57 let peer_view = db::community::read(community.id, self.user_id, cx.db).await?;57 let peer_view = db::community::read(community.id, self.user_id, cx.db).await?;
58 cx.emit(To::User(self.user_id), Event::CommunityAdded(peer_view));58 cx.emit(To::User(self.user_id), Event::CommunityAdded(peer_view));
59+ introduce(me.id, &[self.user_id], cx).await?;
59 60
60 Ok(community)61 Ok(community)
61 }62 }
@@ -91,7 +92,10 @@ impl Handle for AddFriend {
91 Error::Taken,92 Error::Taken,
92 )?;93 )?;
93 match db::friend::pair(self.user_id, me.id, db).await? {94 match db::friend::pair(self.user_id, me.id, db).await? {
94- Some(FriendState::Requested) => db::friend::accept(me.id, self.user_id, db).await?,95+ Some(FriendState::Requested) => {
96+ db::friend::accept(me.id, self.user_id, db).await?;
97+ introduce(me.id, &[self.user_id], cx).await?;
98+ }
95 _ => db::friend::request(me.id, self.user_id, db).await?,99 _ => db::friend::request(me.id, self.user_id, db).await?,
96 }100 }
97 101
@@ -103,6 +107,7 @@ impl Handle for AcceptFriend {
103 async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<(), ServerError> {107 async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<(), ServerError> {
104 let me = me.user()?;108 let me = me.user()?;
105 db::friend::accept(me.id, self.user_id, cx.db).await?;109 db::friend::accept(me.id, self.user_id, cx.db).await?;
110+ introduce(me.id, &[self.user_id], cx).await?;
106 111
107 friends_updated([me.id, self.user_id], cx).await112 friends_updated([me.id, self.user_id], cx).await
108 }113 }
@@ -175,6 +180,8 @@ impl Handle for AcceptInvitation {
175 },180 },
176 };181 };
177 cx.emit(To::Community(self.community_id), event);182 cx.emit(To::Community(self.community_id), event);
183+ let members = db::member::member_ids(self.community_id, db).await?;
184+ introduce(me.id, &members, cx).await?;
178 invitations_updated(me.id, cx).await?;185 invitations_updated(me.id, cx).await?;
179 186
180 Ok(community)187 Ok(community)
crates/server/src/request.rsmodified+97 -2
@@ -55,6 +55,27 @@ pub async fn presence_changed(
55 Ok(vec![Broadcast::new(To::Related(user_id), event)])55 Ok(vec![Broadcast::new(To::Related(user_id), event)])
56 }56 }
57 57
58+pub(crate) async fn introduce(user_id: Id, others: &[Id], cx: &mut Cx<'_>) -> Result<(), ServerError> {
59+ let hub = &cx.sh.services.hub;
60+ let presence = effective_presence(hub, user_id, cx.db).await?;
61+ for &other in others {
62+ if other == user_id {
63+ continue;
64+ }
65+ let theirs = effective_presence(hub, other, cx.db).await?;
66+ cx.emit(To::User(other), Event::PresenceChanged { user_id, presence });
67+ cx.emit(
68+ To::User(user_id),
69+ Event::PresenceChanged {
70+ user_id: other,
71+ presence: theirs,
72+ },
73+ );
74+ }
75+
76+ Ok(())
77+}
78+
58 pub(crate) async fn leave_user_voice(79 pub(crate) async fn leave_user_voice(
59 rooms: &Rooms,80 rooms: &Rooms,
60 user_id: Id,81 user_id: Id,
@@ -113,9 +134,14 @@ pub(crate) async fn voice_closed(
113 }134 }
114 135
115 pub(crate) async fn delete_community(community_id: Id, cx: &mut Cx<'_>) -> Result<(), ServerError> {136 pub(crate) async fn delete_community(community_id: Id, cx: &mut Cx<'_>) -> Result<(), ServerError> {
137+ let member_ids = db::member::member_ids(community_id, cx.db).await?;
116 let closed = voice_closed(&cx.sh.services.rooms, community_id, cx.db).await?;138 let closed = voice_closed(&cx.sh.services.rooms, community_id, cx.db).await?;
117 cx.out.extend(closed);139 cx.out.extend(closed);
118 db::community::delete(community_id, cx.db).await?;140 db::community::delete(community_id, cx.db).await?;
141+ cx.emit(
142+ To::Users(member_ids),
143+ Event::CommunityDeleted { community_id },
144+ );
119 145
120 Ok(())146 Ok(())
121 }147 }
@@ -307,7 +333,8 @@ mod tests {
307 use common::{333 use common::{
308 call::{334 call::{
309 AcceptFriend, AcceptInvitation, AddFriend, CreateCommunity, CreateMessage,335 AcceptFriend, AcceptInvitation, AddFriend, CreateCommunity, CreateMessage,
310- DeleteCommunity, FindUser, Invite, ListCommunities, ListFriends, UpdateCommunity,336+ DeleteCommunity, FindUser, Invite, Kick, LeaveCommunity, ListCommunities,
337+ ListFriends, UpdateCommunity,
311 },338 },
312 error::Error as CommonError,339 error::Error as CommonError,
313 model::{Id, Role},340 model::{Id, Role},
@@ -347,8 +374,12 @@ mod tests {
347 }374 }
348 375
349 async fn signed_in(username: &str, db: &Db) -> Arc<Client> {376 async fn signed_in(username: &str, db: &Db) -> Arc<Client> {
377+ signed_in_as(username, Role::User, db).await
378+ }
379+
380+ async fn signed_in_as(username: &str, role: Role, db: &Db) -> Arc<Client> {
350 let conn = db.lock().await.unwrap();381 let conn = db.lock().await.unwrap();
351- let user = db::user::create(username, "hash", Role::User, &conn)382+ let user = db::user::create(username, "hash", role, &conn)
352 .await383 .await
353 .unwrap();384 .unwrap();
354 let session_id = db::session::insert(&token_hash(username), user.id, &conn)385 let session_id = db::session::insert(&token_hash(username), user.id, &conn)
@@ -553,4 +584,68 @@ mod tests {
553 584
554 assert!(remaining.is_empty());585 assert!(remaining.is_empty());
555 }586 }
587+
588+ async fn group(owner: &Arc<Client>, db: &Db, services: &Services) -> Id {
589+ call(
590+ CreateCommunity {
591+ name: "The Community".to_string(),
592+ },
593+ owner,
594+ db,
595+ services,
596+ )
597+ .await
598+ .unwrap()
599+ .id
600+ }
601+
602+ async fn user_id(username: &str, by: &Arc<Client>, db: &Db, services: &Services) -> Id {
603+ call(
604+ FindUser {
605+ username: username.to_string(),
606+ },
607+ by,
608+ db,
609+ services,
610+ )
611+ .await
612+ .unwrap()
613+ .id
614+ }
615+
616+ #[tokio::test]
617+ async fn kick_of_non_member_is_not_found() {
618+ let db = test::db().await;
619+ let services = Services::default();
620+ let owner = signed_in("Owner", &db).await;
621+ signed_in("Outsider", &db).await;
622+ let community_id = group(&owner, &db, &services).await;
623+ let outsider = user_id("Outsider", &owner, &db, &services).await;
624+
625+ let kick = Kick {
626+ community_id,
627+ user_id: outsider,
628+ reason: None,
629+ };
630+
631+ assert!(matches!(
632+ call(kick, &owner, &db, &services).await,
633+ Err(CommonError::NotFound)
634+ ));
635+ }
636+
637+ #[tokio::test]
638+ async fn builder_cannot_leave_owned_group() {
639+ let db = test::db().await;
640+ let services = Services::default();
641+ let builder = signed_in_as("Builder", Role::Builder, &db).await;
642+ let community_id = group(&builder, &db, &services).await;
643+
644+ let leave = LeaveCommunity { community_id };
645+
646+ assert!(matches!(
647+ call(leave, &builder, &db, &services).await,
648+ Err(CommonError::Forbidden)
649+ ));
650+ }
556 }651 }