client-api: Add get and set methods to Capabilities
This commit is contained in:
parent
12c294422b
commit
f9eefaf02b
@ -1,12 +1,15 @@
|
||||
//! [GET /_matrix/client/r0/capabilities](https://matrix.org/docs/spec/client_server/r0.6.1#get-matrix-client-r0-capabilities)
|
||||
|
||||
use std::{borrow::Cow, collections::BTreeMap};
|
||||
|
||||
use maplit::btreemap;
|
||||
use ruma_api::ruma_api;
|
||||
use ruma_identifiers::RoomVersionId;
|
||||
use ruma_serde::StringEnum;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value as JsonValue;
|
||||
use std::collections::BTreeMap;
|
||||
use serde_json::{
|
||||
from_value as from_json_value, to_value as to_json_value, Error, Value as JsonValue,
|
||||
};
|
||||
|
||||
ruma_api! {
|
||||
metadata: {
|
||||
@ -72,7 +75,7 @@ pub struct Capabilities {
|
||||
/// Any other custom capabilities that the server supports outside of the specification,
|
||||
/// labeled using the Java package naming convention and stored as arbitrary JSON values.
|
||||
#[serde(flatten)]
|
||||
pub custom_capabilities: BTreeMap<String, JsonValue>,
|
||||
custom_capabilities: BTreeMap<String, JsonValue>,
|
||||
}
|
||||
|
||||
impl Capabilities {
|
||||
@ -80,6 +83,31 @@ impl Capabilities {
|
||||
pub fn new() -> Self {
|
||||
Default::default()
|
||||
}
|
||||
|
||||
/// Returns value of the given capability.
|
||||
pub fn get(&self, capability: &str) -> Result<Option<Cow<'_, JsonValue>>, Error> {
|
||||
let value = match capability {
|
||||
"m.change_password" => Some(Cow::Owned(to_json_value(&self.change_password)?)),
|
||||
"m.room_versions" => Some(Cow::Owned(to_json_value(&self.room_versions)?)),
|
||||
_ => match self.custom_capabilities.get(capability) {
|
||||
Some(value) => Some(Cow::Borrowed(value)),
|
||||
None => None,
|
||||
},
|
||||
};
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
/// Sets the given value to a capability.
|
||||
pub fn set(&mut self, capability_label: &str, capability: JsonValue) -> Result<(), Error> {
|
||||
match capability_label {
|
||||
"m.change_password" => self.change_password = from_json_value(capability)?,
|
||||
"m.room_versions" => self.room_versions = from_json_value(capability)?,
|
||||
_ => {
|
||||
self.custom_capabilities.insert(capability_label.to_owned(), capability);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Information about the m.change_password capability
|
||||
|
Loading…
x
Reference in New Issue
Block a user