use anyhow::Context;
use apalis_core::{context::JobContext, executor::TokioExecutor, monitor::Monitor};
use mas_storage::{
compat::CompatSessionFilter,
job::{DeactivateUserJob, JobWithSpanContext, ReactivateUserJob},
oauth2::OAuth2SessionFilter,
user::{BrowserSessionFilter, UserRepository},
RepositoryAccess,
};
use tracing::info;
use crate::{storage::PostgresStorageFactory, JobContextExt, State};
#[tracing::instrument(
name = "job.deactivate_user"
fields(user.id = %job.user_id(), erase = %job.hs_erase()),
skip_all,
err(Debug),
)]
async fn deactivate_user(
job: JobWithSpanContext<DeactivateUserJob>,
ctx: JobContext,
) -> Result<(), anyhow::Error> {
let state = ctx.state();
let clock = state.clock();
let matrix = state.matrix_connection();
let mut repo = state.repository().await?;
let user = repo
.user()
.lookup(job.user_id())
.await?
.context("User not found")?;
let user = repo
.user()
.lock(&clock, user)
.await
.context("Failed to lock user")?;
let n = repo
.browser_session()
.finish_bulk(
&clock,
BrowserSessionFilter::new().for_user(&user).active_only(),
)
.await?;
info!(affected = n, "Killed all browser sessions for user");
let n = repo
.oauth2_session()
.finish_bulk(
&clock,
OAuth2SessionFilter::new().for_user(&user).active_only(),
)
.await?;
info!(affected = n, "Killed all OAuth 2.0 sessions for user");
let n = repo
.compat_session()
.finish_bulk(
&clock,
CompatSessionFilter::new().for_user(&user).active_only(),
)
.await?;
info!(affected = n, "Killed all compatibility sessions for user");
repo.save().await?;
let mxid = matrix.mxid(&user.username);
info!("Deactivating user {} on homeserver", mxid);
matrix.delete_user(&mxid, job.hs_erase()).await?;
Ok(())
}
#[tracing::instrument(
name = "job.reactivate_user",
fields(user.id = %job.user_id()),
skip_all,
err(Debug),
)]
pub async fn reactivate_user(
job: JobWithSpanContext<ReactivateUserJob>,
ctx: JobContext,
) -> Result<(), anyhow::Error> {
let state = ctx.state();
let matrix = state.matrix_connection();
let mut repo = state.repository().await?;
let user = repo
.user()
.lookup(job.user_id())
.await?
.context("User not found")?;
let mxid = matrix.mxid(&user.username);
info!("Reactivating user {} on homeserver", mxid);
matrix.reactivate_user(&mxid).await?;
let _user = repo.user().unlock(user).await?;
repo.save().await?;
Ok(())
}
pub(crate) fn register(
suffix: &str,
monitor: Monitor<TokioExecutor>,
state: &State,
storage_factory: &PostgresStorageFactory,
) -> Monitor<TokioExecutor> {
let deactivate_user_worker =
crate::build!(DeactivateUserJob => deactivate_user, suffix, state, storage_factory);
let reactivate_user_worker =
crate::build!(ReactivateUserJob => reactivate_user, suffix, state, storage_factory);
monitor
.register(deactivate_user_worker)
.register(reactivate_user_worker)
}