Browse Source

Update profile model, use proper follower relationship and add new methods

Daniel Supernault 7 years ago
parent
commit
21d7e47673
1 changed files with 57 additions and 20 deletions
  1. 57 20
      app/Profile.php

+ 57 - 20
app/Profile.php

@@ -2,8 +2,9 @@
 
 namespace App;
 
-use Illuminate\Database\Eloquent\Model;
 use Storage;
+use App\Util\Lexer\PrettyNumber;
+use Illuminate\Database\Eloquent\Model;
 
 class Profile extends Model
 {
@@ -15,14 +16,14 @@ class Profile extends Model
 
     public function url($suffix = '')
     {
-        return url('/@' . $this->username . $suffix);
+        return url($this->username . $suffix);
     }
 
     public function permalink($suffix = '')
     {
         return url('users/' . $this->username . $suffix);
     }
-    
+
     public function emailUrl()
     {
         $domain = parse_url(config('app.url'), PHP_URL_HOST);
@@ -31,31 +32,67 @@ class Profile extends Model
 
     public function statuses()
     {
-      return $this->hasMany(Status::class);
+        return $this->hasMany(Status::class);
+    }
+
+    public function followingCount($short = false)
+    {
+        $count = $this->following()->count();
+        if($short) {
+            return PrettyNumber::convert($count);
+        } else {
+            return $count;
+        }
+    }
+
+    public function followerCount($short = false)
+    {
+        $count = $this->followers()->count();
+        if($short) {
+            return PrettyNumber::convert($count);
+        } else {
+            return $count;
+        }
     }
 
     public function following()
     {
-      return $this->hasManyThrough(
-        Profile::class,
-        Follower::class,
-        'profile_id',
-        'id',
-        'id',
-        'id'
-      );
+        return $this->belongsToMany(
+            Profile::class,
+            'followers',
+            'profile_id',
+            'following_id'
+        );
     }
 
     public function followers()
     {
-      return $this->hasManyThrough(
-        Profile::class,
-        Follower::class,
-        'following_id',
-        'id',
-        'id',
-        'id'
-      );
+        return $this->belongsToMany(
+            Profile::class,
+            'followers',
+            'following_id',
+            'profile_id'
+        );
+    }
+
+    public function follows($profile)
+    {
+        return Follower::whereProfileId($this->id)->whereFollowingId($profile->id)->count();
+    }
+
+    public function followedBy($profile)
+    {
+        return Follower::whereProfileId($profile->id)->whereFollowingId($this->id)->count();
+    }
+
+    public function bookmarks()
+    {
+        return $this->belongsToMany(
+            Status::class,
+            'bookmarks',
+            'profile_id',
+            'status_id'
+        );
     }
 
     public function likes()