Skip to main content

chatty_rs/models/
storage.rs

1use crate::models::Conversation;
2
3#[derive(Debug, Clone, Default)]
4pub struct FilterConversation {
5    id: Option<String>,
6    title: Option<String>,
7    message_contains: Option<String>,
8    updated_at_from: Option<chrono::DateTime<chrono::Utc>>,
9    updated_at_to: Option<chrono::DateTime<chrono::Utc>>,
10    created_at_from: Option<chrono::DateTime<chrono::Utc>>,
11    created_at_to: Option<chrono::DateTime<chrono::Utc>>,
12}
13
14impl FilterConversation {
15    pub fn with_id(mut self, id: impl Into<String>) -> Self {
16        self.id = Some(id.into());
17        self
18    }
19
20    pub fn with_title(mut self, title: impl Into<String>) -> Self {
21        self.title = Some(title.into());
22        self
23    }
24
25    pub fn with_message_contains(mut self, message_contains: impl Into<String>) -> Self {
26        self.message_contains = Some(message_contains.into());
27        self
28    }
29
30    pub fn with_updated_at_from(mut self, from: chrono::DateTime<chrono::Utc>) -> Self {
31        self.updated_at_from = Some(from);
32        self
33    }
34
35    pub fn with_updated_at_to(mut self, to: chrono::DateTime<chrono::Utc>) -> Self {
36        self.updated_at_to = Some(to);
37        self
38    }
39
40    pub fn with_created_at_from(mut self, from: chrono::DateTime<chrono::Utc>) -> Self {
41        self.created_at_from = Some(from);
42        self
43    }
44
45    pub fn with_created_at_to(mut self, to: chrono::DateTime<chrono::Utc>) -> Self {
46        self.created_at_to = Some(to);
47        self
48    }
49
50    pub fn id(&self) -> Option<&str> {
51        self.id.as_deref()
52    }
53
54    pub fn title(&self) -> Option<&str> {
55        self.title.as_deref()
56    }
57
58    pub fn message_contains(&self) -> Option<&str> {
59        self.message_contains.as_deref()
60    }
61
62    pub fn updated_at_from(&self) -> Option<chrono::DateTime<chrono::Utc>> {
63        self.updated_at_from
64    }
65
66    pub fn updated_at_to(&self) -> Option<chrono::DateTime<chrono::Utc>> {
67        self.updated_at_to
68    }
69
70    pub fn created_at_from(&self) -> Option<chrono::DateTime<chrono::Utc>> {
71        self.created_at_from
72    }
73
74    pub fn created_at_to(&self) -> Option<chrono::DateTime<chrono::Utc>> {
75        self.created_at_to
76    }
77
78    pub fn matches(&self, conversation: &Conversation) -> bool {
79        if let Some(id) = &self.id {
80            if conversation.id() != id {
81                return false;
82            }
83        }
84
85        if let Some(title) = &self.title {
86            if conversation.title() != title {
87                return false;
88            }
89        }
90
91        if let Some(message_contains) = &self.message_contains {
92            if !conversation
93                .messages()
94                .iter()
95                .any(|msg| msg.text().contains(message_contains))
96            {
97                return false;
98            }
99        }
100
101        if let Some(from) = self.updated_at_from {
102            if conversation.updated_at() < from {
103                return false;
104            }
105        }
106
107        if let Some(to) = self.updated_at_to {
108            if conversation.updated_at() > to {
109                return false;
110            }
111        }
112
113        if let Some(from) = self.created_at_from {
114            if conversation.created_at() < from {
115                return false;
116            }
117        }
118
119        if let Some(to) = self.created_at_to {
120            if conversation.created_at() > to {
121                return false;
122            }
123        }
124
125        true
126    }
127}