1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
//! 客户端。
//!
//! 更多信息,请参考 [`Client`]。
pub mod account_info;
pub mod friend;
pub mod friend_group;
pub mod friend_list;
pub mod group;
pub mod group_member;
pub mod group_member_list;
pub mod message_receipt;
use std::{collections::HashMap, sync::Arc, time::Duration};
use ricq::RQError;
use self::{
friend::FriendSelector,
friend_group::FriendGroupSelector,
friend_list::{FetchFriendListError, FriendList},
};
use crate::client::group_member_list::{FetchGroupMemberListError, GroupMemberListSelector};
use crate::meta::cache::{Cached, CachedMap};
use crate::{
client::{
account_info::{AccountInfo, AccountInfoSelector, ReadAccountInfoError},
group_member::{FetchGroupMemberInfoError, GroupMember},
},
meta::selector::SingleSelector,
};
use crate::{
client::{
friend::FetchFriendInfoError,
friend_group::{FetchFriendGroupError, FriendGroup},
friend_list::FriendListSelector,
group::{AllGroupSelector, FetchGroupInfoError, Group, GroupSelector, MultiGroupSelector},
group_member::GroupMemberSelector,
group_member_list::GroupMemberList,
},
consts::*,
meta::selector::{MultiSelector, OptionSelector},
};
/// 客户端。
///
/// # Python
/// ```python
/// class Client:
/// @property
/// def uin(self) -> int:
/// ```
pub struct Client {
pub(crate) inner: Arc<ricq::Client>,
/// 当前账号的 QQ 号。
pub uin: i64,
pub(crate) friend_list: Cached<FriendList>,
pub(crate) groups: CachedMap<Group>,
pub(crate) group_member_lists: CachedMap<GroupMemberList>,
}
impl std::fmt::Debug for Client {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Client").field("uin", &self.uin).finish()
}
}
impl Client {
pub(crate) async fn new(client: Arc<ricq::Client>) -> Self {
let uin = client.uin().await;
Self {
inner: client,
uin,
friend_list: Cached::new(FRIEND_LIST_CACHE_TIME),
groups: CachedMap::new(GROUP_CACHE_TIME),
group_member_lists: CachedMap::new(GROUP_MEMBER_LIST_CACHE_TIME),
}
}
/// 设置好友列表缓存过期时间。
///
/// # Python
/// ```python
/// async def set_friend_list_cache_time(self, time: datetime.timedelta) -> None: ...
/// ```
pub async fn set_friend_list_cache_time(&self, time: Duration) {
self.friend_list.set_cache_time(time).await;
}
/// 设置群信息缓存过期时间。
///
/// # Python
/// ```python
/// async def set_group_cache_time(self, time: datetime.timedelta) -> None: ...
/// ```
pub async fn set_group_cache_time(&self, time: Duration) {
self.groups.set_cache_time(time).await;
}
/// 设置群成员列表缓存过期时间。
///
/// # Python
/// ```python
/// async def set_group_member_list_cache_time(self, time: datetime.timedelta) -> None: ...
/// ```
pub async fn set_group_member_list_cache_time(&self, time: Duration) {
self.group_member_lists.set_cache_time(time).await;
}
/// 当前账号是否在线。
///
/// # Python
/// ```python
/// def is_online(self) -> bool: ...
/// ```
pub fn is_online(&self) -> bool {
self.inner.online.load(std::sync::atomic::Ordering::Acquire)
}
/// 构造账号信息选择器。
///
/// # Python
/// ```python
/// def account_info(self) -> AccountInfoSelector: ...
/// ```
pub fn account_info(self: &Arc<Self>) -> AccountInfoSelector {
AccountInfoSelector::new(self.clone())
}
/// 获取账号信息。
///
/// # Python
/// ```python
/// async def get_account_info(self) -> AccountInfo: ...
/// ```
pub async fn get_account_info(self: &Arc<Self>) -> Result<AccountInfo, ReadAccountInfoError> {
self.account_info().fetch().await
}
/// 构造好友分组选择器。
///
/// # Python
/// ```python
/// def friend_group(self, group_id: int) -> FriendGroupSelector: ...
/// ```
pub fn friend_list(self: &Arc<Self>) -> FriendListSelector {
FriendListSelector::new(self.clone())
}
/// 获取好友列表对象。
///
/// 好友列表会被缓存,如果缓存未过期则直接返回缓存的值。
/// 如果需要强制刷新好友列表缓存,请使用 [`flush_friend_list`]。
///
/// # Python
/// ```python
/// async def get_friend_list(self) -> FriendList: ...
/// ```
///
/// [`flush_friend_list`]: Self::flush_friend_list
pub async fn get_friend_list(
self: &Arc<Self>,
) -> Result<Arc<FriendList>, FetchFriendListError> {
self.friend_list.get(self).await
}
/// 刷新好友列表缓存。
///
/// # Python
/// ```python
/// async def flush_friend_list(self) -> None: ...
/// ```
pub async fn flush_friend_list(self: &Arc<Self>) -> Result<(), FetchFriendListError> {
self.friend_list.make_dirty().await;
Ok(())
}
/// 构造好友选择器。
///
/// # Python
/// ```python
/// def friend(self, uin: int) -> FriendSelector:
/// ```
pub fn friend(self: &Arc<Self>, uin: i64) -> FriendSelector {
FriendSelector::new(self.clone(), uin)
}
/// 获取好友对象。
///
/// 好友对象会缓存在好友列表缓存中,如果缓存未过期则直接返回缓存的值。
/// 如果需要强制刷新好友对象缓存,请使用 [`FriendSelector::flush`] 或 [`flush_friend_list`]。
///
/// # Python
/// ```python
/// async def get_friend(self, uin: int) -> Friend | None: ...
/// ```
///
/// [`FriendSelector::flush`]: crate::meta::selector::Selector::flush
/// [`flush_friend_list`]: Self::flush_friend_list
pub async fn get_friend(
self: &Arc<Self>,
uin: i64,
) -> Result<Option<Arc<friend::Friend>>, FetchFriendInfoError> {
self.friend(uin).fetch().await
}
/// 构造好友分组选择器。
///
/// # Python
/// ```python
/// def friend_group(self, group_id: int) -> FriendGroupSelector: ...
/// ```
pub fn friend_group(self: &Arc<Self>, id: u8) -> FriendGroupSelector {
FriendGroupSelector::new(self.clone(), id)
}
/// 获取好友分组对象。
///
/// 好友分组对象会缓存在好友列表缓存中,如果缓存未过期则直接返回缓存的值。
/// 如果需要强制刷新好友分组对象缓存,请使用 [`FriendGroupSelector::flush`] 或 [`flush_friend_list`]。
///
/// # Python
/// ```python
/// async def get_friend_group(self, id: int) -> FriendGroup | None: ...
/// ```
///
/// [`FriendGroupSelector::flush`]: crate::meta::selector::Selector::flush
/// [`flush_friend_list`]: Self::flush_friend_list
pub async fn get_friend_group(
self: &Arc<Self>,
id: u8,
) -> Result<Option<Arc<FriendGroup>>, FetchFriendGroupError> {
self.friend_group(id).fetch().await
}
/// 创建好友分组。
///
/// 此方法会强制更新好友列表缓存。
///
/// # Python
/// ```python
/// async def create_friend_group(self, name: str) -> None: ...
/// ```
pub async fn create_friend_group(self: &Arc<Self>, name: String) -> Result<(), RQError> {
// https://github.com/takayama-lily/oicq/blob/870652fbabc688371372aeec775c4233dbb770bc/lib/internal/internal.ts#L134
self.inner.friend_list_add_group(0xd, name).await?;
self.friend_list.make_dirty().await;
Ok(())
}
/// 构造群选择器。
///
/// # Python
/// ```python
/// def group(self, code: int) -> GroupSelector: ...
/// ```
pub fn group(self: &Arc<Self>, code: i64) -> GroupSelector {
GroupSelector::new(self.clone(), code)
}
/// 获取群对象。
///
/// 群对象会被缓存,如果缓存未过期则直接返回缓存的值。
/// 如果需要强制刷新群对象缓存,请使用 [`GroupSelector::flush`]。
///
/// # Python
/// ```python
/// async def get_group(self, code: int) -> Group | None: ...
/// ```
///
/// [`GroupSelector::flush`]: crate::meta::selector::Selector::flush
pub async fn get_group(
self: &Arc<Self>,
code: i64,
) -> Result<Option<Arc<Group>>, FetchGroupInfoError> {
self.group(code).fetch().await
}
/// 构造多个群选择器。
///
/// # Python
/// ```python
/// def groups(self, *codes: int) -> MultiGroupSelector: ...
/// ```
pub fn groups(self: &Arc<Self>, codes: Vec<i64>) -> MultiGroupSelector {
MultiGroupSelector::new(self.clone(), codes)
}
/// 获取多个群对象。
///
/// 群对象会被缓存,如果缓存未过期则直接返回缓存的值。
/// 如果需要强制刷新群对象缓存,请使用 [`MultiGroupSelector::flush`]。
///
/// # Python
/// ```python
/// async def get_groups(self, *codes: int) -> dict[int, Group]: ...
/// ```
///
/// [`MultiGroupSelector::flush`]: crate::meta::selector::Selector::flush
pub async fn get_groups(
self: &Arc<Self>,
codes: Vec<i64>,
) -> Result<HashMap<i64, Arc<Group>>, FetchGroupInfoError> {
self.groups(codes).fetch().await
}
/// 构造所有群选择器。
///
/// # Python
/// ```python
/// def all_groups(self) -> AllGroupSelector: ...
/// ```
pub fn all_groups(self: &Arc<Self>) -> AllGroupSelector {
AllGroupSelector::new(self.clone())
}
/// 获取所有群对象。
///
/// 此方法会刷新所有群对象的缓存。
///
/// # Python
/// ```python
/// async def get_all_groups(self) -> dict[int, Group]: ...
/// ```
pub async fn get_all_groups(
self: &Arc<Self>,
) -> Result<HashMap<i64, Arc<Group>>, FetchGroupInfoError> {
self.all_groups().fetch().await
}
/// 构造群成员选择器。
///
/// # Python
/// ```python
/// def group_member(self, group_code: int, uin: int) -> GroupMemberSelector: ...
/// ```
pub fn group_member(self: &Arc<Self>, group_code: i64, uin: i64) -> GroupMemberSelector {
GroupMemberSelector::new(self.clone(), group_code, uin)
}
/// 获取群成员对象。
///
/// 群成员对象会被缓存,如果缓存未过期则直接返回缓存的值。
/// 如果需要强制刷新群成员对象缓存,请使用 [`GroupMemberSelector::flush`]。
///
/// # Python
/// ```python
/// async def get_group_member(self, group_code: int, uin: int) -> GroupMember | None: ...
/// ```
///
/// [`GroupMemberSelector::flush`]: crate::meta::selector::Selector::flush
pub async fn get_group_member(
self: &Arc<Self>,
group_code: i64,
uin: i64,
) -> Result<Option<Arc<GroupMember>>, FetchGroupMemberInfoError> {
self.group_member(group_code, uin).fetch().await
}
/// 构造群成员列表选择器。
///
/// # Python
/// ```python
/// def group_member_list(self, group_code: int) -> GroupMemberListSelector: ...
/// ```
pub fn group_member_list(self: &Arc<Self>, group_code: i64) -> GroupMemberListSelector {
GroupMemberListSelector::new(self.clone(), group_code)
}
/// 获取群成员列表。
///
/// 群成员列表会被缓存,如果缓存未过期则直接返回缓存的值。
/// 如果需要强制刷新群成员列表缓存,请使用 [`GroupMemberListSelector::flush`]。
///
/// # Python
/// ```python
/// async def get_group_member_list(self, group_code: int) -> GroupMemberList | None: ...
/// ```
///
/// [`GroupMemberListSelector::flush`]: crate::meta::selector::Selector::flush
pub async fn get_group_member_list(
self: &Arc<Self>,
group_code: i64,
) -> Result<Option<Arc<GroupMemberList>>, FetchGroupMemberListError> {
self.group_member_list(group_code).fetch().await
}
}