Followers listing

And clean up models functions a bit
This commit is contained in:
Bat
2018-05-13 12:53:58 +01:00
parent 601fe7cf4f
commit c6b2560eb0
12 changed files with 84 additions and 20 deletions
+2 -2
View File
@@ -50,13 +50,13 @@ impl Comment {
.into_iter().nth(0)
}
pub fn for_post(conn: &PgConnection, post_id: i32) -> Vec<Comment> {
pub fn find_by_post(conn: &PgConnection, post_id: i32) -> Vec<Comment> {
comments::table.filter(comments::post_id.eq(post_id))
.load::<Comment>(conn)
.expect("Error loading comment by post id")
}
pub fn get_by_ap_url(conn: &PgConnection, ap_url: String) -> Option<Comment> {
pub fn find_by_ap_url(conn: &PgConnection, ap_url: String) -> Option<Comment> {
comments::table.filter(comments::ap_url.eq(ap_url))
.limit(1)
.load::<Comment>(conn)
+4 -2
View File
@@ -61,7 +61,7 @@ impl Instance {
.into_iter().nth(0)
}
pub fn get_by_domain(conn: &PgConnection, domain: String) -> Option<Instance> {
pub fn find_by_domain(conn: &PgConnection, domain: String) -> Option<Instance> {
instances::table.filter(instances::public_domain.eq(domain))
.limit(1)
.load::<Instance>(conn)
@@ -69,7 +69,9 @@ impl Instance {
.into_iter().nth(0)
}
pub fn block(&self) {}
pub fn block(&self) {
unimplemented!()
}
pub fn has_admin(&self, conn: &PgConnection) -> bool {
users::table.filter(users::instance_id.eq(self.id))
+1 -1
View File
@@ -57,7 +57,7 @@ impl Like {
.into_iter().nth(0)
}
pub fn for_user_on_post(conn: &PgConnection, user: &User, post: &Post) -> Option<Like> {
pub fn find_by_user_on_post(conn: &PgConnection, user: &User, post: &Post) -> Option<Like> {
likes::table.filter(likes::post_id.eq(post.id))
.filter(likes::user_id.eq(user.id))
.limit(1)
+2 -2
View File
@@ -39,7 +39,7 @@ pub struct NewPost {
}
impl Post {
pub fn insert (conn: &PgConnection, new: NewPost) -> Post {
pub fn insert(conn: &PgConnection, new: NewPost) -> Post {
diesel::insert_into(posts::table)
.values(new)
.get_result(conn)
@@ -62,7 +62,7 @@ impl Post {
.into_iter().nth(0)
}
pub fn get_by_ap_url(conn: &PgConnection, ap_url: String) -> Option<Post> {
pub fn find_by_ap_url(conn: &PgConnection, ap_url: String) -> Option<Post> {
posts::table.filter(posts::ap_url.eq(ap_url))
.limit(1)
.load::<Post>(conn)
+13 -4
View File
@@ -67,9 +67,14 @@ pub struct NewUser {
}
impl User {
pub fn grant_admin_rights() {}
pub fn grant_admin_rights(&self, conn: &PgConnection) {
diesel::update(self)
.set(users::is_admin.eq(true))
.load::<User>(conn)
.expect("Couldn't grant admin rights");
}
pub fn insert (conn: &PgConnection, new: NewUser) -> User {
pub fn insert(conn: &PgConnection, new: NewUser) -> User {
diesel::insert_into(users::table)
.values(new)
.get_result(conn)
@@ -118,7 +123,7 @@ impl User {
pub fn find_by_fqn(conn: &PgConnection, fqn: String) -> Option<User> {
if fqn.contains("@") { // remote user
match Instance::get_by_domain(conn, String::from(fqn.split("@").last().unwrap())) {
match Instance::find_by_domain(conn, String::from(fqn.split("@").last().unwrap())) {
Some(instance) => {
match User::find_by_name(conn, String::from(fqn.split("@").nth(0).unwrap()), instance.id) {
Some(u) => Some(u),
@@ -157,7 +162,7 @@ impl User {
}
fn from_activity(conn: &PgConnection, acct: serde_json::Value, inst: String) -> User {
let instance = match Instance::get_by_domain(conn, inst.clone()) {
let instance = match Instance::find_by_domain(conn, inst.clone()) {
Some(instance) => instance,
None => {
Instance::insert(conn, inst.clone(), inst.clone(), false)
@@ -219,6 +224,10 @@ impl User {
posts.into_iter().map(|p| Arc::new(Create::new(self, &p, conn)) as Arc<Activity>).collect::<Vec<Arc<Activity>>>()
}
pub fn get_fqn(&self, conn: &PgConnection) -> String {
format!("{}@{}", self.username, self.get_instance(conn).public_domain)
}
pub fn get_followers(&self, conn: &PgConnection) -> Vec<User> {
use schema::follows;
let follows = Follow::belonging_to(self).select(follows::follower_id);