1
0

Profile.php 6.3 KB

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