1use crate::{
3 auth,
4 config::{self, BackendKind},
5 desktop::{self, DesktopClient},
6 error::AppError,
7 graph::{GraphClient, Page},
8};
9use serde_json::Value;
10
11pub enum TeamsBackend {
12 Graph(GraphClient),
13 Desktop(DesktopClient),
14}
15
16impl TeamsBackend {
17 pub async fn connect(profile: Option<&str>) -> Result<Self, AppError> {
18 let (name, selected) = config::load(profile)?;
19 match selected.backend {
20 BackendKind::Graph => Ok(Self::Graph(GraphClient::new(
21 auth::access_token(&name, &selected).await?,
22 ))),
23 BackendKind::Desktop => Ok(Self::Desktop(DesktopClient::new(selected.desktop_port)?)),
24 }
25 }
26
27 fn graph(&self) -> Result<&GraphClient, AppError> {
28 match self {
29 Self::Graph(client) => Ok(client),
30 Self::Desktop(_) => Err(desktop::messaging_unsupported()),
31 }
32 }
33
34 pub async fn me(&self) -> Result<Value, AppError> {
35 self.graph()?.me().await
36 }
37 pub async fn teams(&self, limit: u16, offset: usize) -> Result<Page, AppError> {
38 self.graph()?.teams(limit, offset).await
39 }
40 pub async fn teams_available(&self) -> Result<(), AppError> {
41 self.graph()?.teams_available().await
42 }
43 pub async fn channels(&self, team: &str, limit: u16, offset: usize) -> Result<Page, AppError> {
44 self.graph()?.channels(team, limit, offset).await
45 }
46 pub async fn chats(&self, limit: u16, cursor: Option<&str>) -> Result<Page, AppError> {
47 self.graph()?.chats(limit, cursor).await
48 }
49 pub async fn messages(
50 &self,
51 chat: Option<&str>,
52 team: Option<&str>,
53 channel: Option<&str>,
54 limit: u16,
55 cursor: Option<&str>,
56 ) -> Result<Page, AppError> {
57 self.graph()?
58 .messages(chat, team, channel, limit, cursor)
59 .await
60 }
61 pub async fn send(
62 &self,
63 chat: Option<&str>,
64 team: Option<&str>,
65 channel: Option<&str>,
66 body: &str,
67 ) -> Result<Value, AppError> {
68 self.graph()?.send(chat, team, channel, body).await
69 }
70}