Skip to content

Commit

Permalink
add ci
Browse files Browse the repository at this point in the history
  • Loading branch information
foxzool committed Jun 25, 2024
1 parent 0fcb6b4 commit 0bb8446
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 28 deletions.
81 changes: 81 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Based on https://github.com/actions-rs/meta/blob/master/recipes/quickstart.md

on:
push:
branches: [main]
pull_request:
branches: [main]

name: Continuous Integration

jobs:
check:
name: Run cargo check
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v3

- name: Install stable toolchain
uses: dtolnay/rust-toolchain@stable

- name: Run cargo check
run: cargo check --all-targets

- name: Run cargo check with atlas feature
run: cargo check --all-targets --features atlas

- name: Run cargo check headless
run: cargo check --all-targets --no-default-features

docs:
name: Run cargo doc
env:
RUSTDOCFLAGS: -D warnings
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v3

- name: Install stable toolchain
uses: dtolnay/rust-toolchain@stable

- name: Run cargo doc
run: cargo doc --all-features --no-deps

build_examples:
name: Build examples
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v3

- name: Install stable toolchain
uses: dtolnay/rust-toolchain@stable

- name: Build examples
run: cargo build --examples

- name: Clean
run: cargo clean

- name: Build examples with atlas feature
run: cargo build --examples --features atlas

test:
name: Tests
strategy:
# Tests are most likely to have OS-specific behavior
matrix:
os: [ubuntu-latest, windows-latest, macOS-latest]

runs-on: ${{ matrix.os }}
steps:
- name: Checkout sources
uses: actions/checkout@v3

- name: Install stable toolchain
uses: dtolnay/rust-toolchain@stable

- name: Run cargo test
run: cargo test
16 changes: 12 additions & 4 deletions examples/ws_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ use dotenvy::dotenv;
use log::info;

use open_lark::{
client::ws_client::LarkWsClient, event::dispatcher::EventDispatcherHandler,
service::im::v1::p2_im_message_receive_v1::P2ImMessageReceiveV1,
client::ws_client::LarkWsClient,
event::dispatcher::EventDispatcherHandler,
service::im::v1::{
p2_im_message_read_v1::P2ImMessageReadV1, p2_im_message_receive_v1::P2ImMessageReceiveV1,
},
};

#[tokio::main]
Expand All @@ -16,7 +19,8 @@ async fn main() {
let app_secret = env::var("APP_SECRET").unwrap();

let event_handler = EventDispatcherHandler::builder()
.register_p2_im_message_receive_v1(Box::new(handle_p2_im_message_receive_v1))
.register_p2_im_message_receive_v1(handle_p2_im_message_receive_v1)
.register_p2_im_message_read_v1(handle_p2_im_message_read_v1)
.build();

LarkWsClient::open(&app_id, &app_secret, event_handler)
Expand All @@ -25,5 +29,9 @@ async fn main() {
}

fn handle_p2_im_message_receive_v1(data: P2ImMessageReceiveV1) {
info!("receive message: {:?}", data);
info!("p2.im.message.receive_v1: {:?}", data);
}

fn handle_p2_im_message_read_v1(data: P2ImMessageReadV1) {
info!("p2.im.message.message_read_v1: {:?}", data);
}
21 changes: 0 additions & 21 deletions src/event/dispatcher/im_v1_event.rs

This file was deleted.

38 changes: 35 additions & 3 deletions src/event/dispatcher/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use std::collections::HashMap;

use crate::event::context::EventContext;

mod im_v1_event;
use crate::{
event::context::EventContext,
service::im::v1::p2_im_message_receive_v1::{
P2ImMessageReceiveV1, P2ImMessageReceiveV1ProcessorImpl,
},
};
use crate::service::im::v1::p2_im_message_read_v1::{P2ImMessageReadV1, P2ImMessageReadV1ProcessorImpl};

/// 事件分发处理器
pub struct EventDispatcherHandler {
Expand Down Expand Up @@ -77,3 +81,31 @@ impl EventDispatcherHandlerBuilder {
}
}
}

impl EventDispatcherHandlerBuilder {
pub fn register_p2_im_message_receive_v1<F>(mut self, f: F) -> Self
where
F: Fn(P2ImMessageReceiveV1) + 'static + Sync + Send,
{
let key = "p2.im.message.receive_v1".to_string();
if self.processor_map.contains_key(&key) {
panic!("processor already registered, type: {}", key);
}
let processor = P2ImMessageReceiveV1ProcessorImpl::new(f);
self.processor_map.insert(key, Box::new(processor));
self
}

pub fn register_p2_im_message_read_v1<F>(mut self, f: F) -> Self
where
F: Fn(P2ImMessageReadV1) + 'static + Sync + Send,
{
let key = "p2.im.message.message_read_v1".to_string();
if self.processor_map.contains_key(&key) {
panic!("processor already registered, type: {}", key);
}
let processor = P2ImMessageReadV1ProcessorImpl::new(f);
self.processor_map.insert(key, Box::new(processor));
self
}
}
1 change: 1 addition & 0 deletions src/service/im/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
pub mod chats;
pub mod message;
pub mod p2_im_message_receive_v1;
pub mod p2_im_message_read_v1;

pub struct V1 {
pub chats: ChatsService,
Expand Down
47 changes: 47 additions & 0 deletions src/service/im/v1/p2_im_message_read_v1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use serde::{Deserialize, Serialize};

use crate::{
event::{context::EventHeader, dispatcher::EventHandler},
service::im::v1::p2_im_message_receive_v1::{EventMessage, EventSender},
};

#[derive(Debug, Serialize, Deserialize)]
pub struct P2ImMessageReadV1 {
pub schema: String,
pub header: EventHeader,
pub event: P2ImMessageMessageReadV1Data,
}

/// 事件
#[derive(Debug, Serialize, Deserialize)]
pub struct P2ImMessageMessageReadV1Data {
pub sender: EventSender,
pub message: EventMessage,
}

pub struct P2ImMessageReadV1ProcessorImpl<F>
where
F: Fn(P2ImMessageReadV1) + 'static,
{
f: F,
}

impl<F> P2ImMessageReadV1ProcessorImpl<F>
where
F: Fn(P2ImMessageReadV1) + 'static,
{
pub fn new(f: F) -> Self {
P2ImMessageReadV1ProcessorImpl { f }
}
}

impl<F> EventHandler for P2ImMessageReadV1ProcessorImpl<F>
where
F: Fn(P2ImMessageReadV1) + 'static + Sync + Send,
{
fn handle(&self, payload: &[u8]) -> anyhow::Result<()> {
let message: P2ImMessageReadV1 = serde_json::from_slice(payload)?;
(self.f)(message);
Ok(())
}
}

0 comments on commit 0bb8446

Please sign in to comment.