2018-04-16 00:52:22 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2018-08-28 18:54:37 +00:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Auth;
|
|
|
|
use Cache;
|
2022-01-25 07:39:46 +00:00
|
|
|
use DB;
|
2019-12-05 04:21:08 +00:00
|
|
|
use View;
|
2023-05-19 09:41:01 +00:00
|
|
|
use App\AccountInterstitial;
|
2018-08-28 03:07:36 +00:00
|
|
|
use App\Follower;
|
2019-07-20 03:56:46 +00:00
|
|
|
use App\FollowRequest;
|
2018-08-28 03:07:36 +00:00
|
|
|
use App\Profile;
|
2020-01-03 21:06:07 +00:00
|
|
|
use App\Story;
|
2023-07-30 06:49:23 +00:00
|
|
|
use App\Status;
|
2018-08-28 03:07:36 +00:00
|
|
|
use App\User;
|
2023-05-19 10:53:52 +00:00
|
|
|
use App\UserSetting;
|
2018-08-28 18:54:37 +00:00
|
|
|
use App\UserFilter;
|
2018-06-01 02:40:50 +00:00
|
|
|
use League\Fractal;
|
2022-01-23 08:47:31 +00:00
|
|
|
use App\Services\AccountService;
|
2021-08-31 06:35:29 +00:00
|
|
|
use App\Services\FollowerService;
|
2022-01-25 07:39:46 +00:00
|
|
|
use App\Services\StatusService;
|
2018-08-28 18:54:37 +00:00
|
|
|
use App\Util\Lexer\Nickname;
|
|
|
|
use App\Util\Webfinger\Webfinger;
|
|
|
|
use App\Transformer\ActivityPub\ProfileOutbox;
|
|
|
|
use App\Transformer\ActivityPub\ProfileTransformer;
|
2018-04-16 00:52:22 +00:00
|
|
|
|
|
|
|
class ProfileController extends Controller
|
|
|
|
{
|
2021-05-12 00:17:03 +00:00
|
|
|
public function show(Request $request, $username)
|
|
|
|
{
|
2022-02-18 10:38:35 +00:00
|
|
|
// redirect authed users to Metro 2.0
|
|
|
|
if($request->user()) {
|
|
|
|
// unless they force static view
|
|
|
|
if(!$request->has('fs') || $request->input('fs') != '1') {
|
|
|
|
$pid = AccountService::usernameToId($username);
|
|
|
|
if($pid) {
|
|
|
|
return redirect('/i/web/profile/' . $pid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-12 00:17:03 +00:00
|
|
|
$user = Profile::whereNull('domain')
|
|
|
|
->whereNull('status')
|
|
|
|
->whereUsername($username)
|
|
|
|
->firstOrFail();
|
|
|
|
|
2023-05-19 09:41:01 +00:00
|
|
|
|
2021-05-12 00:17:03 +00:00
|
|
|
if($request->wantsJson() && config_cache('federation.activitypub.enabled')) {
|
|
|
|
return $this->showActivityPub($request, $user);
|
|
|
|
}
|
2023-05-19 09:41:01 +00:00
|
|
|
|
|
|
|
$aiCheck = Cache::remember('profile:ai-check:spam-login:' . $user->id, 86400, function() use($user) {
|
|
|
|
$exists = AccountInterstitial::whereUserId($user->user_id)->where('is_spam', 1)->count();
|
|
|
|
if($exists) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
if($aiCheck) {
|
|
|
|
return redirect('/login');
|
|
|
|
}
|
2021-05-12 00:17:03 +00:00
|
|
|
return $this->buildProfile($request, $user);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function buildProfile(Request $request, $user)
|
|
|
|
{
|
|
|
|
$username = $user->username;
|
|
|
|
$loggedIn = Auth::check();
|
|
|
|
$isPrivate = false;
|
|
|
|
$isBlocked = false;
|
|
|
|
if(!$loggedIn) {
|
|
|
|
$key = 'profile:settings:' . $user->id;
|
|
|
|
$ttl = now()->addHours(6);
|
|
|
|
$settings = Cache::remember($key, $ttl, function() use($user) {
|
|
|
|
return $user->user->settings;
|
|
|
|
});
|
|
|
|
|
|
|
|
if ($user->is_private == true) {
|
2021-07-07 06:40:01 +00:00
|
|
|
$profile = null;
|
|
|
|
return view('profile.private', compact('user'));
|
2021-05-12 00:17:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$owner = false;
|
|
|
|
$is_following = false;
|
|
|
|
|
|
|
|
$profile = $user;
|
|
|
|
$settings = [
|
|
|
|
'crawlable' => $settings->crawlable,
|
|
|
|
'following' => [
|
|
|
|
'count' => $settings->show_profile_following_count,
|
|
|
|
'list' => $settings->show_profile_following
|
|
|
|
],
|
|
|
|
'followers' => [
|
|
|
|
'count' => $settings->show_profile_follower_count,
|
|
|
|
'list' => $settings->show_profile_followers
|
|
|
|
]
|
|
|
|
];
|
2022-04-06 03:22:32 +00:00
|
|
|
return view('profile.show', compact('profile', 'settings'));
|
2021-05-12 00:17:03 +00:00
|
|
|
} else {
|
|
|
|
$key = 'profile:settings:' . $user->id;
|
|
|
|
$ttl = now()->addHours(6);
|
|
|
|
$settings = Cache::remember($key, $ttl, function() use($user) {
|
|
|
|
return $user->user->settings;
|
|
|
|
});
|
|
|
|
|
|
|
|
if ($user->is_private == true) {
|
|
|
|
$isPrivate = $this->privateProfileCheck($user, $loggedIn);
|
|
|
|
}
|
|
|
|
|
|
|
|
$isBlocked = $this->blockedProfileCheck($user);
|
|
|
|
|
|
|
|
$owner = $loggedIn && Auth::id() === $user->user_id;
|
|
|
|
$is_following = ($owner == false && Auth::check()) ? $user->followedBy(Auth::user()->profile) : false;
|
|
|
|
|
|
|
|
if ($isPrivate == true || $isBlocked == true) {
|
|
|
|
$requested = Auth::check() ? FollowRequest::whereFollowerId(Auth::user()->profile_id)
|
|
|
|
->whereFollowingId($user->id)
|
|
|
|
->exists() : false;
|
|
|
|
return view('profile.private', compact('user', 'is_following', 'requested'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$is_admin = is_null($user->domain) ? $user->user->is_admin : false;
|
|
|
|
$profile = $user;
|
|
|
|
$settings = [
|
|
|
|
'crawlable' => $settings->crawlable,
|
|
|
|
'following' => [
|
|
|
|
'count' => $settings->show_profile_following_count,
|
|
|
|
'list' => $settings->show_profile_following
|
|
|
|
],
|
|
|
|
'followers' => [
|
|
|
|
'count' => $settings->show_profile_follower_count,
|
|
|
|
'list' => $settings->show_profile_followers
|
|
|
|
]
|
|
|
|
];
|
2022-08-16 04:29:07 +00:00
|
|
|
return view('profile.show', compact('profile', 'settings'));
|
2021-05-12 00:17:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function permalinkRedirect(Request $request, $username)
|
|
|
|
{
|
|
|
|
$user = Profile::whereNull('domain')->whereUsername($username)->firstOrFail();
|
|
|
|
|
|
|
|
if ($request->wantsJson() && config_cache('federation.activitypub.enabled')) {
|
|
|
|
return $this->showActivityPub($request, $user);
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect($user->url());
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function privateProfileCheck(Profile $profile, $loggedIn)
|
|
|
|
{
|
|
|
|
if (!Auth::check()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$user = Auth::user()->profile;
|
|
|
|
if($user->id == $profile->id || !$profile->is_private) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$follows = Follower::whereProfileId($user->id)->whereFollowingId($profile->id)->exists();
|
|
|
|
if ($follows == false) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function accountCheck(Profile $profile)
|
|
|
|
{
|
|
|
|
switch ($profile->status) {
|
|
|
|
case 'disabled':
|
|
|
|
case 'suspended':
|
|
|
|
case 'delete':
|
|
|
|
return view('profile.disabled');
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return abort(404);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function blockedProfileCheck(Profile $profile)
|
|
|
|
{
|
|
|
|
$pid = Auth::user()->profile->id;
|
|
|
|
$blocks = UserFilter::whereUserId($profile->id)
|
|
|
|
->whereFilterType('block')
|
|
|
|
->whereFilterableType('App\Profile')
|
|
|
|
->pluck('filterable_id')
|
|
|
|
->toArray();
|
|
|
|
if (in_array($pid, $blocks)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function showActivityPub(Request $request, $user)
|
|
|
|
{
|
|
|
|
abort_if(!config_cache('federation.activitypub.enabled'), 404);
|
|
|
|
abort_if($user->domain, 404);
|
|
|
|
|
2022-11-18 04:28:24 +00:00
|
|
|
return Cache::remember('pf:activitypub:user-object:by-id:' . $user->id, 3600, function() use($user) {
|
|
|
|
$fractal = new Fractal\Manager();
|
|
|
|
$resource = new Fractal\Resource\Item($user, new ProfileTransformer);
|
|
|
|
$res = $fractal->createData($resource)->toArray();
|
2022-12-23 14:02:33 +00:00
|
|
|
return response(json_encode($res['data']))->header('Content-Type', 'application/activity+json');
|
2022-11-18 04:28:24 +00:00
|
|
|
});
|
2021-05-12 00:17:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function showAtomFeed(Request $request, $user)
|
|
|
|
{
|
|
|
|
abort_if(!config('federation.atom.enabled'), 404);
|
|
|
|
|
2022-01-25 07:39:46 +00:00
|
|
|
$pid = AccountService::usernameToId($user);
|
|
|
|
|
|
|
|
abort_if(!$pid, 404);
|
|
|
|
|
2022-11-18 04:28:24 +00:00
|
|
|
$profile = AccountService::get($pid, true);
|
2022-01-25 07:39:46 +00:00
|
|
|
|
|
|
|
abort_if(!$profile || $profile['locked'] || !$profile['local'], 404);
|
|
|
|
|
2023-05-19 09:41:01 +00:00
|
|
|
$aiCheck = Cache::remember('profile:ai-check:spam-login:' . $profile['id'], 86400, function() use($profile) {
|
|
|
|
$uid = User::whereProfileId($profile['id'])->first();
|
|
|
|
if(!$uid) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$exists = AccountInterstitial::whereUserId($uid->id)->where('is_spam', 1)->count();
|
|
|
|
if($exists) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
abort_if($aiCheck, 404);
|
|
|
|
|
2023-05-19 10:53:52 +00:00
|
|
|
$enabled = Cache::remember('profile:atom:enabled:' . $profile['id'], 84600, function() use ($profile) {
|
|
|
|
$uid = User::whereProfileId($profile['id'])->first();
|
|
|
|
if(!$uid) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$settings = UserSetting::whereUserId($uid->id)->first();
|
|
|
|
if(!$settings) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $settings->show_atom;
|
|
|
|
});
|
|
|
|
|
|
|
|
abort_if(!$enabled, 404);
|
|
|
|
|
2023-05-19 09:41:01 +00:00
|
|
|
$data = Cache::remember('pf:atom:user-feed:by-id:' . $profile['id'], 900, function() use($pid, $profile) {
|
2023-07-30 06:49:23 +00:00
|
|
|
$items = Status::whereProfileId($pid)
|
|
|
|
->whereScope('public')
|
|
|
|
->whereIn('type', ['photo', 'photo:album'])
|
2022-11-18 04:28:24 +00:00
|
|
|
->orderByDesc('id')
|
|
|
|
->take(10)
|
|
|
|
->get()
|
|
|
|
->map(function($status) {
|
2023-07-30 06:49:23 +00:00
|
|
|
return StatusService::get($status->id, true);
|
2022-11-18 04:28:24 +00:00
|
|
|
})
|
|
|
|
->filter(function($status) {
|
|
|
|
return $status &&
|
|
|
|
isset($status['account']) &&
|
|
|
|
isset($status['media_attachments']) &&
|
|
|
|
count($status['media_attachments']);
|
|
|
|
})
|
|
|
|
->values();
|
|
|
|
$permalink = config('app.url') . "/users/{$profile['username']}.atom";
|
|
|
|
$headers = ['Content-Type' => 'application/atom+xml'];
|
|
|
|
|
|
|
|
if($items && $items->count()) {
|
|
|
|
$headers['Last-Modified'] = now()->parse($items->first()['created_at'])->toRfc7231String();
|
|
|
|
}
|
|
|
|
|
|
|
|
return compact('items', 'permalink', 'headers');
|
|
|
|
});
|
2023-05-19 09:41:01 +00:00
|
|
|
abort_if(!$data || !isset($data['items']) || !isset($data['permalink']), 404);
|
2022-01-25 07:39:46 +00:00
|
|
|
return response()
|
2022-11-18 04:28:24 +00:00
|
|
|
->view('atom.user',
|
|
|
|
[
|
|
|
|
'profile' => $profile,
|
|
|
|
'items' => $data['items'],
|
|
|
|
'permalink' => $data['permalink']
|
|
|
|
]
|
|
|
|
)
|
|
|
|
->withHeaders($data['headers']);
|
2021-05-12 00:17:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function meRedirect()
|
|
|
|
{
|
|
|
|
abort_if(!Auth::check(), 404);
|
|
|
|
return redirect(Auth::user()->url());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function embed(Request $request, $username)
|
|
|
|
{
|
|
|
|
$res = view('profile.embed-removed');
|
|
|
|
|
2022-08-16 08:57:52 +00:00
|
|
|
if(!config('instance.embed.profile')) {
|
|
|
|
return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
|
|
|
|
}
|
|
|
|
|
2021-05-12 00:17:03 +00:00
|
|
|
if(strlen($username) > 15 || strlen($username) < 2) {
|
|
|
|
return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$profile = Profile::whereUsername($username)
|
|
|
|
->whereIsPrivate(false)
|
|
|
|
->whereNull('status')
|
|
|
|
->whereNull('domain')
|
|
|
|
->first();
|
|
|
|
|
|
|
|
if(!$profile) {
|
|
|
|
return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
|
|
|
|
}
|
|
|
|
|
2023-05-19 09:41:01 +00:00
|
|
|
$aiCheck = Cache::remember('profile:ai-check:spam-login:' . $profile->id, 86400, function() use($profile) {
|
|
|
|
$exists = AccountInterstitial::whereUserId($profile->user_id)->where('is_spam', 1)->count();
|
|
|
|
if($exists) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
if($aiCheck) {
|
|
|
|
return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
|
|
|
|
}
|
|
|
|
|
2022-01-23 08:47:31 +00:00
|
|
|
if(AccountService::canEmbed($profile->user_id) == false) {
|
|
|
|
return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
|
|
|
|
}
|
2021-05-12 00:17:03 +00:00
|
|
|
|
2022-01-23 08:47:31 +00:00
|
|
|
$profile = AccountService::get($profile->id);
|
2022-01-23 09:44:34 +00:00
|
|
|
$res = view('profile.embed', compact('profile'));
|
|
|
|
return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
|
2021-05-12 00:17:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function stories(Request $request, $username)
|
|
|
|
{
|
2021-05-12 00:30:32 +00:00
|
|
|
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404);
|
2021-05-12 00:17:03 +00:00
|
|
|
$profile = Profile::whereNull('domain')->whereUsername($username)->firstOrFail();
|
|
|
|
$pid = $profile->id;
|
2021-08-31 06:35:29 +00:00
|
|
|
$authed = Auth::user()->profile_id;
|
|
|
|
abort_if($pid != $authed && !FollowerService::follows($authed, $pid), 404);
|
2021-05-12 00:17:03 +00:00
|
|
|
$exists = Story::whereProfileId($pid)
|
2021-08-31 06:35:29 +00:00
|
|
|
->whereActive(true)
|
|
|
|
->exists();
|
|
|
|
abort_unless($exists, 404);
|
2021-05-12 00:17:03 +00:00
|
|
|
return view('profile.story', compact('pid', 'profile'));
|
|
|
|
}
|
2018-04-16 00:52:22 +00:00
|
|
|
}
|