diff --git a/src/models/users.rs b/src/models/users.rs index 86771548..0b10dfc4 100644 --- a/src/models/users.rs +++ b/src/models/users.rs @@ -285,6 +285,16 @@ impl User { users::table.filter(users::id.eq(any(follows))).load::(conn).unwrap() } + pub fn is_following(&self, conn: &PgConnection, other_id: i32) -> bool { + use schema::follows; + follows::table + .filter(follows::follower_id.eq(other_id)) + .filter(follows::following_id.eq(self.id)) + .load::(conn) + .expect("Couldn't load follow relationship") + .len() > 0 + } + pub fn has_liked(&self, conn: &PgConnection, post: &Post) -> bool { use schema::likes; use models::likes::Like; diff --git a/src/routes/user.rs b/src/routes/user.rs index 7bb7b982..bb096c8d 100644 --- a/src/routes/user.rs +++ b/src/routes/user.rs @@ -44,6 +44,7 @@ fn details(name: String, conn: DbConn, account: Option) -> Template { "user": serde_json::to_value(user.clone()).unwrap(), "instance_url": user.get_instance(&*conn).public_domain, "is_remote": user.instance_id != Instance::local_id(&*conn), + "follows": account.clone().map(|x| x.is_following(&*conn, user.id)).unwrap_or(false), "account": account, "recents": recents.into_iter().map(|p| { json!({ @@ -120,6 +121,7 @@ fn followers(name: String, conn: DbConn, account: Option) -> Template { "user": serde_json::to_value(user.clone()).unwrap(), "instance_url": user.get_instance(&*conn).public_domain, "is_remote": user.instance_id != Instance::local_id(&*conn), + "follows": account.clone().map(|x| x.is_following(&*conn, user.id)).unwrap_or(false), "followers": user.get_followers(&*conn).into_iter().map(|f| { let fqn = f.get_fqn(&*conn); let mut json = serde_json::to_value(f).unwrap(); diff --git a/templates/users/details.html.tera b/templates/users/details.html.tera index 0d86c414..a9184745 100644 --- a/templates/users/details.html.tera +++ b/templates/users/details.html.tera @@ -32,8 +32,12 @@ Open on {{ instance_url }} {% endif %} - {% if not is_self %} - Follow + {% if not is_self and account %} + {% if follows %} + Follow + {% else %} + Unfollow + {% endif %} {% endif %} diff --git a/templates/users/followers.html.tera b/templates/users/followers.html.tera index 802316f3..827813cc 100644 --- a/templates/users/followers.html.tera +++ b/templates/users/followers.html.tera @@ -27,8 +27,12 @@ Open on {{ instance_url }} {% endif %} - {% if not is_self %} - Follow + {% if not is_self and account %} + {% if follows %} + Follow + {% else %} + Unfollow + {% endif %} {% endif %}