refactor(appservices): avoid cloning frequently
This commit is contained in:
parent
5c650bb67e
commit
b20483aa13
7 changed files with 44 additions and 40 deletions
|
@ -17,9 +17,7 @@ pub(crate) async fn send_request<T: OutgoingRequest>(
|
||||||
where
|
where
|
||||||
T: Debug,
|
T: Debug,
|
||||||
{
|
{
|
||||||
let Some(destination) = registration.url else {
|
let destination = registration.url?;
|
||||||
return None;
|
|
||||||
};
|
|
||||||
|
|
||||||
let hs_token = registration.hs_token.as_str();
|
let hs_token = registration.hs_token.as_str();
|
||||||
|
|
||||||
|
|
|
@ -100,7 +100,7 @@ pub(crate) async fn get_alias_helper(
|
||||||
match services().rooms.alias.resolve_local_alias(&room_alias)? {
|
match services().rooms.alias.resolve_local_alias(&room_alias)? {
|
||||||
Some(r) => room_id = Some(r),
|
Some(r) => room_id = Some(r),
|
||||||
None => {
|
None => {
|
||||||
for appservice in services().appservice.all().await {
|
for appservice in services().appservice.read().await.values() {
|
||||||
if appservice.aliases.is_match(room_alias.as_str())
|
if appservice.aliases.is_match(room_alias.as_str())
|
||||||
&& if let Some(opt_result) = services()
|
&& if let Some(opt_result) = services()
|
||||||
.sending
|
.sending
|
||||||
|
|
|
@ -80,10 +80,11 @@ where
|
||||||
|
|
||||||
let mut json_body = serde_json::from_slice::<CanonicalJsonValue>(&body).ok();
|
let mut json_body = serde_json::from_slice::<CanonicalJsonValue>(&body).ok();
|
||||||
|
|
||||||
let appservices = services().appservice.all().await;
|
let appservice_registration = if let Some(token) = token {
|
||||||
let appservice_registration = appservices
|
services().appservice.find_from_token(token).await
|
||||||
.iter()
|
} else {
|
||||||
.find(|info| Some(info.registration.as_token.as_str()) == token);
|
None
|
||||||
|
};
|
||||||
|
|
||||||
let (sender_user, sender_device, sender_servername, from_appservice) =
|
let (sender_user, sender_device, sender_servername, from_appservice) =
|
||||||
if let Some(info) = appservice_registration {
|
if let Some(info) = appservice_registration {
|
||||||
|
|
|
@ -369,25 +369,13 @@ impl Service {
|
||||||
)),
|
)),
|
||||||
},
|
},
|
||||||
AdminCommand::ListAppservices => {
|
AdminCommand::ListAppservices => {
|
||||||
if let Ok(appservices) = services()
|
let appservices = services().appservice.iter_ids().await;
|
||||||
.appservice
|
|
||||||
.iter_ids()
|
|
||||||
.map(|ids| ids.collect::<Vec<_>>())
|
|
||||||
{
|
|
||||||
let count = appservices.len();
|
|
||||||
let output = format!(
|
let output = format!(
|
||||||
"Appservices ({}): {}",
|
"Appservices ({}): {}",
|
||||||
count,
|
appservices.len(),
|
||||||
appservices
|
appservices.join(", ")
|
||||||
.into_iter()
|
|
||||||
.filter_map(|r| r.ok())
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.join(", ")
|
|
||||||
);
|
);
|
||||||
RoomMessageEventContent::text_plain(output)
|
RoomMessageEventContent::text_plain(output)
|
||||||
} else {
|
|
||||||
RoomMessageEventContent::text_plain("Failed to get appservices.")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
AdminCommand::ListRooms => {
|
AdminCommand::ListRooms => {
|
||||||
let room_ids = services().rooms.metadata.iter_ids();
|
let room_ids = services().rooms.metadata.iter_ids();
|
||||||
|
|
|
@ -4,6 +4,7 @@ use std::collections::BTreeMap;
|
||||||
|
|
||||||
pub use data::Data;
|
pub use data::Data;
|
||||||
|
|
||||||
|
use futures_util::Future;
|
||||||
use regex::RegexSet;
|
use regex::RegexSet;
|
||||||
use ruma::api::appservice::{Namespace, Registration};
|
use ruma::api::appservice::{Namespace, Registration};
|
||||||
use tokio::sync::RwLock;
|
use tokio::sync::RwLock;
|
||||||
|
@ -147,20 +148,36 @@ impl Service {
|
||||||
self.db.unregister_appservice(service_name)
|
self.db.unregister_appservice(service_name)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_registration(&self, id: &str) -> Result<Option<Registration>> {
|
pub async fn get_registration(&self, id: &str) -> Option<Registration> {
|
||||||
self.db.get_registration(id)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn iter_ids(&self) -> Result<impl Iterator<Item = Result<String>> + '_> {
|
|
||||||
self.db.iter_ids()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn all(&self) -> Vec<RegistrationInfo> {
|
|
||||||
self.registration_info
|
self.registration_info
|
||||||
.read()
|
.read()
|
||||||
.await
|
.await
|
||||||
.values()
|
.get(id)
|
||||||
|
.cloned()
|
||||||
|
.map(|info| info.registration)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn iter_ids(&self) -> Vec<String> {
|
||||||
|
self.registration_info
|
||||||
|
.read()
|
||||||
|
.await
|
||||||
|
.keys()
|
||||||
.cloned()
|
.cloned()
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn find_from_token(&self, token: &str) -> Option<RegistrationInfo> {
|
||||||
|
self.read()
|
||||||
|
.await
|
||||||
|
.values()
|
||||||
|
.find(|info| info.registration.as_token == token)
|
||||||
|
.cloned()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read(
|
||||||
|
&self,
|
||||||
|
) -> impl Future<Output = tokio::sync::RwLockReadGuard<'_, BTreeMap<String, RegistrationInfo>>>
|
||||||
|
{
|
||||||
|
self.registration_info.read()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -524,11 +524,11 @@ impl Service {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for appservice in services().appservice.all().await {
|
for appservice in services().appservice.read().await.values() {
|
||||||
if services()
|
if services()
|
||||||
.rooms
|
.rooms
|
||||||
.state_cache
|
.state_cache
|
||||||
.appservice_in_room(&pdu.room_id, &appservice)?
|
.appservice_in_room(&pdu.room_id, appservice)?
|
||||||
{
|
{
|
||||||
services()
|
services()
|
||||||
.sending
|
.sending
|
||||||
|
|
|
@ -488,7 +488,7 @@ impl Service {
|
||||||
services()
|
services()
|
||||||
.appservice
|
.appservice
|
||||||
.get_registration(id)
|
.get_registration(id)
|
||||||
.map_err(|e| (kind.clone(), e))?
|
.await
|
||||||
.ok_or_else(|| {
|
.ok_or_else(|| {
|
||||||
(
|
(
|
||||||
kind.clone(),
|
kind.clone(),
|
||||||
|
|
Loading…
Add table
Reference in a new issue