Subscription working (ish)

This commit is contained in:
2021-05-22 22:36:13 +02:00
parent baec902243
commit 78774909fc
4 changed files with 72 additions and 16 deletions

View File

@ -1,6 +1,7 @@
use serde::{Deserialize, Serialize};
use serde_json;
use crate::subscription;
use crate::database;
use crate::error::*;
use crypto::digest::Digest;
@ -18,8 +19,9 @@ pub fn endpoint(envelope: Envelope) -> Result<serde_json::Value, Error> {
// TODO domain_check
match envelope.endpoint.as_str() {
"echo" => Ok(serde_json::to_value(echo(serde_json::from_value(envelope.data)?)?)?),
"authorize" => Ok(serde_json::to_value(authorize(serde_json::from_value(envelope.data)?)?)?),
"notify" => Ok(serde_json::to_value(notify(serde_json::from_value(envelope.data)?)?)?),
"authenticate" => Ok(serde_json::to_value(authenticate(serde_json::from_value(envelope.data)?)?)?),
"subscribe" => Ok(serde_json::to_value(subscribe(serde_json::from_value(envelope.data)?)?)?),
"send_event" => Ok(serde_json::to_value(send_event(serde_json::from_value(envelope.data)?)?)?),
_ => return Err(Error::new(Kind::InvalidEndpointError, Class::ClientError)),
}
}
@ -64,24 +66,24 @@ pub fn echo(input: EchoInput) -> Result<EchoOutput, Error> {
}
#[derive(Serialize, Deserialize)]
pub struct AuthorizeInput {
pub struct AuthenticateInput {
r#type: String,
name: String,
password: String,
}
#[derive(Serialize, Deserialize)]
pub struct AuthorizeOutput {
pub struct AuthenticateOutput {
token: String,
}
pub fn authorize(input: AuthorizeInput) -> Result<AuthorizeOutput, Error> {
pub fn authenticate(input: AuthenticateInput) -> Result<AuthenticateOutput, Error> {
let backend = database::client()?;
let mut token;
match backend {
database::Client::Postgres(mut client) => {
let res = client.query("SELECT account_id FROM accounts WHERE name = $1", &[&input.name])?;
let res = client.query("SELECT account_id FROM accounts WHERE account_name = $1", &[&input.name])?;
if res.len() == 0 {
return Err(Error::new(Kind::AuthenticationError, Class::ClientError));
}
@ -89,7 +91,7 @@ pub fn authorize(input: AuthorizeInput) -> Result<AuthorizeOutput, Error> {
}
}
Ok(AuthorizeOutput { token })
Ok(AuthenticateOutput { token })
}
#[derive(Serialize, Deserialize)]
@ -110,23 +112,30 @@ pub fn send_event(input: SendEventInput) -> Result<SendEventOutput, Error> {
match backend {
database::Client::Postgres(mut client) => {
client.execute("INSERT INTO events (event_id, room_id, data) VALUES ($1, $2, $3)", &[&event_id, &input.room_id, &data])?;
client.execute("INSERT INTO events (event_id, room_id, data) VALUES ($1, $2, to_jsonb($3::text))", &[&event_id, &input.room_id, &data])?;
}
}
subscription::notify(subscription::Event {
data: input.data
});
Ok(SendEventOutput { event_id })
}
#[derive(Serialize, Deserialize)]
pub struct NotifyInput {
pub struct SubscribeInput {
}
#[derive(Serialize, Deserialize)]
pub struct NotifyOutput {
pub struct SubscribeOutput {
event: subscription::Event,
}
pub fn notify(input: NotifyInput) -> Result<NotifyOutput, Error> {
Ok(NotifyOutput {})
pub fn subscribe(input: SubscribeInput) -> Result<SubscribeOutput, Error> {
let rx = subscription::subscribe();
let event = rx.recv().unwrap();
subscription::unsubscribe(rx);
Ok(SubscribeOutput { event })
}