Profile.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. namespace App;
  3. use Auth, Cache, DB, Storage;
  4. use App\Util\Lexer\PrettyNumber;
  5. use App\HasSnowflakePrimary;
  6. use Illuminate\Database\Eloquent\{Model, SoftDeletes};
  7. use App\Services\FollowerService;
  8. class Profile extends Model
  9. {
  10. use HasSnowflakePrimary, SoftDeletes;
  11. /**
  12. * Indicates if the IDs are auto-incrementing.
  13. *
  14. * @var bool
  15. */
  16. public $incrementing = false;
  17. protected $dates = [
  18. 'deleted_at',
  19. 'last_fetched_at',
  20. 'last_status_at'
  21. ];
  22. protected $hidden = ['private_key'];
  23. protected $visible = ['id', 'user_id', 'username', 'name'];
  24. protected $fillable = ['user_id'];
  25. public function user()
  26. {
  27. return $this->belongsTo(User::class);
  28. }
  29. public function url($suffix = null)
  30. {
  31. return $this->remote_url ?? url($this->username . $suffix);
  32. }
  33. public function localUrl($suffix = null)
  34. {
  35. return url($this->username . $suffix);
  36. }
  37. public function permalink($suffix = null)
  38. {
  39. return $this->remote_url ?? url('users/' . $this->username . $suffix);
  40. }
  41. public function emailUrl()
  42. {
  43. if($this->domain) {
  44. return $this->username;
  45. }
  46. $domain = parse_url(config('app.url'), PHP_URL_HOST);
  47. return $this->username.'@'.$domain;
  48. }
  49. public function statuses()
  50. {
  51. return $this->hasMany(Status::class);
  52. }
  53. public function followingCount($short = false)
  54. {
  55. $count = Cache::remember('profile:following_count:'.$this->id, now()->addMonths(1), function() {
  56. if($this->domain == null && $this->user->settings->show_profile_following_count == false) {
  57. return 0;
  58. }
  59. $count = DB::table('followers')->where('profile_id', $this->id)->count();
  60. if($this->following_count != $count) {
  61. $this->following_count = $count;
  62. $this->save();
  63. }
  64. return $count;
  65. });
  66. return $short ? PrettyNumber::convert($count) : $count;
  67. }
  68. public function followerCount($short = false)
  69. {
  70. $count = Cache::remember('profile:follower_count:'.$this->id, now()->addMonths(1), function() {
  71. if($this->domain == null && $this->user->settings->show_profile_follower_count == false) {
  72. return 0;
  73. }
  74. $count = DB::table('followers')->where('following_id', $this->id)->count();
  75. if($this->followers_count != $count) {
  76. $this->followers_count = $count;
  77. $this->save();
  78. }
  79. return $count;
  80. });
  81. return $short ? PrettyNumber::convert($count) : $count;
  82. }
  83. public function statusCount()
  84. {
  85. return $this->status_count;
  86. }
  87. public function following()
  88. {
  89. return $this->belongsToMany(
  90. self::class,
  91. 'followers',
  92. 'profile_id',
  93. 'following_id'
  94. );
  95. }
  96. public function followers()
  97. {
  98. return $this->belongsToMany(
  99. self::class,
  100. 'followers',
  101. 'following_id',
  102. 'profile_id'
  103. );
  104. }
  105. public function follows($profile) : bool
  106. {
  107. return Follower::whereProfileId($this->id)->whereFollowingId($profile->id)->exists();
  108. }
  109. public function followedBy($profile) : bool
  110. {
  111. return Follower::whereProfileId($profile->id)->whereFollowingId($this->id)->exists();
  112. }
  113. public function bookmarks()
  114. {
  115. return $this->belongsToMany(
  116. Status::class,
  117. 'bookmarks',
  118. 'profile_id',
  119. 'status_id'
  120. );
  121. }
  122. public function likes()
  123. {
  124. return $this->hasMany(Like::class);
  125. }
  126. public function avatar()
  127. {
  128. return $this->hasOne(Avatar::class)->withDefault([
  129. 'media_path' => 'public/avatars/default.jpg',
  130. 'change_count' => 0
  131. ]);
  132. }
  133. public function avatarUrl()
  134. {
  135. $url = Cache::remember('avatar:'.$this->id, now()->addYears(1), function () {
  136. $avatar = $this->avatar;
  137. if($avatar->cdn_url) {
  138. return $avatar->cdn_url ?? url('/storage/avatars/default.jpg');
  139. }
  140. if($avatar->is_remote) {
  141. return $avatar->cdn_url ?? url('/storage/avatars/default.jpg');
  142. }
  143. $path = $avatar->media_path;
  144. $path = "{$path}?v={$avatar->change_count}";
  145. return config('app.url') . Storage::url($path);
  146. });
  147. return $url;
  148. }
  149. // deprecated
  150. public function recommendFollowers()
  151. {
  152. return collect([]);
  153. }
  154. public function keyId()
  155. {
  156. if ($this->remote_url) {
  157. return;
  158. }
  159. return $this->permalink('#main-key');
  160. }
  161. public function mutedIds()
  162. {
  163. return UserFilter::whereUserId($this->id)
  164. ->whereFilterableType('App\Profile')
  165. ->whereFilterType('mute')
  166. ->pluck('filterable_id');
  167. }
  168. public function blockedIds()
  169. {
  170. return UserFilter::whereUserId($this->id)
  171. ->whereFilterableType('App\Profile')
  172. ->whereFilterType('block')
  173. ->pluck('filterable_id');
  174. }
  175. public function mutedProfileUrls()
  176. {
  177. $ids = $this->mutedIds();
  178. return $this->whereIn('id', $ids)->get()->map(function($i) {
  179. return $i->url();
  180. });
  181. }
  182. public function blockedProfileUrls()
  183. {
  184. $ids = $this->blockedIds();
  185. return $this->whereIn('id', $ids)->get()->map(function($i) {
  186. return $i->url();
  187. });
  188. }
  189. public function reports()
  190. {
  191. return $this->hasMany(Report::class, 'profile_id');
  192. }
  193. public function media()
  194. {
  195. return $this->hasMany(Media::class, 'profile_id');
  196. }
  197. public function inboxUrl()
  198. {
  199. return $this->inbox_url ?? $this->permalink('/inbox');
  200. }
  201. public function outboxUrl()
  202. {
  203. return $this->outbox_url ?? $this->permalink('/outbox');
  204. }
  205. public function sharedInbox()
  206. {
  207. return $this->sharedInbox ?? $this->inboxUrl();
  208. }
  209. public function getDefaultScope()
  210. {
  211. return $this->is_private == true ? 'private' : 'public';
  212. }
  213. public function getAudience($scope = false)
  214. {
  215. if($this->remote_url) {
  216. return [];
  217. }
  218. $scope = $scope ?? $this->getDefaultScope();
  219. $audience = [];
  220. switch ($scope) {
  221. case 'public':
  222. $audience = [
  223. 'to' => [
  224. 'https://www.w3.org/ns/activitystreams#Public'
  225. ],
  226. 'cc' => [
  227. $this->permalink('/followers')
  228. ]
  229. ];
  230. break;
  231. }
  232. return $audience;
  233. }
  234. public function getAudienceInbox($scope = 'public')
  235. {
  236. return FollowerService::audience($this->id, $scope);
  237. }
  238. public function circles()
  239. {
  240. return $this->hasMany(Circle::class);
  241. }
  242. public function hashtags()
  243. {
  244. return $this->hasManyThrough(
  245. Hashtag::class,
  246. StatusHashtag::class,
  247. 'profile_id',
  248. 'id',
  249. 'id',
  250. 'hashtag_id'
  251. );
  252. }
  253. public function hashtagFollowing()
  254. {
  255. return $this->hasMany(HashtagFollow::class);
  256. }
  257. public function collections()
  258. {
  259. return $this->hasMany(Collection::class);
  260. }
  261. public function hasFollowRequestById(int $id)
  262. {
  263. return FollowRequest::whereFollowerId($id)
  264. ->whereFollowingId($this->id)
  265. ->exists();
  266. }
  267. public function stories()
  268. {
  269. return $this->hasMany(Story::class);
  270. }
  271. public function reported()
  272. {
  273. return $this->hasMany(Report::class, 'object_id');
  274. }
  275. }