Make the follow button a bit smarter

This commit is contained in:
Bat 2018-06-13 19:06:14 +01:00
parent 7b61da9c7f
commit a4a8446d45
4 changed files with 24 additions and 4 deletions

View File

@ -285,6 +285,16 @@ impl User {
users::table.filter(users::id.eq(any(follows))).load::<User>(conn).unwrap() users::table.filter(users::id.eq(any(follows))).load::<User>(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::<Follow>(conn)
.expect("Couldn't load follow relationship")
.len() > 0
}
pub fn has_liked(&self, conn: &PgConnection, post: &Post) -> bool { pub fn has_liked(&self, conn: &PgConnection, post: &Post) -> bool {
use schema::likes; use schema::likes;
use models::likes::Like; use models::likes::Like;

View File

@ -44,6 +44,7 @@ fn details(name: String, conn: DbConn, account: Option<User>) -> Template {
"user": serde_json::to_value(user.clone()).unwrap(), "user": serde_json::to_value(user.clone()).unwrap(),
"instance_url": user.get_instance(&*conn).public_domain, "instance_url": user.get_instance(&*conn).public_domain,
"is_remote": user.instance_id != Instance::local_id(&*conn), "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, "account": account,
"recents": recents.into_iter().map(|p| { "recents": recents.into_iter().map(|p| {
json!({ json!({
@ -120,6 +121,7 @@ fn followers(name: String, conn: DbConn, account: Option<User>) -> Template {
"user": serde_json::to_value(user.clone()).unwrap(), "user": serde_json::to_value(user.clone()).unwrap(),
"instance_url": user.get_instance(&*conn).public_domain, "instance_url": user.get_instance(&*conn).public_domain,
"is_remote": user.instance_id != Instance::local_id(&*conn), "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| { "followers": user.get_followers(&*conn).into_iter().map(|f| {
let fqn = f.get_fqn(&*conn); let fqn = f.get_fqn(&*conn);
let mut json = serde_json::to_value(f).unwrap(); let mut json = serde_json::to_value(f).unwrap();

View File

@ -32,8 +32,12 @@
<a class="inline-block button" href="{{ user.ap_url }}">Open on {{ instance_url }}</a> <a class="inline-block button" href="{{ user.ap_url }}">Open on {{ instance_url }}</a>
{% endif %} {% endif %}
{% if not is_self %} {% if not is_self and account %}
{% if follows %}
<a href="follow/" class="inline-block button">Follow</a> <a href="follow/" class="inline-block button">Follow</a>
{% else %}
<a href="follow/" class="inline-block button">Unfollow</a>
{% endif %}
{% endif %} {% endif %}
</div> </div>

View File

@ -27,8 +27,12 @@
<a class="inline-block button" href="{{ user.ap_url }}">Open on {{ instance_url }}</a> <a class="inline-block button" href="{{ user.ap_url }}">Open on {{ instance_url }}</a>
{% endif %} {% endif %}
{% if not is_self %} {% if not is_self and account %}
<a href="follow/" class="inline-block button">Follow</a> {% if follows %}
<a href="../follow/" class="inline-block button">Follow</a>
{% else %}
<a href="../follow/" class="inline-block button">Unfollow</a>
{% endif %}
{% endif %} {% endif %}
</div> </div>