misskey_api/streaming/channel/
user_list.rs

1use crate::model::{id::Id, note::Note, user::User, user_list::UserList};
2use crate::streaming::channel::NoOutgoing;
3
4use serde::{Deserialize, Serialize};
5
6#[allow(clippy::large_enum_variant)]
7#[derive(Deserialize, Debug, Clone)]
8#[serde(rename_all = "camelCase", tag = "type", content = "body")]
9pub enum UserListEvent {
10    // from userListStream
11    UserAdded(User),
12    UserRemoved(User),
13    // from the channel
14    Note(Note),
15}
16
17#[derive(Serialize, Debug, Clone)]
18#[serde(rename_all = "camelCase")]
19pub struct Request {
20    pub list_id: Id<UserList>,
21}
22
23impl misskey_core::streaming::ConnectChannelRequest for Request {
24    type Incoming = UserListEvent;
25    type Outgoing = NoOutgoing;
26
27    const NAME: &'static str = "userList";
28}
29
30#[cfg(test)]
31mod tests {
32    use super::{Request, UserListEvent};
33    use crate::test::{websocket::TestClient, ClientExt};
34
35    use futures::{future, StreamExt};
36
37    #[tokio::test]
38    async fn subscribe_unsubscribe() {
39        let client = TestClient::new().await;
40        let list = client
41            .test(crate::endpoint::users::lists::create::Request {
42                name: "test".to_string(),
43            })
44            .await;
45
46        let mut stream = client.channel(Request { list_id: list.id }).await.unwrap();
47        stream.disconnect().await.unwrap();
48    }
49
50    #[tokio::test]
51    async fn stream_note() {
52        let client = TestClient::new().await;
53        let admin = client.admin.me().await;
54        let list = client
55            .user
56            .test(crate::endpoint::users::lists::create::Request {
57                name: "test".to_string(),
58            })
59            .await;
60        client
61            .user
62            .test(crate::endpoint::users::lists::push::Request {
63                list_id: list.id.clone(),
64                user_id: admin.id,
65            })
66            .await;
67
68        let mut stream = client
69            .user
70            .channel(Request { list_id: list.id })
71            .await
72            .unwrap();
73
74        future::join(
75            client
76                .admin
77                .create_note(Some("The world is fancy!"), None, None),
78            async {
79                loop {
80                    match stream.next().await.unwrap().unwrap() {
81                        UserListEvent::Note(_) => break,
82                        _ => continue,
83                    }
84                }
85            },
86        )
87        .await;
88    }
89
90    #[tokio::test]
91    async fn stream_added() {
92        let client = TestClient::new().await;
93        let admin = client.admin.me().await;
94        let list = client
95            .user
96            .test(crate::endpoint::users::lists::create::Request {
97                name: "test".to_string(),
98            })
99            .await;
100
101        let mut stream = client
102            .user
103            .channel(Request {
104                list_id: list.id.clone(),
105            })
106            .await
107            .unwrap();
108
109        future::join(
110            client
111                .user
112                .test(crate::endpoint::users::lists::push::Request {
113                    list_id: list.id,
114                    user_id: admin.id,
115                }),
116            async {
117                loop {
118                    match stream.next().await.unwrap().unwrap() {
119                        UserListEvent::UserAdded(_) => break,
120                        _ => continue,
121                    }
122                }
123            },
124        )
125        .await;
126    }
127
128    #[tokio::test]
129    async fn stream_removed() {
130        let client = TestClient::new().await;
131        let admin = client.admin.me().await;
132        let list = client
133            .user
134            .test(crate::endpoint::users::lists::create::Request {
135                name: "test".to_string(),
136            })
137            .await;
138        client
139            .user
140            .test(crate::endpoint::users::lists::push::Request {
141                list_id: list.id.clone(),
142                user_id: admin.id.clone(),
143            })
144            .await;
145
146        let mut stream = client
147            .user
148            .channel(Request {
149                list_id: list.id.clone(),
150            })
151            .await
152            .unwrap();
153
154        future::join(
155            client
156                .user
157                .test(crate::endpoint::users::lists::pull::Request {
158                    list_id: list.id,
159                    user_id: admin.id,
160                }),
161            async {
162                loop {
163                    match stream.next().await.unwrap().unwrap() {
164                        UserListEvent::UserRemoved(_) => break,
165                        _ => continue,
166                    }
167                }
168            },
169        )
170        .await;
171    }
172}