-
-
Notifications
You must be signed in to change notification settings - Fork 41
Add basic implementation for the tags endpoint. #105
Conversation
0d59ebc
to
3d4e32f
Compare
@jimmycuadra Review? |
I need to add an event |
This could done by different PR. It need to fix ruma/ruma-events#4, before this issue could resolve correct. |
|
||
// Check if the given user_id corresponds to the authenticated user. | ||
if user_id != user.id { | ||
Err(ApiError::unauthorized(None))?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add some useful error message instead of None
? You can use this for this particular error, in order to be consistent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolved
room_id: RoomId, | ||
tag: String, | ||
content: Value) | ||
-> Result<(), ApiError> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Until we use something like rustfmt
it would be better to stick with the current style.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolved
152f276
to
4134d2c
Compare
|
||
let mut content = String::new(); | ||
if let Err(_) = request.body.read_to_string(&mut content) { | ||
Err(ApiError::not_found(None))?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You missed this one. Although synapse parses it as json, so you can skip this check and just add the JsonRequest
middleware.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolved!
I think the json middleware could be refactored in the future.
let user = request.extensions.get::<User>() | ||
.expect("AccessTokenAuth should ensure a user").clone(); | ||
let params = request.extensions.get::<Router>().expect("Params object is missing").clone(); | ||
let tag = match params.find("tag") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can implement this check as a middleware (e.g TagParam
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolved!
use tags::RoomTag; | ||
use user::User; | ||
|
||
pub type MapTags = BTreeMap<String, Value>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why BTreeMap
over HashMap
?
Waiting for #114 |
@jimmycuadra Could you start reviewing? Thanks |
#[test] | ||
fn basic_create_tag() { | ||
let test = Test::new(); | ||
let access_token = test.create_access_token(); // @carl:ruma.test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can remove the comment by using the create_access_token_with_username("carl")
let room_id = test.create_public_room(&access_token); | ||
let put_tag_path = format!( | ||
"/_matrix/client/r0/user/{}/rooms/{}/tags/{}?access_token={}", | ||
"@carls:ruma.test", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Create another access token e.g alice. The user is not registered, so the action is not forbidden but the user id was not found.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would do this after is fixed #121.
} | ||
|
||
#[test] | ||
fn delete_tag_forbidden() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this forbidden?
.filter(room_tags::user_id.eq(user_id)) | ||
.get_results(connection) | ||
.map_err(|err| match err { | ||
DieselError::NotFound => ApiError::not_found(None), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use the err instead of None.
delete(tag) | ||
.execute(connection) | ||
.map_err(|err| match err { | ||
DieselError::NotFound => ApiError::not_found(None), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also a missing error message here.
user_id: UserId, | ||
room_id: RoomId, | ||
tag: String, | ||
content: Value, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why Value
instead of String
?
} | ||
|
||
#[test] | ||
fn update_tag() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can add another test where you update a non-existent tag.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, See Line 177, 179
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, my bad. Rename the helper function to put_tag
so there is no confusion.
fn handle(&self, request: &mut Request) -> IronResult<Response> { | ||
let user_id = request.extensions.get::<UserIdParam>() | ||
.expect("UserIdParam should ensure a UserId").clone(); | ||
let room_id = request.extensions.get::<RoomIdParam>() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to check that the room and user the exist before adding a tag. Also add the relevant tests. We don't have foreign keys so there will be no error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll only check room_id
. The user is check by forbidden.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Solved for all three, but not nice for delete.
map.insert(tag.tag, info); | ||
} | ||
|
||
let tags = TagEventContent { tags: map }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better use a custom TagsResponse
in the api handler and here just return the hashmap.
Fantastic work. :D |
No description provided.