use std::sync::atomic::AtomicBool;
use std::sync::Arc;
use async_trait::async_trait;
use bytes::Bytes;
use tokio::sync::mpsc;
use tokio::task::JoinHandle;
use crate::content::{InboundMessage, OutboundMessage, ProviderAck};
use crate::error::{ChannelError, Result};
use crate::spec::{Capabilities, ProviderKind};
pub struct ProviderHandle {
pub stop: Arc<AtomicBool>,
pub join: JoinHandle<()>,
}
impl ProviderHandle {
pub fn new(stop: Arc<AtomicBool>, join: JoinHandle<()>) -> Self {
Self { stop, join }
}
pub fn signal_stop(&self) {
self.stop
.store(true, std::sync::atomic::Ordering::Relaxed);
}
}
#[async_trait]
pub trait ChannelProvider: Send + Sync + 'static {
fn kind(&self) -> ProviderKind;
fn capabilities(&self) -> Capabilities;
async fn start(&self, inbound_tx: mpsc::Sender<InboundMessage>) -> Result<ProviderHandle>;
async fn send(&self, msg: OutboundMessage) -> Result<ProviderAck>;
async fn fetch_media(&self, _media_ref: &str) -> Result<Bytes> {
Err(ChannelError::Unsupported("fetch_media"))
}
fn verify_webhook(&self, _headers: &http::HeaderMap, _body: &[u8]) -> Result<()> {
Err(ChannelError::Unsupported("verify_webhook"))
}
fn parse_webhook(
&self,
_headers: &http::HeaderMap,
_body: &[u8],
) -> Result<Vec<InboundMessage>> {
Err(ChannelError::Unsupported("parse_webhook"))
}
}