diff --git a/docs/content/endpoints/invites.md b/docs/content/endpoints/invites.md index 987d438..c8b2d61 100644 --- a/docs/content/endpoints/invites.md +++ b/docs/content/endpoints/invites.md @@ -29,7 +29,7 @@ Example > POST /invite/create?id=123&jwt= < { - < "id": , + < "id": , < "uses": 3, < "expires": true < } @@ -59,7 +59,6 @@ Example * status: 0 * permissions: 51 * name: Anonymous - * joindate: Current Unix time since epoch Example @@ -70,7 +69,6 @@ Example < "id": 123, < "secret": "super secret", < "name": "Anonymous", - < "joindate": 134256, < "status": 0, < "permissions": 51 < } diff --git a/docs/content/endpoints/members.md b/docs/content/endpoints/members.md index a0f3bce..8f5307c 100644 --- a/docs/content/endpoints/members.md +++ b/docs/content/endpoints/members.md @@ -40,7 +40,6 @@ Example < { < "name": "nickname-here", < "id": 123, - < "joindate": , < "permissions": <64-bit-mask> < } ``` @@ -65,7 +64,6 @@ Example < { < "name": "nickname-here", < "id": 0, // always 0 - < "joindate": , < "status": 1|2 < "permissions": <64-bit-mask> < } diff --git a/docs/content/endpoints/mesages.md b/docs/content/endpoints/mesages.md index a80c123..ada0044 100644 --- a/docs/content/endpoints/mesages.md +++ b/docs/content/endpoints/mesages.md @@ -16,15 +16,15 @@ weight: 15 * jwt: String * channel_id: u64 - * type: String - - Valid values: - - * text - * jpeg - * png - * webm - * mp4 +* Required headers: + Note that for any content that it is required that non-`text/plain` content be base64 encoded + * content-type: String + * text/plain + * image/jpg | image/jpeg + * image/png + * application/webm + * application/mp4 + * application/mp3 * Required body: * Content itself should always go in the body @@ -41,9 +41,9 @@ weight: 15 * channel_id: u64 * start_time: i64 - * Unix timestamp (seconds) + * Unix timestamp (milli-seconds) * end_time: i64 - * Unix timestamp (seconds) + * Unix timestamp (milli-seconds) * limit: Optional * Maximum = 1000 diff --git a/docs/content/structures/users.md b/docs/content/structures/users.md index 405faf5..f14f6f0 100644 --- a/docs/content/structures/users.md +++ b/docs/content/structures/users.md @@ -12,7 +12,6 @@ Name | Type :------:|:-----: id | `u64` secret | `String` -joindate| `i64` status | `i32` permissions| `u64` @@ -30,7 +29,6 @@ This data is retrieved after a sucessful `/login` hit. :-----:|:-----: id | `u64` name | `String` -joindate| `i64` status | `i32` permissions| `u64` diff --git a/json-api/db/src/lib.rs b/json-api/db/src/lib.rs index cc8c5c0..be275ec 100644 --- a/json-api/db/src/lib.rs +++ b/json-api/db/src/lib.rs @@ -82,3 +82,11 @@ pub struct Member { pub permissions: UBigInt, } +#[derive(Serialize)] +pub struct PublicMember { + pub id: UBigInt, + pub name: VarChar, + pub status: Integer, + pub permissions: UBigInt, +} + diff --git a/json-api/db/src/member.rs b/json-api/db/src/member.rs index a2455d0..2655a1c 100644 --- a/json-api/db/src/member.rs +++ b/json-api/db/src/member.rs @@ -5,7 +5,7 @@ use mysql_async::error::Error as SqlError; use crate::{Response, no_conn, sql_err}; use crate::{UBigInt, Integer, VarChar}; -use crate::Member; +use crate::{PublicMember, Member}; @@ -152,3 +152,32 @@ impl Member { Ok(()) } } + +impl PublicMember { + pub async fn get_online(p: &Pool, status: Integer) -> Result, SqlError> { + let valid_status = status == STATUS_ONLINE || + status == STATUS_AWAY || + status == STATUS_OFFLINE || + status == STATUS_DO_NOT_DISTURB; + + if !valid_status { + return Ok(Response::RestrictedInput(format!("Invalid status value"))); + } else { + let conn = p.get_conn().await?; + let q = "SELECT id, name, permissions FROM members + WHERE status = :status LIMIT 1000"; + + let result = conn.prep_exec(q, params!{"status" => status}).await?; + let (_, data): (_, Vec) = result.map_and_drop(|row| { + let (id, name, permissions): (UBigInt, VarChar, UBigInt) = + mysql_async::from_row(row); + + PublicMember { id, name, permissions, status } + }).await?; + + return Ok(Response::Set(data)); + } + } +} + + diff --git a/json-api/src/members.rs b/json-api/src/members.rs index b4f120f..9ed870c 100644 --- a/json-api/src/members.rs +++ b/json-api/src/members.rs @@ -4,7 +4,7 @@ use mysql_async::Pool; use serde_json::json; use db::member::STATUS_ONLINE; -use db::Member; +use db::{PublicMember, Member}; use db::Response::*; use crate::http::set_json_body; @@ -12,19 +12,22 @@ use crate::qs_param; pub async fn get_online_members(p: &Pool, response: &mut Response) { - // TODO: at some point we should provide a way of not querying literally every user in - // existance - // TODO: loggin at some point or something idklol - return match Member::filter(p, STATUS_ONLINE).await { - Set(users) => { - set_json_body(response, json!(users)); + // Lists out the available online members at the time + // Member structs are wildly small so for most cases this query really isn't that expensive + match PublicMember::get_online(p, STATUS_ONLINE).await { + Ok(dbresponse) => { + match dbresponse { + Set(users) => set_json_body(response, json!(users)), + Other(e) => { + eprintln!("{}", e); + *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR; + }, + // Unreachable code but required for rustc to be happy + _ => *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR + }; }, - db::Response::Other(e) => { - eprintln!("{}", e); - *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR; - }, - _ => *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR - } + Err(e) => eprintln!("Sql error: {}", e) + }; } pub async fn get_self(p: &Pool, response: &mut Response, params: HashMap) { @@ -117,3 +120,4 @@ mod tests { println!("Response: {:?}", resp); } } +