Instant message #24

Merged
phoenix merged 14 commits from instant_message into alot_of_changes 2026-06-20 20:07:19 -04:00
9 changed files with 1057 additions and 32 deletions
+1 -1
View File
@@ -8,6 +8,6 @@ DB_MAIN_SSLMODE=disable
DATABASE_URL=postgres://${DB_MAIN_USER}:${DB_MAIN_PASSWORD}@${DB_MAIN_HOST}:${DB_MAIN_PORT}/${DB_MAIN_NAME}
TWILIO_AUTH_SID=9M438C93R943U4329MCU43C34U
TWILIO_SERVICE_SID=9M4J3X8439U398NUVT3342MC349C348T
TWILIO_AUTH_TOKEN="f4a1f2b0b79ea3735078c2d8ee9684e1"
TWILIO_AUTH_TOKEN="MA08HJp9tYtkfs72m8JIIROofRVm74oR0ixEYBHGYpB9"
TWILIO_PHONE_NUMBER=+10123456789
ALLOWED_ORIGINS="http://textsender.com"
+1 -1
View File
@@ -8,6 +8,6 @@ DB_MAIN_SSLMODE=disable
DATABASE_URL=postgres://${DB_MAIN_USER}:${DB_MAIN_PASSWORD}@${DB_MAIN_HOST}:${DB_MAIN_PORT}/${DB_MAIN_NAME}
TWILIO_AUTH_SID=9M438C93R943U4329MCU43C34U
TWILIO_SERVICE_SID=9M4J3X8439U398NUVT3342MC349C348T
TWILIO_AUTH_TOKEN="f4a1f2b0b79ea3735078c2d8ee9684e1"
TWILIO_AUTH_TOKEN="MA08HJp9tYtkfs72m8JIIROofRVm74oR0ixEYBHGYpB9"
TWILIO_PHONE_NUMBER=+10123456789
ALLOWED_ORIGINS="http://textsender.com"
Generated
+836 -27
View File
File diff suppressed because it is too large Load Diff
+2 -1
View File
@@ -22,7 +22,8 @@ jsonwebtoken = { version = "10.4.0", features = ["rust_crypto"] }
josekit = { version = "0.10.3" }
utoipa = { version = "5.5.0", features = ["axum_extras"] }
utoipa-swagger-ui = { version = "9.0.2", features = ["axum"] }
textsender_models = { git = "ssh://git@git.kundeng.us/phoenix/textsender_models.git", tag = "v0.4.3-29-384dd41690-111" }
textsender_models = { git = "ssh://git@git.kundeng.us/phoenix/textsender_models.git", tag = "v0.4.3-29-7e5bc016b1-111" }
swoosh = { git = "ssh://git@git.kundeng.us/phoenix/swoosh.git", tag = "v0.4.1-10-9e8ec15910-871" }
[dev-dependencies]
url = { version = "2.5.8" }
+206
View File
@@ -0,0 +1,206 @@
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 load_config().await {
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 mut results: Vec<serde_json::Value> = Vec::new();
let pp = swoosh::twilio::types::Parameters {
schedule: false,
schedule_at: None,
};
for contact in &contacts {
match swoosh::twilio::api::send_mssage(
&message, contact, &pp, &t_config,
)
.await
{
Ok(result) => {
println!("Response: {result:?}");
let val = swoosh::twilio::api::response_to_json(result).await;
// results.push(val);
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))
}
}
async fn load_config()
-> Result<textsender_models::config::auxiliary::TwilioConfig, std::io::Error> {
let mut config = textsender_models::config::auxiliary::TwilioConfig {
..Default::default()
};
let auth_sid_var = textsender_models::envy::environment::get_env("TWILIO_AUTH_SID").await;
println!("{auth_sid_var:?}");
let service_sid_var =
textsender_models::envy::environment::get_env("TWILIO_SERVICE_SID").await;
println!("{service_sid_var:?}");
let auth_token_var =
textsender_models::envy::environment::get_env("TWILIO_AUTH_TOKEN").await;
println!("{auth_token_var:?}");
let phone_number_var =
textsender_models::envy::environment::get_env("TWILIO_PHONE_NUMBER").await;
println!("{phone_number_var:?}");
if auth_sid_var.value.is_empty()
|| service_sid_var.value.is_empty()
|| auth_token_var.value.is_empty()
|| phone_number_var.value.is_empty()
{
Err(std::io::Error::other("Config not set"))
} else {
config.account_sid = auth_sid_var.value;
config.service_sid = service_sid_var.value;
config.auth_token = auth_token_var.value;
config.phone_number = phone_number_var.value;
Ok(config)
}
}
}
+1 -1
View File
@@ -75,7 +75,7 @@ pub mod endpoint {
if payload.is_valid() {
let mut mer = textsender_models::message::event::MessageEventResponse {
scheduled_message_event_id: payload.scheduled_message_event_id,
scheduled_message_event_id: Some(payload.scheduled_message_event_id),
user_id: payload.user_id,
sent: payload.sent,
status: payload.status,
+3
View File
@@ -1,4 +1,5 @@
pub mod contact;
pub mod instant;
pub mod message;
pub mod endpoints {
@@ -33,6 +34,8 @@ pub mod endpoints {
"/api/v1/schedule/message/event/response/record";
/// Constant for getting Message Event Response endpoint
pub const GET_MESSAGE_EVENT_RESPONSE: &str = "/api/v1/schedule/message/event/response";
/// Constant for sending a message instantly. No scheduling
pub const INSTANT_MESSAGE: &str = "/api/v1/instant/message";
}
pub mod response {
+6
View File
@@ -183,6 +183,12 @@ pub mod init {
axum::middleware::from_fn(crate::auth::auth::<axum::body::Body>),
),
)
.route(
crate::caller::endpoints::INSTANT_MESSAGE,
post(crate::caller::instant::endpoint::send_message).route_layer(
axum::middleware::from_fn(crate::auth::auth::<axum::body::Body>),
),
)
.layer(cors::configure_cors().await)
}
+1 -1
View File
@@ -106,7 +106,7 @@ async fn parse_row(
message_id,
status,
sent: Some(sent),
scheduled_message_event_id,
scheduled_message_event_id: Some(scheduled_message_event_id),
user_id,
})
}