Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f9c098b92f | ||
|
|
6801b1e635 | ||
|
|
50aa9089f0 | ||
|
|
8dbfd88aae | ||
|
|
71ba4a9c56 | ||
|
|
8fc4bb7626 |
@@ -4,6 +4,9 @@ on:
|
|||||||
pull_request:
|
pull_request:
|
||||||
branches:
|
branches:
|
||||||
- main
|
- main
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
|
||||||
concurrency:
|
concurrency:
|
||||||
group: ${{ github.workflow }}-${{ github.ref }}
|
group: ${{ github.workflow }}-${{ github.ref }}
|
||||||
@@ -14,10 +17,10 @@ jobs:
|
|||||||
name: Check
|
name: Check
|
||||||
runs-on: ubuntu-24.04
|
runs-on: ubuntu-24.04
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v5
|
- uses: actions/checkout@v6
|
||||||
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||||
with:
|
with:
|
||||||
toolchain: 1.96
|
toolchain: 1.97
|
||||||
- uses: Swatinem/rust-cache@v2
|
- uses: Swatinem/rust-cache@v2
|
||||||
- run: |
|
- run: |
|
||||||
mkdir -p ~/.ssh
|
mkdir -p ~/.ssh
|
||||||
@@ -33,10 +36,10 @@ jobs:
|
|||||||
name: Rustfmt
|
name: Rustfmt
|
||||||
runs-on: ubuntu-24.04
|
runs-on: ubuntu-24.04
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v5
|
- uses: actions/checkout@v6
|
||||||
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||||
with:
|
with:
|
||||||
toolchain: 1.96
|
toolchain: 1.97
|
||||||
- run: rustup component add rustfmt
|
- run: rustup component add rustfmt
|
||||||
- uses: Swatinem/rust-cache@v2
|
- uses: Swatinem/rust-cache@v2
|
||||||
- run: |
|
- run: |
|
||||||
@@ -53,10 +56,10 @@ jobs:
|
|||||||
name: Clippy
|
name: Clippy
|
||||||
runs-on: ubuntu-24.04
|
runs-on: ubuntu-24.04
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v5
|
- uses: actions/checkout@v6
|
||||||
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||||
with:
|
with:
|
||||||
toolchain: 1.96
|
toolchain: 1.97
|
||||||
- run: rustup component add clippy
|
- run: rustup component add clippy
|
||||||
- uses: Swatinem/rust-cache@v2
|
- uses: Swatinem/rust-cache@v2
|
||||||
- run: |
|
- run: |
|
||||||
|
|||||||
Generated
+227
-649
File diff suppressed because it is too large
Load Diff
+8
-8
@@ -1,18 +1,18 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "catapult"
|
name = "catapult"
|
||||||
version = "0.3.0"
|
version = "0.3.5"
|
||||||
rust-version = "1.96"
|
rust-version = "1.97"
|
||||||
|
license = "MIT"
|
||||||
|
description = "Service to carry out text message scheduling"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
tokio = { version = "1.52.3", features = ["full"] }
|
tokio = { version = "1.52.3", features = ["full"] }
|
||||||
futures = { version = "0.3.32" }
|
|
||||||
reqwest = { version = "0.13.4", features = ["json", "stream", "multipart"] }
|
reqwest = { version = "0.13.4", features = ["json", "stream", "multipart"] }
|
||||||
serde = { version = "1.0.228", features = ["derive"] }
|
|
||||||
serde_json = { version = "1.0.150" }
|
serde_json = { version = "1.0.150" }
|
||||||
time = { version = "0.3.49", features = ["formatting", "macros", "parsing", "serde"] }
|
time = { version = "0.3.53", features = ["formatting", "macros", "parsing", "serde"] }
|
||||||
uuid = { version = "1.23.3", features = ["v4", "serde"] }
|
uuid = { version = "1.23.5", features = ["v4", "serde"] }
|
||||||
textsender_models = { git = "ssh://git@git.kundeng.us/phoenix/textsender_models.git", tag = "v0.4.10" }
|
schedtxt_models = { git = "ssh://git@git.kundeng.us/phoenix/schedtxt_models.git", tag = "v0.5.3" }
|
||||||
swoosh = { git = "ssh://git@git.kundeng.us/phoenix/swoosh.git", tag = "v0.4.2.0" }
|
swoosh = { git = "ssh://git@git.kundeng.us/phoenix/swoosh.git", tag = "v0.5.4" }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
|||||||
+1
-18
@@ -1,5 +1,4 @@
|
|||||||
# Stage 1: Build the application
|
FROM rust:1.97 as builder
|
||||||
FROM rust:1.96 as builder
|
|
||||||
|
|
||||||
# Set the working directory inside the container
|
# Set the working directory inside the container
|
||||||
WORKDIR /usr/src/app
|
WORKDIR /usr/src/app
|
||||||
@@ -11,35 +10,23 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
|
|||||||
openssh-client git \
|
openssh-client git \
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
# << --- ADD HOST KEY HERE --- >>
|
|
||||||
# Replace 'yourgithost.com' with the actual hostname (e.g., github.com)
|
|
||||||
RUN mkdir -p -m 0700 ~/.ssh && \
|
RUN mkdir -p -m 0700 ~/.ssh && \
|
||||||
ssh-keyscan git.kundeng.us >> ~/.ssh/known_hosts
|
ssh-keyscan git.kundeng.us >> ~/.ssh/known_hosts
|
||||||
|
|
||||||
# Copy Cargo manifests
|
|
||||||
COPY Cargo.toml Cargo.lock ./
|
COPY Cargo.toml Cargo.lock ./
|
||||||
|
|
||||||
# Build *only* dependencies to leverage Docker cache
|
|
||||||
# This dummy build caches dependencies as a separate layer
|
|
||||||
RUN --mount=type=ssh mkdir src && \
|
RUN --mount=type=ssh mkdir src && \
|
||||||
echo "fn main() {println!(\"if you see this, the build broke\")}" > src/main.rs && \
|
echo "fn main() {println!(\"if you see this, the build broke\")}" > src/main.rs && \
|
||||||
cargo build --release --quiet && \
|
cargo build --release --quiet && \
|
||||||
rm -rf src target/release/deps/catapult*
|
rm -rf src target/release/deps/catapult*
|
||||||
|
|
||||||
# Copy the actual source code
|
|
||||||
COPY src ./src
|
COPY src ./src
|
||||||
# If you have other directories like `templates` or `static`, copy them too
|
# If you have other directories like `templates` or `static`, copy them too
|
||||||
COPY .env ./.env
|
COPY .env ./.env
|
||||||
|
|
||||||
# << --- SSH MOUNT ADDED HERE --- >>
|
|
||||||
# Build *only* dependencies to leverage Docker cache
|
|
||||||
# This dummy build caches dependencies as a separate layer
|
|
||||||
# Mount the SSH agent socket for this command
|
|
||||||
RUN --mount=type=ssh \
|
RUN --mount=type=ssh \
|
||||||
cargo build --release --quiet
|
cargo build --release --quiet
|
||||||
|
|
||||||
# Stage 2: Create the final, smaller runtime image
|
|
||||||
# Use a minimal base image like debian-slim or even distroless for security/size
|
|
||||||
FROM debian:trixie-slim
|
FROM debian:trixie-slim
|
||||||
|
|
||||||
# Install runtime dependencies if needed (e.g., SSL certificates)
|
# Install runtime dependencies if needed (e.g., SSL certificates)
|
||||||
@@ -48,12 +35,8 @@ RUN apt-get update && apt-get install -y ca-certificates libssl-dev libssl3 && r
|
|||||||
# Set the working directory
|
# Set the working directory
|
||||||
WORKDIR /usr/local/bin
|
WORKDIR /usr/local/bin
|
||||||
|
|
||||||
# Copy the compiled binary from the builder stage
|
|
||||||
# Replace 'catapult' with the actual name of your binary (usually the crate name)
|
|
||||||
COPY --from=builder /usr/src/app/target/release/catapult .
|
COPY --from=builder /usr/src/app/target/release/catapult .
|
||||||
|
|
||||||
# Copy other necessary files like .env (if used for runtime config) or static assets
|
|
||||||
# It's generally better to configure via environment variables in Docker though
|
|
||||||
COPY --from=builder /usr/src/app/.env .
|
COPY --from=builder /usr/src/app/.env .
|
||||||
|
|
||||||
# Set the command to run your application
|
# Set the command to run your application
|
||||||
|
|||||||
+22
@@ -0,0 +1,22 @@
|
|||||||
|
Copyright (c) 2026 Kun Deng.
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person
|
||||||
|
obtaining a copy of this software and associated documentation
|
||||||
|
files (the "Software"), to deal in the Software without
|
||||||
|
restriction, including without limitation the rights to use,
|
||||||
|
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the
|
||||||
|
Software is furnished to do so, subject to the following
|
||||||
|
conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be
|
||||||
|
included in all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||||
|
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||||
|
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||||
|
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||||
|
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||||
|
OTHER DEALINGS IN THE SOFTWARE.
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
# catapult
|
||||||
|
A service that manages scheduling text messages.
|
||||||
|
|
||||||
|
|
||||||
|
## Getting started
|
||||||
|
Docker isn't required, but it is the quickest way to get started.
|
||||||
|
|
||||||
|
Copy over the `.env.docker.sample` to `.env`. From that point, make edits to the
|
||||||
|
file to get it working.
|
||||||
|
|
||||||
|
### Messaging configuration
|
||||||
|
The service vendor used to send the messages is `twilio`. After signing up and being
|
||||||
|
provided credentials, update the `TWILIO_*` variables.
|
||||||
|
|
||||||
|
### Client-Server interaction
|
||||||
|
In order for `catapult` to function properly, credentials need to be provided via
|
||||||
|
`SERVICE_USERNAME` and `SERVICE_PASSPHRASE`. The credentials are created from making
|
||||||
|
an API call to register a service user in `textsender_auth`. So, you need to have credentials
|
||||||
|
set in the `.env` file and use those same credentials to create the account in the
|
||||||
|
API call.
|
||||||
|
|
||||||
|
### API sources
|
||||||
|
The `AUTH_URL` and `API_URL` variables need to be set for the base URL for the
|
||||||
|
`textsender_auth` and `textsender_api` respectively.
|
||||||
|
|
||||||
|
|
||||||
|
Since this is meant to run along with `textsender_auth` and `textsender_api`, it isn't
|
||||||
|
meant to be run by itself. So no need to build or run the container in isolation, but
|
||||||
|
that capability is present.
|
||||||
+2
-2
@@ -4,8 +4,8 @@ services:
|
|||||||
songparser:
|
songparser:
|
||||||
build:
|
build:
|
||||||
context: .
|
context: .
|
||||||
ssh: ["default"] # Uses host's SSH agent
|
ssh: ["default"]
|
||||||
container_name: catapult
|
container_name: catapult
|
||||||
env_file:
|
env_file:
|
||||||
- .env
|
- .env
|
||||||
restart: unless-stopped # Optional: Restart policy
|
restart: unless-stopped
|
||||||
|
|||||||
+25
-21
@@ -9,26 +9,24 @@ pub struct App {
|
|||||||
pub auth_url: String,
|
pub auth_url: String,
|
||||||
pub service_username: String,
|
pub service_username: String,
|
||||||
pub service_passphrase: String,
|
pub service_passphrase: String,
|
||||||
pub twilio_config: textsender_models::config::auxiliary::TwilioConfig,
|
pub twilio_config: schedtxt_models::config::auxiliary::TwilioConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Change this to non-async at some point.
|
pub fn load_app() -> Result<App, std::io::Error> {
|
||||||
// Will require changes in other libraries
|
use schedtxt_models::envy;
|
||||||
pub async fn load_app() -> Result<App, std::io::Error> {
|
let auth_url_env = envy::environment::get_env("AUTH_URL");
|
||||||
use textsender_models::envy;
|
let api_url_env = envy::environment::get_env("API_URL");
|
||||||
let auth_url_env = envy::environment::get_env("AUTH_URL").await;
|
let service_username_env = envy::environment::get_env("SERVICE_USERNAME");
|
||||||
let api_url_env = envy::environment::get_env("API_URL").await;
|
let service_passphrase_env = envy::environment::get_env("SERVICE_PASSPHRASE");
|
||||||
let service_username_env = envy::environment::get_env("SERVICE_USERNAME").await;
|
|
||||||
let service_passphrase_env = envy::environment::get_env("SERVICE_PASSPHRASE").await;
|
|
||||||
|
|
||||||
let envs = vec![
|
let envs = vec![
|
||||||
api_url_env,
|
&api_url_env,
|
||||||
auth_url_env,
|
&auth_url_env,
|
||||||
service_username_env,
|
&service_username_env,
|
||||||
service_passphrase_env,
|
&service_passphrase_env,
|
||||||
];
|
];
|
||||||
|
|
||||||
let check_envs = |vs: &Vec<envy::EnvVar>| -> (bool, Option<String>) {
|
let check_envs = |vs: &Vec<&envy::EnvVar>| -> (bool, Option<String>) {
|
||||||
for env in vs {
|
for env in vs {
|
||||||
if env.value.is_empty() {
|
if env.value.is_empty() {
|
||||||
return (false, Some(format!("Key {} is not provided", env.key)));
|
return (false, Some(format!("Key {} is not provided", env.key)));
|
||||||
@@ -38,19 +36,25 @@ pub async fn load_app() -> Result<App, std::io::Error> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let (envs_valid, reason) = check_envs(&envs);
|
let (envs_valid, reason) = check_envs(&envs);
|
||||||
|
|
||||||
if envs_valid {
|
if envs_valid {
|
||||||
match textsender_models::config::auxiliary::load_config().await {
|
let auth_url: String = auth_url_env.value;
|
||||||
|
let api_url: String = api_url_env.value;
|
||||||
|
let service_username: String = service_username_env.value;
|
||||||
|
let service_passphrase: String = service_passphrase_env.value;
|
||||||
|
match schedtxt_models::config::auxiliary::load_config() {
|
||||||
Ok(twilio_config) => Ok(App {
|
Ok(twilio_config) => Ok(App {
|
||||||
api_url: envs[0].value.clone(),
|
api_url,
|
||||||
auth_url: envs[1].value.clone(),
|
auth_url,
|
||||||
service_username: envs[2].value.clone(),
|
service_username,
|
||||||
service_passphrase: envs[3].value.clone(),
|
service_passphrase,
|
||||||
twilio_config,
|
twilio_config,
|
||||||
}),
|
}),
|
||||||
Err(err) => Err(err),
|
Err(err) => Err(err),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Err(std::io::Error::other(reason.unwrap()))
|
match reason {
|
||||||
|
Some(reason) => Err(std::io::Error::other(reason)),
|
||||||
|
None => Err(std::io::Error::other("No reason found")),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-7
@@ -6,8 +6,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
std::process::exit(-1);
|
std::process::exit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
let app = match catapult::app::load_app().await {
|
let app = match catapult::app::load_app() {
|
||||||
Ok(app) => app,
|
Ok(app) => std::sync::Arc::new(app),
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
eprintln!("Error: {err:?}");
|
eprintln!("Error: {err:?}");
|
||||||
std::process::exit(-1);
|
std::process::exit(-1);
|
||||||
@@ -16,9 +16,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
|
|
||||||
println!("App: {app:?}");
|
println!("App: {app:?}");
|
||||||
|
|
||||||
let auth = catapult::service::auth::Auth { app: app.clone() };
|
let auth = catapult::service::auth::Auth {
|
||||||
|
app: std::sync::Arc::clone(&app),
|
||||||
|
};
|
||||||
let token = match auth.get_token().await {
|
let token = match auth.get_token().await {
|
||||||
Ok(token) => token,
|
Ok(token) => std::sync::Arc::new(token),
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
eprintln!("Error getting token");
|
eprintln!("Error getting token");
|
||||||
eprintln!("Error: {err:?}");
|
eprintln!("Error: {err:?}");
|
||||||
@@ -27,8 +29,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let mut svc = catapult::service::core::Service {
|
let mut svc = catapult::service::core::Service {
|
||||||
app: app.clone(),
|
app: std::sync::Arc::clone(&app),
|
||||||
token,
|
token: std::sync::Arc::clone(&token),
|
||||||
};
|
};
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
@@ -36,7 +38,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
match auth.get_refresh_token(&svc.token).await {
|
match auth.get_refresh_token(&svc.token).await {
|
||||||
Ok(refresh) => {
|
Ok(refresh) => {
|
||||||
println!("Refresh token: {refresh:?}");
|
println!("Refresh token: {refresh:?}");
|
||||||
svc.token = refresh;
|
let refreshed = std::sync::Arc::new(refresh);
|
||||||
|
svc.token = refreshed;
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
eprintln!("Error: {err:?}");
|
eprintln!("Error: {err:?}");
|
||||||
|
|||||||
+7
-7
@@ -1,9 +1,9 @@
|
|||||||
pub struct Auth {
|
pub struct Auth {
|
||||||
pub app: crate::app::App,
|
pub app: std::sync::Arc<crate::app::App>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Auth {
|
impl Auth {
|
||||||
pub async fn get_token(&self) -> Result<textsender_models::token::Login, std::io::Error> {
|
pub async fn get_token(&self) -> Result<schedtxt_models::token::Login, std::io::Error> {
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
let endpoint = String::from("api/v1/service/login");
|
let endpoint = String::from("api/v1/service/login");
|
||||||
let api_url = format!("{}/{endpoint}", self.app.auth_url);
|
let api_url = format!("{}/{endpoint}", self.app.auth_url);
|
||||||
@@ -37,8 +37,8 @@ impl Auth {
|
|||||||
|
|
||||||
pub async fn get_refresh_token(
|
pub async fn get_refresh_token(
|
||||||
&self,
|
&self,
|
||||||
login_result: &textsender_models::token::Login,
|
login_result: &schedtxt_models::token::Login,
|
||||||
) -> Result<textsender_models::token::Login, std::io::Error> {
|
) -> Result<schedtxt_models::token::Login, std::io::Error> {
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
let endpoint = String::from("api/v1/token/refresh");
|
let endpoint = String::from("api/v1/token/refresh");
|
||||||
let api_url = format!("{}/{endpoint}", self.app.auth_url);
|
let api_url = format!("{}/{endpoint}", self.app.auth_url);
|
||||||
@@ -70,7 +70,7 @@ impl Auth {
|
|||||||
|
|
||||||
async fn parse_token_response(
|
async fn parse_token_response(
|
||||||
response: &str,
|
response: &str,
|
||||||
) -> Result<textsender_models::token::LoginResult, std::io::Error> {
|
) -> Result<schedtxt_models::token::LoginResult, std::io::Error> {
|
||||||
match serde_json::from_str::<serde_json::Value>(response) {
|
match serde_json::from_str::<serde_json::Value>(response) {
|
||||||
Ok(j) => {
|
Ok(j) => {
|
||||||
println!("Good");
|
println!("Good");
|
||||||
@@ -85,7 +85,7 @@ async fn parse_token_response(
|
|||||||
Err(std::io::Error::other("Error response is empty"))
|
Err(std::io::Error::other("Error response is empty"))
|
||||||
} else {
|
} else {
|
||||||
let lr = lrs[0].clone();
|
let lr = lrs[0].clone();
|
||||||
let ll: textsender_models::token::LoginResult =
|
let ll: schedtxt_models::token::LoginResult =
|
||||||
match serde_json::from_value(lr) {
|
match serde_json::from_value(lr) {
|
||||||
Ok(lr) => lr,
|
Ok(lr) => lr,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
@@ -102,7 +102,7 @@ async fn parse_token_response(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn has_token_expired(token: &textsender_models::token::LoginResult) -> bool {
|
pub async fn has_token_expired(token: &schedtxt_models::token::LoginResult) -> bool {
|
||||||
let now = time::OffsetDateTime::now_utc();
|
let now = time::OffsetDateTime::now_utc();
|
||||||
let expire = match time::OffsetDateTime::from_unix_timestamp(token.expires_in) {
|
let expire = match time::OffsetDateTime::from_unix_timestamp(token.expires_in) {
|
||||||
Ok(res) => res,
|
Ok(res) => res,
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
pub struct Contact {
|
pub struct Contact {
|
||||||
pub app: crate::app::App,
|
pub app: std::sync::Arc<crate::app::App>,
|
||||||
pub token: textsender_models::token::LoginResult,
|
pub token: std::sync::Arc<schedtxt_models::token::LoginResult>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Contact {
|
impl Contact {
|
||||||
pub async fn get(
|
pub async fn get(
|
||||||
&self,
|
&self,
|
||||||
contact_id: &uuid::Uuid,
|
contact_id: &uuid::Uuid,
|
||||||
) -> Result<Vec<textsender_models::contact::Contact>, std::io::Error> {
|
) -> Result<Vec<schedtxt_models::contact::Contact>, std::io::Error> {
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
let endpoint = format!("api/v1/contact?id={contact_id}");
|
let endpoint = format!("api/v1/contact?id={contact_id}");
|
||||||
let api_url = format!("{}/{endpoint}", self.app.api_url);
|
let api_url = format!("{}/{endpoint}", self.app.api_url);
|
||||||
@@ -34,17 +34,17 @@ impl Contact {
|
|||||||
|
|
||||||
async fn parse_response(
|
async fn parse_response(
|
||||||
response: &str,
|
response: &str,
|
||||||
) -> Result<Vec<textsender_models::contact::Contact>, std::io::Error> {
|
) -> Result<Vec<schedtxt_models::contact::Contact>, std::io::Error> {
|
||||||
match serde_json::from_str::<serde_json::Value>(response) {
|
match serde_json::from_str::<serde_json::Value>(response) {
|
||||||
Ok(j) => match j.get("data") {
|
Ok(j) => match j.get("data") {
|
||||||
Some(serde_json::Value::Array(lrs)) => {
|
Some(serde_json::Value::Array(lrs)) => {
|
||||||
if lrs.is_empty() {
|
if lrs.is_empty() {
|
||||||
Err(std::io::Error::other("Error response is empty"))
|
Err(std::io::Error::other("Error response is empty"))
|
||||||
} else {
|
} else {
|
||||||
let mut events: Vec<textsender_models::contact::Contact> = Vec::new();
|
let mut events: Vec<schedtxt_models::contact::Contact> = Vec::new();
|
||||||
|
|
||||||
for event in lrs.iter() {
|
for event in lrs.iter() {
|
||||||
let ll: textsender_models::contact::Contact =
|
let ll: schedtxt_models::contact::Contact =
|
||||||
match serde_json::from_value(event.clone()) {
|
match serde_json::from_value(event.clone()) {
|
||||||
Ok(lr) => lr,
|
Ok(lr) => lr,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
|
|||||||
+29
-45
@@ -1,18 +1,17 @@
|
|||||||
pub struct Service {
|
pub struct Service {
|
||||||
pub app: crate::app::App,
|
pub app: std::sync::Arc<crate::app::App>,
|
||||||
pub token: textsender_models::token::Login,
|
pub token: std::sync::Arc<schedtxt_models::token::Login>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Service {
|
impl Service {
|
||||||
pub async fn do_the_work(&mut self) {
|
pub async fn do_the_work(&mut self) {
|
||||||
println!("Do some work");
|
|
||||||
println!("Checking queue");
|
println!("Checking queue");
|
||||||
|
|
||||||
use textsender_models::message::scheduling::ScheduledMessage;
|
use schedtxt_models::message::scheduling::ScheduledMessage;
|
||||||
|
|
||||||
let queue = super::queue::Queue {
|
let queue = super::queue::Queue {
|
||||||
app: self.app.clone(),
|
app: std::sync::Arc::clone(&self.app),
|
||||||
token: self.token.clone(),
|
token: std::sync::Arc::clone(&self.token),
|
||||||
};
|
};
|
||||||
let queue_item: Option<ScheduledMessage> = match queue.get_queue().await {
|
let queue_item: Option<ScheduledMessage> = match queue.get_queue().await {
|
||||||
Ok(item) => Some(item),
|
Ok(item) => Some(item),
|
||||||
@@ -38,8 +37,8 @@ impl Service {
|
|||||||
println!("Can schedule");
|
println!("Can schedule");
|
||||||
|
|
||||||
let events_req = super::event::Event {
|
let events_req = super::event::Event {
|
||||||
app: self.app.clone(),
|
app: std::sync::Arc::clone(&self.app),
|
||||||
token: self.token.clone(),
|
token: std::sync::Arc::clone(&self.token),
|
||||||
};
|
};
|
||||||
|
|
||||||
let scheduled_message_id = queue_item.id;
|
let scheduled_message_id = queue_item.id;
|
||||||
@@ -52,23 +51,31 @@ impl Service {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let contacts = super::contact::Contact {
|
let contacts = super::contact::Contact {
|
||||||
app: self.app.clone(),
|
app: std::sync::Arc::clone(&self.app),
|
||||||
token: self.token.clone(),
|
token: std::sync::Arc::clone(&self.token),
|
||||||
};
|
};
|
||||||
let messages = super::message::Message {
|
let messages = super::message::Message {
|
||||||
app: self.app.clone(),
|
app: std::sync::Arc::clone(&self.app),
|
||||||
token: self.token.clone(),
|
token: std::sync::Arc::clone(&self.token),
|
||||||
};
|
};
|
||||||
let sch_msg = super::scheduler::ScheduledMessage {
|
let sch_msg = super::scheduler::ScheduledMessage {
|
||||||
app: self.app.clone(),
|
app: std::sync::Arc::clone(&self.app),
|
||||||
token: self.token.clone(),
|
token: std::sync::Arc::clone(&self.token),
|
||||||
};
|
};
|
||||||
let mer_req = super::mer::MessageEventResponse {
|
let mer_req = super::mer::MessageEventResponse {
|
||||||
app: self.app.clone(),
|
app: std::sync::Arc::clone(&self.app),
|
||||||
token: self.token.clone(),
|
token: std::sync::Arc::clone(&self.token),
|
||||||
};
|
};
|
||||||
|
|
||||||
let events = events.unwrap_or_default();
|
let events = events.unwrap_or_default();
|
||||||
|
let mut sendmsg = swoosh::SendMsg::default();
|
||||||
|
sendmsg.load_config(
|
||||||
|
&self.app.twilio_config.auth_token,
|
||||||
|
&self.app.twilio_config.phone_number,
|
||||||
|
&self.app.twilio_config.service_sid,
|
||||||
|
&self.app.twilio_config.account_sid,
|
||||||
|
);
|
||||||
|
|
||||||
for event in &events {
|
for event in &events {
|
||||||
println!("Event: {event:?}");
|
println!("Event: {event:?}");
|
||||||
let contact = &match contacts.get(&event.contact_id).await {
|
let contact = &match contacts.get(&event.contact_id).await {
|
||||||
@@ -105,13 +112,15 @@ impl Service {
|
|||||||
let twilio_config = &self.app.twilio_config;
|
let twilio_config = &self.app.twilio_config;
|
||||||
println!("Config: {twilio_config:?}");
|
println!("Config: {twilio_config:?}");
|
||||||
println!("SM: {scheduled_message:?}");
|
println!("SM: {scheduled_message:?}");
|
||||||
|
sendmsg.load_message(&message.content);
|
||||||
|
sendmsg.load_recipient(&contact.phone_number);
|
||||||
|
|
||||||
match swoosh::twilio::api::send_message(message, contact, ¶m, twilio_config).await {
|
match sendmsg.send_message(¶m).await {
|
||||||
Ok(resp) => {
|
Ok(resp) => {
|
||||||
println!("Message scheduled");
|
println!("Message scheduled");
|
||||||
// MER response
|
// MER response
|
||||||
let result = swoosh::twilio::api::response_to_json(resp).await;
|
let result = swoosh::twilio::api::response_to_json(resp).await;
|
||||||
let mer = textsender_models::message::event::MessageEventResponse {
|
let mer = schedtxt_models::message::event::MessageEventResponse {
|
||||||
contact_id: contact.id.unwrap(),
|
contact_id: contact.id.unwrap(),
|
||||||
scheduled_message_event_id: Some(event.id),
|
scheduled_message_event_id: Some(event.id),
|
||||||
response: result,
|
response: result,
|
||||||
@@ -138,39 +147,14 @@ impl Service {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
todo!(
|
|
||||||
r#"
|
|
||||||
Need to finish this
|
|
||||||
|
|
||||||
[X] - This function should fetch a token, check if the token is valid, and obtain a refresh token if
|
|
||||||
it has expired.
|
|
||||||
|
|
||||||
Then do the real work.
|
|
||||||
|
|
||||||
1. Check the queue - Done
|
|
||||||
2. If there is an item, get the data - Done
|
|
||||||
3. Evaluate if it is schedulable - Done
|
|
||||||
4. Get the events and iterate each one - Done
|
|
||||||
5. Starting with the message - Done
|
|
||||||
6. Get the contact - Done
|
|
||||||
7. Send the message
|
|
||||||
8. Record the message event response - Added, not used
|
|
||||||
9. Add to scheduler
|
|
||||||
"#
|
|
||||||
);
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_schedulable(
|
fn is_schedulable(
|
||||||
&self,
|
&self,
|
||||||
queue_item: &textsender_models::message::scheduling::ScheduledMessage,
|
queue_item: &schedtxt_models::message::scheduling::ScheduledMessage,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
let now = time::OffsetDateTime::now_utc();
|
|
||||||
|
|
||||||
match queue_item.scheduled {
|
match queue_item.scheduled {
|
||||||
Some(date) => date > now,
|
Some(date) => date > time::OffsetDateTime::now_utc(),
|
||||||
None => false,
|
None => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
pub struct Event {
|
pub struct Event {
|
||||||
pub app: crate::app::App,
|
pub app: std::sync::Arc<crate::app::App>,
|
||||||
pub token: textsender_models::token::LoginResult,
|
pub token: std::sync::Arc<schedtxt_models::token::LoginResult>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Event {
|
impl Event {
|
||||||
pub async fn get(
|
pub async fn get(
|
||||||
&self,
|
&self,
|
||||||
scheduled_message_id: &uuid::Uuid,
|
scheduled_message_id: &uuid::Uuid,
|
||||||
) -> Result<Vec<textsender_models::message::scheduling::ScheduledMessageEvent>, std::io::Error>
|
) -> Result<Vec<schedtxt_models::message::scheduling::ScheduledMessageEvent>, std::io::Error>
|
||||||
{
|
{
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
let endpoint =
|
let endpoint =
|
||||||
@@ -38,7 +38,7 @@ impl Event {
|
|||||||
|
|
||||||
async fn parse_response(
|
async fn parse_response(
|
||||||
response: &str,
|
response: &str,
|
||||||
) -> Result<Vec<textsender_models::message::scheduling::ScheduledMessageEvent>, std::io::Error> {
|
) -> Result<Vec<schedtxt_models::message::scheduling::ScheduledMessageEvent>, std::io::Error> {
|
||||||
match serde_json::from_str::<serde_json::Value>(response) {
|
match serde_json::from_str::<serde_json::Value>(response) {
|
||||||
Ok(j) => match j.get("data") {
|
Ok(j) => match j.get("data") {
|
||||||
Some(serde_json::Value::Array(lrs)) => {
|
Some(serde_json::Value::Array(lrs)) => {
|
||||||
@@ -46,11 +46,11 @@ async fn parse_response(
|
|||||||
Err(std::io::Error::other("Error response is empty"))
|
Err(std::io::Error::other("Error response is empty"))
|
||||||
} else {
|
} else {
|
||||||
let mut events: Vec<
|
let mut events: Vec<
|
||||||
textsender_models::message::scheduling::ScheduledMessageEvent,
|
schedtxt_models::message::scheduling::ScheduledMessageEvent,
|
||||||
> = Vec::new();
|
> = Vec::new();
|
||||||
|
|
||||||
for event in lrs.iter() {
|
for event in lrs.iter() {
|
||||||
let ll: textsender_models::message::scheduling::ScheduledMessageEvent =
|
let ll: schedtxt_models::message::scheduling::ScheduledMessageEvent =
|
||||||
match serde_json::from_value(event.clone()) {
|
match serde_json::from_value(event.clone()) {
|
||||||
Ok(lr) => lr,
|
Ok(lr) => lr,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
|
|||||||
+7
-7
@@ -1,13 +1,13 @@
|
|||||||
pub struct MessageEventResponse {
|
pub struct MessageEventResponse {
|
||||||
pub app: crate::app::App,
|
pub app: std::sync::Arc<crate::app::App>,
|
||||||
pub token: textsender_models::token::LoginResult,
|
pub token: std::sync::Arc<schedtxt_models::token::LoginResult>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MessageEventResponse {
|
impl MessageEventResponse {
|
||||||
pub async fn record_message_event_response(
|
pub async fn record_message_event_response(
|
||||||
&self,
|
&self,
|
||||||
mer: &textsender_models::message::event::MessageEventResponse,
|
mer: &schedtxt_models::message::event::MessageEventResponse,
|
||||||
) -> Result<textsender_models::message::event::MessageEventResponse, std::io::Error> {
|
) -> Result<schedtxt_models::message::event::MessageEventResponse, std::io::Error> {
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
let endpoint = String::from("api/v1/schedule/message/event/response/record");
|
let endpoint = String::from("api/v1/schedule/message/event/response/record");
|
||||||
let api_url = format!("{}/{endpoint}", self.app.api_url);
|
let api_url = format!("{}/{endpoint}", self.app.api_url);
|
||||||
@@ -27,7 +27,7 @@ impl MessageEventResponse {
|
|||||||
"response": mer.response,
|
"response": mer.response,
|
||||||
"user_id": mer.user_id,
|
"user_id": mer.user_id,
|
||||||
"sent": sent,
|
"sent": sent,
|
||||||
"status": textsender_models::message::event::MESSAGE_EVENT_RESPONSE_STATUS_SCHEDULED,
|
"status": schedtxt_models::message::event::MESSAGE_EVENT_RESPONSE_STATUS_SCHEDULED,
|
||||||
"contact_id": mer.contact_id,
|
"contact_id": mer.contact_id,
|
||||||
"message_id": mer.message_id
|
"message_id": mer.message_id
|
||||||
});
|
});
|
||||||
@@ -79,7 +79,7 @@ impl MessageEventResponse {
|
|||||||
|
|
||||||
async fn parse_response(
|
async fn parse_response(
|
||||||
response: &str,
|
response: &str,
|
||||||
) -> Result<textsender_models::message::event::MessageEventResponse, std::io::Error> {
|
) -> Result<schedtxt_models::message::event::MessageEventResponse, std::io::Error> {
|
||||||
match serde_json::from_str::<serde_json::Value>(response) {
|
match serde_json::from_str::<serde_json::Value>(response) {
|
||||||
Ok(j) => {
|
Ok(j) => {
|
||||||
println!("Good");
|
println!("Good");
|
||||||
@@ -94,7 +94,7 @@ async fn parse_response(
|
|||||||
Err(std::io::Error::other("Error response is empty"))
|
Err(std::io::Error::other("Error response is empty"))
|
||||||
} else {
|
} else {
|
||||||
let lr = lrs[0].clone();
|
let lr = lrs[0].clone();
|
||||||
let ll: textsender_models::message::event::MessageEventResponse =
|
let ll: schedtxt_models::message::event::MessageEventResponse =
|
||||||
match serde_json::from_value(lr) {
|
match serde_json::from_value(lr) {
|
||||||
Ok(lr) => lr,
|
Ok(lr) => lr,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
pub struct Message {
|
pub struct Message {
|
||||||
pub app: crate::app::App,
|
pub app: std::sync::Arc<crate::app::App>,
|
||||||
pub token: textsender_models::token::LoginResult,
|
pub token: std::sync::Arc<schedtxt_models::token::LoginResult>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Message {
|
impl Message {
|
||||||
pub async fn get(
|
pub async fn get(
|
||||||
&self,
|
&self,
|
||||||
message_id: &uuid::Uuid,
|
message_id: &uuid::Uuid,
|
||||||
) -> Result<Vec<textsender_models::message::Message>, std::io::Error> {
|
) -> Result<Vec<schedtxt_models::message::Message>, std::io::Error> {
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
let endpoint = format!("api/v1/message?id={message_id}");
|
let endpoint = format!("api/v1/message?id={message_id}");
|
||||||
let api_url = format!("{}/{endpoint}", self.app.api_url);
|
let api_url = format!("{}/{endpoint}", self.app.api_url);
|
||||||
@@ -34,17 +34,17 @@ impl Message {
|
|||||||
|
|
||||||
async fn parse_response(
|
async fn parse_response(
|
||||||
response: &str,
|
response: &str,
|
||||||
) -> Result<Vec<textsender_models::message::Message>, std::io::Error> {
|
) -> Result<Vec<schedtxt_models::message::Message>, std::io::Error> {
|
||||||
match serde_json::from_str::<serde_json::Value>(response) {
|
match serde_json::from_str::<serde_json::Value>(response) {
|
||||||
Ok(j) => match j.get("data") {
|
Ok(j) => match j.get("data") {
|
||||||
Some(serde_json::Value::Array(lrs)) => {
|
Some(serde_json::Value::Array(lrs)) => {
|
||||||
if lrs.is_empty() {
|
if lrs.is_empty() {
|
||||||
Err(std::io::Error::other("Error response is empty"))
|
Err(std::io::Error::other("Error response is empty"))
|
||||||
} else {
|
} else {
|
||||||
let mut events: Vec<textsender_models::message::Message> = Vec::new();
|
let mut events: Vec<schedtxt_models::message::Message> = Vec::new();
|
||||||
|
|
||||||
for event in lrs.iter() {
|
for event in lrs.iter() {
|
||||||
let ll: textsender_models::message::Message =
|
let ll: schedtxt_models::message::Message =
|
||||||
match serde_json::from_value(event.clone()) {
|
match serde_json::from_value(event.clone()) {
|
||||||
Ok(lr) => lr,
|
Ok(lr) => lr,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
pub struct Queue {
|
pub struct Queue {
|
||||||
pub app: crate::app::App,
|
pub app: std::sync::Arc<crate::app::App>,
|
||||||
pub token: textsender_models::token::LoginResult,
|
pub token: std::sync::Arc<schedtxt_models::token::LoginResult>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Queue {
|
impl Queue {
|
||||||
pub async fn get_queue(
|
pub async fn get_queue(
|
||||||
&self,
|
&self,
|
||||||
) -> Result<textsender_models::message::scheduling::ScheduledMessage, std::io::Error> {
|
) -> Result<schedtxt_models::message::scheduling::ScheduledMessage, std::io::Error> {
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
let endpoint = String::from("api/v1/schedule/message/fetch");
|
let endpoint = String::from("api/v1/schedule/message/fetch");
|
||||||
let api_url = format!("{}/{endpoint}", self.app.api_url);
|
let api_url = format!("{}/{endpoint}", self.app.api_url);
|
||||||
@@ -36,7 +36,7 @@ impl Queue {
|
|||||||
|
|
||||||
async fn parse_queue_response(
|
async fn parse_queue_response(
|
||||||
response: &str,
|
response: &str,
|
||||||
) -> Result<textsender_models::message::scheduling::ScheduledMessage, std::io::Error> {
|
) -> Result<schedtxt_models::message::scheduling::ScheduledMessage, std::io::Error> {
|
||||||
match serde_json::from_str::<serde_json::Value>(response) {
|
match serde_json::from_str::<serde_json::Value>(response) {
|
||||||
Ok(j) => {
|
Ok(j) => {
|
||||||
let message = &j["message"];
|
let message = &j["message"];
|
||||||
@@ -50,7 +50,7 @@ async fn parse_queue_response(
|
|||||||
Err(std::io::Error::other("Error response is empty"))
|
Err(std::io::Error::other("Error response is empty"))
|
||||||
} else {
|
} else {
|
||||||
let lr = lrs[0].clone();
|
let lr = lrs[0].clone();
|
||||||
let ll: textsender_models::message::scheduling::ScheduledMessage =
|
let ll: schedtxt_models::message::scheduling::ScheduledMessage =
|
||||||
match serde_json::from_value(lr) {
|
match serde_json::from_value(lr) {
|
||||||
Ok(lr) => lr,
|
Ok(lr) => lr,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
pub struct ScheduledMessage {
|
pub struct ScheduledMessage {
|
||||||
pub app: crate::app::App,
|
pub app: std::sync::Arc<crate::app::App>,
|
||||||
pub token: textsender_models::token::LoginResult,
|
pub token: std::sync::Arc<schedtxt_models::token::LoginResult>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ScheduledMessage {
|
impl ScheduledMessage {
|
||||||
pub async fn get(
|
pub async fn get(
|
||||||
&self,
|
&self,
|
||||||
scheduled_message_id: &uuid::Uuid,
|
scheduled_message_id: &uuid::Uuid,
|
||||||
) -> Result<textsender_models::message::scheduling::ScheduledMessage, std::io::Error> {
|
) -> Result<schedtxt_models::message::scheduling::ScheduledMessage, std::io::Error> {
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
let endpoint = format!("api/v1/schedule/message?id={scheduled_message_id}");
|
let endpoint = format!("api/v1/schedule/message?id={scheduled_message_id}");
|
||||||
let api_url = format!("{}/{endpoint}", self.app.api_url);
|
let api_url = format!("{}/{endpoint}", self.app.api_url);
|
||||||
@@ -34,14 +34,14 @@ impl ScheduledMessage {
|
|||||||
|
|
||||||
async fn parse_response(
|
async fn parse_response(
|
||||||
response: &str,
|
response: &str,
|
||||||
) -> Result<textsender_models::message::scheduling::ScheduledMessage, std::io::Error> {
|
) -> Result<schedtxt_models::message::scheduling::ScheduledMessage, std::io::Error> {
|
||||||
match serde_json::from_str::<serde_json::Value>(response) {
|
match serde_json::from_str::<serde_json::Value>(response) {
|
||||||
Ok(j) => match j.get("data") {
|
Ok(j) => match j.get("data") {
|
||||||
Some(serde_json::Value::Array(lrs)) => {
|
Some(serde_json::Value::Array(lrs)) => {
|
||||||
if lrs.is_empty() {
|
if lrs.is_empty() {
|
||||||
Err(std::io::Error::other("Error response is empty"))
|
Err(std::io::Error::other("Error response is empty"))
|
||||||
} else {
|
} else {
|
||||||
let ll: textsender_models::message::scheduling::ScheduledMessage =
|
let ll: schedtxt_models::message::scheduling::ScheduledMessage =
|
||||||
match serde_json::from_value(lrs[0].clone()) {
|
match serde_json::from_value(lrs[0].clone()) {
|
||||||
Ok(lr) => lr,
|
Ok(lr) => lr,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user