Profile.php 7.6 KB

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