Skip to content
This repository has been archived by the owner on Oct 21, 2023. It is now read-only.

Commit

Permalink
message receipt, forward message, face, new error, rename, new api fo…
Browse files Browse the repository at this point in the history
…r sending forward message, add group invite api
  • Loading branch information
LaoLittle committed Dec 8, 2022
1 parent 79e2dd1 commit 6e296bb
Show file tree
Hide file tree
Showing 19 changed files with 540 additions and 100 deletions.
32 changes: 16 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "atri_bot"
version = "0.6.0"
version = "0.7.0"
edition = "2021"
authors = ["LaoLittle"]
description = "A simple bot"
Expand Down Expand Up @@ -60,7 +60,7 @@ version = "0.1.17"
version = "0.1.0"

[dependencies.atri_ffi]
version = "0.5.0"
version = "0.6.1"

[profile.release]
lto = true
Expand Down
26 changes: 14 additions & 12 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::{config, global_status};
pub struct Client(Arc<imp::Client>);

impl Client {
pub async fn new(id: i64, conf: BotConfiguration) -> Self {
pub async fn new(id: i64, conf: ClientConfiguration) -> Self {
let b = imp::Client::new(id, conf).await;
Self(Arc::new(b))
}
Expand Down Expand Up @@ -307,7 +307,7 @@ mod imp {

use crate::channel::GlobalEventBroadcastHandler;
use crate::client::info::BotAccountInfo;
use crate::client::BotConfiguration;
use crate::client::ClientConfiguration;
use crate::contact::friend::Friend;
use crate::contact::group::Group;

Expand All @@ -322,7 +322,7 @@ mod imp {
}

impl Client {
pub async fn new(id: i64, conf: BotConfiguration) -> Self {
pub async fn new(id: i64, conf: ClientConfiguration) -> Self {
let work_dir = conf.work_dir(id);

if !work_dir.is_dir() {
Expand Down Expand Up @@ -406,14 +406,16 @@ mod imp {
.pop()
.ok_or_else(|| io::Error::new(ErrorKind::AddrNotAvailable, "重连失败"))?;

if let Ok(Ok(s)) =
tokio::time::timeout(Duration::from_secs(2), socket.connect(addr)).await
{
break s;
} else {
times += 1;
warn!("连接服务器{}失败, 尝试重连({}/{})", addr, times, total);
match tokio::time::timeout(Duration::from_secs(2), socket.connect(addr)).await {
Ok(Ok(s)) => break s,
Ok(Err(e)) => warn!(
"连接服务器{}失败, 尝试重连({}/{}): {}",
addr, times, total, e
),
Err(_) => warn!("连接服务器{}超时, 尝试重连({}/{})", addr, times, total),
}

times += 1;
})
}

Expand All @@ -432,12 +434,12 @@ mod imp {
}
}

pub struct BotConfiguration {
pub struct ClientConfiguration {
pub work_dir: Option<PathBuf>,
pub version: ricq::version::Version,
}

impl BotConfiguration {
impl ClientConfiguration {
fn work_dir(&self, id: i64) -> PathBuf {
self.work_dir
.as_ref()
Expand Down
11 changes: 10 additions & 1 deletion src/contact/friend.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::error::{AtriError, AtriResult};
use crate::message::forward::ForwardMessage;
use crate::message::image::Image;
use crate::message::meta::{MessageReceipt, RecallMessage};
use crate::message::MessageChain;
Expand Down Expand Up @@ -48,7 +49,7 @@ impl Friend {
map.is_some()
}

pub async fn send_message(&self, chain: MessageChain) -> AtriResult<MessageReceipt> {
async fn _send_message(&self, chain: MessageChain) -> AtriResult<MessageReceipt> {
let result = self
.client()
.request_client()
Expand All @@ -68,6 +69,14 @@ impl Friend {
result.map(MessageReceipt::from).map_err(AtriError::from)
}

pub async fn send_message<M: Into<MessageChain>>(&self, msg: M) -> AtriResult<MessageReceipt> {
self._send_message(msg.into()).await
}

async fn _send_forward_message(&self, _forward: ForwardMessage) -> AtriResult<MessageReceipt> {
Err(AtriError::NotSupported)
}

pub async fn upload_image(&self, image: Vec<u8>) -> AtriResult<Image> {
let f = self
.client()
Expand Down
25 changes: 25 additions & 0 deletions src/contact/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use tracing::error;

use crate::contact::member::NamedMember;
use crate::error::{AtriError, AtriResult};
use crate::message::forward::ForwardMessage;
use crate::message::image::Image;
use crate::message::meta::{MessageReceipt, RecallMessage};
use crate::message::MessageChain;
Expand Down Expand Up @@ -128,6 +129,22 @@ impl Group {
self._send_message(msg.into()).await
}

async fn _send_forward_message(&self, forward: ForwardMessage) -> AtriResult<MessageReceipt> {
self.client()
.request_client()
.send_group_forward_message(self.id(), forward.into())
.await
.map(MessageReceipt::from)
.map_err(AtriError::from)
}

pub async fn send_forward_message<M: Into<ForwardMessage>>(
&self,
msg: M,
) -> AtriResult<MessageReceipt> {
self._send_forward_message(msg.into()).await
}

async fn _upload_image(&self, image: Vec<u8>) -> AtriResult<Image> {
self.client()
.request_client()
Expand Down Expand Up @@ -175,6 +192,14 @@ impl Group {
self._change_name(name.into()).await
}

pub async fn invite(&self, id: i64) -> AtriResult<()> {
self.client()
.request_client()
.group_invite(self.id(), id)
.await
.map_err(AtriError::from)
}

pub async fn kick<M: ToKickMember, S: AsRef<str>>(
&self,
member: M,
Expand Down
53 changes: 23 additions & 30 deletions src/contact/member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ use crate::message::at::At;
use crate::message::meta::{Anonymous, MessageReceipt};
use crate::message::MessageChain;
use crate::{Client, GroupMemberInfo};
use atri_ffi::contact::{FFIMember, MemberUnion};
use atri_ffi::contact::FFIMember;
use atri_ffi::ffi::ForFFI;
use atri_ffi::ManagedCloneable;
use ricq::RQError;
use std::mem::ManuallyDrop;
use std::sync::Arc;
use std::time::Duration;

Expand Down Expand Up @@ -35,45 +34,39 @@ impl Member {
pub async fn send_message(&self, chain: MessageChain) -> AtriResult<MessageReceipt> {
match self {
Self::Named(named) => named.send_message(chain).await,
Self::Anonymous(..) => Err(AtriError::Protocol(RQError::Other(
"Send message to anonymous member is not supported".into(),
))),
Self::Anonymous(..) => Err(AtriError::NotSupported),
}
}
}

pub fn into_ffi(self) -> FFIMember {
match self {
impl ForFFI for Member {
type FFIValue = FFIMember;

fn into_ffi(self) -> Self::FFIValue {
let (is_named, ma) = match self {
Self::Named(named) => {
let ma = ManagedCloneable::from_value(named);
FFIMember {
is_named: true,
inner: MemberUnion {
named: ManuallyDrop::new(ma),
},
}
(true, ma)
}
Self::Anonymous(ano) => {
let ma = ManagedCloneable::from_value(ano);
FFIMember {
is_named: false,
inner: MemberUnion {
anonymous: ManuallyDrop::new(ma),
},
}
(false, ma)
}
};

FFIMember {
is_named,
inner: ma,
}
}

pub fn from_ffi(ffi: FFIMember) -> Self {
unsafe {
if ffi.is_named {
let named: NamedMember = ManuallyDrop::into_inner(ffi.inner.named).into_value();
Self::Named(named)
} else {
let ano: AnonymousMember =
ManuallyDrop::into_inner(ffi.inner.anonymous).into_value();
Self::Anonymous(ano)
}
fn from_ffi(value: Self::FFIValue) -> Self {
let ma = value.inner;

if value.is_named {
Self::Named(ma.into_value())
} else {
Self::Anonymous(ma.into_value())
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub enum AtriError {
IO(io::Error),
Protocol(ricq::RQError),
Login(LoginError),
NotSupported,
}

impl Display for AtriError {
Expand All @@ -21,6 +22,7 @@ impl Display for AtriError {
}
Self::PluginError(e) => Display::fmt(e, f),
Self::Protocol(e) => Display::fmt(e, f),
Self::NotSupported => f.write_str("operation not supported"),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/event/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ unsafe impl Send for Listener {}

unsafe impl Sync for Listener {}

#[must_use = "if unused the Listener will immediately close"]
pub struct ListenerGuard {
name: Arc<String>,
closed: Arc<AtomicBool>,
Expand Down
6 changes: 3 additions & 3 deletions src/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,11 @@ impl ContactSubject for FriendMessageEvent {
}
}

pub type ClientLoginEvent = SharedEvent<imp::BotOnlineEvent>;
pub type ClientLoginEvent = SharedEvent<imp::ClientLoginEvent>;

impl ClientLoginEvent {
pub fn from(bot: Client) -> Self {
Self::new(imp::BotOnlineEvent { bot })
Self::new(imp::ClientLoginEvent { bot })
}
}

Expand All @@ -294,7 +294,7 @@ mod imp {
pub message: MessageChain,
}

pub struct BotOnlineEvent {
pub struct ClientLoginEvent {
pub bot: Client,
}
}
Expand Down
Loading

0 comments on commit 6e296bb

Please sign in to comment.