You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pixelfed/app/Http/Controllers/Settings/PrivacySettings.php

293 lines
9.9 KiB
PHP

<?php
namespace App\Http\Controllers\Settings;
use App\Jobs\HomeFeedPipeline\FeedUnfollowPipeline;
use App\Models\FeatureAuthorization;
use App\Models\Follower;
use App\Models\Profile;
use App\Models\UserFilter;
use App\Services\AccountService;
use App\Services\FeaturedCollectionService;
use App\Services\RelationshipService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
trait PrivacySettings
{
public function privacy(Request $request)
{
$user = $request->user();
$settings = $user->settings;
$profile = $user->profile;
$is_private = $profile->is_private;
$cachedSettings = AccountService::getAccountSettings($profile->id);
$settings['is_private'] = (bool) $is_private;
if ($cachedSettings && isset($cachedSettings['disable_embeds'])) {
$settings['disable_embeds'] = (bool) $cachedSettings['disable_embeds'];
} else {
$settings['disable_embeds'] = false;
}
return view('settings.privacy', ['settings' => $settings, 'profile' => $profile]);
}
public function privacyStore(Request $request)
{
$settings = $request->user()->settings;
$profile = $request->user()->profile;
$other = $settings->other;
$fields = [
'is_private',
'crawlable',
'public_dm',
'show_profile_follower_count',
'show_profile_following_count',
'indexable',
'show_atom',
];
$profile->indexable = $request->input('indexable') == 'on';
$profile->is_suggestable = $request->input('is_suggestable') == 'on';
$profile->save();
if ($request->has('disable_embeds')) {
$other['disable_embeds'] = true;
$settings->other = $other;
$settings->save();
} else {
$other['disable_embeds'] = false;
$settings->other = $other;
$settings->save();
}
foreach ($fields as $field) {
$form = $request->input($field);
if ($field === 'is_private') {
if ($form == 'on') {
$profile->{$field} = true;
$settings->show_guests = false;
$settings->show_discover = false;
$profile->save();
} else {
$profile->{$field} = false;
$profile->save();
}
Cache::forget('profiles:private');
} elseif ($field === 'crawlable') {
if ($form == 'on') {
$settings->{$field} = false;
} else {
$settings->{$field} = true;
}
} elseif ($field === 'public_dm') {
if ($form == 'on') {
$settings->{$field} = true;
} else {
$settings->{$field} = false;
}
} elseif ($field === 'indexable') {
} else {
if ($form == 'on') {
$settings->{$field} = true;
} else {
$settings->{$field} = false;
}
}
$settings->save();
}
$pid = $profile->id;
$canFeature = $request->input('can_feature');
if (in_array($canFeature, FeaturedCollectionService::POLICIES, true) && $canFeature !== $settings->can_feature) {
$settings->can_feature = $canFeature;
$settings->save();
FeatureAuthorization::whereProfileId($pid)->revoked()->delete();
FeaturedCollectionService::forgetPolicy($pid);
}
Cache::forget('profile:settings:'.$pid);
Cache::forget('user:account:id:'.$profile->user_id);
Cache::forget('profile:follower_count:'.$pid);
Cache::forget('profile:following_count:'.$pid);
Cache::forget('profile:atom:enabled:'.$pid);
Cache::forget('profile:embed:'.$pid);
Cache::forget('pf:acct:settings:hidden-followers:'.$pid);
Cache::forget('pf:acct:settings:hidden-following:'.$pid);
Cache::forget('pf:acct-trans:hideFollowing:'.$pid);
Cache::forget('pf:acct-trans:hideFollowers:'.$pid);
Cache::forget('pfc:cached-user:wt:'.strtolower($profile->username));
Cache::forget('pfc:cached-user:wot:'.strtolower($profile->username));
AccountService::forgetAccountSettings($profile->id);
return redirect(route('settings.privacy'))->with('status', 'Settings successfully updated!');
}
public function mutedUsers(Request $request)
{
$pid = $request->user()->profile->id;
$ids = (new UserFilter)->mutedUserIds($pid);
$users = Profile::whereIn('id', $ids)->simplePaginate(15);
return view('settings.privacy.muted', ['users' => $users]);
}
public function mutedUsersUpdate(Request $request)
{
$this->validate($request, [
'profile_id' => 'required|integer|min:1',
]);
$fid = $request->input('profile_id');
$pid = $request->user()->profile->id;
DB::transaction(function () use ($fid, $pid) {
$filter = UserFilter::whereUserId($pid)
->whereFilterableId($fid)
->whereFilterableType(Profile::class)
->whereFilterType('mute')
->firstOrFail();
$filter->delete();
});
RelationshipService::refresh($pid, $fid);
return redirect()->back();
}
public function featuredCollections(Request $request)
{
$pid = $request->user()->profile->id;
$collections = FeatureAuthorization::whereProfileId($pid)
->approved()
->with('actor')
->orderByDesc('created_at')
->simplePaginate(15);
return view('settings.privacy.featured-collections', ['collections' => $collections]);
}
public function featuredCollectionsRemove(Request $request)
{
$this->validate($request, [
'id' => 'required|integer|min:1',
]);
$pid = $request->user()->profile->id;
$auth = FeatureAuthorization::whereProfileId($pid)
->approved()
->findOrFail($request->input('id'));
FeaturedCollectionService::revoke($auth);
return redirect()->back()->with('status', 'You have been removed from the collection.');
}
public function blockedUsers(Request $request)
{
$pid = $request->user()->profile->id;
$ids = (new UserFilter)->blockedUserIds($pid);
$users = Profile::whereIn('id', $ids)->simplePaginate(15);
return view('settings.privacy.blocked', ['users' => $users]);
}
public function blockedUsersUpdate(Request $request)
{
$this->validate($request, [
'profile_id' => 'required|integer|min:1',
]);
$fid = $request->input('profile_id');
$pid = $request->user()->profile->id;
DB::transaction(function () use ($fid, $pid) {
$filter = UserFilter::whereUserId($pid)
->whereFilterableId($fid)
->whereFilterableType(Profile::class)
->whereFilterType('block')
->firstOrFail();
$filter->delete();
});
RelationshipService::refresh($pid, $fid);
return redirect()->back();
}
public function blockedInstances()
{
// deprecated
abort(404);
}
public function domainBlocks()
{
return view('settings.privacy.domain-blocks');
}
public function blockedInstanceStore(Request $request)
{
// deprecated
abort(404);
}
public function blockedInstanceUnblock(Request $request)
{
// deprecated
abort(404);
}
public function blockedKeywords()
{
return view('settings.privacy.blocked-keywords');
}
public function privateAccountOptions(Request $request)
{
$this->validate($request, [
'mode' => 'required|string|in:keep-all,mutual-only,only-followers,remove-all',
'duration' => 'required|integer|min:60|max:525600',
]);
$mode = $request->input('mode');
$duration = $request->input('duration');
// $newRequests = $request->input('newrequests');
$profile = $request->user()->profile;
$settings = $request->user()->settings;
if ($mode !== 'keep-all') {
switch ($mode) {
case 'mutual-only':
$following = $profile->following()->pluck('profiles.id');
Follower::whereFollowingId($profile->id)->whereNotIn('profile_id', $following)->delete();
break;
case 'only-followers':
$ts = now()->subMinutes($duration);
Follower::whereFollowingId($profile->id)->where('created_at', '>', $ts)->delete();
break;
case 'remove-all':
Follower::whereFollowingId($profile->id)
->chunkById(100, function ($followers) {
foreach ($followers as $follower) {
FeedUnfollowPipeline::dispatch($follower->profile_id, $follower->following_id)->onQueue('feed');
}
});
Follower::whereFollowingId($profile->id)->delete();
break;
default:
// code...
break;
}
}
$profile->is_private = true;
// Clear directory listing when going private so the profile can't leak
// into the public directory (which lists is_suggestable profiles).
$profile->is_suggestable = false;
$settings->show_guests = false;
$settings->show_discover = false;
$settings->save();
$profile->save();
Cache::forget('profiles:private');
return [200];
}
}