mirror of
https://github.com/Blah-IM/blahrs.git
synced 2025-05-01 00:31:09 +00:00
feat(webapi): impl member information query
This commit is contained in:
parent
8378c4d230
commit
920a1cc3b9
4 changed files with 74 additions and 4 deletions
|
@ -377,7 +377,6 @@ pub struct UpdateMemberPayload {
|
||||||
pub enum RoomAdminOp {
|
pub enum RoomAdminOp {
|
||||||
AddMember(AddMemberPayload),
|
AddMember(AddMemberPayload),
|
||||||
RemoveMember(RemoveMemberPayload),
|
RemoveMember(RemoveMemberPayload),
|
||||||
// TODO: R
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bitflags::bitflags! {
|
bitflags::bitflags! {
|
||||||
|
|
|
@ -149,7 +149,7 @@ impl AppState {
|
||||||
type ArcState = State<Arc<AppState>>;
|
type ArcState = State<Arc<AppState>>;
|
||||||
|
|
||||||
pub fn router(st: Arc<AppState>) -> Router {
|
pub fn router(st: Arc<AppState>) -> Router {
|
||||||
use axum::routing::{delete, get, post};
|
use axum::routing::{get, post};
|
||||||
|
|
||||||
// NB. Use consistent handler naming: `<method>_<path>[_<details>]`.
|
// NB. Use consistent handler naming: `<method>_<path>[_<details>]`.
|
||||||
// Use prefix `list` for GET with pagination.
|
// Use prefix `list` for GET with pagination.
|
||||||
|
@ -171,7 +171,7 @@ pub fn router(st: Arc<AppState>) -> Router {
|
||||||
// TODO!: remove this.
|
// TODO!: remove this.
|
||||||
.route("/room/:rid/admin", post(post_room_admin))
|
.route("/room/:rid/admin", post(post_room_admin))
|
||||||
.route("/room/:rid/member", get(list_room_member).post(post_room_member))
|
.route("/room/:rid/member", get(list_room_member).post(post_room_member))
|
||||||
.route("/room/:rid/member/:uid", delete(delete_room_member).patch(patch_room_member))
|
.route("/room/:rid/member/:uid", get(get_room_member).delete(delete_room_member).patch(patch_room_member))
|
||||||
;
|
;
|
||||||
|
|
||||||
let router = router
|
let router = router
|
||||||
|
@ -527,6 +527,23 @@ async fn post_room_member(
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn get_room_member(
|
||||||
|
st: ArcState,
|
||||||
|
R(Path((rid, id_key)), _): RE<Path<(Id, PubKey)>>,
|
||||||
|
Auth(user): Auth,
|
||||||
|
) -> Result<Json<RoomMember>, ApiError> {
|
||||||
|
st.db.with_read(|txn| {
|
||||||
|
// Check membership.
|
||||||
|
let _ = txn.get_room_member(rid, &user)?;
|
||||||
|
let (_uid, permission, last_seen_cid) = txn.get_room_member_by_id_key(rid, &id_key)?;
|
||||||
|
Ok(Json(RoomMember {
|
||||||
|
id_key,
|
||||||
|
permission,
|
||||||
|
last_seen_cid: (last_seen_cid != Id(0)).then_some(last_seen_cid),
|
||||||
|
}))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
async fn delete_room_member(
|
async fn delete_room_member(
|
||||||
st: ArcState,
|
st: ArcState,
|
||||||
R(Path((rid, id_key)), _): RE<Path<(Id, PubKey)>>,
|
R(Path((rid, id_key)), _): RE<Path<(Id, PubKey)>>,
|
||||||
|
|
|
@ -1711,7 +1711,7 @@ async fn room_mgmt_remove(server: Server) {
|
||||||
|
|
||||||
#[rstest]
|
#[rstest]
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn room_mgmt_update_perm(server: Server) {
|
async fn room_mgmt_perm(server: Server) {
|
||||||
let rid = server
|
let rid = server
|
||||||
.create_room(&ALICE, RoomAttrs::PUBLIC_JOINABLE, "public")
|
.create_room(&ALICE, RoomAttrs::PUBLIC_JOINABLE, "public")
|
||||||
.await
|
.await
|
||||||
|
@ -1721,6 +1721,22 @@ async fn room_mgmt_update_perm(server: Server) {
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
let get_bob = || {
|
||||||
|
server.get::<RoomMember>(
|
||||||
|
&format!("/room/{rid}/member/{}", BOB.pubkeys.id_key),
|
||||||
|
Some(&auth(&BOB)),
|
||||||
|
)
|
||||||
|
};
|
||||||
|
// Initial permission.
|
||||||
|
assert_eq!(
|
||||||
|
get_bob().await.unwrap(),
|
||||||
|
RoomMember {
|
||||||
|
id_key: BOB.pubkeys.id_key.clone(),
|
||||||
|
permission: MemberPermission::MAX_SELF_ADD,
|
||||||
|
last_seen_cid: None,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
// OK, Alice grants Bob permission to change permission.
|
// OK, Alice grants Bob permission to change permission.
|
||||||
server
|
server
|
||||||
.update_member_perm(
|
.update_member_perm(
|
||||||
|
@ -1731,6 +1747,10 @@ async fn room_mgmt_update_perm(server: Server) {
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
get_bob().await.unwrap().permission,
|
||||||
|
MemberPermission::POST_CHAT | MemberPermission::UPDATE_MEMBER
|
||||||
|
);
|
||||||
|
|
||||||
// Cannot restrict a member with higher permission.
|
// Cannot restrict a member with higher permission.
|
||||||
server
|
server
|
||||||
|
@ -1743,6 +1763,10 @@ async fn room_mgmt_update_perm(server: Server) {
|
||||||
.update_member_perm(rid, &BOB, &BOB, MemberPermission::empty())
|
.update_member_perm(rid, &BOB, &BOB, MemberPermission::empty())
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
get_bob().await.unwrap().permission,
|
||||||
|
MemberPermission::empty(),
|
||||||
|
);
|
||||||
|
|
||||||
// Cannot self-grant permission.
|
// Cannot self-grant permission.
|
||||||
server
|
server
|
||||||
|
@ -1761,6 +1785,10 @@ async fn room_mgmt_update_perm(server: Server) {
|
||||||
.update_member_perm(rid, &ALICE, &BOB, MemberPermission::POST_CHAT)
|
.update_member_perm(rid, &ALICE, &BOB, MemberPermission::POST_CHAT)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
get_bob().await.unwrap().permission,
|
||||||
|
MemberPermission::POST_CHAT,
|
||||||
|
);
|
||||||
|
|
||||||
// Bob can chat again.
|
// Bob can chat again.
|
||||||
server.post_chat(rid, &BOB, "yay").await.unwrap();
|
server.post_chat(rid, &BOB, "yay").await.unwrap();
|
||||||
|
|
|
@ -595,6 +595,32 @@ paths:
|
||||||
$ref: '#/components/schemas/ApiError'
|
$ref: '#/components/schemas/ApiError'
|
||||||
|
|
||||||
/_blah/room/{rid}/member/{member_id_key}:
|
/_blah/room/{rid}/member/{member_id_key}:
|
||||||
|
get:
|
||||||
|
summary: Get information of a room member
|
||||||
|
|
||||||
|
parameters:
|
||||||
|
- name: Authorization
|
||||||
|
in: header
|
||||||
|
description: User authentication token.
|
||||||
|
schema:
|
||||||
|
$ref: '#/components/schemas/Signed-Auth'
|
||||||
|
|
||||||
|
responses:
|
||||||
|
200:
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: '#/components/schemas/RoomMember'
|
||||||
|
|
||||||
|
404:
|
||||||
|
description: |
|
||||||
|
Room does not exist, the user does not have permission for the
|
||||||
|
operation, or the operand user is not a room member.
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: '#/components/schemas/ApiError'
|
||||||
|
|
||||||
patch:
|
patch:
|
||||||
summary: Update permission of a room member
|
summary: Update permission of a room member
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue