git-next/src/server/actors/webhook/router.rs

44 lines
1.1 KiB
Rust
Raw Normal View History

use std::collections::HashMap;
use actix::prelude::*;
use tracing::debug;
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());
debug!(?repo_alias, "Router...");
if let Some(recipient) = self.repos.get(&repo_alias) {
debug!("Sending to recipient");
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);
}
}