Skip to content
This repository has been archived by the owner on Sep 2, 2021. It is now read-only.

Add basic implementation for the tags endpoint. #105

Merged
merged 1 commit into from
Dec 21, 2016

Conversation

farodin91
Copy link
Member

No description provided.

@farodin91
Copy link
Member Author

@jimmycuadra Review?

@farodin91
Copy link
Member Author

I need to add an event m.tag.

@farodin91
Copy link
Member Author

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))?;
Copy link

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.

Copy link
Member Author

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> {
Copy link

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved

@farodin91 farodin91 force-pushed the tags branch 2 times, most recently from 152f276 to 4134d2c Compare October 17, 2016 20:02

let mut content = String::new();
if let Err(_) = request.body.read_to_string(&mut content) {
Err(ApiError::not_found(None))?;
Copy link

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.

Copy link
Member Author

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") {
Copy link

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).

Copy link
Member Author

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>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why BTreeMap over HashMap?

@farodin91
Copy link
Member Author

Waiting for #114

@farodin91
Copy link
Member Author

@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
Copy link

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",
Copy link

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.

Copy link
Member Author

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() {
Copy link

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),
Copy link

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),
Copy link

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,
Copy link

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() {
Copy link

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.

Copy link
Member Author

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

Copy link

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>()
Copy link

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.

Copy link
Member Author

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.

Copy link
Member Author

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 };
Copy link

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.

@jimmycuadra jimmycuadra merged commit fba8be9 into ruma:master Dec 21, 2016
@jimmycuadra
Copy link
Member

Fantastic work. :D

@farodin91 farodin91 deleted the tags branch April 29, 2017 12:43
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

Successfully merging this pull request may close these issues.

3 participants