| 
									
										
										
										
											2018-04-18 02:17:42 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace App; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-10 02:10:16 +00:00
										 |  |  | use Auth, Storage; | 
					
						
							| 
									
										
										
										
											2018-05-31 22:03:31 +00:00
										 |  |  | use App\Util\Lexer\PrettyNumber; | 
					
						
							|  |  |  | use Illuminate\Database\Eloquent\Model; | 
					
						
							| 
									
										
										
										
											2018-06-14 00:52:42 +00:00
										 |  |  | use Illuminate\Database\Eloquent\SoftDeletes; | 
					
						
							| 
									
										
										
										
											2018-04-18 02:17:42 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class Profile extends Model | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-06-14 00:52:42 +00:00
										 |  |  |     use SoftDeletes; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * The attributes that should be mutated to dates. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @var array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected $dates = ['deleted_at']; | 
					
						
							| 
									
										
										
										
											2018-05-22 23:56:20 +00:00
										 |  |  |     protected $hidden = [ | 
					
						
							|  |  |  |         'private_key', | 
					
						
							|  |  |  |     ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     protected $visible = ['id', 'username', 'name']; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-09 23:31:48 +00:00
										 |  |  |     public function user() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->belongsTo(User::class); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-22 23:56:20 +00:00
										 |  |  |     public function url($suffix = '') | 
					
						
							| 
									
										
										
										
											2018-07-12 16:40:25 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |         if($this->remote_url) { | 
					
						
							|  |  |  |             return $this->remote_url; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             return url($this->username . $suffix); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function localUrl($suffix = '') | 
					
						
							| 
									
										
										
										
											2018-04-19 05:57:16 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-05-31 22:03:31 +00:00
										 |  |  |         return url($this->username . $suffix); | 
					
						
							| 
									
										
										
										
											2018-05-22 23:56:20 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function permalink($suffix = '') | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return url('users/' . $this->username . $suffix); | 
					
						
							| 
									
										
										
										
											2018-04-19 05:57:16 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-05-31 22:03:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-22 23:56:20 +00:00
										 |  |  |     public function emailUrl() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $domain = parse_url(config('app.url'), PHP_URL_HOST); | 
					
						
							|  |  |  |         return $this->username . '@' . $domain; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-18 02:17:42 +00:00
										 |  |  |     public function statuses() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-05-31 22:03:31 +00:00
										 |  |  |         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; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-04-18 02:17:42 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function following() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-05-31 22:03:31 +00:00
										 |  |  |         return $this->belongsToMany( | 
					
						
							|  |  |  |             Profile::class, | 
					
						
							|  |  |  |             'followers', | 
					
						
							|  |  |  |             'profile_id', | 
					
						
							|  |  |  |             'following_id' | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2018-04-18 02:17:42 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function followers() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-05-31 22:03:31 +00:00
										 |  |  |         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' | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2018-04-18 02:17:42 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-04-19 05:57:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function likes() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->hasMany(Like::class); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-05-22 23:56:20 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function avatar() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->hasOne(Avatar::class); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function avatarUrl() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $url = url(Storage::url($this->avatar->media_path ?? 'public/avatars/default.png')); | 
					
						
							|  |  |  |         return $url; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-07-12 16:40:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function statusCount() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->statuses()->whereHas('media')->count(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-08-10 02:10:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function recommendFollowers() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $follows = $this->following()->pluck('followers.id'); | 
					
						
							|  |  |  |         $following = $this->following() | 
					
						
							|  |  |  |             ->orderByRaw('rand()') | 
					
						
							|  |  |  |             ->take(3) | 
					
						
							|  |  |  |             ->pluck('following_id'); | 
					
						
							|  |  |  |         $following->push(Auth::id()); | 
					
						
							|  |  |  |         $following = Follower::whereNotIn('profile_id', $follows) | 
					
						
							|  |  |  |             ->whereNotIn('following_id', $following) | 
					
						
							|  |  |  |             ->whereNotIn('following_id', $follows) | 
					
						
							|  |  |  |             ->whereIn('profile_id', $following) | 
					
						
							|  |  |  |             ->orderByRaw('rand()') | 
					
						
							|  |  |  |             ->limit(3) | 
					
						
							|  |  |  |             ->pluck('following_id'); | 
					
						
							|  |  |  |         $recommended = []; | 
					
						
							|  |  |  |         foreach($following as $follow) { | 
					
						
							|  |  |  |             $recommended[] = Profile::findOrFail($follow); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $recommended; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function keyId() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if($this->remote_url) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return $this->permalink('#main-key'); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-04-18 02:17:42 +00:00
										 |  |  | } |