Merge branch 'cleanup_events_after_unregister_appservice' into 'next'

Cleanup events after unregister appservice / update appservice error handling / return ID of new appservice

See merge request famedly/conduit!276
This commit is contained in:
Timo Kösters 2022-01-31 13:24:01 +00:00
commit 95ca43e20f
3 changed files with 55 additions and 7 deletions

View file

@ -109,10 +109,29 @@ impl Admin {
}
}
AdminCommand::RegisterAppservice(yaml) => {
guard.appservice.register_appservice(yaml).unwrap(); // TODO handle error
match guard.appservice.register_appservice(yaml) {
Ok(id) => {
let msg: String = format!("OK. Appservice {} created", id);
send_message(RoomMessageEventContent::text_plain(msg), guard, &state_lock);
}
Err(_) => {
send_message(RoomMessageEventContent::text_plain("ERR: Failed register appservice. Check server log"), guard, &state_lock);
}
}
}
AdminCommand::UnregisterAppservice(service_name) => {
guard.appservice.unregister_appservice(&service_name).unwrap(); // TODO: see above
if let Ok(_) = guard.appservice.unregister_appservice(&service_name) {
if let Ok(_) = guard.sending.cleanup_events(&service_name) {
let msg: String = format!("OK. Appservice {} removed", service_name);
send_message(RoomMessageEventContent::text_plain(msg), guard, &state_lock);
} else {
let msg: String = format!("WARN: Appservice {} removed, but failed to cleanup events", service_name);
send_message(RoomMessageEventContent::text_plain(msg), guard, &state_lock);
}
} else {
let msg: String = format!("ERR. Appservice {} not removed", service_name);
send_message(RoomMessageEventContent::text_plain(msg), guard, &state_lock);
}
}
AdminCommand::ListAppservices => {
if let Ok(appservices) = guard.appservice.iter_ids().map(|ids| ids.collect::<Vec<_>>()) {

View file

@ -12,7 +12,9 @@ pub struct Appservice {
}
impl Appservice {
pub fn register_appservice(&self, yaml: serde_yaml::Value) -> Result<()> {
/// Registers an appservice and returns the ID to the caller
///
pub fn register_appservice(&self, yaml: serde_yaml::Value) -> Result<String> {
// TODO: Rumaify
let id = yaml.get("id").unwrap().as_str().unwrap();
self.id_appserviceregistrations.insert(
@ -22,9 +24,9 @@ impl Appservice {
self.cached_registrations
.write()
.unwrap()
.insert(id.to_owned(), yaml);
.insert(id.to_owned(), yaml.to_owned());
Ok(())
Ok(id.to_owned())
}
/// Remove an appservice registration

View file

@ -480,6 +480,26 @@ impl Sending {
hash.as_ref().to_owned()
}
/// Cleanup event data
/// Used for instance after we remove an appservice registration
///
#[tracing::instrument(skip(self))]
pub fn cleanup_events(&self, key_id: &str) -> Result<()> {
let mut prefix = b"+".to_vec();
prefix.extend_from_slice(key_id.as_bytes());
prefix.push(0xff);
for (key, _) in self.servercurrentevent_data.scan_prefix(prefix.clone()) {
self.servercurrentevent_data.remove(&key).unwrap();
}
for (key, _) in self.servernameevent_data.scan_prefix(prefix.clone()) {
self.servernameevent_data.remove(&key).unwrap();
}
Ok(())
}
#[tracing::instrument(skip(db, events, kind))]
async fn handle_events(
kind: OutgoingKind,
@ -520,8 +540,15 @@ impl Sending {
&db.globals,
db.appservice
.get_registration(server.as_str())
.unwrap()
.unwrap(), // TODO: handle error
.map_err(|e| (kind.clone(), e))?
.ok_or_else(|| {
(
kind.clone(),
Error::bad_database(
"[Appservice] Could not load registration from db.",
),
)
})?,
appservice::event::push_events::v1::Request {
events: &pdu_jsons,
txn_id: (&*base64::encode_config(