2021-10-18 00:04:39 +02:00
|
|
|
use std::{
|
|
|
|
convert::{
|
|
|
|
TryFrom,
|
|
|
|
TryInto,
|
|
|
|
},
|
|
|
|
fmt::Display,
|
|
|
|
str::FromStr,
|
|
|
|
};
|
2021-07-21 10:23:14 +02:00
|
|
|
use juniper::{
|
|
|
|
EmptySubscription,
|
|
|
|
ID,
|
|
|
|
RootNode,
|
|
|
|
graphql_object,
|
|
|
|
GraphQLObject,
|
2021-10-17 01:52:58 +02:00
|
|
|
GraphQLInputObject,
|
2021-07-21 10:23:14 +02:00
|
|
|
GraphQLEnum,
|
2021-07-27 21:11:40 +02:00
|
|
|
FieldResult,
|
|
|
|
FieldError,
|
2021-07-21 10:23:14 +02:00
|
|
|
};
|
|
|
|
use sqlx::{
|
|
|
|
types::chrono::{
|
|
|
|
DateTime,
|
|
|
|
Utc,
|
|
|
|
},
|
2021-07-27 21:11:40 +02:00
|
|
|
FromRow,
|
|
|
|
Encode,
|
|
|
|
Decode,
|
|
|
|
Type,
|
|
|
|
Row,
|
|
|
|
Result as SqlxResult,
|
|
|
|
Error as SqlxError,
|
|
|
|
};
|
|
|
|
#[cfg(feature = "sqlite")]
|
|
|
|
use sqlx::{
|
|
|
|
SqlitePool,
|
|
|
|
sqlite::SqliteRow,
|
2021-07-21 10:23:14 +02:00
|
|
|
};
|
|
|
|
use url::Url;
|
2021-10-17 01:52:58 +02:00
|
|
|
use uuid::Uuid;
|
2021-07-21 10:23:14 +02:00
|
|
|
|
2021-10-18 00:04:39 +02:00
|
|
|
#[inline]
|
2021-10-18 14:37:29 +02:00
|
|
|
fn into_limit<T>(
|
2021-07-27 21:11:40 +02:00
|
|
|
#[cfg(feature = "sqlite")]
|
|
|
|
row: &SqliteRow,
|
|
|
|
index: &str
|
2021-10-18 14:37:29 +02:00
|
|
|
) -> SqlxResult<Vec<T>>
|
|
|
|
where
|
|
|
|
T: FromStr,
|
|
|
|
<T as FromStr>::Err: Into<Box<dyn std::error::Error + Send + Sync>>,
|
|
|
|
{
|
2021-10-18 00:04:39 +02:00
|
|
|
Ok(from_sql_formatted_array(row.try_get(index)?, index)?.collect())
|
2021-07-21 10:23:14 +02:00
|
|
|
}
|
|
|
|
|
2021-07-27 21:11:40 +02:00
|
|
|
#[inline]
|
|
|
|
fn into_column_decode_err<E>(index: &str) -> impl FnOnce(E) -> SqlxError + '_
|
|
|
|
where
|
|
|
|
E: Into<Box<dyn std::error::Error + Send + Sync>>
|
|
|
|
{
|
|
|
|
move |err: E| SqlxError::ColumnDecode {
|
|
|
|
index: index.to_owned(),
|
|
|
|
source: err.into(),
|
|
|
|
}
|
|
|
|
}
|
2021-07-21 10:23:14 +02:00
|
|
|
|
2021-10-18 00:04:39 +02:00
|
|
|
fn from_sql_formatted_array<T>(raw: String, index: &str) -> SqlxResult<impl Iterator<Item = T>>
|
2021-07-27 21:11:40 +02:00
|
|
|
where
|
2021-10-18 00:04:39 +02:00
|
|
|
T: FromStr,
|
|
|
|
<T as FromStr>::Err: Into<Box<dyn std::error::Error + Send + Sync>>,
|
2021-07-27 21:11:40 +02:00
|
|
|
{
|
2021-10-18 00:04:39 +02:00
|
|
|
let mut raw_values: Vec<_> = raw.split(',').collect();
|
|
|
|
if raw_values[0] == raw {
|
|
|
|
raw_values.clear();
|
|
|
|
}
|
|
|
|
let mut values = Vec::new();
|
|
|
|
for r in raw_values.drain(..) {
|
|
|
|
values.push(r.parse().map_err(into_column_decode_err(index))?);
|
|
|
|
}
|
|
|
|
Ok(values.into_iter())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn format_array_for_sql<T>(values: &[T]) -> String
|
|
|
|
where
|
|
|
|
T: Display
|
|
|
|
{
|
|
|
|
match values.len() {
|
|
|
|
1.. => {
|
|
|
|
let mut output = values[0].to_string();
|
|
|
|
for v in &values[1..] {
|
|
|
|
output += ",";
|
|
|
|
output += &v.to_string();
|
|
|
|
}
|
|
|
|
output
|
|
|
|
},
|
|
|
|
_ => values.first().map(|v| v.to_string()).unwrap_or(String::new()),
|
|
|
|
}
|
2021-07-27 21:11:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn into_toml_iter_from_row<'d, T>(
|
|
|
|
#[cfg(feature = "sqlite")]
|
|
|
|
row: &'d SqliteRow,
|
|
|
|
index: &str
|
|
|
|
) -> SqlxResult<impl Iterator<Item = T>>
|
|
|
|
where
|
2021-10-18 00:04:39 +02:00
|
|
|
T: FromStr,
|
|
|
|
<T as FromStr>::Err: Into<Box<dyn std::error::Error + Send + Sync>>,
|
2021-07-27 21:11:40 +02:00
|
|
|
{
|
2021-10-18 00:04:39 +02:00
|
|
|
from_sql_formatted_array(row.try_get(index)?, index)
|
2021-07-27 21:11:40 +02:00
|
|
|
}
|
|
|
|
|
2021-10-18 00:04:39 +02:00
|
|
|
#[inline]
|
2021-10-17 01:52:58 +02:00
|
|
|
fn id_to_uuid(id: &ID) -> Result<Uuid, uuid::Error> {
|
|
|
|
Uuid::from_str(&id)
|
|
|
|
}
|
|
|
|
|
2021-10-25 17:47:51 +02:00
|
|
|
fn is_valid_user_name(username: &str) -> bool {
|
|
|
|
if !username.starts_with('@') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for c in username[1..].chars() {
|
|
|
|
if !(
|
2021-10-25 20:00:20 +02:00
|
|
|
c.is_alphabetic() && c.to_string() == c.to_lowercase().to_string() ||
|
2021-10-25 17:47:51 +02:00
|
|
|
c.is_ascii_digit() ||
|
|
|
|
"_-.".contains(c)
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2021-07-27 21:11:40 +02:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct Context {
|
|
|
|
#[cfg(feature = "sqlite")]
|
2021-09-26 16:59:56 +02:00
|
|
|
pub db: SqlitePool,
|
2021-07-27 21:11:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl juniper::Context for Context {}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Decode, Type)]
|
2021-07-21 10:23:14 +02:00
|
|
|
pub struct User {
|
2021-10-17 01:52:58 +02:00
|
|
|
// can we change this to Uuid?
|
|
|
|
pub id: ID,
|
|
|
|
pub user_name: String,
|
|
|
|
pub display_name: Option<String>,
|
|
|
|
pub activated: bool,
|
|
|
|
pub created: DateTime<Utc>,
|
|
|
|
pub last_online: Option<DateTime<Utc>>,
|
2021-07-21 10:23:14 +02:00
|
|
|
}
|
|
|
|
|
2021-07-27 21:11:40 +02:00
|
|
|
#[graphql_object(context = Context)]
|
|
|
|
impl User {
|
|
|
|
pub const fn id(&self) -> &ID {
|
|
|
|
&self.id
|
|
|
|
}
|
|
|
|
|
|
|
|
pub const fn user_name(&self) -> &String {
|
|
|
|
&self.user_name
|
|
|
|
}
|
|
|
|
|
|
|
|
pub const fn display_name(&self) -> Option<&String> {
|
|
|
|
self.display_name.as_ref()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub const fn activated(&self) -> bool {
|
|
|
|
self.activated
|
|
|
|
}
|
|
|
|
|
|
|
|
pub const fn created(&self) -> &DateTime<Utc> {
|
|
|
|
&self.created
|
|
|
|
}
|
|
|
|
|
|
|
|
pub const fn last_online(&self) -> Option<&DateTime<Utc>> {
|
|
|
|
self.last_online.as_ref()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-18 00:04:39 +02:00
|
|
|
impl TryFrom<SqliteRow> for User {
|
|
|
|
type Error = FieldError;
|
|
|
|
|
|
|
|
fn try_from(row: SqliteRow) -> FieldResult<Self> {
|
|
|
|
let id = ID::new(row.try_get::<String, _>("id")?);
|
2021-07-27 21:11:40 +02:00
|
|
|
Ok(User {
|
2021-10-18 00:04:39 +02:00
|
|
|
id,
|
2021-07-27 21:11:40 +02:00
|
|
|
user_name: row.try_get("user_name")?,
|
|
|
|
display_name: row.try_get("display_name")?,
|
|
|
|
activated: row.try_get("activated")?,
|
|
|
|
created: row.try_get("created")?,
|
|
|
|
last_online: row.try_get("last_online")?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-17 01:52:58 +02:00
|
|
|
#[derive(Clone, Debug, GraphQLInputObject)]
|
|
|
|
pub struct NewUser {
|
|
|
|
pub(crate) user_name: String,
|
|
|
|
pub(crate) display_name: Option<String>,
|
|
|
|
pub(crate) password_hash: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<NewUser> for User {
|
|
|
|
fn from(new_user: NewUser) -> Self {
|
|
|
|
User {
|
|
|
|
// TODO: how do we generate uuids?
|
|
|
|
id: ID::new(Uuid::new_v4().to_string()),
|
|
|
|
user_name: new_user.user_name,
|
|
|
|
display_name: new_user.display_name,
|
|
|
|
activated: false,
|
|
|
|
created: Utc::now(),
|
|
|
|
last_online: None,
|
2021-10-18 00:04:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<NewUser> for UserPreferences {
|
|
|
|
fn from(new_user: NewUser) -> Self {
|
|
|
|
UserPreferences {
|
|
|
|
privacy_preferences: Default::default(),
|
|
|
|
security_preferences: SecurityPreferences {
|
|
|
|
account_tokens: Vec::new(),
|
|
|
|
password_hash: new_user.password_hash,
|
2021-10-17 01:52:58 +02:00
|
|
|
},
|
2021-10-18 00:04:39 +02:00
|
|
|
notification_preferences: Default::default(),
|
|
|
|
external_servers_preferences: Default::default(),
|
2021-10-17 01:52:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-27 21:11:40 +02:00
|
|
|
#[derive(Clone, Debug, GraphQLObject, Decode, Encode, FromRow)]
|
2021-07-21 10:23:14 +02:00
|
|
|
pub struct UserPreferences {
|
|
|
|
privacy_preferences: PrivacyPreferences,
|
|
|
|
notification_preferences: NotificationPreferences,
|
|
|
|
security_preferences: SecurityPreferences,
|
|
|
|
external_servers_preferences: ExternalServersPreferences,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, GraphQLObject)]
|
|
|
|
pub struct PrivacyPreferences {
|
|
|
|
discovery: RestrictionPolicy,
|
2021-10-17 01:52:58 +02:00
|
|
|
discovery_user_limit: Vec<String>,
|
|
|
|
discovery_server_limit: Vec<Url>,
|
2021-07-21 10:23:14 +02:00
|
|
|
last_seen: RestrictionPolicy,
|
2021-10-17 01:52:58 +02:00
|
|
|
last_seen_user_limit: Vec<String>,
|
|
|
|
last_seen_server_limit: Vec<Url>,
|
2021-07-21 10:23:14 +02:00
|
|
|
last_seen_course: bool,
|
|
|
|
info: RestrictionPolicy,
|
2021-10-17 01:52:58 +02:00
|
|
|
info_user_limit: Vec<String>,
|
|
|
|
info_server_limit: Vec<Url>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for PrivacyPreferences {
|
|
|
|
fn default() -> Self {
|
|
|
|
PrivacyPreferences {
|
|
|
|
discovery: Default::default(),
|
|
|
|
discovery_user_limit: Default::default(),
|
|
|
|
discovery_server_limit: Default::default(),
|
|
|
|
last_seen: Default::default(),
|
|
|
|
last_seen_user_limit: Default::default(),
|
|
|
|
last_seen_server_limit: Default::default(),
|
|
|
|
last_seen_course: true,
|
|
|
|
info: Default::default(),
|
|
|
|
info_user_limit: Default::default(),
|
|
|
|
info_server_limit: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
2021-07-21 10:23:14 +02:00
|
|
|
}
|
|
|
|
|
2021-07-27 21:11:40 +02:00
|
|
|
impl PrivacyPreferences {
|
|
|
|
fn from_row_cfg(
|
|
|
|
#[cfg(feature = "sqlite")]
|
|
|
|
row: &SqliteRow,
|
|
|
|
) -> SqlxResult<Self> {
|
|
|
|
Ok(PrivacyPreferences {
|
|
|
|
discovery: row.try_get("discovery")?,
|
2021-10-18 14:37:29 +02:00
|
|
|
discovery_user_limit: into_limit(row, "discovery_user_limit")?,
|
|
|
|
discovery_server_limit: into_limit(row, "discovery_server_limit")?,
|
2021-07-27 21:11:40 +02:00
|
|
|
last_seen: row.try_get("last_seen")?,
|
2021-10-18 14:37:29 +02:00
|
|
|
last_seen_user_limit: into_limit(row, "last_seen_user_limit")?,
|
|
|
|
last_seen_server_limit: into_limit(row, "last_seen_server_limit")?,
|
2021-07-27 21:11:40 +02:00
|
|
|
last_seen_course: row.try_get("last_seen_course")?,
|
|
|
|
info: row.try_get("info")?,
|
2021-10-18 14:37:29 +02:00
|
|
|
info_user_limit: into_limit(row, "info_user_limit")?,
|
|
|
|
info_server_limit: into_limit(row, "info_server_limit")?,
|
2021-07-27 21:11:40 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "sqlite")]
|
|
|
|
impl FromRow<'_, SqliteRow> for PrivacyPreferences {
|
|
|
|
fn from_row(row: &SqliteRow) -> SqlxResult<Self> {
|
|
|
|
Self::from_row_cfg(row)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-18 14:37:29 +02:00
|
|
|
#[derive(Clone, Copy, Debug, Default, GraphQLObject, Type, FromRow)]
|
2021-07-21 10:23:14 +02:00
|
|
|
pub struct NotificationPreferences {
|
|
|
|
lock_details: bool,
|
|
|
|
do_not_disturb: bool,
|
|
|
|
}
|
|
|
|
|
2021-07-27 21:11:40 +02:00
|
|
|
#[derive(Clone, Debug, GraphQLObject, Decode, Encode)]
|
2021-07-21 10:23:14 +02:00
|
|
|
pub struct SecurityPreferences {
|
|
|
|
account_tokens: Vec<ID>,
|
|
|
|
password_hash: String,
|
|
|
|
}
|
|
|
|
|
2021-07-27 21:11:40 +02:00
|
|
|
impl SecurityPreferences {
|
|
|
|
fn from_row_cfg(
|
|
|
|
#[cfg(feature = "sqlite")]
|
|
|
|
row: &SqliteRow,
|
|
|
|
) -> SqlxResult<Self> {
|
|
|
|
Ok(SecurityPreferences {
|
2021-10-18 00:04:39 +02:00
|
|
|
account_tokens: into_toml_iter_from_row::<String>(row, "account_tokens")?
|
2021-07-27 21:11:40 +02:00
|
|
|
.map(ID::new)
|
|
|
|
.collect(),
|
|
|
|
password_hash: row.try_get("password_hash")?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "sqlite")]
|
|
|
|
impl FromRow<'_, SqliteRow> for SecurityPreferences {
|
|
|
|
fn from_row(row: &SqliteRow) -> SqlxResult<Self> {
|
|
|
|
Self::from_row_cfg(row)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-17 01:52:58 +02:00
|
|
|
#[derive(Clone, Debug, Default, GraphQLObject, Decode, Encode, FromRow)]
|
2021-07-21 10:23:14 +02:00
|
|
|
pub struct ExternalServersPreferences {
|
|
|
|
privacy_preferences: PrivacyPreferences,
|
|
|
|
external_servers: RestrictionPolicy,
|
2021-10-17 01:52:58 +02:00
|
|
|
external_servers_limit: Vec<Url>,
|
2021-07-21 10:23:14 +02:00
|
|
|
}
|
|
|
|
|
2021-07-27 21:11:40 +02:00
|
|
|
#[derive(Clone, Copy, Debug, GraphQLEnum, Type)]
|
2021-10-18 00:04:39 +02:00
|
|
|
#[repr(u8)]
|
2021-07-21 10:23:14 +02:00
|
|
|
pub enum RestrictionPolicy {
|
2021-07-27 21:11:40 +02:00
|
|
|
Everyone = 0,
|
|
|
|
Excluding = 1,
|
|
|
|
Including = 2,
|
|
|
|
None = 3,
|
2021-07-21 10:23:14 +02:00
|
|
|
}
|
|
|
|
|
2021-10-17 01:52:58 +02:00
|
|
|
impl Default for RestrictionPolicy {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::Everyone
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-21 10:23:14 +02:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
2021-07-27 21:11:40 +02:00
|
|
|
pub struct Query;
|
|
|
|
|
|
|
|
#[graphql_object(context = Context)]
|
|
|
|
impl Query {
|
2021-10-17 19:09:06 +02:00
|
|
|
async fn getUserID(context: &Context, username: String) -> FieldResult<ID> {
|
2021-10-25 17:47:51 +02:00
|
|
|
if !is_valid_user_name(&username) {
|
|
|
|
return Err(format!("{:?} is not a valid username", username).into());
|
|
|
|
}
|
|
|
|
|
2021-10-17 18:28:50 +02:00
|
|
|
sqlx::query(format!(
|
2021-10-25 17:47:51 +02:00
|
|
|
"SELECT id FROM users WHERE user_name={:?}",
|
2021-10-17 18:28:50 +02:00
|
|
|
username
|
|
|
|
).as_str()).fetch_one(&context.db).await?
|
|
|
|
.try_get::<String, _>("id")
|
|
|
|
.map(ID::new)
|
|
|
|
.map_err(FieldError::from)
|
|
|
|
}
|
|
|
|
|
2021-07-27 21:11:40 +02:00
|
|
|
async fn users(context: &Context) -> FieldResult<Vec<User>> {
|
2021-10-18 00:04:39 +02:00
|
|
|
let rows = sqlx::query("SELECT * FROM users")
|
|
|
|
.fetch_all(&context.db).await?;
|
|
|
|
|
|
|
|
let mut users = Vec::with_capacity(rows.capacity());
|
|
|
|
for row in rows {
|
|
|
|
users.push(row.try_into()?);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(users)
|
2021-07-27 21:11:40 +02:00
|
|
|
}
|
|
|
|
|
2021-10-18 00:04:39 +02:00
|
|
|
async fn userPreferences(context: &Context, id: ID, password_hash: String) -> FieldResult<UserPreferences> {
|
|
|
|
let uuid = id_to_uuid(&id)?.to_simple();
|
|
|
|
|
|
|
|
let security_preferences: SecurityPreferences = sqlx::query_as(format!(
|
|
|
|
r#"SELECT * FROM security_preferences WHERE id = "{}""#,
|
|
|
|
uuid,
|
|
|
|
).as_str()).fetch_one(&context.db).await?;
|
|
|
|
|
|
|
|
if security_preferences.password_hash != password_hash {
|
|
|
|
return Err("incorrect password hash".into())
|
|
|
|
}
|
|
|
|
|
|
|
|
let privacy_preferences: PrivacyPreferences = sqlx::query_as(format!(
|
|
|
|
r#"SELECT * FROM privacy_preferences WHERE id = "{}""#,
|
|
|
|
uuid,
|
|
|
|
).as_str()).fetch_one(&context.db).await?;
|
|
|
|
|
|
|
|
let notification_preferences: NotificationPreferences = sqlx::query_as(format!(
|
|
|
|
r#"SELECT * FROM notification_preferences WHERE id = "{}""#,
|
|
|
|
uuid,
|
|
|
|
).as_str()).fetch_one(&context.db).await?;
|
|
|
|
|
|
|
|
let external_servers_preferences = sqlx::query(format!(
|
|
|
|
r#"SELECT * FROM external_servers_preferences WHERE id = "{}""#,
|
|
|
|
uuid,
|
|
|
|
).as_str()).fetch_one(&context.db).await?;
|
|
|
|
|
|
|
|
let external_servers_privacy_preferences: PrivacyPreferences = sqlx::query_as(format!(
|
|
|
|
r#"SELECT * FROM external_servers_privacy_preferences WHERE id = "{}""#,
|
|
|
|
uuid,
|
|
|
|
).as_str()).fetch_one(&context.db).await?;
|
|
|
|
|
|
|
|
Ok(UserPreferences {
|
|
|
|
privacy_preferences,
|
|
|
|
notification_preferences,
|
|
|
|
security_preferences,
|
|
|
|
external_servers_preferences: ExternalServersPreferences {
|
|
|
|
external_servers: external_servers_preferences.try_get("external_servers")?,
|
2021-10-18 14:37:29 +02:00
|
|
|
external_servers_limit: into_limit(&external_servers_preferences, "external_servers_limit")?,
|
2021-10-18 00:04:39 +02:00
|
|
|
privacy_preferences: external_servers_privacy_preferences,
|
|
|
|
},
|
|
|
|
})
|
2021-07-21 10:23:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-17 01:52:58 +02:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
pub struct Mutation;
|
|
|
|
|
|
|
|
#[graphql_object(context = Context)]
|
|
|
|
impl Mutation {
|
2021-10-17 18:58:04 +02:00
|
|
|
async fn createUser(context: &Context, new_user: NewUser) -> FieldResult<ID> {
|
2021-10-18 00:04:39 +02:00
|
|
|
let user: User = new_user.clone().into();
|
2021-10-25 17:47:51 +02:00
|
|
|
if !is_valid_user_name(&user.user_name) {
|
|
|
|
return Err(format!("{:?} is not a valid username", user.user_name).into());
|
|
|
|
}
|
|
|
|
|
2021-10-18 00:04:39 +02:00
|
|
|
let uuid = id_to_uuid(&user.id)?.to_simple();
|
|
|
|
let user_preferences: UserPreferences = new_user.into();
|
2021-10-17 01:52:58 +02:00
|
|
|
|
|
|
|
if sqlx::query(
|
2021-10-25 17:47:51 +02:00
|
|
|
format!("SELECT id FROM users WHERE user_name={:?}", user.user_name).as_str()
|
2021-10-17 01:52:58 +02:00
|
|
|
).fetch_all(&context.db).await?.len() > 0 {
|
|
|
|
return Err("username is already in use".into());
|
|
|
|
}
|
|
|
|
|
|
|
|
sqlx::query(format!(
|
2021-10-25 17:47:51 +02:00
|
|
|
r#"INSERT INTO users VALUES ("{}", {:?}, {}, {}, {}, {})"#,
|
2021-10-18 00:04:39 +02:00
|
|
|
uuid,
|
2021-10-17 01:52:58 +02:00
|
|
|
user.user_name,
|
2021-10-25 17:47:51 +02:00
|
|
|
user.display_name.map(|x| format!("{:?}", x)).unwrap_or("NULL".to_string()),
|
2021-10-17 01:52:58 +02:00
|
|
|
user.activated as u8,
|
|
|
|
user.created.timestamp(),
|
|
|
|
match user.last_online {
|
|
|
|
None => "NULL".to_string(),
|
|
|
|
Some(d) => format!("{}", d.timestamp()),
|
|
|
|
},
|
|
|
|
).as_str()).execute(&context.db).await?;
|
|
|
|
|
2021-10-18 00:04:39 +02:00
|
|
|
let privacy_preferences = user_preferences.privacy_preferences;
|
2021-10-17 01:52:58 +02:00
|
|
|
sqlx::query(format!(
|
2021-10-25 17:47:51 +02:00
|
|
|
r#"INSERT INTO privacy_preferences VALUES ("{}", {}, {:?}, {:?}, {}, {:?}, {:?}, {}, {}, {:?}, {:?})"#,
|
2021-10-18 00:04:39 +02:00
|
|
|
uuid,
|
2021-10-17 01:52:58 +02:00
|
|
|
privacy_preferences.discovery as u8,
|
2021-10-18 00:04:39 +02:00
|
|
|
format_array_for_sql(&privacy_preferences.discovery_user_limit),
|
|
|
|
format_array_for_sql(&privacy_preferences.discovery_server_limit),
|
2021-10-17 01:52:58 +02:00
|
|
|
privacy_preferences.last_seen as u8,
|
2021-10-18 00:04:39 +02:00
|
|
|
format_array_for_sql(&privacy_preferences.last_seen_user_limit),
|
|
|
|
format_array_for_sql(&privacy_preferences.last_seen_server_limit),
|
2021-10-17 01:52:58 +02:00
|
|
|
privacy_preferences.last_seen_course as u8,
|
|
|
|
privacy_preferences.info as u8,
|
2021-10-18 00:04:39 +02:00
|
|
|
format_array_for_sql(&privacy_preferences.info_user_limit),
|
|
|
|
format_array_for_sql(&privacy_preferences.info_server_limit),
|
2021-10-17 01:52:58 +02:00
|
|
|
).as_str()).execute(&context.db).await?;
|
|
|
|
|
2021-10-18 00:04:39 +02:00
|
|
|
let notification_preferences = user_preferences.notification_preferences;
|
2021-10-17 01:52:58 +02:00
|
|
|
sqlx::query(format!(
|
|
|
|
r#"INSERT INTO notification_preferences VALUES ("{}", {}, {})"#,
|
2021-10-18 00:04:39 +02:00
|
|
|
uuid,
|
2021-10-17 01:52:58 +02:00
|
|
|
notification_preferences.lock_details as u8,
|
|
|
|
notification_preferences.do_not_disturb as u8,
|
|
|
|
).as_str()).execute(&context.db).await?;
|
|
|
|
|
2021-10-18 00:04:39 +02:00
|
|
|
let security_preferences = user_preferences.security_preferences;
|
2021-10-17 01:52:58 +02:00
|
|
|
sqlx::query(format!(
|
2021-10-25 17:47:51 +02:00
|
|
|
r#"INSERT INTO security_preferences VALUES ("{}", {:?}, {:?})"#,
|
2021-10-18 00:04:39 +02:00
|
|
|
uuid,
|
|
|
|
format_array_for_sql(&security_preferences.account_tokens),
|
2021-10-17 01:52:58 +02:00
|
|
|
security_preferences.password_hash,
|
|
|
|
).as_str()).execute(&context.db).await?;
|
|
|
|
|
2021-10-18 00:04:39 +02:00
|
|
|
let external_servers_preferences = user_preferences.external_servers_preferences;
|
2021-10-17 01:52:58 +02:00
|
|
|
sqlx::query(format!(
|
2021-10-25 17:47:51 +02:00
|
|
|
r#"INSERT INTO external_servers_preferences VALUES ("{}", {}, {:?})"#,
|
2021-10-18 00:04:39 +02:00
|
|
|
uuid,
|
2021-10-17 01:52:58 +02:00
|
|
|
external_servers_preferences.external_servers as u8,
|
2021-10-18 00:04:39 +02:00
|
|
|
format_array_for_sql(&external_servers_preferences.external_servers_limit),
|
2021-10-17 01:52:58 +02:00
|
|
|
).as_str()).execute(&context.db).await?;
|
|
|
|
|
|
|
|
let external_servers_privacy_preferences = external_servers_preferences.privacy_preferences;
|
|
|
|
sqlx::query(format!(
|
2021-10-25 17:47:51 +02:00
|
|
|
r#"INSERT INTO external_servers_privacy_preferences VALUES ("{}", {}, {:?}, {:?}, {}, {:?}, {:?}, {}, {}, {:?}, {:?})"#,
|
2021-10-18 00:04:39 +02:00
|
|
|
uuid,
|
2021-10-17 01:52:58 +02:00
|
|
|
external_servers_privacy_preferences.discovery as u8,
|
2021-10-18 00:04:39 +02:00
|
|
|
format_array_for_sql(&external_servers_privacy_preferences.discovery_user_limit),
|
|
|
|
format_array_for_sql(&external_servers_privacy_preferences.discovery_server_limit),
|
2021-10-17 01:52:58 +02:00
|
|
|
external_servers_privacy_preferences.last_seen as u8,
|
2021-10-18 00:04:39 +02:00
|
|
|
format_array_for_sql(&external_servers_privacy_preferences.last_seen_user_limit),
|
|
|
|
format_array_for_sql(&external_servers_privacy_preferences.last_seen_server_limit),
|
2021-10-17 01:52:58 +02:00
|
|
|
external_servers_privacy_preferences.last_seen_course as u8,
|
|
|
|
external_servers_privacy_preferences.info as u8,
|
2021-10-18 00:04:39 +02:00
|
|
|
format_array_for_sql(&external_servers_privacy_preferences.info_user_limit),
|
|
|
|
format_array_for_sql(&external_servers_privacy_preferences.info_server_limit),
|
2021-10-17 01:52:58 +02:00
|
|
|
).as_str()).execute(&context.db).await?;
|
|
|
|
|
2021-10-17 18:58:04 +02:00
|
|
|
Ok(ID::new(id_to_uuid(&user.id)?.to_simple().to_string()))
|
2021-10-17 01:52:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type Schema<'root_node> = RootNode<'root_node, Query, Mutation, EmptySubscription<Context>>;
|
2021-07-21 10:23:14 +02:00
|
|
|
|
2021-07-27 21:11:40 +02:00
|
|
|
pub fn schema<'root_node>() -> Schema<'root_node> {
|
2021-10-17 01:52:58 +02:00
|
|
|
Schema::new(Query, Mutation, EmptySubscription::new())
|
2021-07-21 10:23:14 +02:00
|
|
|
}
|