Remove name as it maps to emails in stalwart

This commit is contained in:
traxys 2023-10-08 11:37:52 +02:00
parent 1a5573f68d
commit 56b71230e9
3 changed files with 14 additions and 80 deletions

View file

@ -0,0 +1,2 @@
-- Add migration script here
ALTER TABLE accounts DROP COLUMN name;

View file

@ -567,11 +567,6 @@ async fn home(
)
.fetch(&state.db);
let name = sqlx::query!("SELECT name FROM accounts WHERE id = $1", user)
.fetch_optional(&state.db)
.await?
.and_then(|r| r.name);
let mut aliases: HashMap<_, _> = aliases.into_iter().map(|a| (a.mail, Vec::new())).collect();
while let Some(alias) = alias_stream.try_next().await? {
aliases.get_mut(&alias.alias).unwrap().push(alias.recipient);
@ -587,7 +582,6 @@ async fn home(
context.insert("mails", &mails);
context.insert("mail_domain", &state.mail_domain);
context.insert("aliases", &aliases);
context.insert("name", &name);
if let Some(err) = query.user_error {
tracing::info!("User error: {err:?}");
context.insert("user_error", &err.to_string());
@ -844,43 +838,6 @@ async fn set_password(
Ok(Redirect::to("/"))
}
#[derive(Deserialize, Debug)]
struct Name {
name: String,
}
#[tracing::instrument(skip(state))]
async fn set_name(
state: State<Arc<AppState>>,
User(user): User,
Form(name): Form<Name>,
) -> Result<Redirect, Error> {
let taken = sqlx::query!(
"SELECT COUNT(*) FROM accounts WHERE name = $1 AND id != $2",
name.name,
user
)
.fetch_one(&state.db)
.await?
.count
.expect("count returned null")
!= 0;
if taken {
Ok(UserError::NameAlreadyExists(name.name).into())
} else {
sqlx::query!(
"UPDATE accounts SET name = $1 WHERE id = $2",
name.name,
user
)
.execute(&state.db)
.await?;
Ok(Redirect::to("/"))
}
}
#[tokio::main]
async fn main() -> color_eyre::Result<()> {
color_eyre::install()?;
@ -918,7 +875,6 @@ async fn main() -> color_eyre::Result<()> {
.route("/alias/recipient/delete", post(delete_recipient))
.route("/alias/delete", post(delete_alias))
.route("/password", post(set_password))
.route("/name", post(set_name))
.fallback(page_not_found)
.with_state(Arc::new(AppState {
db,

View file

@ -99,43 +99,19 @@
{% if user_error %}
<div class="alert alert-danger">{{ user_error }}</div>
{% endif %}
<h2>
Account
{% if name %}
({{ name }})
{% else %}
(no name, disabled)
{% endif %}
</h2>
<div class="container text-center">
<div class="row">
<div class="col">
<form action="/name" method="post">
<div class="form-floating mb-3">
<input type="text"
class="form-control"
id="newName"
placeholder="account name"
name="name">
<label for="newName">New Name</label>
</div>
<button type="submit" class="btn btn-primary">Change name</button>
</form>
<h2>Account</h2>
<div class="text-center">
<form action="/password" method="post">
<div class="form-floating mb-3">
<input type="password"
class="form-control"
id="newPassword"
placeholder="password"
name="password">
<label for="newPassword">New Password</label>
</div>
<div class="col">
<form action="/password" method="post">
<div class="form-floating mb-3">
<input type="password"
class="form-control"
id="newPassword"
placeholder="password"
name="password">
<label for="newPassword">New Password</label>
</div>
<button type="submit" class="btn btn-primary">Change password</button>
</form>
</div>
</div>
<button type="submit" class="btn btn-primary">Change password</button>
</form>
</div>
<h2 class="title is-2 mt-2">Mails</h2>
<ul class="list-group">