Add functions to get followers/followings

This commit is contained in:
Bat 2018-05-01 14:23:23 +01:00
parent 78f3062f6d
commit 14534d1ff3
2 changed files with 16 additions and 1 deletions

View File

@ -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,

View File

@ -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::<Post>(conn).unwrap();
posts.into_iter().map(|p| Activity::create(self, p, conn)).collect::<Vec<Activity>>()
}
pub fn get_followers(&self, conn: &PgConnection) -> Vec<User> {
use schema::follows;
let follows = Follow::belonging_to(self).select(follows::follower_id);
users::table.filter(users::id.eq(any(follows))).load::<User>(conn).unwrap()
}
pub fn get_following(&self, conn: &PgConnection) -> Vec<User> {
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::<User>(conn).unwrap()
}
}
impl<'a, 'r> FromRequest<'a, 'r> for User {