implement MSC4133 only with MSC4175 for GET/PUT/DELETE

Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
strawberry 2024-09-07 09:26:50 -04:00
parent 5ae9a5ff31
commit f163ebf3bb
7 changed files with 217 additions and 10 deletions

View file

@ -32,6 +32,7 @@ pub struct Data {
userid_password: Arc<Map>,
userid_selfsigningkeyid: Arc<Map>,
userid_usersigningkeyid: Arc<Map>,
useridprofilekey_value: Arc<Map>,
services: Services,
}
@ -64,6 +65,7 @@ impl Data {
userid_password: db["userid_password"].clone(),
userid_selfsigningkeyid: db["userid_selfsigningkeyid"].clone(),
userid_usersigningkeyid: db["userid_usersigningkeyid"].clone(),
useridprofilekey_value: db["useridprofilekey_value"].clone(),
services: Services {
server: args.server.clone(),
globals: args.depend::<globals::Service>("globals"),
@ -231,6 +233,57 @@ impl Data {
.transpose()
}
/// Get the timezone of a user.
pub(super) fn timezone(&self, user_id: &UserId) -> Result<Option<String>> {
// first check the unstable prefix
let mut key = user_id.as_bytes().to_vec();
key.push(0xFF);
key.extend_from_slice(b"us.cloke.msc4175.tz");
let value = self
.useridprofilekey_value
.get(&key)?
.map(|bytes| utils::string_from_bytes(&bytes).map_err(|e| err!(Database("Timezone in db is invalid. {e}"))))
.transpose()
.unwrap();
// TODO: transparently migrate unstable key usage to the stable key once MSC4133
// and MSC4175 are stable, likely a remove/insert in this block
if value.is_none() || value.as_ref().is_some_and(String::is_empty) {
// check the stable prefix
let mut key = user_id.as_bytes().to_vec();
key.push(0xFF);
key.extend_from_slice(b"m.tz");
return self
.useridprofilekey_value
.get(&key)?
.map(|bytes| {
utils::string_from_bytes(&bytes).map_err(|e| err!(Database("Timezone in db is invalid. {e}")))
})
.transpose();
}
Ok(value)
}
/// Sets a new timezone or removes it if timezone is None.
pub(super) fn set_timezone(&self, user_id: &UserId, timezone: Option<String>) -> Result<()> {
let mut key = user_id.as_bytes().to_vec();
key.push(0xFF);
key.extend_from_slice(b"us.cloke.msc4175.tz");
// TODO: insert to the stable MSC4175 key when it's stable
if let Some(timezone) = timezone {
self.useridprofilekey_value
.insert(&key, timezone.as_bytes())?;
} else {
self.useridprofilekey_value.remove(&key)?;
}
Ok(())
}
/// Sets a new avatar_url or removes it if avatar_url is None.
pub(super) fn set_blurhash(&self, user_id: &UserId, blurhash: Option<String>) -> Result<()> {
if let Some(blurhash) = blurhash {

View file

@ -327,6 +327,13 @@ impl Service {
/// Get the blurhash of a user.
pub fn blurhash(&self, user_id: &UserId) -> Result<Option<String>> { self.db.blurhash(user_id) }
pub fn timezone(&self, user_id: &UserId) -> Result<Option<String>> { self.db.timezone(user_id) }
/// Sets a new tz or removes it if tz is None.
pub async fn set_timezone(&self, user_id: &UserId, tz: Option<String>) -> Result<()> {
self.db.set_timezone(user_id, tz)
}
/// Sets a new blurhash or removes it if blurhash is None.
pub async fn set_blurhash(&self, user_id: &UserId, blurhash: Option<String>) -> Result<()> {
self.db.set_blurhash(user_id, blurhash)