mirror of
https://github.com/Blah-IM/blahrs.git
synced 2025-08-20 11:02:41 +00:00
feat(webapi): impl identity description retrieval
This commit is contained in:
parent
d5cc097e7a
commit
c3842a6d3b
6 changed files with 126 additions and 6 deletions
|
@ -10,8 +10,10 @@ use blah_types::msg::{
|
|||
use blah_types::server::RoomMetadata;
|
||||
use blah_types::{Id, PubKey, Signee, UserKey};
|
||||
use parking_lot::Mutex;
|
||||
use rusqlite::types::FromSqlError;
|
||||
use rusqlite::{named_params, params, prepare_cached_and_bind, Connection, OpenFlags, Row};
|
||||
use serde::Deserialize;
|
||||
use serde_json::value::RawValue as JsonRawValue;
|
||||
|
||||
use crate::middleware::ApiError;
|
||||
|
||||
|
@ -170,6 +172,27 @@ fn parse_room_metadata(row: &Row<'_>) -> Result<RoomMetadata> {
|
|||
pub trait TransactionOps {
|
||||
fn conn(&self) -> &Connection;
|
||||
|
||||
fn get_user_id_desc_by_uid(&self, uid: i64) -> Result<Box<JsonRawValue>> {
|
||||
prepare_cached_and_bind!(
|
||||
self.conn(),
|
||||
r"
|
||||
SELECT `id_desc`
|
||||
FROM `user`
|
||||
WHERE `uid` = :uid
|
||||
"
|
||||
)
|
||||
.raw_query()
|
||||
.and_then(|row| {
|
||||
let json = JsonRawValue::from_string(row.get(0)?).map_err(|err| {
|
||||
FromSqlError::Other(format!("invalid id_desc in database: {err}").into())
|
||||
})?;
|
||||
Ok::<_, rusqlite::Error>(json)
|
||||
})
|
||||
.next()
|
||||
.ok_or(ApiError::UserNotFound)?
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
fn get_user(&self, UserKey { id_key, act_key }: &UserKey) -> Result<(i64, ServerPermission)> {
|
||||
prepare_cached_and_bind!(
|
||||
self.conn(),
|
||||
|
|
|
@ -18,7 +18,7 @@ use blah_types::msg::{
|
|||
};
|
||||
use blah_types::server::{
|
||||
ErrorResponseWithChallenge, RoomList, RoomMember, RoomMemberList, RoomMetadata, RoomMsgs,
|
||||
ServerCapabilities, ServerMetadata,
|
||||
ServerCapabilities, ServerMetadata, UserIdentityDescResponse,
|
||||
};
|
||||
use blah_types::{get_timestamp, Id, PubKey, Signed, UserKey};
|
||||
use data_encoding::BASE64_NOPAD;
|
||||
|
@ -29,6 +29,7 @@ use parking_lot::Mutex;
|
|||
use serde::de::DeserializeOwned;
|
||||
use serde::{Deserialize, Deserializer, Serialize};
|
||||
use serde_inline_default::serde_inline_default;
|
||||
use serde_json::value::RawValue as JsonRawValue;
|
||||
use sha2::Digest;
|
||||
use url::Url;
|
||||
use utils::ExpiringSet;
|
||||
|
@ -172,7 +173,8 @@ pub fn router(st: Arc<AppState>) -> Router {
|
|||
// TODO!: remove this.
|
||||
.route("/room/:rid/admin", r().post(post_room_admin))
|
||||
.route("/room/:rid/member", r().get(list_room_member).post(post_room_member))
|
||||
.route("/room/:rid/member/:uid", r().get(get_room_member).delete(delete_room_member).patch(patch_room_member))
|
||||
.route("/room/:rid/member/:idkey", r().get(get_room_member).delete(delete_room_member).patch(patch_room_member))
|
||||
.route("/room/:rid/member/:idkey/identity", r().get(get_room_member_identity))
|
||||
.fallback(fallback_route)
|
||||
;
|
||||
|
||||
|
@ -620,6 +622,20 @@ async fn patch_room_member(
|
|||
})
|
||||
}
|
||||
|
||||
async fn get_room_member_identity(
|
||||
st: ArcState,
|
||||
R(Path((rid, id_key)), _): RE<Path<(Id, PubKey)>>,
|
||||
Auth(user): Auth,
|
||||
) -> Result<Json<UserIdentityDescResponse<Box<JsonRawValue>>>, ApiError> {
|
||||
st.db.with_read(|txn| {
|
||||
// Check membership.
|
||||
let _ = txn.get_room_member(rid, &user)?;
|
||||
let (uid, ..) = txn.get_room_member_by_id_key(rid, &id_key)?;
|
||||
let identity = txn.get_user_id_desc_by_uid(uid)?;
|
||||
Ok(Json(UserIdentityDescResponse { identity }))
|
||||
})
|
||||
}
|
||||
|
||||
async fn delete_room(
|
||||
st: ArcState,
|
||||
R(Path(rid), _): RE<Path<Id>>,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue