Compare commits
No commits in common. "main" and "wip/member-improvements" have entirely different histories.
main
...
wip/member
8 changed files with 10 additions and 82 deletions
|
@ -1,2 +0,0 @@
|
||||||
-- This file should undo anything in `up.sql`
|
|
||||||
ALTER TABLE users DROP COLUMN online_status;
|
|
|
@ -1,2 +0,0 @@
|
||||||
-- Your SQL goes here
|
|
||||||
ALTER TABLE users ADD COLUMN online_status INT2 NOT NULL DEFAULT 0;
|
|
|
@ -49,7 +49,6 @@ struct NewInfo {
|
||||||
email: Option<String>,
|
email: Option<String>,
|
||||||
pronouns: Option<String>,
|
pronouns: Option<String>,
|
||||||
about: Option<String>,
|
about: Option<String>,
|
||||||
online_status: Option<i16>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn update(
|
pub async fn update(
|
||||||
|
@ -111,10 +110,5 @@ pub async fn update(
|
||||||
.await?;
|
.await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(online_status) = &json.online_status {
|
|
||||||
me.set_online_status(&mut conn, &app_state.cache_pool, *online_status)
|
|
||||||
.await?;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(StatusCode::OK)
|
Ok(StatusCode::OK)
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,7 +29,6 @@ pub struct Me {
|
||||||
avatar: Option<String>,
|
avatar: Option<String>,
|
||||||
pronouns: Option<String>,
|
pronouns: Option<String>,
|
||||||
about: Option<String>,
|
about: Option<String>,
|
||||||
online_status: i16,
|
|
||||||
pub email: String,
|
pub email: String,
|
||||||
pub email_verified: bool,
|
pub email_verified: bool,
|
||||||
}
|
}
|
||||||
|
@ -278,35 +277,6 @@ impl Me {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn set_online_status(
|
|
||||||
&mut self,
|
|
||||||
conn: &mut Conn,
|
|
||||||
cache_pool: &redis::Client,
|
|
||||||
new_status: i16,
|
|
||||||
) -> Result<(), Error> {
|
|
||||||
if !(0..=4).contains(&new_status) {
|
|
||||||
return Err(Error::BadRequest("Invalid status code".to_string()));
|
|
||||||
}
|
|
||||||
self.online_status = new_status;
|
|
||||||
|
|
||||||
use users::dsl;
|
|
||||||
update(users::table)
|
|
||||||
.filter(dsl::uuid.eq(self.uuid))
|
|
||||||
.set(dsl::online_status.eq(new_status))
|
|
||||||
.execute(conn)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
if cache_pool
|
|
||||||
.get_cache_key::<User>(self.uuid.to_string())
|
|
||||||
.await
|
|
||||||
.is_ok()
|
|
||||||
{
|
|
||||||
cache_pool.del_cache_key(self.uuid.to_string()).await?
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn friends_with(
|
pub async fn friends_with(
|
||||||
&self,
|
&self,
|
||||||
conn: &mut Conn,
|
conn: &mut Conn,
|
||||||
|
|
|
@ -107,7 +107,7 @@ impl MemberBuilder {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub struct Member {
|
pub struct Member {
|
||||||
pub uuid: Uuid,
|
pub uuid: Uuid,
|
||||||
pub nickname: Option<String>,
|
pub nickname: Option<String>,
|
||||||
|
@ -150,17 +150,14 @@ impl Member {
|
||||||
pub async fn fetch_one(
|
pub async fn fetch_one(
|
||||||
conn: &mut Conn,
|
conn: &mut Conn,
|
||||||
cache_pool: &redis::Client,
|
cache_pool: &redis::Client,
|
||||||
me: Option<&Me>,
|
me: &Me,
|
||||||
user_uuid: Uuid,
|
user_uuid: Uuid,
|
||||||
guild_uuid: Uuid,
|
guild_uuid: Uuid,
|
||||||
) -> Result<Self, Error> {
|
) -> Result<Self, Error> {
|
||||||
let member: MemberBuilder;
|
|
||||||
let user: UserBuilder;
|
|
||||||
let friend: Option<Friend>;
|
|
||||||
use friends::dsl as fdsl;
|
use friends::dsl as fdsl;
|
||||||
use guild_members::dsl;
|
use guild_members::dsl;
|
||||||
if let Some(me) = me {
|
let (member, user, friend): (MemberBuilder, UserBuilder, Option<Friend>) =
|
||||||
(member, user, friend) = dsl::guild_members
|
dsl::guild_members
|
||||||
.filter(dsl::guild_uuid.eq(guild_uuid))
|
.filter(dsl::guild_uuid.eq(guild_uuid))
|
||||||
.filter(dsl::user_uuid.eq(user_uuid))
|
.filter(dsl::user_uuid.eq(user_uuid))
|
||||||
.inner_join(users::table)
|
.inner_join(users::table)
|
||||||
|
@ -177,17 +174,6 @@ impl Member {
|
||||||
))
|
))
|
||||||
.get_result(conn)
|
.get_result(conn)
|
||||||
.await?;
|
.await?;
|
||||||
} else {
|
|
||||||
(member, user) = dsl::guild_members
|
|
||||||
.filter(dsl::guild_uuid.eq(guild_uuid))
|
|
||||||
.filter(dsl::user_uuid.eq(user_uuid))
|
|
||||||
.inner_join(users::table)
|
|
||||||
.select((MemberBuilder::as_select(), UserBuilder::as_select()))
|
|
||||||
.get_result(conn)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
friend = None;
|
|
||||||
}
|
|
||||||
|
|
||||||
member
|
member
|
||||||
.build_with_parts(conn, cache_pool, user, friend)
|
.build_with_parts(conn, cache_pool, user, friend)
|
||||||
|
|
|
@ -1,15 +1,10 @@
|
||||||
use diesel::{ExpressionMethods, Insertable, QueryDsl, Queryable, Selectable};
|
use diesel::{Insertable, Queryable, Selectable};
|
||||||
use diesel_async::RunQueryDsl;
|
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::{
|
use crate::{Conn, error::Error, schema::messages};
|
||||||
Conn,
|
|
||||||
error::Error,
|
|
||||||
schema::{channels, guilds, messages},
|
|
||||||
};
|
|
||||||
|
|
||||||
use super::Member;
|
use super::User;
|
||||||
|
|
||||||
#[derive(Clone, Queryable, Selectable, Insertable)]
|
#[derive(Clone, Queryable, Selectable, Insertable)]
|
||||||
#[diesel(table_name = messages)]
|
#[diesel(table_name = messages)]
|
||||||
|
@ -28,16 +23,7 @@ impl MessageBuilder {
|
||||||
conn: &mut Conn,
|
conn: &mut Conn,
|
||||||
cache_pool: &redis::Client,
|
cache_pool: &redis::Client,
|
||||||
) -> Result<Message, Error> {
|
) -> Result<Message, Error> {
|
||||||
use channels::dsl;
|
let user = User::fetch_one(conn, cache_pool, self.user_uuid).await?;
|
||||||
|
|
||||||
let guild_uuid = dsl::channels
|
|
||||||
.filter(dsl::uuid.eq(self.channel_uuid))
|
|
||||||
.inner_join(guilds::table)
|
|
||||||
.select(guilds::uuid)
|
|
||||||
.get_result(conn)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
let member = Member::fetch_one(conn, cache_pool, None, self.user_uuid, guild_uuid).await?;
|
|
||||||
|
|
||||||
Ok(Message {
|
Ok(Message {
|
||||||
uuid: self.uuid,
|
uuid: self.uuid,
|
||||||
|
@ -45,7 +31,7 @@ impl MessageBuilder {
|
||||||
user_uuid: self.user_uuid,
|
user_uuid: self.user_uuid,
|
||||||
message: self.message.clone(),
|
message: self.message.clone(),
|
||||||
reply_to: self.reply_to,
|
reply_to: self.reply_to,
|
||||||
member,
|
user,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -57,5 +43,5 @@ pub struct Message {
|
||||||
user_uuid: Uuid,
|
user_uuid: Uuid,
|
||||||
message: String,
|
message: String,
|
||||||
reply_to: Option<Uuid>,
|
reply_to: Option<Uuid>,
|
||||||
member: Member,
|
user: User,
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,7 +18,6 @@ pub struct UserBuilder {
|
||||||
avatar: Option<String>,
|
avatar: Option<String>,
|
||||||
pronouns: Option<String>,
|
pronouns: Option<String>,
|
||||||
about: Option<String>,
|
about: Option<String>,
|
||||||
online_status: i16,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl UserBuilder {
|
impl UserBuilder {
|
||||||
|
@ -30,7 +29,6 @@ impl UserBuilder {
|
||||||
avatar: self.avatar,
|
avatar: self.avatar,
|
||||||
pronouns: self.pronouns,
|
pronouns: self.pronouns,
|
||||||
about: self.about,
|
about: self.about,
|
||||||
online_status: self.online_status,
|
|
||||||
friends_since: None,
|
friends_since: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -44,7 +42,6 @@ pub struct User {
|
||||||
avatar: Option<String>,
|
avatar: Option<String>,
|
||||||
pronouns: Option<String>,
|
pronouns: Option<String>,
|
||||||
about: Option<String>,
|
about: Option<String>,
|
||||||
online_status: i16,
|
|
||||||
pub friends_since: Option<DateTime<Utc>>,
|
pub friends_since: Option<DateTime<Utc>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -157,7 +157,6 @@ diesel::table! {
|
||||||
pronouns -> Nullable<Varchar>,
|
pronouns -> Nullable<Varchar>,
|
||||||
#[max_length = 200]
|
#[max_length = 200]
|
||||||
about -> Nullable<Varchar>,
|
about -> Nullable<Varchar>,
|
||||||
online_status -> Int2,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue