MCPcopy Create free account
hub / github.com/chatmail/core / build_sync_json

Method build_sync_json

src/sync.rs:249–287  ·  view source on GitHub ↗

Copies all sync items to a JSON string and clears the sync-table. Returns the JSON string and a comma-separated string of the IDs used.

(&self)

Source from the content-addressed store, hash-verified

247 /// Copies all sync items to a JSON string and clears the sync-table.
248 /// Returns the JSON string and a comma-separated string of the IDs used.
249 pub(crate) async fn build_sync_json(&self) -> Result<Option<(String, String)>> {
250 let (ids, serialized) = self
251 .sql
252 .query_map(
253 "SELECT id, item FROM multi_device_sync ORDER BY id;",
254 (),
255 |row| {
256 let id: u32 = row.get(0)?;
257 let item: String = row.get(1)?;
258 Ok((id, item))
259 },
260 |rows| {
261 let mut ids = vec![];
262 let mut serialized = String::default();
263 for row in rows {
264 let (id, item) = row?;
265 ids.push(id);
266 if !serialized.is_empty() {
267 serialized.push_str(",\n");
268 }
269 serialized.push_str(&item);
270 }
271 Ok((ids, serialized))
272 },
273 )
274 .await?;
275
276 if ids.is_empty() {
277 Ok(None)
278 } else {
279 Ok(Some((
280 format!("{{\"items\":[\n{serialized}\n]}}"),
281 ids.iter()
282 .map(|x| x.to_string())
283 .collect::<Vec<String>>()
284 .join(","),
285 )))
286 }
287 }
288
289 pub(crate) fn build_sync_part(&self, json: String) -> MimePart<'static> {
290 MimePart::new("application/json", json).attachment("multi-device-sync.json")

Callers 2

send_sync_msgMethod · 0.80
test_build_sync_jsonFunction · 0.80

Calls 4

query_mapMethod · 0.80
getMethod · 0.45
is_emptyMethod · 0.45
iterMethod · 0.45

Tested by 1

test_build_sync_jsonFunction · 0.64