Commit a6834d59 authored by Alejandro Domínguez's avatar Alejandro Domínguez

API, user: Separate endpoint connection from query build

parent f2e3b2a3
Pipeline #68900 passed with stages
in 17 minutes and 50 seconds
......@@ -12,8 +12,8 @@ use crate::widgets;
use crate::widgets::AvatarExt;
use crate::cache::download_to_cache;
use fractal_api::r0::account::Medium;
use fractal_api::types::ThirdPartyIdentifier;
use fractal_api::r0::contact::get_identifiers::ThirdPartyIdentifier;
use fractal_api::r0::Medium;
impl AppOp {
pub fn set_three_pid(&self, data: Option<Vec<ThirdPartyIdentifier>>) {
......
use fractal_api::r0::account::Medium;
use fractal_api::r0::Medium;
use glib::signal;
use gtk;
use gtk::prelude::*;
......
......@@ -105,69 +105,40 @@ impl Backend {
}
// User module
Ok(BKCommand::GetUsername) => {
let r = user::get_username(self);
bkerror!(r, tx, BKResponse::UserNameError);
}
Ok(BKCommand::SetUserName(name)) => {
let r = user::set_username(self, name);
bkerror!(r, tx, BKResponse::SetUserNameError);
}
Ok(BKCommand::GetThreePID) => {
let r = user::get_threepid(self);
bkerror!(r, tx, BKResponse::GetThreePIDError);
}
Ok(BKCommand::GetUsername) => user::get_username(self),
Ok(BKCommand::SetUserName(name)) => user::set_username(self, name),
Ok(BKCommand::GetThreePID) => user::get_threepid(self),
Ok(BKCommand::GetTokenEmail(identity, email, client_secret)) => {
let r = user::get_email_token(self, identity, email, client_secret);
bkerror!(r, tx, BKResponse::GetTokenEmailError);
user::get_email_token(self, identity, email, client_secret)
}
Ok(BKCommand::GetTokenPhone(identity, phone, client_secret)) => {
let r = user::get_phone_token(self, identity, phone, client_secret);
bkerror!(r, tx, BKResponse::GetTokenEmailError);
user::get_phone_token(self, identity, phone, client_secret)
}
Ok(BKCommand::SubmitPhoneToken(identity, client_secret, sid, token)) => {
let r = user::submit_phone_token(self, &identity, client_secret, sid, token);
bkerror!(r, tx, BKResponse::SubmitPhoneTokenError);
Ok(BKCommand::SubmitPhoneToken(_, client_secret, sid, token)) => {
user::submit_phone_token(self, client_secret, sid, token)
}
Ok(BKCommand::AddThreePID(identity, client_secret, sid)) => {
let r = user::add_threepid(self, identity, client_secret, sid);
bkerror!(r, tx, BKResponse::AddThreePIDError);
user::add_threepid(self, identity, client_secret, sid)
}
Ok(BKCommand::DeleteThreePID(medium, address)) => {
user::delete_three_pid(self, medium, address);
user::delete_three_pid(self, medium, address)
}
Ok(BKCommand::ChangePassword(username, old_password, new_password)) => {
let r = user::change_password(self, username, old_password, new_password);
bkerror!(r, tx, BKResponse::ChangePasswordError);
user::change_password(self, username, old_password, new_password)
}
Ok(BKCommand::AccountDestruction(username, password, _)) => {
let r = user::account_destruction(self, username, password);
bkerror!(r, tx, BKResponse::AccountDestructionError);
}
Ok(BKCommand::GetAvatar) => {
let r = user::get_avatar(self);
bkerror!(r, tx, BKResponse::AvatarError);
}
Ok(BKCommand::SetUserAvatar(file)) => {
let r = user::set_user_avatar(self, file);
bkerror!(r, tx, BKResponse::SetUserAvatarError);
}
Ok(BKCommand::GetAvatarAsync(member, ctx)) => {
let r = user::get_avatar_async(self, member, ctx);
bkerror!(r, tx, BKResponse::CommandError);
user::account_destruction(self, username, password)
}
Ok(BKCommand::GetAvatar) => user::get_avatar(self),
Ok(BKCommand::SetUserAvatar(file)) => user::set_user_avatar(self, file),
Ok(BKCommand::GetAvatarAsync(member, ctx)) => user::get_avatar_async(self, member, ctx),
Ok(BKCommand::GetUserInfoAsync(sender, ctx)) => {
let r = user::get_user_info_async(self, &sender, ctx);
bkerror!(r, tx, BKResponse::CommandError);
user::get_user_info_async(self, &sender, ctx)
}
Ok(BKCommand::GetUserNameAsync(sender, ctx)) => {
let r = user::get_username_async(self, sender, ctx);
bkerror!(r, tx, BKResponse::CommandError);
}
Ok(BKCommand::UserSearch(term)) => {
let r = user::search(self, term);
bkerror!(r, tx, BKResponse::CommandError);
user::get_username_async(self, sender, ctx)
}
Ok(BKCommand::UserSearch(term)) => user::search(self, term),
// Sync module
Ok(BKCommand::Sync(since, initial)) => sync::sync(self, since, initial),
......
......@@ -16,8 +16,8 @@ use crate::r0::account::register::Parameters as RegisterParameters;
use crate::r0::account::register::RegistrationKind;
use crate::r0::account::register::Response as RegisterResponse;
use crate::r0::account::Identifier;
use crate::r0::account::Medium;
use crate::r0::account::UserIdentifier;
use crate::r0::Medium;
use crate::util::HTTP_CLIENT;
use crate::backend::types::BKResponse;
......
......@@ -4,15 +4,15 @@ use std::sync::{Arc, Condvar, Mutex};
use crate::error::Error;
use crate::r0::account::Medium;
use crate::r0::contact::get_identifiers::ThirdPartyIdentifier;
use crate::r0::thirdparty::get_supported_protocols::ProtocolInstance;
use crate::r0::Medium;
use crate::types::Event;
use crate::types::Member;
use crate::types::Message;
use crate::types::Room;
use crate::types::Sticker;
use crate::types::StickerGroup;
use crate::types::ThirdPartyIdentifier;
use crate::cache::CacheMap;
use url::Url;
......
This diff is collapsed.
use reqwest::Client;
use reqwest::Error;
use reqwest::Request;
use serde::{Deserialize, Serialize};
use url::Url;
#[derive(Clone, Debug, Serialize)]
pub struct Body {
pub sid: String,
pub client_secret: String,
pub token: String,
}
#[derive(Clone, Debug, Deserialize)]
pub struct Response {
pub success: bool,
}
pub fn request(base: Url, body: &Body) -> Result<Request, Error> {
let url = base
.join("/_matrix/identity/api/v1/validate/msisdn/submitToken")
.expect("Malformed URL in msisdn submit_token");
Client::new().post(url).json(body).build()
}
......@@ -5,6 +5,7 @@ pub mod globals;
pub mod backend;
pub mod cache;
pub mod identity;
mod model;
pub mod r0;
mod ser;
......
use crate::types::User;
use crate::r0::search::user::User;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
......
......@@ -4,4 +4,3 @@ pub mod member;
pub mod message;
pub mod room;
pub mod stickers;
pub mod user;
use crate::r0::account::{AuthenticationData, Medium, ThreePIDCredentials};
use serde::{Deserialize, Serialize};
use std::ops::Not;
#[derive(Clone, Debug, Deserialize)]
pub struct GetDisplayNameResponse {
pub displayname: Option<String>,
}
#[derive(Clone, Debug, Serialize)]
pub struct PutDisplayNameRequest {
#[serde(skip_serializing_if = "Option::is_none")]
pub displayname: Option<String>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct ThirdPartyIDResponse {
pub threepids: Vec<ThirdPartyIdentifier>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct ThirdPartyIdentifier {
pub added_at: u64,
pub medium: Medium,
pub validated_at: u64,
pub address: String,
}
#[derive(Clone, Debug, Serialize)]
pub struct EmailTokenRequest {
pub client_secret: String,
pub email: String,
pub id_server: String,
pub send_attempt: u64,
#[serde(skip_serializing_if = "Option::is_none")]
pub next_link: Option<String>,
}
#[derive(Clone, Debug, Serialize)]
pub struct PhoneTokenRequest {
pub client_secret: String,
pub phone_number: String,
pub country: String,
pub id_server: String,
pub send_attempt: u64,
#[serde(skip_serializing_if = "Option::is_none")]
pub next_link: Option<String>,
}
#[derive(Clone, Debug, Deserialize)]
pub struct ThirdPartyTokenResponse {
pub sid: String,
}
#[derive(Clone, Debug, Serialize)]
pub struct AddThreePIDRequest {
pub three_pid_creds: ThreePIDCredentials,
#[serde(skip_serializing_if = "Not::not")]
pub bind: bool,
}
#[derive(Clone, Debug, Serialize)]
pub struct SubmitPhoneTokenRequest {
pub sid: String,
pub client_secret: String,
pub token: String,
}
#[derive(Clone, Debug, Deserialize)]
pub struct SubmitPhoneTokenResponse {
pub success: bool,
}
#[derive(Clone, Debug, Serialize)]
pub struct DeleteThreePIDRequest {
pub medium: Medium,
pub address: String,
}
#[derive(Clone, Debug, Serialize)]
pub struct ChangePasswordRequest {
pub new_password: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub auth: Option<AuthenticationData>,
}
#[derive(Clone, Debug, Serialize)]
pub struct DeactivateAccountRequest {
#[serde(skip_serializing_if = "Option::is_none")]
pub auth: Option<AuthenticationData>,
}
#[derive(Clone, Debug, Serialize)]
pub struct SearchUserRequest {
pub search_term: String,
#[serde(skip_serializing_if = "u64_is_10")]
pub limit: u64,
}
impl Default for SearchUserRequest {
fn default() -> Self {
Self {
search_term: Default::default(),
limit: 10,
}
}
}
#[derive(Clone, Debug, Deserialize)]
pub struct SearchUserResponse {
pub results: Vec<User>,
pub limited: bool,
}
#[derive(Clone, Debug, Deserialize)]
pub struct User {
pub user_id: String,
#[serde(default)]
pub display_name: Option<String>,
#[serde(default)]
pub avatar_url: Option<String>,
}
fn u64_is_10(number: &u64) -> bool {
number == &10
}
pub mod account;
pub mod contact;
pub mod directory;
pub mod filter;
pub mod media;
pub mod profile;
pub mod search;
pub mod sync;
pub mod thirdparty;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename = "lowercase")]
pub enum Medium {
Email,
MsIsdn,
}
#[derive(Clone, Debug, Serialize)]
pub struct ThreePIDCredentials {
pub client_secret: String,
pub id_server: String,
pub sid: String,
}
pub mod change_password;
pub mod deactivate;
pub mod login;
pub mod logout;
pub mod register;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum Medium {
#[serde(rename = "email")]
Email,
#[serde(rename = "msisdn")]
MsIsdn,
}
use crate::r0::{Medium, ThreePIDCredentials};
use serde::Serialize;
#[derive(Clone, Debug, Serialize)]
#[serde(tag = "type")]
......@@ -66,13 +61,6 @@ impl Identifier {
}
}
#[derive(Clone, Debug, Serialize)]
pub struct ThreePIDCredentials {
pub client_secret: String,
pub id_server: String,
pub sid: String,
}
#[derive(Clone, Debug, Serialize)]
#[serde(tag = "type")]
pub enum AuthenticationData {
......
use super::AuthenticationData;
use reqwest::Client;
use reqwest::Error;
use reqwest::Request;
use serde::Serialize;
use url::Url;
#[derive(Clone, Debug, Serialize)]
pub struct Parameters {
#[serde(skip_serializing_if = "String::is_empty")]
pub access_token: String,
}
#[derive(Clone, Debug, Serialize)]
pub struct Body {
pub new_password: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub auth: Option<AuthenticationData>,
}
pub fn request(base: Url, params: &Parameters, body: &Body) -> Result<Request, Error> {
let url = base
.join("/_matrix/client/r0/account/password")
.expect("Malformed URL in change_password");
Client::new().post(url).query(params).json(body).build()
}
use super::AuthenticationData;
use reqwest::Client;
use reqwest::Error;
use reqwest::Request;
use serde::Serialize;
use url::Url;
#[derive(Clone, Debug, Serialize)]
pub struct Parameters {
#[serde(skip_serializing_if = "String::is_empty")]
pub access_token: String,
}
#[derive(Clone, Debug, Serialize)]
pub struct Body {
#[serde(skip_serializing_if = "Option::is_none")]
pub auth: Option<AuthenticationData>,
}
pub fn request(base: Url, params: &Parameters, body: &Body) -> Result<Request, Error> {
let url = base
.join("/_matrix/client/r0/account/deactivate")
.expect("Malformed URL in deactivate");
Client::new().post(url).query(params).json(body).build()
}
pub mod create;
pub mod delete;
pub mod get_identifiers;
pub mod request_verification_token_email;
pub mod request_verification_token_msisdn;
use crate::r0::ThreePIDCredentials;
use reqwest::Client;
use reqwest::Error;
use reqwest::Request;
use serde::Serialize;
use std::ops::Not;
use url::Url;
#[derive(Debug, Clone, Serialize)]
pub struct Parameters {
pub access_token: String,
}
#[derive(Clone, Debug, Serialize)]
pub struct Body {
pub three_pid_creds: ThreePIDCredentials,
#[serde(skip_serializing_if = "Not::not")]
pub bind: bool,
}
pub fn request(base: Url, params: &Parameters, body: &Body) -> Result<Request, Error> {
let url = base
.join("/_matrix/client/r0/account/3pid")
.expect("Malformed URL in contact create");
Client::new().post(url).query(params).json(body).build()
}
use crate::r0::Medium;
use reqwest::Client;
use reqwest::Error;
use reqwest::Request;
use serde::Serialize;
use url::Url;
#[derive(Debug, Clone, Serialize)]
pub struct Parameters {
pub access_token: String,
}
#[derive(Clone, Debug, Serialize)]
pub struct Body {
pub address: String,
pub medium: Medium,
}
pub fn request(base: Url, params: &Parameters, body: &Body) -> Result<Request, Error> {
let url = base
.join("/_matrix/client/r0/account/3pid/delete")
.expect("Malformed URL in contact delete");
Client::new().post(url).query(params).json(body).build()
}
use crate::r0::Medium;
use reqwest::Client;
use reqwest::Error;
use reqwest::Request;
use serde::{Deserialize, Serialize};
use url::Url;
#[derive(Debug, Clone, Serialize)]
pub struct Parameters {
pub access_token: String,
}
#[derive(Debug, Clone, Deserialize)]
pub struct Response {
#[serde(default)]
pub threepids: Vec<ThirdPartyIdentifier>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct ThirdPartyIdentifier {
pub added_at: u64,
pub medium: Medium,
pub validated_at: u64,
pub address: String,
}
pub fn request(base: Url, params: &Parameters) -> Result<Request, Error> {
let url = base
.join("/_matrix/client/r0/account/3pid")
.expect("Malformed URL in get_identifiers");
Client::new().get(url).query(params).build()
}
use reqwest::Client;
use reqwest::Error;
use reqwest::Request;
use serde::{Deserialize, Serialize};
use url::Url;
#[derive(Clone, Debug, Serialize)]
pub struct Parameters {
pub access_token: String,
}
#[derive(Clone, Debug, Serialize)]
pub struct Body {
pub client_secret: String,
pub email: String,
pub id_server: String,
pub send_attempt: u64,
#[serde(skip_serializing_if = "Option::is_none")]
pub next_link: Option<String>,
}
#[derive(Clone, Debug, Deserialize)]
pub struct Response {
pub sid: String,
}
pub fn request(base: Url, params: &Parameters, body: &Body) -> Result<Request, Error> {
let url = base
.join("/_matrix/client/r0/account/3pid/email/requestToken")
.expect("Malformed URL in request_verification_token_email");
Client::new().post(url).query(params).json(body).build()
}
use reqwest::Client;
use reqwest::Error;
use reqwest::Request;
use serde::{Deserialize, Serialize};
use url::Url;
#[derive(Clone, Debug, Serialize)]
pub struct Parameters {
pub access_token: String,
}
#[derive(Clone, Debug, Serialize)]
pub struct Body {
pub client_secret: String,
pub phone_number: String,
pub country: String,
pub id_server: String,
pub send_attempt: u64,
#[serde(skip_serializing_if = "Option::is_none")]
pub next_link: Option<String>,
}
#[derive(Clone, Debug, Deserialize)]
pub struct Response {
pub sid: String,
}
pub fn request(base: Url, params: &Parameters, body: &Body) -> Result<Request, Error> {
let url = base
.join("/_matrix/client/r0/account/3pid/msisdn/requestToken")
.expect("Malformed URL in request_verification_token_msisdn");
Client::new().post(url).query(params).json(body).build()
}
use reqwest::header::{HeaderValue, CONTENT_TYPE};
use reqwest::Client;
use reqwest::Error;
use reqwest::Request;
use serde::{Deserialize, Serialize};
use url::Url;
#[derive(Clone, Debug, Serialize)]
pub struct Parameters {
pub access_token: String,
pub filename: Option<String>,
}
#[derive(Clone, Debug, Deserialize)]
pub struct Response {
pub content_uri: String,
}
pub fn request(
base: Url,
params: &Parameters,
file: Vec<u8>,
content_type: Option<HeaderValue>,
) -> Result<Request, Error> {
let header = content_type
.map(|mime| (CONTENT_TYPE, mime))
.into_iter()
.collect();
let url = base
.join("/_matrix/media/r0/upload")
.expect("Malformed URL in upload");
Client::new()
.post(url)
.query(params)
.body(file)
.headers(header)
.build()
}
pub mod get_display_name;
pub mod get_profile;
pub mod set_avatar_url;
pub mod set_display_name;
use reqwest::Client;
use reqwest::Error;
use reqwest::Request;
use serde::Deserialize;
use url::Url;
#[derive(Clone, Debug, Deserialize)]
pub struct Response {
pub displayname: Option<String>,
}
pub fn request(base: Url, user_id: &str) -> Result<Request, Error> {
let url = base
.join(&format!(
"/_matrix/client/r0/profile/{}/displayname",
user_id
))
.expect("Malformed URL in get_display_name");
Client::new().get(url).build()
}
use reqwest::Client;
use reqwest::Error;
use reqwest::Request;
use serde::Deserialize;
use url::Url;
#[derive(Clone, Debug, Deserialize)]
pub struct Response {
pub avatar_url: Option<String>,
pub displayname: Option<String>,
}
pub fn request(base: Url, user_id: &str) -> Result<Request, Error> {
let url = base
.join(&format!("/_matrix/client/r0/profile/{}", user_id))
.expect("Malformed URL in get_profile_avatar");
Client