summaryrefslogtreecommitdiff
path: root/src/test/webpush.rs
blob: c86d03f2900162806de05e4008ad6f451c5c5494 (plain)
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
use std::{
    mem,
    sync::{Arc, Mutex},
};

use web_push::{WebPushClient, WebPushError, WebPushMessage};

#[derive(Clone)]
pub struct Client {
    sent: Arc<Mutex<Vec<WebPushMessage>>>,
}

impl Client {
    pub fn new() -> Self {
        Self {
            sent: Arc::default(),
        }
    }

    // Clears the list of sent messages (for all clones of this Client) when called, because we
    // can't clone `WebPushMessage`s so we either need to move them or try to reconstruct them,
    // either of which sucks but moving them sucks less.
    pub fn sent(&self) -> Vec<WebPushMessage> {
        let mut sent = self.sent.lock().unwrap();
        mem::replace(&mut *sent, Vec::new())
    }
}

#[async_trait::async_trait]
impl WebPushClient for Client {
    async fn send(&self, message: WebPushMessage) -> Result<(), WebPushError> {
        let mut sent = self.sent.lock().unwrap();
        sent.push(message);

        Ok(())
    }
}