Can set Matrix access token
This commit is contained in:
@ -16,6 +16,8 @@ pub enum HttpFailure {
|
||||
SessionError(#[from] actix_session::SessionGetError),
|
||||
#[error("an unspecified open id error occurred: {0}")]
|
||||
OpenID(Box<dyn Error>),
|
||||
#[error("an error occurred while fetching user configuration: {0}")]
|
||||
FetchUserConfig(anyhow::Error),
|
||||
#[error("an unspecified internal error occurred: {0}")]
|
||||
InternalError(#[from] anyhow::Error),
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
use crate::app_config::AppConfig;
|
||||
use crate::constants::{STATE_KEY, USER_SESSION_KEY};
|
||||
use crate::server::{HttpFailure, HttpResult};
|
||||
use crate::user::{User, UserID};
|
||||
use crate::user::{User, UserConfig, UserID};
|
||||
use crate::utils;
|
||||
use actix_session::Session;
|
||||
use actix_web::{web, HttpResponse};
|
||||
@ -32,10 +32,21 @@ pub async fn static_file(path: web::Path<String>) -> HttpResult {
|
||||
struct HomeTemplate {
|
||||
name: String,
|
||||
matrix_token: String,
|
||||
success_message: Option<String>,
|
||||
error_message: Option<String>,
|
||||
}
|
||||
|
||||
/// Update matrix token request
|
||||
#[derive(serde::Deserialize)]
|
||||
pub struct UpdateMatrixToken {
|
||||
new_matrix_token: Option<String>,
|
||||
}
|
||||
|
||||
/// Main route
|
||||
pub async fn home(session: Session) -> HttpResult {
|
||||
pub async fn home(
|
||||
session: Session,
|
||||
update_matrix_token: Option<web::Form<UpdateMatrixToken>>,
|
||||
) -> HttpResult {
|
||||
// Get user information, requesting authentication if information is missing
|
||||
let Some(user): Option<User> = session.get(USER_SESSION_KEY)? else {
|
||||
// Generate auth state
|
||||
@ -54,12 +65,37 @@ pub async fn home(session: Session) -> HttpResult {
|
||||
.finish());
|
||||
};
|
||||
|
||||
let mut success_message = None;
|
||||
let mut error_message = None;
|
||||
|
||||
// Retrieve user configuration
|
||||
let mut config = UserConfig::load(&user.id)
|
||||
.await
|
||||
.map_err(HttpFailure::FetchUserConfig)?;
|
||||
|
||||
// Update matrix token, if requested
|
||||
if let Some(update_matrix_token) = update_matrix_token {
|
||||
if let Some(t) = update_matrix_token.0.new_matrix_token {
|
||||
if t.len() < 3 {
|
||||
error_message = Some("Specified Matrix token is too short!".to_string());
|
||||
} else {
|
||||
// TODO : invalidate all existing connections
|
||||
config.matrix_token = t;
|
||||
config.save().await?;
|
||||
success_message = Some("Matrix token was successfully updated!".to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Render page
|
||||
Ok(HttpResponse::Ok()
|
||||
.insert_header(("content-type", "text/html"))
|
||||
.body(
|
||||
HomeTemplate {
|
||||
name: user.name,
|
||||
matrix_token: "TODO".to_string(),
|
||||
matrix_token: config.obfuscated_matrix_token(),
|
||||
success_message,
|
||||
error_message,
|
||||
}
|
||||
.render()
|
||||
.unwrap(),
|
||||
|
Reference in New Issue
Block a user