mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2026-11-15 13:16:56 +00:00
style: Eliminate implement
This commit is contained in:
3 files changed
+113
-113
No files matched your search
+15
-15
@@ -1,23 +1,23 @@
|
|||||||
use conduwuit::Result;
|
use conduwuit::Result;
|
||||||
use conduwuit_macros::implement;
|
|
||||||
use futures::StreamExt;
|
use futures::StreamExt;
|
||||||
|
|
||||||
use crate::Context;
|
use crate::Context;
|
||||||
|
|
||||||
#[implement(Context, params = "<'_>")]
|
impl Context {
|
||||||
pub(super) async fn check_all_users(&self) -> Result {
|
pub(super) async fn check_all_users(&self) -> Result {
|
||||||
let timer = tokio::time::Instant::now();
|
let timer = tokio::time::Instant::now();
|
||||||
let users = self.services.users.stream().collect::<Vec<_>>().await;
|
let users = self.services.users.stream().collect::<Vec<_>>().await;
|
||||||
let query_time = timer.elapsed();
|
let query_time = timer.elapsed();
|
||||||
|
|
||||||
let total = users.len();
|
let total = users.len();
|
||||||
let err_count = users.iter().filter(|_user| false).count();
|
let err_count = users.iter().filter(|_user| false).count();
|
||||||
let ok_count = users.iter().filter(|_user| true).count();
|
let ok_count = users.iter().filter(|_user| true).count();
|
||||||
|
|
||||||
self.write_str(&format!(
|
self.write_str(&format!(
|
||||||
"Database query completed in {query_time:?}:\n\n```\nTotal entries: \
|
"Database query completed in {query_time:?}:\n\n```\nTotal entries: \
|
||||||
{total:?}\nFailure/Invalid user count: {err_count:?}\nSuccess/Valid user count: \
|
{total:?}\nFailure/Invalid user count: {err_count:?}\nSuccess/Valid user count: \
|
||||||
{ok_count:?}\n```"
|
{ok_count:?}\n```"
|
||||||
))
|
))
|
||||||
.await
|
.await
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,32 +1,34 @@
|
|||||||
use ruma::{RoomVersionId, canonical_json::redact_content_in_place};
|
use ruma::{RoomVersionId, canonical_json::redact_content_in_place};
|
||||||
use serde_json::{Value as JsonValue, json, value::to_raw_value};
|
use serde_json::{Value as JsonValue, json, value::to_raw_value};
|
||||||
|
|
||||||
use crate::{Err, Result, err, implement};
|
use crate::{Err, Result, err};
|
||||||
|
|
||||||
#[implement(super::Pdu)]
|
impl super::Pdu {
|
||||||
pub fn redact(&mut self, room_version_id: &RoomVersionId, reason: JsonValue) -> Result {
|
pub fn redact(&mut self, room_version_id: &RoomVersionId, reason: JsonValue) -> Result {
|
||||||
let Some(rules) = room_version_id.rules() else {
|
let Some(rules) = room_version_id.rules() else {
|
||||||
return Err!("Cannot redact event for unknown room version {room_version_id}");
|
return Err!("Cannot redact event for unknown room version {room_version_id}");
|
||||||
};
|
};
|
||||||
|
|
||||||
self.unsigned = None;
|
self.unsigned = None;
|
||||||
|
|
||||||
let mut content = serde_json::from_str(self.content.get())
|
let mut content = serde_json::from_str(self.content.get()).map_err(|e| {
|
||||||
.map_err(|e| err!(Request(BadJson("Failed to deserialize content into type: {e}"))))?;
|
err!(Request(BadJson("Failed to deserialize content into type: {e}")))
|
||||||
|
})?;
|
||||||
|
|
||||||
redact_content_in_place(&mut content, &rules.redaction, self.kind.to_string());
|
redact_content_in_place(&mut content, &rules.redaction, self.kind.to_string());
|
||||||
|
|
||||||
let reason = serde_json::to_value(reason).expect("Failed to preserialize reason");
|
let reason = serde_json::to_value(reason).expect("Failed to preserialize reason");
|
||||||
|
|
||||||
let redacted_because = json!({
|
let redacted_because = json!({
|
||||||
"redacted_because": reason,
|
"redacted_because": reason,
|
||||||
});
|
});
|
||||||
|
|
||||||
self.unsigned = to_raw_value(&redacted_because)
|
self.unsigned = to_raw_value(&redacted_because)
|
||||||
.expect("Failed to serialize unsigned")
|
.expect("Failed to serialize unsigned")
|
||||||
.into();
|
.into();
|
||||||
|
|
||||||
self.content = to_raw_value(&content).expect("Failed to serialize content");
|
self.content = to_raw_value(&content).expect("Failed to serialize content");
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -4,85 +4,83 @@ use ruma::MilliSecondsSinceUnixEpoch;
|
|||||||
use serde_json::value::{RawValue as RawJsonValue, Value as JsonValue, to_raw_value};
|
use serde_json::value::{RawValue as RawJsonValue, Value as JsonValue, to_raw_value};
|
||||||
|
|
||||||
use super::Pdu;
|
use super::Pdu;
|
||||||
use crate::{Result, err, implement, result::LogErr};
|
use crate::{Result, err, result::LogErr};
|
||||||
|
|
||||||
/// Set the `unsigned` field of the PDU using only information in the PDU.
|
impl Pdu {
|
||||||
/// Some unsigned data is already set within the database (eg. prev events,
|
/// Set the `unsigned` field of the PDU using only information in the PDU.
|
||||||
/// threads). Once this is done, other data must be calculated from the database
|
/// Some unsigned data is already set within the database (eg. prev events,
|
||||||
/// (eg. relations) This is for server-to-client events.
|
/// threads). Once this is done, other data must be calculated from the
|
||||||
/// Backfill handles this itself.
|
/// database (eg. relations) This is for server-to-client events.
|
||||||
#[implement(Pdu)]
|
/// Backfill handles this itself.
|
||||||
pub fn set_unsigned(&mut self, user_id: Option<&ruma::UserId>) {
|
pub fn set_unsigned(&mut self, user_id: Option<&ruma::UserId>) {
|
||||||
if Some(self.sender.borrow()) != user_id {
|
if Some(self.sender.borrow()) != user_id {
|
||||||
self.remove_transaction_id().log_err().ok();
|
self.remove_transaction_id().log_err().ok();
|
||||||
|
}
|
||||||
|
self.add_age().log_err().ok();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn remove_transaction_id(&mut self) -> Result {
|
||||||
|
use BTreeMap as Map;
|
||||||
|
|
||||||
|
let Some(unsigned) = &self.unsigned else {
|
||||||
|
return Ok(());
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut unsigned: Map<&str, Box<RawJsonValue>> = serde_json::from_str(unsigned.get())
|
||||||
|
.map_err(|e| err!(Database("Invalid unsigned in pdu event: {e}")))?;
|
||||||
|
|
||||||
|
unsigned.remove("transaction_id");
|
||||||
|
self.unsigned = to_raw_value(&unsigned)
|
||||||
|
.map(Some)
|
||||||
|
.expect("unsigned is valid");
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_age(&mut self) -> Result {
|
||||||
|
use BTreeMap as Map;
|
||||||
|
|
||||||
|
let mut unsigned: Map<&str, Box<RawJsonValue>> = self
|
||||||
|
.unsigned
|
||||||
|
.as_deref()
|
||||||
|
.map(RawJsonValue::get)
|
||||||
|
.map_or_else(|| Ok(Map::new()), serde_json::from_str)
|
||||||
|
.map_err(|e| err!(Database("Invalid unsigned in pdu event: {e}")))?;
|
||||||
|
|
||||||
|
// deliberately allowing for the possibility of negative age
|
||||||
|
let now: i128 = MilliSecondsSinceUnixEpoch::now().get().into();
|
||||||
|
let then: i128 = self.origin_server_ts.into();
|
||||||
|
let this_age = now.saturating_sub(then);
|
||||||
|
|
||||||
|
unsigned.insert("age", to_raw_value(&this_age)?);
|
||||||
|
self.unsigned = Some(to_raw_value(&unsigned)?);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_relation(&mut self, name: &str, pdu: Option<&Pdu>) -> Result {
|
||||||
|
use serde_json::Map;
|
||||||
|
|
||||||
|
let mut unsigned: Map<String, JsonValue> = self
|
||||||
|
.unsigned
|
||||||
|
.as_deref()
|
||||||
|
.map(RawJsonValue::get)
|
||||||
|
.map_or_else(|| Ok(Map::new()), serde_json::from_str)
|
||||||
|
.map_err(|e| err!(Database("Invalid unsigned in pdu event: {e}")))?;
|
||||||
|
|
||||||
|
let pdu = pdu
|
||||||
|
.map(serde_json::to_value)
|
||||||
|
.transpose()?
|
||||||
|
.unwrap_or_else(|| JsonValue::Object(Map::new()));
|
||||||
|
|
||||||
|
unsigned
|
||||||
|
.entry("m.relations")
|
||||||
|
.or_insert(JsonValue::Object(Map::new()))
|
||||||
|
.as_object_mut()
|
||||||
|
.map(|object| object.insert(name.to_owned(), pdu));
|
||||||
|
|
||||||
|
self.unsigned = Some(to_raw_value(&unsigned)?);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
self.add_age().log_err().ok();
|
|
||||||
}
|
|
||||||
|
|
||||||
#[implement(Pdu)]
|
|
||||||
pub fn remove_transaction_id(&mut self) -> Result {
|
|
||||||
use BTreeMap as Map;
|
|
||||||
|
|
||||||
let Some(unsigned) = &self.unsigned else {
|
|
||||||
return Ok(());
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut unsigned: Map<&str, Box<RawJsonValue>> = serde_json::from_str(unsigned.get())
|
|
||||||
.map_err(|e| err!(Database("Invalid unsigned in pdu event: {e}")))?;
|
|
||||||
|
|
||||||
unsigned.remove("transaction_id");
|
|
||||||
self.unsigned = to_raw_value(&unsigned)
|
|
||||||
.map(Some)
|
|
||||||
.expect("unsigned is valid");
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[implement(Pdu)]
|
|
||||||
pub fn add_age(&mut self) -> Result {
|
|
||||||
use BTreeMap as Map;
|
|
||||||
|
|
||||||
let mut unsigned: Map<&str, Box<RawJsonValue>> = self
|
|
||||||
.unsigned
|
|
||||||
.as_deref()
|
|
||||||
.map(RawJsonValue::get)
|
|
||||||
.map_or_else(|| Ok(Map::new()), serde_json::from_str)
|
|
||||||
.map_err(|e| err!(Database("Invalid unsigned in pdu event: {e}")))?;
|
|
||||||
|
|
||||||
// deliberately allowing for the possibility of negative age
|
|
||||||
let now: i128 = MilliSecondsSinceUnixEpoch::now().get().into();
|
|
||||||
let then: i128 = self.origin_server_ts.into();
|
|
||||||
let this_age = now.saturating_sub(then);
|
|
||||||
|
|
||||||
unsigned.insert("age", to_raw_value(&this_age)?);
|
|
||||||
self.unsigned = Some(to_raw_value(&unsigned)?);
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[implement(Pdu)]
|
|
||||||
pub fn add_relation(&mut self, name: &str, pdu: Option<&Pdu>) -> Result {
|
|
||||||
use serde_json::Map;
|
|
||||||
|
|
||||||
let mut unsigned: Map<String, JsonValue> = self
|
|
||||||
.unsigned
|
|
||||||
.as_deref()
|
|
||||||
.map(RawJsonValue::get)
|
|
||||||
.map_or_else(|| Ok(Map::new()), serde_json::from_str)
|
|
||||||
.map_err(|e| err!(Database("Invalid unsigned in pdu event: {e}")))?;
|
|
||||||
|
|
||||||
let pdu = pdu
|
|
||||||
.map(serde_json::to_value)
|
|
||||||
.transpose()?
|
|
||||||
.unwrap_or_else(|| JsonValue::Object(Map::new()));
|
|
||||||
|
|
||||||
unsigned
|
|
||||||
.entry("m.relations")
|
|
||||||
.or_insert(JsonValue::Object(Map::new()))
|
|
||||||
.as_object_mut()
|
|
||||||
.map(|object| object.insert(name.to_owned(), pdu));
|
|
||||||
|
|
||||||
self.unsigned = Some(to_raw_value(&unsigned)?);
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user