Profile.php 7.3 KB

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