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

Commit

Permalink
Support media api, do some refactor and add API as unimplemented.
Browse files Browse the repository at this point in the history
  • Loading branch information
farodin91 committed Apr 6, 2017
1 parent 8eda5e5 commit 2c6621f
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 72 deletions.
38 changes: 38 additions & 0 deletions src/api/r0/content.rs
Original file line number Diff line number Diff line change
@@ -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<Response> {
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<Response> {
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<Response> {
Err(IronError::from(ApiError::unimplemented(None)))
}
}
10 changes: 8 additions & 2 deletions src/api/r0/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,27 @@ 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};
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;
Expand All @@ -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;
16 changes: 16 additions & 0 deletions src/api/r0/preview_url.rs
Original file line number Diff line number Diff line change
@@ -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<Response> {
Err(IronError::from(ApiError::unimplemented(None)))
}
}
16 changes: 16 additions & 0 deletions src/api/r0/thumbnail.rs
Original file line number Diff line number Diff line change
@@ -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<Response> {
Err(IronError::from(ApiError::unimplemented(None)))
}
}
166 changes: 96 additions & 70 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use api::r0::{
DeactivateAccount,
DeleteRoomAlias,
DeleteTag,
Download,
DownloadFile,
GetAvatarUrl,
GetDisplayName,
GetFilter,
Expand All @@ -32,6 +34,7 @@ use api::r0::{
Members,
PostFilter,
PostPresenceList,
PreviewUrl,
Profile,
PutAccountData,
PutAvatarUrl,
Expand All @@ -45,6 +48,8 @@ use api::r0::{
SendMessageEvent,
StateMessageEvent,
Sync,
Thumbnail,
Upload,
Versions,
};
use config::Config;
Expand Down Expand Up @@ -74,101 +79,122 @@ impl<'a> Server<'a> {
r2d2_config: R2D2Config<PgConnection, R2D2DieselError>,
set_up_db: bool,
) -> Result<Server, CliError> {
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::<Config>::one(ruma_config.clone()));
r0.link_before(Write::<DB>::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::<Config>::one(ruma_config.clone()));
r0_client.link_before(Write::<DB>::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::<Config>::one(ruma_config.clone()));
r0_media.link_before(Write::<DB>::one(connection_pool));
r0_media.link_after(ResponseHeaders);

mount.mount("/_matrix/media/r0/", r0_media);

mount_swagger(&mut mount);

Expand Down

0 comments on commit 2c6621f

Please sign in to comment.