Add method to get user’s followers
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Lucien Cartier-Tilet 2023-01-18 11:21:34 +01:00
parent 4faaaf244d
commit 233ea7c416
Signed by: phundrak
GPG Key ID: BD7789E705CB8DCA

View File

@ -64,7 +64,46 @@ impl User {
None
}
}
})
.collect::<Vec<User>>())
}
#[graphql(description = "Who follows this user")]
pub fn followers(&self, context: &Context) -> FieldResult<Vec<User>> {
use schema::userfollows::dsl;
let conn = &mut context.db.conn().map_err(|e| {
DatabaseError::new(
format!("Failed to connect to database: {e:?}"),
"Database connection error",
)
})?;
Ok(dsl::userfollows
.filter(dsl::following.eq(self.id.clone()))
.load::<UserFollow>(conn)
.map_err(|e| {
DatabaseError::new(
format!(
"Failed to retrieve user follows from database: {e:?}"
),
"Database reading error",
)
})?
.iter()
.filter_map(|user_follow| {
use schema::users::dsl;
match dsl::users
.find(user_follow.follower.clone())
.first::<User>(conn) {
Ok(user) => Some(user),
Err(e) => {
let err = DatabaseError::new(
format!("Failed to retrieve user {} from database: {e:?}",
user_follow.follower.clone()),
"Database reading error");
debug!("{}", err);
None
}
}
})
.collect::<Vec<User>>())
}