Add better interface for getting and setting room tag account data.

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk
2025-10-07 12:45:14 +00:00
parent 4d8d64f5c7
commit 4ca68deef8
3 changed files with 46 additions and 33 deletions

View File

@@ -1,3 +1,5 @@
mod room_tags;
use std::sync::Arc;
use futures::{Stream, StreamExt, TryFutureExt};

View File

@@ -0,0 +1,39 @@
use futures::TryFutureExt;
use ruma::{
RoomId, UserId,
events::{
RoomAccountDataEventType,
tag::{TagEvent, TagEventContent, TagInfo, TagName, Tags},
},
};
use tuwunel_core::{Result, implement};
#[implement(super::Service)]
pub async fn set_room_tag(
&self,
user_id: &UserId,
room_id: &RoomId,
tag: TagName,
info: Option<TagInfo>,
) -> Result {
let mut tags = self
.get_room_tags(user_id, room_id)
.await
.unwrap_or_default();
tags.insert(tag, info.unwrap_or_default());
let event = serde_json::to_value(TagEvent { content: TagEventContent { tags } })?;
self.update(Some(room_id), user_id, RoomAccountDataEventType::Tag, &event)
.await
}
#[implement(super::Service)]
pub async fn get_room_tags(&self, user_id: &UserId, room_id: &RoomId) -> Result<Tags> {
self.services
.account_data
.get_room(room_id, user_id, RoomAccountDataEventType::Tag)
.map_ok(|content: TagEventContent| content.tags)
.await
}

View File

@@ -1,16 +1,13 @@
use std::collections::BTreeMap;
use futures::FutureExt;
use ruma::{
RoomId, UserId,
UserId,
events::{
RoomAccountDataEventType, StateEventType,
StateEventType,
room::{
member::{MembershipState, RoomMemberEventContent},
message::RoomMessageEventContent,
power_levels::RoomPowerLevelsEventContent,
},
tag::{TagEvent, TagEventContent, TagInfo},
},
};
use tuwunel_core::{
@@ -137,7 +134,9 @@ pub async fn make_user_admin(&self, user_id: &UserId) -> Result {
if !room_tag.is_empty() {
if let Err(e) = self
.set_room_tag(&room_id, user_id, room_tag)
.services
.account_data
.set_room_tag(user_id, &room_id, room_tag.into(), None)
.await
{
error!(?room_id, ?user_id, ?room_tag, "Failed to set tag for admin grant: {e}");
@@ -164,33 +163,6 @@ pub async fn make_user_admin(&self, user_id: &UserId) -> Result {
Ok(())
}
#[implement(super::Service)]
async fn set_room_tag(&self, room_id: &RoomId, user_id: &UserId, tag: &str) -> Result {
let mut event = self
.services
.account_data
.get_room(room_id, user_id, RoomAccountDataEventType::Tag)
.await
.unwrap_or_else(|_| TagEvent {
content: TagEventContent { tags: BTreeMap::new() },
});
event
.content
.tags
.insert(tag.to_owned().into(), TagInfo::new());
self.services
.account_data
.update(
Some(room_id),
user_id,
RoomAccountDataEventType::Tag,
&serde_json::to_value(event)?,
)
.await
}
/// Demote an admin, removing its rights.
#[implement(super::Service)]
pub async fn revoke_admin(&self, user_id: &UserId) -> Result {