|
5 | 5 | use std::sync::mpsc::{Receiver, Sender};
|
6 | 6 | use std::thread;
|
7 | 7 |
|
8 |
| -use heed::RwTxn; |
9 |
| -use nostr::nips::nip01::Coordinate; |
10 |
| -use nostr::{Event, Kind, Timestamp}; |
11 |
| -use nostr_database::{FlatBufferBuilder, RejectedReason, SaveEventStatus}; |
| 8 | +use nostr::Event; |
| 9 | +use nostr_database::{FlatBufferBuilder, SaveEventStatus}; |
12 | 10 | use tokio::sync::oneshot;
|
13 | 11 |
|
14 | 12 | use super::error::Error;
|
@@ -97,150 +95,24 @@ impl Ingester {
|
97 | 95 | event: Event,
|
98 | 96 | fbb: &mut FlatBufferBuilder,
|
99 | 97 | ) -> nostr::Result<SaveEventStatus, Error> {
|
100 |
| - if event.kind.is_ephemeral() { |
101 |
| - return Ok(SaveEventStatus::Rejected(RejectedReason::Ephemeral)); |
102 |
| - } |
103 |
| - |
104 |
| - // Initial read txn checks |
105 |
| - { |
106 |
| - // Acquire read txn |
107 |
| - let read_txn = self.db.read_txn()?; |
108 |
| - |
109 |
| - // Already exists |
110 |
| - if self.db.has_event(&read_txn, event.id.as_bytes())? { |
111 |
| - return Ok(SaveEventStatus::Rejected(RejectedReason::Duplicate)); |
112 |
| - } |
113 |
| - |
114 |
| - // Reject event if ID was deleted |
115 |
| - if self.db.is_deleted(&read_txn, &event.id)? { |
116 |
| - return Ok(SaveEventStatus::Rejected(RejectedReason::Deleted)); |
117 |
| - } |
118 |
| - |
119 |
| - // Reject event if ADDR was deleted after it's created_at date |
120 |
| - // (non-parameterized or parameterized) |
121 |
| - if let Some(coordinate) = event.coordinate() { |
122 |
| - if let Some(time) = self.db.when_is_coordinate_deleted(&read_txn, &coordinate)? { |
123 |
| - if event.created_at <= time { |
124 |
| - return Ok(SaveEventStatus::Rejected(RejectedReason::Deleted)); |
125 |
| - } |
126 |
| - } |
127 |
| - } |
128 |
| - |
129 |
| - read_txn.commit()?; |
130 |
| - } |
131 |
| - |
132 |
| - // Acquire write transaction |
133 |
| - let mut txn = self.db.write_txn()?; |
134 |
| - |
135 |
| - // Remove replaceable events being replaced |
136 |
| - if event.kind.is_replaceable() { |
137 |
| - // Find replaceable event |
138 |
| - if let Some(stored) = self |
139 |
| - .db |
140 |
| - .find_replaceable_event(&txn, &event.pubkey, event.kind)? |
141 |
| - { |
142 |
| - if stored.created_at > event.created_at { |
143 |
| - txn.abort(); |
144 |
| - return Ok(SaveEventStatus::Rejected(RejectedReason::Replaced)); |
145 |
| - } |
146 |
| - |
147 |
| - // Acquire read txn |
148 |
| - let read_txn = self.db.read_txn()?; |
149 |
| - |
150 |
| - let coordinate: Coordinate = Coordinate::new(event.kind, event.pubkey); |
151 |
| - self.db |
152 |
| - .remove_replaceable(&read_txn, &mut txn, &coordinate, event.created_at)?; |
153 |
| - |
154 |
| - read_txn.commit()?; |
155 |
| - } |
156 |
| - } |
157 |
| - |
158 |
| - // Remove parameterized replaceable events being replaced |
159 |
| - if event.kind.is_addressable() { |
160 |
| - if let Some(identifier) = event.tags.identifier() { |
161 |
| - let coordinate: Coordinate = |
162 |
| - Coordinate::new(event.kind, event.pubkey).identifier(identifier); |
163 |
| - |
164 |
| - // Find param replaceable event |
165 |
| - if let Some(stored) = self.db.find_addressable_event(&txn, &coordinate)? { |
166 |
| - if stored.created_at > event.created_at { |
167 |
| - txn.abort(); |
168 |
| - return Ok(SaveEventStatus::Rejected(RejectedReason::Replaced)); |
169 |
| - } |
170 |
| - |
171 |
| - // Acquire read txn |
172 |
| - let read_txn = self.db.read_txn()?; |
173 |
| - |
174 |
| - self.db.remove_addressable( |
175 |
| - &read_txn, |
176 |
| - &mut txn, |
177 |
| - &coordinate, |
178 |
| - Timestamp::max(), |
179 |
| - )?; |
180 |
| - |
181 |
| - read_txn.commit()?; |
182 |
| - } |
183 |
| - } |
184 |
| - } |
185 |
| - |
186 |
| - // Handle deletion events |
187 |
| - if let Kind::EventDeletion = event.kind { |
188 |
| - let invalid: bool = self.handle_deletion_event(&mut txn, &event)?; |
189 |
| - |
190 |
| - if invalid { |
191 |
| - txn.abort(); |
192 |
| - return Ok(SaveEventStatus::Rejected(RejectedReason::InvalidDelete)); |
193 |
| - } |
194 |
| - } |
195 |
| - |
196 |
| - // Store and index the event |
197 |
| - self.db.store(&mut txn, fbb, &event)?; |
198 |
| - |
199 |
| - // Commit |
200 |
| - txn.commit()?; |
201 |
| - |
202 |
| - Ok(SaveEventStatus::Success) |
203 |
| - } |
204 |
| - |
205 |
| - fn handle_deletion_event(&self, txn: &mut RwTxn, event: &Event) -> nostr::Result<bool, Error> { |
206 |
| - // Acquire read txn |
207 | 98 | let read_txn = self.db.read_txn()?;
|
| 99 | + let mut write_txn = self.db.write_txn()?; |
208 | 100 |
|
209 |
| - for id in event.tags.event_ids() { |
210 |
| - if let Some(target) = self.db.get_event_by_id(&read_txn, id.as_bytes())? { |
211 |
| - // Author must match |
212 |
| - if target.pubkey != event.pubkey.as_bytes() { |
213 |
| - return Ok(true); |
214 |
| - } |
215 |
| - |
216 |
| - // Mark as deleted and remove event |
217 |
| - self.db.mark_deleted(txn, id)?; |
218 |
| - self.db.remove(txn, &target)?; |
219 |
| - } |
220 |
| - } |
| 101 | + let status: SaveEventStatus = |
| 102 | + self.db |
| 103 | + .save_event_with_txn(&read_txn, &mut write_txn, fbb, &event)?; |
221 | 104 |
|
222 |
| - for coordinate in event.tags.coordinates() { |
223 |
| - // Author must match |
224 |
| - if coordinate.public_key != event.pubkey { |
225 |
| - return Ok(true); |
| 105 | + match &status { |
| 106 | + SaveEventStatus::Success => { |
| 107 | + write_txn.commit()?; |
| 108 | + read_txn.commit()?; |
226 | 109 | }
|
227 |
| - |
228 |
| - // Mark deleted |
229 |
| - self.db |
230 |
| - .mark_coordinate_deleted(txn, &coordinate.borrow(), event.created_at)?; |
231 |
| - |
232 |
| - // Remove events (up to the created_at of the deletion event) |
233 |
| - if coordinate.kind.is_replaceable() { |
234 |
| - self.db |
235 |
| - .remove_replaceable(&read_txn, txn, coordinate, event.created_at)?; |
236 |
| - } else if coordinate.kind.is_addressable() { |
237 |
| - self.db |
238 |
| - .remove_addressable(&read_txn, txn, coordinate, event.created_at)?; |
| 110 | + SaveEventStatus::Rejected(_) => { |
| 111 | + write_txn.abort(); |
| 112 | + read_txn.commit()?; |
239 | 113 | }
|
240 | 114 | }
|
241 | 115 |
|
242 |
| - read_txn.commit()?; |
243 |
| - |
244 |
| - Ok(false) |
| 116 | + Ok(status) |
245 | 117 | }
|
246 | 118 | }
|
0 commit comments