Connect to XMPP, join room, send message

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2023-05-20 20:30:56 +02:00
commit 0cba90dd68
5 changed files with 257 additions and 19 deletions

View file

@ -14,13 +14,15 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use crate::error::Error;
use crate::webhook::WebHook;
use std::convert::Infallible;
use std::str::from_utf8;
use std::sync::{Arc, Mutex};
use gitlab::webhooks::WebHook;
use hyper::{body, header, Body, Method, Request, Response};
use log::{debug, error};
use tokio::sync::mpsc::UnboundedSender;
fn error_res<E: std::fmt::Debug>(e: E) -> Result<Response<Body>, Infallible> {
error!("error response: {:?}", e);
@ -33,7 +35,7 @@ fn error_res<E: std::fmt::Debug>(e: E) -> Result<Response<Body>, Infallible> {
Ok(res)
}
async fn webhooks_inner(req: Request<Body>) -> Result<Response<Body>, Error> {
async fn webhooks_inner(req: Request<Body>) -> Result<WebHook, Error> {
match req.method() {
&Method::POST => (),
_ => return Err(Error::MethodMismatch),
@ -43,24 +45,33 @@ async fn webhooks_inner(req: Request<Body>) -> Result<Response<Body>, Error> {
let headers = req.headers();
if let Some(content_type) = headers.get(header::CONTENT_TYPE) &&
let Some(token) = headers.get("X-Gitlab-Token") {
if content_type != "application/json" {
return Err(Error::InvalidContentType);
}
let Some(token) = headers.get("X-Gitlab-Token") {
if content_type != "application/json" {
return Err(Error::InvalidContentType);
}
if token != "secret" {
return Err(Error::InvalidToken);
}
}
if token != "secret" {
return Err(Error::InvalidToken);
}
}
let tmp = body::to_bytes(req.into_body()).await?;
let text: &str = from_utf8(&tmp)?;
let json: WebHook = serde_json::from_str(text)?;
debug!("Passed: {:?}", json);
Ok(Response::new("Hello world".into()))
Ok(serde_json::from_str(text)?)
}
pub async fn webhooks(req: Request<Body>) -> Result<Response<Body>, Infallible> {
webhooks_inner(req).await.or_else(error_res)
pub async fn webhooks(
req: Request<Body>,
value_tx: Arc<Mutex<UnboundedSender<WebHook>>>,
) -> Result<Response<Body>, Infallible> {
match webhooks_inner(req).await {
Ok(wh) => {
debug!("Passed: {:?}", wh);
value_tx.lock().unwrap().send(wh).unwrap();
Ok(Response::new("Hello world".into()))
}
Err(err) => error_res(err),
}
}