use std::sync::Arc;
use tokio::sync::mpsc;
use crate::{
Config, Result,
blocking::runtime::BlockingRuntime,
content::{
ContentContext, CreateReplyOptions, CreateTopicOptions, ListTopicRepliesOptions,
MyTopicsOptions, NewsItem, OwnedTopic, TopicItem, TopicReply,
},
};
pub struct ContentContextSync {
rt: BlockingRuntime<ContentContext>,
}
impl ContentContextSync {
pub fn new(config: Arc<Config>) -> Result<Self> {
let rt = BlockingRuntime::try_new(
move || {
let ctx = ContentContext::new(config);
let (tx, rx) = mpsc::unbounded_channel::<std::convert::Infallible>();
std::mem::forget(tx); Ok::<_, crate::Error>((ctx, rx))
},
|_: std::convert::Infallible| {},
)?;
Ok(Self { rt })
}
pub fn my_topics(&self, opts: MyTopicsOptions) -> Result<Vec<OwnedTopic>> {
self.rt
.call(move |ctx| async move { ctx.my_topics(opts).await })
}
pub fn create_topic(&self, opts: CreateTopicOptions) -> Result<String> {
self.rt
.call(move |ctx| async move { ctx.create_topic(opts).await })
}
pub fn topics(&self, symbol: impl Into<String>) -> Result<Vec<TopicItem>> {
let symbol = symbol.into();
self.rt
.call(move |ctx| async move { ctx.topics(symbol).await })
}
pub fn news(&self, symbol: impl Into<String>) -> Result<Vec<NewsItem>> {
let symbol = symbol.into();
self.rt
.call(move |ctx| async move { ctx.news(symbol).await })
}
pub fn topic_detail(&self, id: impl Into<String>) -> Result<OwnedTopic> {
let id = id.into();
self.rt
.call(move |ctx| async move { ctx.topic_detail(id).await })
}
pub fn list_topic_replies(
&self,
topic_id: impl Into<String>,
opts: ListTopicRepliesOptions,
) -> Result<Vec<TopicReply>> {
let topic_id = topic_id.into();
self.rt
.call(move |ctx| async move { ctx.list_topic_replies(topic_id, opts).await })
}
pub fn create_topic_reply(
&self,
topic_id: impl Into<String>,
opts: CreateReplyOptions,
) -> Result<TopicReply> {
let topic_id = topic_id.into();
self.rt
.call(move |ctx| async move { ctx.create_topic_reply(topic_id, opts).await })
}
}