improve appservice::Data interface encap

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-06-29 01:19:23 +00:00
parent 30b5ad3870
commit 0f1432f448
4 changed files with 20 additions and 18 deletions

View file

@ -22,7 +22,7 @@ pub(super) async fn appservice(subcommand: Appservice) -> Result<RoomMessageEven
},
Appservice::All => {
let timer = tokio::time::Instant::now();
let results = services().appservice.db.all();
let results = services().appservice.all();
let query_time = timer.elapsed();
Ok(RoomMessageEventContent::notice_markdown(format!(

View file

@ -10,7 +10,7 @@ pub(super) async fn room_alias(subcommand: RoomAlias) -> Result<RoomMessageEvent
alias,
} => {
let timer = tokio::time::Instant::now();
let results = services().rooms.alias.db.resolve_local_alias(&alias);
let results = services().rooms.alias.resolve_local_alias(&alias);
let query_time = timer.elapsed();
Ok(RoomMessageEventContent::notice_markdown(format!(
@ -21,7 +21,7 @@ pub(super) async fn room_alias(subcommand: RoomAlias) -> Result<RoomMessageEvent
room_id,
} => {
let timer = tokio::time::Instant::now();
let results = services().rooms.alias.db.local_aliases_for_room(&room_id);
let results = services().rooms.alias.local_aliases_for_room(&room_id);
let query_time = timer.elapsed();
let aliases: Vec<_> = results.collect();
@ -32,7 +32,7 @@ pub(super) async fn room_alias(subcommand: RoomAlias) -> Result<RoomMessageEvent
},
RoomAlias::AllLocalAliases => {
let timer = tokio::time::Instant::now();
let results = services().rooms.alias.db.all_local_aliases();
let results = services().rooms.alias.all_local_aliases();
let query_time = timer.elapsed();
let aliases: Vec<_> = results.collect();

View file

@ -51,17 +51,4 @@ impl Data {
.map_err(|_| Error::bad_database("Invalid id bytes in id_appserviceregistrations."))
})))
}
pub fn all(&self) -> Result<Vec<(String, Registration)>> {
self.iter_ids()?
.filter_map(Result::ok)
.map(move |id| {
Ok((
id.clone(),
self.get_registration(&id)?
.expect("iter_ids only returns appservices that exist"),
))
})
.collect()
}
}

View file

@ -124,7 +124,7 @@ impl Service {
let mut registration_info = BTreeMap::new();
let db = Data::new(db);
// Inserting registrations into cache
for appservice in db.all()? {
for appservice in iter_ids(&db)? {
registration_info.insert(
appservice.0,
appservice
@ -140,6 +140,8 @@ impl Service {
})
}
pub fn all(&self) -> Result<Vec<(String, Registration)>> { iter_ids(&self.db) }
/// Registers an appservice and returns the ID to the caller
pub async fn register_appservice(&self, yaml: Registration) -> Result<String> {
//TODO: Check for collisions between exclusive appservice namespaces
@ -231,3 +233,16 @@ impl Service {
self.registration_info.read()
}
}
fn iter_ids(db: &Data) -> Result<Vec<(String, Registration)>> {
db.iter_ids()?
.filter_map(Result::ok)
.map(move |id| {
Ok((
id.clone(),
db.get_registration(&id)?
.expect("iter_ids only returns appservices that exist"),
))
})
.collect()
}