de-global services for services

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-07-18 06:37:47 +00:00
parent 992c0a1e58
commit 010e4ee35a
85 changed files with 2480 additions and 1887 deletions

View file

@ -1,29 +1,40 @@
use std::{mem::size_of, sync::Arc};
use conduit::{checked, utils, Error, Result};
use database::{Database, Map};
use conduit::{checked, utils, Error, PduEvent, Result};
use database::Map;
use ruma::{api::client::threads::get_threads::v1::IncludeThreads, OwnedUserId, RoomId, UserId};
use crate::{services, PduEvent};
use crate::{rooms, Dep};
type PduEventIterResult<'a> = Result<Box<dyn Iterator<Item = Result<(u64, PduEvent)>> + 'a>>;
pub(super) struct Data {
threadid_userids: Arc<Map>,
services: Services,
}
struct Services {
short: Dep<rooms::short::Service>,
timeline: Dep<rooms::timeline::Service>,
}
impl Data {
pub(super) fn new(db: &Arc<Database>) -> Self {
pub(super) fn new(args: &crate::Args<'_>) -> Self {
let db = &args.db;
Self {
threadid_userids: db["threadid_userids"].clone(),
services: Services {
short: args.depend::<rooms::short::Service>("rooms::short"),
timeline: args.depend::<rooms::timeline::Service>("rooms::timeline"),
},
}
}
pub(super) fn threads_until<'a>(
&'a self, user_id: &'a UserId, room_id: &'a RoomId, until: u64, _include: &'a IncludeThreads,
) -> PduEventIterResult<'a> {
let prefix = services()
.rooms
let prefix = self
.services
.short
.get_shortroomid(room_id)?
.expect("room exists")
@ -40,8 +51,8 @@ impl Data {
.map(move |(pduid, _users)| {
let count = utils::u64_from_bytes(&pduid[(size_of::<u64>())..])
.map_err(|_| Error::bad_database("Invalid pduid in threadid_userids."))?;
let mut pdu = services()
.rooms
let mut pdu = self
.services
.timeline
.get_pdu_from_id(&pduid)?
.ok_or_else(|| Error::bad_database("Invalid pduid reference in threadid_userids"))?;

View file

@ -2,7 +2,7 @@ mod data;
use std::{collections::BTreeMap, sync::Arc};
use conduit::{Error, Result};
use conduit::{Error, PduEvent, Result};
use data::Data;
use ruma::{
api::client::{error::ErrorKind, threads::get_threads::v1::IncludeThreads},
@ -11,16 +11,24 @@ use ruma::{
};
use serde_json::json;
use crate::{services, PduEvent};
use crate::{rooms, Dep};
pub struct Service {
services: Services,
db: Data,
}
struct Services {
timeline: Dep<rooms::timeline::Service>,
}
impl crate::Service for Service {
fn build(args: crate::Args<'_>) -> Result<Arc<Self>> {
Ok(Arc::new(Self {
db: Data::new(args.db),
services: Services {
timeline: args.depend::<rooms::timeline::Service>("rooms::timeline"),
},
db: Data::new(&args),
}))
}
@ -35,22 +43,22 @@ impl Service {
}
pub fn add_to_thread(&self, root_event_id: &EventId, pdu: &PduEvent) -> Result<()> {
let root_id = &services()
.rooms
let root_id = self
.services
.timeline
.get_pdu_id(root_event_id)?
.ok_or_else(|| Error::BadRequest(ErrorKind::InvalidParam, "Invalid event id in thread message"))?;
let root_pdu = services()
.rooms
let root_pdu = self
.services
.timeline
.get_pdu_from_id(root_id)?
.get_pdu_from_id(&root_id)?
.ok_or_else(|| Error::BadRequest(ErrorKind::InvalidParam, "Thread root pdu not found"))?;
let mut root_pdu_json = services()
.rooms
let mut root_pdu_json = self
.services
.timeline
.get_pdu_json_from_id(root_id)?
.get_pdu_json_from_id(&root_id)?
.ok_or_else(|| Error::BadRequest(ErrorKind::InvalidParam, "Thread root pdu not found"))?;
if let CanonicalJsonValue::Object(unsigned) = root_pdu_json
@ -93,20 +101,19 @@ impl Service {
);
}
services()
.rooms
self.services
.timeline
.replace_pdu(root_id, &root_pdu_json, &root_pdu)?;
.replace_pdu(&root_id, &root_pdu_json, &root_pdu)?;
}
let mut users = Vec::new();
if let Some(userids) = self.db.get_participants(root_id)? {
if let Some(userids) = self.db.get_participants(&root_id)? {
users.extend_from_slice(&userids);
} else {
users.push(root_pdu.sender);
}
users.push(pdu.sender.clone());
self.db.update_participants(root_id, &users)
self.db.update_participants(&root_id, &users)
}
}