forked from kemitix/git-next
44 lines
1.1 KiB
Rust
44 lines
1.1 KiB
Rust
|
use std::collections::HashMap;
|
||
|
|
||
|
use actix::prelude::*;
|
||
|
use tracing::info;
|
||
|
|
||
|
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());
|
||
|
info!(?repo_alias, "router...");
|
||
|
if let Some(recipient) = self.repos.get(&repo_alias) {
|
||
|
info!("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);
|
||
|
}
|
||
|
}
|