#plugin #nostr #relay #extension

nostr-rs-plugin

Nostr Rust中继插件特性,用于nostr-rs-ext模块

7个版本

0.1.6 2023年3月3日
0.1.5 2023年3月1日
0.1.1 2023年2月27日

#32 in #relay

每月 48 次下载
用于 nostr-rs-ext

MIT 许可

4KB

nostr-rs-plugin

Nostr Rust relay 插件特性,用于 nostr-rs-ext 扩展/插件模块。

nostr-rs-ext 将尝试加载 Plugin 特性的实现(查看示例):pub fn get_plugin() -> *mut dyn Plugin;

示例插件

use nostr_rs_plugin::Plugin;
use nostr_rs_proto::nauthz_grpc::{EventRequest, EventReply, Decision};

struct Accept1;

// accept only kind 1 events
impl Plugin for Accept1 {
    fn start(&self) {}

    fn name(&self) -> String {
        return "Accept1".to_owned();
    }

    fn admit_event(&self, request: &EventRequest) -> EventReply {
        let reply;
        let opt_event = &request.event;

        match opt_event {
            Some(event) => {
                if event.kind == 1 {
                    reply = EventReply {
                        decision: Decision::Permit as i32,
                        message: Some(format!("I like kind 1")),
                    }
                } else {
                    reply = EventReply {
                        decision: Decision::Deny as i32,
                        message: Some(format!("I don't like kind {}", event.kind)),
                    }
                }
            },
            None => {
                reply = EventReply {
                    decision: Decision::Deny as i32,
                    message: Some(format!("No event in request!")),
                }
            }
        }

        return reply;
    }

    fn stop(&self) {}
}

#[no_mangle]
pub fn get_plugin() -> *mut dyn Plugin {
    // Return a raw pointer to an instance of our plugin
    Box::into_raw(Box::new(Accept1 {}))
}

依赖项

约5.5–7.5MB
约128K SLoC