From 2c6621f27efd8f5d43fa7e1870affa45f7db9a43 Mon Sep 17 00:00:00 2001 From: farodin91 Date: Thu, 6 Apr 2017 19:37:46 +0200 Subject: [PATCH] Support media api, do some refactor and add API as unimplemented. --- src/api/r0/content.rs | 38 +++++++++ src/api/r0/mod.rs | 10 ++- src/api/r0/preview_url.rs | 16 ++++ src/api/r0/thumbnail.rs | 16 ++++ src/server.rs | 166 ++++++++++++++++++++++---------------- 5 files changed, 174 insertions(+), 72 deletions(-) create mode 100644 src/api/r0/content.rs create mode 100644 src/api/r0/preview_url.rs create mode 100644 src/api/r0/thumbnail.rs diff --git a/src/api/r0/content.rs b/src/api/r0/content.rs new file mode 100644 index 00000000..3aa07767 --- /dev/null +++ b/src/api/r0/content.rs @@ -0,0 +1,38 @@ +//! Endpoints for content. +use iron::{Chain, Handler, IronError, IronResult, Request, Response}; + +use error::ApiError; +use middleware::{AccessTokenAuth, MiddlewareChain}; + +/// The `/upload` endpoint. +pub struct Upload; + +middleware_chain!(Upload, [AccessTokenAuth]); + +impl Handler for Upload { + fn handle(&self, _: &mut Request) -> IronResult { + Err(IronError::from(ApiError::unimplemented(None))) + } +} + +/// The `/download/:server_name/:media_id` endpoint. +pub struct Download; + +middleware_chain!(Download, [AccessTokenAuth]); + +impl Handler for Download { + fn handle(&self, _: &mut Request) -> IronResult { + Err(IronError::from(ApiError::unimplemented(None))) + } +} + +/// The `/download/:server_name/:media_id/:file_name` endpoint. +pub struct DownloadFile; + +middleware_chain!(DownloadFile, [AccessTokenAuth]); + +impl Handler for DownloadFile { + fn handle(&self, _: &mut Request) -> IronResult { + Err(IronError::from(ApiError::unimplemented(None))) + } +} diff --git a/src/api/r0/mod.rs b/src/api/r0/mod.rs index 6683d62b..62a135e0 100644 --- a/src/api/r0/mod.rs +++ b/src/api/r0/mod.rs @@ -6,6 +6,7 @@ pub use self::account::{ PutAccountData, PutRoomAccountData, }; +pub use self::content::{Download, DownloadFile, Upload}; pub use self::directory::{GetRoomAlias, DeleteRoomAlias, PutRoomAlias}; pub use self::event_creation::{SendMessageEvent, StateMessageEvent}; pub use self::join::{InviteToRoom, JoinRoom, JoinRoomWithIdOrAlias, KickFromRoom, LeaveRoom}; @@ -13,16 +14,19 @@ pub use self::login::Login; pub use self::logout::Logout; pub use self::members::Members; pub use self::presence::{GetPresenceList, GetPresenceStatus, PostPresenceList, PutPresenceStatus}; +pub use self::preview_url::PreviewUrl; pub use self::profile::{Profile, GetAvatarUrl, PutAvatarUrl, GetDisplayName, PutDisplayName}; pub use self::registration::Register; pub use self::room_creation::CreateRoom; pub use self::room_info::RoomState; -pub use self::tags::{DeleteTag, GetTags, PutTag}; pub use self::sync::Sync; +pub use self::tags::{DeleteTag, GetTags, PutTag}; +pub use self::thumbnail::Thumbnail; pub use self::versions::Versions; pub use self::filter::{GetFilter, PostFilter}; mod account; +mod content; mod directory; mod event_creation; mod filter; @@ -31,10 +35,12 @@ mod login; mod logout; mod members; mod presence; +mod preview_url; mod profile; mod registration; mod room_creation; mod room_info; -mod tags; mod sync; +mod tags; +mod thumbnail; mod versions; diff --git a/src/api/r0/preview_url.rs b/src/api/r0/preview_url.rs new file mode 100644 index 00000000..46b2a90d --- /dev/null +++ b/src/api/r0/preview_url.rs @@ -0,0 +1,16 @@ +//! Endpoints for content. +use iron::{Chain, Handler, IronError, IronResult, Request, Response}; + +use error::ApiError; +use middleware::{AccessTokenAuth, MiddlewareChain}; + +/// The `/preview_url` endpoint. +pub struct PreviewUrl; + +middleware_chain!(PreviewUrl, [AccessTokenAuth]); + +impl Handler for PreviewUrl { + fn handle(&self, _: &mut Request) -> IronResult { + Err(IronError::from(ApiError::unimplemented(None))) + } +} diff --git a/src/api/r0/thumbnail.rs b/src/api/r0/thumbnail.rs new file mode 100644 index 00000000..0312e8da --- /dev/null +++ b/src/api/r0/thumbnail.rs @@ -0,0 +1,16 @@ +//! Endpoints for content. +use iron::{Chain, Handler, IronError, IronResult, Request, Response}; + +use error::ApiError; +use middleware::{AccessTokenAuth, MiddlewareChain}; + +/// The `/thumbnail/:server_name/:media_id` endpoint. +pub struct Thumbnail; + +middleware_chain!(Thumbnail, [AccessTokenAuth]); + +impl Handler for Thumbnail { + fn handle(&self, _: &mut Request) -> IronResult { + Err(IronError::from(ApiError::unimplemented(None))) + } +} diff --git a/src/server.rs b/src/server.rs index d327e2ae..b5a1861f 100644 --- a/src/server.rs +++ b/src/server.rs @@ -15,6 +15,8 @@ use api::r0::{ DeactivateAccount, DeleteRoomAlias, DeleteTag, + Download, + DownloadFile, GetAvatarUrl, GetDisplayName, GetFilter, @@ -32,6 +34,7 @@ use api::r0::{ Members, PostFilter, PostPresenceList, + PreviewUrl, Profile, PutAccountData, PutAvatarUrl, @@ -45,6 +48,8 @@ use api::r0::{ SendMessageEvent, StateMessageEvent, Sync, + Thumbnail, + Upload, Versions, }; use config::Config; @@ -74,101 +79,122 @@ impl<'a> Server<'a> { r2d2_config: R2D2Config, set_up_db: bool, ) -> Result { - let mut r0_router = Router::new(); + debug!("Connecting to PostgreSQL."); + let connection_pool = DB::create_connection_pool(r2d2_config, &ruma_config.postgres_url)?; + let connection = connection_pool.get()?; + + if set_up_db { + debug!("Setting up database."); + setup_database(&*connection).map_err(CliError::from)?; + + debug!("Running pending database migrations."); + run_pending_migrations(&*connection).map_err(CliError::from)?; + } + + let mut mount = Mount::new(); + + + /// Add version endpoint + let mut versions_router = Router::new(); + + versions_router.get("/versions", Versions::supported(), "versions"); + + let mut versions = Chain::new(versions_router); + versions.link_after(ResponseHeaders); + + mount.mount("/_matrix/client/", versions); + + + /// Add client endpoint + let mut r0_client_router = Router::new(); - r0_router.post("/account/password", AccountPassword::chain(), "account_password"); - r0_router.post("/account/deactivate", DeactivateAccount::chain(), "deactivate_account"); - r0_router.post("/createRoom", CreateRoom::chain(), "create_room"); - r0_router.get("/directory/room/:room_alias", GetRoomAlias::chain(), "get_room_alias"); - r0_router.delete( + r0_client_router.post("/account/password", AccountPassword::chain(), "account_password"); + r0_client_router.post("/account/deactivate", DeactivateAccount::chain(), "deactivate_account"); + r0_client_router.post("/createRoom", CreateRoom::chain(), "create_room"); + r0_client_router.get("/directory/room/:room_alias", GetRoomAlias::chain(), "get_room_alias"); + r0_client_router.delete( "/directory/room/:room_alias", DeleteRoomAlias::chain(), "delete_room_alias", ); - r0_router.put("/directory/room/:room_alias", PutRoomAlias::chain(), "put_room_alias"); - r0_router.post("/login", Login::chain(), "login"); - r0_router.post("/logout", Logout::chain(), "logout"); - r0_router.post("/register", Register::chain(), "register"); - r0_router.post("/tokenrefresh", deprecated, "token_refresh"); - r0_router.put( + r0_client_router.put("/directory/room/:room_alias", PutRoomAlias::chain(), "put_room_alias"); + r0_client_router.post("/login", Login::chain(), "login"); + r0_client_router.post("/logout", Logout::chain(), "logout"); + r0_client_router.post("/register", Register::chain(), "register"); + r0_client_router.post("/tokenrefresh", deprecated, "token_refresh"); + r0_client_router.put( "/user/:user_id/account_data/:type", PutAccountData::chain(), "put_account_data", ); - r0_router.put( + r0_client_router.put( "/user/:user_id/rooms/:room_id/account_data/:type", PutRoomAccountData::chain(), "put_room_account_data", ); - r0_router.put( + r0_client_router.put( "/rooms/:room_id/send/:event_type/:transaction_id", SendMessageEvent::chain(), "send_message_event", ); - r0_router.put( + r0_client_router.put( "/rooms/:room_id/state/:event_type", StateMessageEvent::chain(), "state_message_event", ); - r0_router.put( + r0_client_router.put( "/rooms/:room_id/state/:event_type/:state_key", StateMessageEvent::chain(), "state_message_event_with_key", ); - - /// Joining rooms - r0_router.post("/rooms/:room_id/join", JoinRoom::chain(), "join_room"); - r0_router.post("/rooms/:room_id/invite", InviteToRoom::chain(), "invite_to_room"); - r0_router.post("/join/:room_id_or_alias", JoinRoomWithIdOrAlias::chain(), "join_room_with_alias"); - r0_router.post("rooms/:room_id/kick", KickFromRoom::chain(), "kick_from_room"); - r0_router.post("rooms/:room_id/leave", LeaveRoom::chain(), "leave_room"); - r0_router.get("/rooms/:room_id/members", Members::chain(), "members"); - r0_router.get("/rooms/:room_id/state", RoomState::chain(), "get_room_state"); - r0_router.get("/profile/:user_id", Profile::chain(), "profile"); - r0_router.get("/profile/:user_id/avatar_url", GetAvatarUrl::chain(), "get_avatar_url"); - r0_router.get("/profile/:user_id/displayname", GetDisplayName::chain(), "get_display_name"); - r0_router.put("/profile/:user_id/avatar_url", PutAvatarUrl::chain(), "put_avatar_url"); - r0_router.put("/profile/:user_id/displayname", PutDisplayName::chain(), "put_display_name"); - r0_router.get("/user/:user_id/rooms/:room_id/tags", GetTags::chain(), "get_tags"); - r0_router.put("/user/:user_id/rooms/:room_id/tags/:tag", PutTag::chain(), "add_tag"); - r0_router.delete("/user/:user_id/rooms/:room_id/tags/:tag", DeleteTag::chain(), "delete_tag"); - r0_router.get("/user/:user_id/filter/:filter_id", GetFilter::chain(), "get_filter"); - r0_router.post("/user/:user_id/filter", PostFilter::chain(), "post_filter"); - r0_router.get("/sync", Sync::chain(), "sync"); - r0_router.get("/presence/:user_id/status", GetPresenceStatus::chain(), "get_presence_status"); - r0_router.put("/presence/:user_id/status", PutPresenceStatus::chain(), "put_presence_status"); - r0_router.get("/presence/list/:user_id", GetPresenceList::chain(), "get_presence_list"); - r0_router.post("/presence/list/:user_id", PostPresenceList::chain(), "post_presence_list"); - - let mut r0 = Chain::new(r0_router); - - debug!("Connecting to PostgreSQL."); - let connection_pool = DB::create_connection_pool(r2d2_config, &ruma_config.postgres_url)?; - let connection = connection_pool.get()?; - - if set_up_db { - debug!("Setting up database."); - setup_database(&*connection).map_err(CliError::from)?; - - debug!("Running pending database migrations."); - run_pending_migrations(&*connection).map_err(CliError::from)?; - } - - r0.link_before(Read::::one(ruma_config.clone())); - r0.link_before(Write::::one(connection_pool)); - r0.link_after(ResponseHeaders); - - let mut versions_router = Router::new(); - - versions_router.get("/versions", Versions::supported(), "versions"); - - let mut versions = Chain::new(versions_router); - versions.link_after(ResponseHeaders); - - let mut mount = Mount::new(); - - mount.mount("/_matrix/client/", versions); - mount.mount("/_matrix/client/r0/", r0); + r0_client_router.post("/rooms/:room_id/join", JoinRoom::chain(), "join_room"); + r0_client_router.post("/rooms/:room_id/invite", InviteToRoom::chain(), "invite_to_room"); + r0_client_router.post("/join/:room_id_or_alias", JoinRoomWithIdOrAlias::chain(), "join_room_with_alias"); + r0_client_router.post("rooms/:room_id/kick", KickFromRoom::chain(), "kick_from_room"); + r0_client_router.post("rooms/:room_id/leave", LeaveRoom::chain(), "leave_room"); + r0_client_router.get("/rooms/:room_id/members", Members::chain(), "members"); + r0_client_router.get("/rooms/:room_id/state", RoomState::chain(), "get_room_state"); + r0_client_router.get("/profile/:user_id", Profile::chain(), "profile"); + r0_client_router.get("/profile/:user_id/avatar_url", GetAvatarUrl::chain(), "get_avatar_url"); + r0_client_router.get("/profile/:user_id/displayname", GetDisplayName::chain(), "get_display_name"); + r0_client_router.put("/profile/:user_id/avatar_url", PutAvatarUrl::chain(), "put_avatar_url"); + r0_client_router.put("/profile/:user_id/displayname", PutDisplayName::chain(), "put_display_name"); + r0_client_router.get("/user/:user_id/rooms/:room_id/tags", GetTags::chain(), "get_tags"); + r0_client_router.put("/user/:user_id/rooms/:room_id/tags/:tag", PutTag::chain(), "add_tag"); + r0_client_router.delete("/user/:user_id/rooms/:room_id/tags/:tag", DeleteTag::chain(), "delete_tag"); + r0_client_router.get("/user/:user_id/filter/:filter_id", GetFilter::chain(), "get_filter"); + r0_client_router.post("/user/:user_id/filter", PostFilter::chain(), "post_filter"); + r0_client_router.get("/sync", Sync::chain(), "sync"); + r0_client_router.get("/presence/:user_id/status", GetPresenceStatus::chain(), "get_presence_status"); + r0_client_router.put("/presence/:user_id/status", PutPresenceStatus::chain(), "put_presence_status"); + r0_client_router.get("/presence/list/:user_id", GetPresenceList::chain(), "get_presence_list"); + r0_client_router.post("/presence/list/:user_id", PostPresenceList::chain(), "post_presence_list"); + + let mut r0_client = Chain::new(r0_client_router); + + r0_client.link_before(Read::::one(ruma_config.clone())); + r0_client.link_before(Write::::one(connection_pool.clone())); + r0_client.link_after(ResponseHeaders); + + mount.mount("/_matrix/client/r0/", r0_client); + + + /// Add media endpoint + let mut r0_media_router = Router::new(); + + r0_media_router.get("/download/:server_name/:media_id", Download::chain(), "download"); + r0_media_router.get("/download/:server_name/:media_id/:file_name", DownloadFile::chain(), "download_file_name"); + r0_media_router.get("/thumbnail/:server_name/:media_id", Thumbnail::chain(), "thumbnail"); + r0_media_router.post("/upload", Upload::chain(), "upload"); + r0_media_router.post("/preview_url", PreviewUrl::chain(), "preview_url"); + + let mut r0_media = Chain::new(r0_media_router); + + r0_media.link_before(Read::::one(ruma_config.clone())); + r0_media.link_before(Write::::one(connection_pool)); + r0_media.link_after(ResponseHeaders); + + mount.mount("/_matrix/media/r0/", r0_media); mount_swagger(&mut mount);