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
use std::collections::HashMap;

use crate::{message::*, setting::SettingWrapper};
use actix::prelude::*;
use nostr_db::Event;

// TODO: use btree index for fast filter
pub struct Subscriber {
    pub addr: Recipient<SubscribeResult>,
    pub events: Vec<(usize, Event)>,
    /// map session id -> sub id -> subscription
    pub subscriptions: HashMap<usize, HashMap<String, Subscription>>,
    pub setting: SettingWrapper,
}

impl Subscriber {
    pub fn new(addr: Recipient<SubscribeResult>, setting: SettingWrapper) -> Self {
        Self {
            addr,
            events: Vec::new(),
            subscriptions: HashMap::new(),
            setting,
        }
    }
}

impl Actor for Subscriber {
    type Context = Context<Self>;
    fn started(&mut self, ctx: &mut Self::Context) {
        ctx.set_mailbox_capacity(10000);
    }
}

impl Handler<Subscribe> for Subscriber {
    type Result = Subscribed;
    fn handle(&mut self, msg: Subscribe, _: &mut Self::Context) -> Subscribed {
        let map = self.subscriptions.entry(msg.id).or_default();
        let sub_id = msg.subscription.id.clone();
        let r = self.setting.read();
        if map.contains_key(&sub_id) {
            Subscribed::Duplicate
        } else if map.len() >= r.limitation.max_subscriptions {
            Subscribed::Overlimit
        } else {
            map.insert(msg.subscription.id.clone(), msg.subscription);
            Subscribed::Ok
        }
    }
}

impl Handler<Unsubscribe> for Subscriber {
    type Result = ();
    fn handle(&mut self, msg: Unsubscribe, _: &mut Self::Context) {
        if let Some(sub_id) = msg.sub_id {
            if let Some(map) = self.subscriptions.get_mut(&msg.id) {
                map.remove(&sub_id);
            }
        } else {
            self.subscriptions.remove(&msg.id);
        }
    }
}

impl Handler<Dispatch> for Subscriber {
    type Result = ();
    fn handle(&mut self, msg: Dispatch, _: &mut Self::Context) {
        let event = &msg.event;
        let index = event.index();
        let event_str = event.to_string();
        for (session_id, subs) in &self.subscriptions {
            for (sub_id, sub) in subs {
                for filter in &sub.filters {
                    if filter.r#match(index) {
                        self.addr.do_send(SubscribeResult {
                            id: *session_id,
                            msg: OutgoingMessage::event(sub_id, &event_str),
                            sub_id: sub_id.clone(),
                        });
                    }
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::Setting;

    use super::*;
    use actix_rt::time::sleep;
    use anyhow::Result;
    use nostr_db::{Event, Filter};
    use parking_lot::RwLock;
    use std::sync::Arc;
    use std::{str::FromStr, time::Duration};

    #[derive(Default)]
    struct Receiver(Arc<RwLock<Vec<SubscribeResult>>>);
    impl Actor for Receiver {
        type Context = Context<Self>;
    }

    impl Handler<SubscribeResult> for Receiver {
        type Result = ();
        fn handle(&mut self, msg: SubscribeResult, _ctx: &mut Self::Context) {
            self.0.write().push(msg);
        }
    }

    #[actix_rt::test]
    async fn subscribe() -> Result<()> {
        let note = r#"
        {
            "content": "Good morning everyone 😃",
            "created_at": 1680690006,
            "id": "332747c0fab8a1a92def4b0937e177be6df4382ce6dd7724f86dc4710b7d4d7d",
            "kind": 1,
            "pubkey": "7abf57d516b1ff7308ca3bd5650ea6a4674d469c7c5057b1d005fb13d218bfef",
            "sig": "ef4ff4f69ac387239eb1401fb07d7a44a5d5d57127e0dc3466a0403cf7d5486b668608ebfcbe9ff1f8d3b5d710545999fe08ee767284ec0b474e4cf92537678f",
            "tags": [["t", "nostr"]]
          }
        "#;
        let event = Event::from_str(note)?;

        let receiver = Receiver::default();
        let messages = receiver.0.clone();
        let receiver = receiver.start();
        let addr = receiver.recipient();

        let subscriber = Subscriber::new(addr.clone(), Setting::default().into()).start();

        subscriber
            .send(Dispatch {
                id: 0,
                event: event.clone(),
            })
            .await?;

        sleep(Duration::from_millis(100)).await;
        {
            let r = messages.read();
            assert_eq!(r.len(), 0);
            drop(r);
        }

        let res = subscriber
            .send(Subscribe {
                id: 0,
                subscription: Subscription {
                    id: 0.to_string(),
                    filters: vec![Filter {
                        ..Default::default()
                    }],
                },
            })
            .await?;
        assert_eq!(res, Subscribed::Ok);
        let res = subscriber
            .send(Subscribe {
                id: 0,
                subscription: Subscription {
                    id: 0.to_string(),
                    filters: vec![Filter {
                        ..Default::default()
                    }],
                },
            })
            .await?;
        assert_eq!(res, Subscribed::Duplicate);
        let res = subscriber
            .send(Subscribe {
                id: 0,
                subscription: Subscription {
                    id: 1.to_string(),
                    filters: vec![Filter {
                        kinds: Some(vec![1000]),
                        ..Default::default()
                    }],
                },
            })
            .await?;
        assert_eq!(res, Subscribed::Ok);

        subscriber
            .send(Dispatch {
                id: 0,
                event: event.clone(),
            })
            .await?;

        sleep(Duration::from_millis(100)).await;
        let r = messages.read();
        assert_eq!(r.len(), 1);
        drop(r);

        Ok(())
    }
}