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)
| 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") |