2024-04-13 16:16:09 +01:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
use actix::prelude::*;
|
2024-04-14 15:46:21 +01:00
|
|
|
use tracing::debug;
|
2024-04-13 16:16:09 +01:00
|
|
|
|
|
|
|
use crate::server::{actors::webhook::message::WebhookMessage, config::RepoAlias};
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct WebhookRouter {
|
|
|
|
repos: HashMap<RepoAlias, Recipient<WebhookMessage>>,
|
|
|
|
}
|
|
|
|
impl WebhookRouter {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Actor for WebhookRouter {
|
|
|
|
type Context = Context<Self>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Handler<WebhookMessage> for WebhookRouter {
|
|
|
|
type Result = ();
|
|
|
|
|
|
|
|
fn handle(&mut self, msg: WebhookMessage, _ctx: &mut Self::Context) -> Self::Result {
|
|
|
|
let repo_alias = RepoAlias(msg.path().clone());
|
2024-04-14 15:46:21 +01:00
|
|
|
debug!(?repo_alias, "Router...");
|
2024-04-13 16:16:09 +01:00
|
|
|
if let Some(recipient) = self.repos.get(&repo_alias) {
|
2024-04-14 15:46:21 +01:00
|
|
|
debug!("Sending to recipient");
|
2024-04-13 16:16:09 +01:00
|
|
|
recipient.do_send(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Message)]
|
|
|
|
#[rtype(result = "()")]
|
|
|
|
pub struct AddWebhookRecipient(pub RepoAlias, pub Recipient<WebhookMessage>);
|
|
|
|
impl Handler<AddWebhookRecipient> for WebhookRouter {
|
|
|
|
type Result = ();
|
|
|
|
|
|
|
|
fn handle(&mut self, msg: AddWebhookRecipient, _ctx: &mut Self::Context) -> Self::Result {
|
|
|
|
self.repos.insert(msg.0, msg.1);
|
|
|
|
}
|
|
|
|
}
|