2023-01-03 15:11:43 +00:00
|
|
|
use super::super::schema::{userfollows, users};
|
2023-01-04 21:16:48 +00:00
|
|
|
use diesel::prelude::*;
|
|
|
|
|
2023-01-15 16:35:43 +00:00
|
|
|
use crate::graphql::Context;
|
2023-01-03 15:11:43 +00:00
|
|
|
|
|
|
|
#[derive(Queryable, Insertable, Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub struct User {
|
2023-01-15 23:09:50 +00:00
|
|
|
pub id: String,
|
|
|
|
pub username: String,
|
2023-01-03 15:11:43 +00:00
|
|
|
}
|
|
|
|
|
2023-01-15 16:35:43 +00:00
|
|
|
#[juniper::graphql_object(Context = Context)]
|
2023-01-04 18:31:52 +00:00
|
|
|
impl User {
|
2023-01-04 21:16:48 +00:00
|
|
|
#[graphql(description = "Appwrite ID of the user")]
|
|
|
|
pub fn id(&self) -> String {
|
|
|
|
self.id.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[graphql(description = "The user's apparent name")]
|
|
|
|
pub fn username(&self) -> String {
|
|
|
|
self.username.clone()
|
2023-01-04 18:31:52 +00:00
|
|
|
}
|
|
|
|
|
2023-01-04 21:16:48 +00:00
|
|
|
#[graphql(description = "Who the user follows")]
|
2023-01-15 16:35:43 +00:00
|
|
|
pub fn following(&self, context: &Context) -> Vec<User> {
|
2023-01-04 21:16:48 +00:00
|
|
|
use super::super::schema::{userfollows, users};
|
2023-01-15 16:35:43 +00:00
|
|
|
let conn = &mut context.db.conn().unwrap();
|
2023-01-04 21:16:48 +00:00
|
|
|
userfollows::dsl::userfollows
|
|
|
|
.filter(userfollows::dsl::follower.eq(self.id.clone()))
|
|
|
|
.load::<UserFollow>(conn)
|
|
|
|
.unwrap()
|
|
|
|
.iter()
|
|
|
|
.map(|f| {
|
|
|
|
users::dsl::users
|
|
|
|
.find(f.following.clone())
|
|
|
|
.first::<User>(conn)
|
|
|
|
.unwrap()
|
|
|
|
})
|
|
|
|
.collect::<Vec<User>>()
|
2023-01-04 18:31:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-03 15:11:43 +00:00
|
|
|
#[derive(Queryable, Insertable, Debug, Clone, PartialEq, Eq)]
|
|
|
|
#[diesel(table_name = userfollows)]
|
|
|
|
pub struct UserFollow {
|
|
|
|
pub id: i32,
|
|
|
|
pub follower: String,
|
|
|
|
pub following: String,
|
|
|
|
}
|