Rust Build / Test Suite (push) Failing after 58s
Rust Build / Rustfmt (push) Successful in 27s
Rust Build / Check (push) Successful in 2m7s
Rust Build / Clippy (push) Successful in 1m21s
Rust Build / build (push) Successful in 3m5s
textsender_api PR / Rustfmt (pull_request) Successful in 2m0s
textsender_api PR / Check (pull_request) Successful in 2m2s
textsender_api PR / Clippy (pull_request) Successful in 2m37s
Reviewed-on: phoenix/textsender_api#30
177 lines
7.6 KiB
Rust
177 lines
7.6 KiB
Rust
pub mod request {
|
|
use serde::{Deserialize, Serialize};
|
|
use utoipa::ToSchema;
|
|
|
|
#[derive(Debug, Deserialize, Serialize, ToSchema)]
|
|
pub struct InstantMessageRequest {
|
|
pub contact_ids: Vec<uuid::Uuid>,
|
|
pub message_id: uuid::Uuid,
|
|
pub user_id: uuid::Uuid,
|
|
}
|
|
|
|
impl InstantMessageRequest {
|
|
pub fn is_valid(&self) -> bool {
|
|
!self.contact_ids.is_empty() || !self.message_id.is_nil() || !self.user_id.is_nil()
|
|
}
|
|
}
|
|
}
|
|
|
|
pub mod response {
|
|
use serde::{Deserialize, Serialize};
|
|
use utoipa::ToSchema;
|
|
|
|
#[derive(Debug, Default, Deserialize, Serialize, ToSchema)]
|
|
pub struct InstantMessageResponse {
|
|
pub message: String,
|
|
pub data: Vec<textsender_models::message::event::MessageEventResponse>,
|
|
}
|
|
}
|
|
|
|
pub mod endpoint {
|
|
use crate::repo::contact as contact_repo;
|
|
use crate::repo::message as message_repo;
|
|
use message_repo::event as event_repo;
|
|
|
|
/// Endpoint to create Message
|
|
#[utoipa::path(
|
|
post,
|
|
path = super::super::endpoints::ADD_MESSAGE,
|
|
request_body(
|
|
content = super::request::InstantMessageRequest,
|
|
description = "Data needed to create a Message",
|
|
content_type = "application/json"
|
|
),
|
|
responses(
|
|
(status = 201, description = "Message created", body = super::response::InstantMessageResponse),
|
|
(status = 400, description = "Error", body = super::response::InstantMessageResponse),
|
|
(status = 500, description = "Error creating Message", body = super::response::InstantMessageResponse)
|
|
)
|
|
)]
|
|
pub async fn send_message(
|
|
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
|
axum::Json(payload): axum::Json<super::request::InstantMessageRequest>,
|
|
) -> (
|
|
axum::http::StatusCode,
|
|
axum::Json<super::response::InstantMessageResponse>,
|
|
) {
|
|
let mut response = super::response::InstantMessageResponse::default();
|
|
|
|
if payload.is_valid() {
|
|
let mut contacts: Vec<textsender_models::contact::Contact> = Vec::new();
|
|
|
|
for contact_id in &payload.contact_ids {
|
|
match contact_repo::get(&pool, contact_id).await {
|
|
Ok(contact) => {
|
|
contacts.push(contact);
|
|
}
|
|
Err(err) => {
|
|
eprintln!("Error: {err:?}");
|
|
response.message = String::from("Invalid");
|
|
return (
|
|
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
|
axum::Json(response),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
if !contacts.is_empty() {
|
|
println!("Valid contacts");
|
|
|
|
match message_repo::get(&pool, &payload.message_id).await {
|
|
Ok(message) => {
|
|
println!("Valid message");
|
|
let t_config = match textsender_models::config::auxiliary::load_config() {
|
|
Ok(config) => config,
|
|
Err(err) => {
|
|
eprintln!("Error: {err:?}");
|
|
response.message = String::from("Error getting config");
|
|
return (
|
|
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
|
axum::Json(response),
|
|
);
|
|
}
|
|
};
|
|
let mut results: Vec<
|
|
textsender_models::message::event::MessageEventResponse,
|
|
> = Vec::new();
|
|
let pp = swoosh::twilio::types::Parameters {
|
|
schedule: false,
|
|
schedule_at: None,
|
|
};
|
|
|
|
let mut sendmsg = swoosh::SendMsg::default();
|
|
sendmsg.load_config(
|
|
&t_config.auth_token,
|
|
&t_config.phone_number,
|
|
&t_config.service_sid,
|
|
&t_config.account_sid,
|
|
);
|
|
|
|
for contact in &contacts {
|
|
sendmsg.load_message(&message.content);
|
|
sendmsg.load_recipient(&contact.phone_number);
|
|
match sendmsg.send_message(&pp).await {
|
|
Ok(result) => {
|
|
let val = swoosh::twilio::api::response_to_json(result).await;
|
|
let mer = textsender_models::message::event::MessageEventResponse {
|
|
user_id: payload.user_id,
|
|
// TODO: Make this more accurate
|
|
sent: Some(time::OffsetDateTime::now_utc()),
|
|
status: String::from(textsender_models::message::event::MESSAGE_EVENT_RESPONSE_STATUS_INSTANT),
|
|
contact_id: contact.id.unwrap(),
|
|
message_id: message.id.unwrap(),
|
|
response: val,
|
|
..Default::default()
|
|
};
|
|
results.push(mer);
|
|
}
|
|
Err(err) => {
|
|
eprintln!("Error: {err:?}");
|
|
}
|
|
}
|
|
}
|
|
|
|
if results.is_empty() {
|
|
eprintln!("Error sending");
|
|
(
|
|
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
|
axum::Json(response),
|
|
)
|
|
} else {
|
|
for result in &mut results {
|
|
match event_repo::insert(&pool, result).await {
|
|
Ok(i) => {
|
|
result.id = i;
|
|
}
|
|
Err(err) => {
|
|
eprintln!("Error: {err:?}");
|
|
return (
|
|
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
|
axum::Json(response),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
response.data = results;
|
|
response.message = String::from(super::super::response::SUCCESSFUL);
|
|
(axum::http::StatusCode::OK, axum::Json(response))
|
|
}
|
|
}
|
|
Err(err) => {
|
|
eprintln!("Error: {err:?}");
|
|
response.message = String::from("Request body is not valid");
|
|
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
|
}
|
|
}
|
|
} else {
|
|
response.message = String::from("Request body is not valid");
|
|
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
|
}
|
|
} else {
|
|
response.message = String::from("Request body is not valid");
|
|
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
|
}
|
|
}
|
|
}
|