Skip to content

Commit

Permalink
misc typing cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: strawberry <[email protected]>
  • Loading branch information
girlbossceo committed Dec 18, 2024
1 parent 3675c94 commit 4dd809f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/api/client/sync/v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ async fn load_joined_room(
.rooms
.typing
.typings_all(room_id, sender_user)
.await?;
.await;

Ok(vec![serde_json::from_str(&serde_json::to_string(&typings)?)?])
})
Expand Down
2 changes: 1 addition & 1 deletion src/service/rooms/read_receipt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl Service {
.expect("room flush failed");
// update appservices
let edu = EphemeralData::Receipt(event);
let _ = self
_ = self
.services
.sending
.send_edu_appservice_room(
Expand Down
72 changes: 35 additions & 37 deletions src/service/rooms/typing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,32 +146,34 @@ impl Service {
}
};

if !removable.is_empty() {
let typing = &mut self.typing.write().await;
let room = typing.entry(room_id.to_owned()).or_default();
for user in &removable {
debug_info!("typing timeout {user:?} in {room_id:?}");
room.remove(user);
}
if removable.is_empty() {
return Ok(());
}

// update clients
self.last_typing_update
.write()
.await
.insert(room_id.to_owned(), self.services.globals.next_count()?);
let typing = &mut self.typing.write().await;
let room = typing.entry(room_id.to_owned()).or_default();
for user in &removable {
debug_info!("typing timeout {user:?} in {room_id:?}");
room.remove(user);
}

if self.typing_update_sender.send(room_id.to_owned()).is_err() {
trace!("receiver found what it was looking for and is no longer interested");
}
// update clients
self.last_typing_update
.write()
.await
.insert(room_id.to_owned(), self.services.globals.next_count()?);

if self.typing_update_sender.send(room_id.to_owned()).is_err() {
trace!("receiver found what it was looking for and is no longer interested");
}

// update appservices
self.appservice_send(room_id).await?;
// update appservices
self.appservice_send(room_id).await?;

// update federation
for user in &removable {
if self.services.globals.user_is_local(user) {
self.federation_send(room_id, user, false).await?;
}
// update federation
for user in &removable {
if self.services.globals.user_is_local(user) {
self.federation_send(room_id, user, false).await?;
}
}

Expand All @@ -190,31 +192,31 @@ impl Service {
.unwrap_or(0))
}

/// Returns a new typing EDU.
pub async fn typings_content(&self, room_id: &RoomId) -> Result<TypingEventContent> {
/// Returns a new typing EDU's content.
pub async fn typings_content(&self, room_id: &RoomId) -> TypingEventContent {
let room_typing_indicators = self.typing.read().await.get(room_id).cloned();

let Some(typing_indicators) = room_typing_indicators else {
return Ok(TypingEventContent { user_ids: Vec::new() });
return TypingEventContent { user_ids: Vec::new() };
};

let user_ids: Vec<_> = typing_indicators.into_keys().collect();

Ok(TypingEventContent { user_ids })
TypingEventContent { user_ids }
}

/// Returns a new typing EDU.
pub async fn typings_all(
&self,
room_id: &RoomId,
sender_user: &UserId,
) -> Result<SyncEphemeralRoomEvent<TypingEventContent>> {
) -> SyncEphemeralRoomEvent<TypingEventContent> {
let room_typing_indicators = self.typing.read().await.get(room_id).cloned();

let Some(typing_indicators) = room_typing_indicators else {
return Ok(SyncEphemeralRoomEvent {
return SyncEphemeralRoomEvent {
content: TypingEventContent { user_ids: Vec::new() },
});
};
};

let user_ids: Vec<_> = typing_indicators
Expand All @@ -231,7 +233,7 @@ impl Service {
.collect()
.await;

Ok(SyncEphemeralRoomEvent { content: TypingEventContent { user_ids } })
SyncEphemeralRoomEvent { content: TypingEventContent { user_ids } }
}

async fn federation_send(
Expand All @@ -254,14 +256,12 @@ impl Service {
self.services
.sending
.send_edu_room(room_id, serde_json::to_vec(&edu).expect("Serialized Edu::Typing"))
.await?;

Ok(())
.await
}

async fn appservice_send(&self, room_id: &RoomId) -> Result<()> {
let edu = EphemeralData::Typing(EphemeralRoomEvent {
content: self.typings_content(room_id).await?,
content: self.typings_content(room_id).await,
room_id: room_id.into(),
});

Expand All @@ -271,8 +271,6 @@ impl Service {
room_id,
serde_json::to_vec(&edu).expect("Serialized EphemeralData::Typing"),
)
.await?;

Ok(())
.await
}
}
6 changes: 3 additions & 3 deletions src/service/sending/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ impl Service {
}

#[tracing::instrument(skip(self, serialized), level = "debug")]
pub fn send_edu_appservice(&self, appservice_id: String, serialized: Vec<u8>) -> Result {
let dest = Destination::Appservice(appservice_id);
pub fn send_edu_appservice(&self, appservice_id: &str, serialized: Vec<u8>) -> Result {
let dest = Destination::Appservice(appservice_id.to_owned());
let event = SendingEvent::Edu(serialized);
let _cork = self.db.db.cork();
let keys = self.db.queue_requests(once((&event, &dest)));
Expand Down Expand Up @@ -224,7 +224,7 @@ impl Service {
.appservice_in_room(room_id, appservice)
.await
{
self.send_edu_appservice(appservice.registration.id.clone(), serialized.clone())?;
self.send_edu_appservice(&appservice.registration.id, serialized.clone())?;
}
}
Ok(())
Expand Down

0 comments on commit 4dd809f

Please sign in to comment.