Skip to content

Commit

Permalink
Merge pull request #1905 from scpwiki/WJ-1165-name-change-token
Browse files Browse the repository at this point in the history
[WJ-1165] Implement recurring job to add user name change tokens
  • Loading branch information
emmiegit authored May 2, 2024
2 parents a39a6c4 + 7f87216 commit 045250d
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 14 deletions.
1 change: 1 addition & 0 deletions deepwell/migrations/20220906103252_deepwell.sql
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ CREATE TABLE "user" (
name TEXT NOT NULL,
slug TEXT NOT NULL,
name_changes_left SMALLINT NOT NULL, -- Default set in runtime configuration.
last_name_change_added_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
last_renamed_at TIMESTAMP WITH TIME ZONE,
email TEXT NOT NULL, -- Can be empty, for instance with system accounts.
email_is_alias BOOLEAN,
Expand Down
10 changes: 7 additions & 3 deletions deepwell/src/config/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,9 +403,13 @@ impl ConfigFile {
special_page_banned,
default_name_changes: i16::from(default_name_changes),
maximum_name_changes: i16::from(maximum_name_changes),
refill_name_change: StdDuration::from_secs(
refill_name_change_days * 24 * 60 * 60,
),
refill_name_change: if refill_name_change_days == 0 {
None
} else {
Some(StdDuration::from_secs(
refill_name_change_days * 24 * 60 * 60,
))
},
minimum_name_bytes,
maximum_message_subject_bytes,
maximum_message_body_bytes,
Expand Down
3 changes: 2 additions & 1 deletion deepwell/src/config/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ pub struct Config {
pub maximum_name_changes: i16,

/// How long until a user gets another name change token.
pub refill_name_change: StdDuration,
/// `None` means that no name change tokens are refilled.
pub refill_name_change: Option<StdDuration>,

/// Minimum length of bytes in a username.
pub minimum_name_bytes: usize,
Expand Down
3 changes: 2 additions & 1 deletion deepwell/src/endpoints/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,6 @@ pub async fn user_add_name_change(
) -> Result<i16> {
let GetUser { user: reference } = params.parse()?;
info!("Adding user name change token to {:?}", reference);
UserService::add_name_change_token(ctx, reference).await
let user = UserService::get(ctx, reference).await?;
UserService::add_name_change_token(ctx, &user).await
}
1 change: 1 addition & 0 deletions deepwell/src/models/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub struct Model {
#[sea_orm(column_type = "Text")]
pub slug: String,
pub name_changes_left: i16,
pub last_name_change_added_at: TimeDateTimeWithTimeZone,
pub last_renamed_at: Option<TimeDateTimeWithTimeZone>,
#[sea_orm(column_type = "Text")]
pub email: String,
Expand Down
8 changes: 2 additions & 6 deletions deepwell/src/services/job/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

use super::prelude::*;
use crate::api::ServerState;
use crate::services::{PageRevisionService, SessionService, TextService};
use crate::services::{PageRevisionService, SessionService, TextService, UserService};
use crate::utils::debug_pointer;
use rsmq_async::{PooledRsmq, RsmqConnection, RsmqMessage};
use sea_orm::TransactionTrait;
Expand Down Expand Up @@ -204,11 +204,7 @@ impl JobWorker {
}
Job::NameChangeRefill => {
debug!("Checking users for those who can get a name change token refill");
// TODO implement name change refill
//
// check users whose time since refill_name_change_days
// refill_name_change_days being zero means disable
// add user credits to each where they are above that time
UserService::refresh_name_change_tokens(ctx).await?;
NextJob::Next {
job: Job::NameChangeRefill,
delay: Some(self.state.config.job_name_change_refill),
Expand Down
30 changes: 27 additions & 3 deletions deepwell/src/services/user/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,17 +563,41 @@ impl UserService {
Ok(())
}

pub async fn refresh_name_change_tokens(ctx: &ServiceContext<'_>) -> Result<()> {
info!("Refreshing name change tokens for all users who need one");

let needs_token_time = match ctx.config().refill_name_change {
Some(refill_name_change) => now() - refill_name_change,
None => return Ok(()),
};

let txn = ctx.transaction();
let users = User::find()
.filter(user::Column::LastNameChangeAddedAt.gte(needs_token_time))
.all(txn)
.await?;

debug!(
"Found {} users in need of a name refresh token",
users.len(),
);

for user in users {
Self::add_name_change_token(ctx, &user).await?;
}

Ok(())
}

/// Adds an additional rename token, up to the cap.
///
/// # Returns
/// The current number of rename tokens the user has.
pub async fn add_name_change_token(
ctx: &ServiceContext<'_>,
reference: Reference<'_>,
user: &UserModel,
) -> Result<i16> {
let txn = ctx.transaction();
let user = Self::get(ctx, reference).await?;

let max_name_changes = ctx.config().maximum_name_changes;
let name_changes = cmp::min(user.name_changes_left + 1, max_name_changes);
let model = user::ActiveModel {
Expand Down

0 comments on commit 045250d

Please sign in to comment.