From 14534d1ff325b4c3ef09f53b4658e83f80cd5a5b Mon Sep 17 00:00:00 2001 From: Bat Date: Tue, 1 May 2018 14:23:23 +0100 Subject: [PATCH] Add functions to get followers/followings --- src/models/follows.rs | 4 +++- src/models/users.rs | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/models/follows.rs b/src/models/follows.rs index 23aae984..6debe46e 100644 --- a/src/models/follows.rs +++ b/src/models/follows.rs @@ -1,8 +1,10 @@ use diesel::{self, PgConnection, ExpressionMethods, QueryDsl, RunQueryDsl}; +use models::users::User; use schema::follows; -#[derive(Queryable, Identifiable)] +#[derive(Queryable, Identifiable, Associations)] +#[belongs_to(User, foreign_key = "following_id")] pub struct Follow { pub id: i32, pub follower_id: i32, diff --git a/src/models/users.rs b/src/models/users.rs index 6374b3f2..369801e5 100644 --- a/src/models/users.rs +++ b/src/models/users.rs @@ -14,6 +14,7 @@ use activity_pub::actor::{ActorType, Actor}; use activity_pub::outbox::Outbox; use activity_pub::webfinger::{Webfinger, resolve}; use db_conn::DbConn; +use models::follows::Follow; use models::instance::Instance; use models::post_authors::PostAuthor; use models::posts::Post; @@ -180,6 +181,18 @@ impl User { let posts = posts::table.filter(posts::id.eq(any(posts_by_self))).load::(conn).unwrap(); posts.into_iter().map(|p| Activity::create(self, p, conn)).collect::>() } + + pub fn get_followers(&self, conn: &PgConnection) -> Vec { + use schema::follows; + let follows = Follow::belonging_to(self).select(follows::follower_id); + users::table.filter(users::id.eq(any(follows))).load::(conn).unwrap() + } + + pub fn get_following(&self, conn: &PgConnection) -> Vec { + use schema::follows; + let follows = follows::table.filter(follows::follower_id.eq(self.id)).select(follows::following_id); + users::table.filter(users::id.eq(any(follows))).load::(conn).unwrap() + } } impl<'a, 'r> FromRequest<'a, 'r> for User {