pixelfed/app/Http/Controllers/Settings/PrivacySettings.php

231 lines
7.6 KiB
PHP
Raw Normal View History

2018-09-17 01:45:36 +00:00
<?php
namespace App\Http\Controllers\Settings;
use App\AccountLog;
use App\EmailVerification;
2019-01-31 19:54:17 +00:00
use App\Instance;
2019-06-27 01:58:39 +00:00
use App\Follower;
2018-09-17 01:45:36 +00:00
use App\Media;
use App\Profile;
use App\User;
use App\UserFilter;
use App\Util\Lexer\PrettyNumber;
2019-06-24 05:31:07 +00:00
use App\Util\ActivityPub\Helpers;
2019-02-25 06:22:06 +00:00
use Auth, Cache, DB;
2018-09-17 01:45:36 +00:00
use Illuminate\Http\Request;
trait PrivacySettings
{
public function privacy()
{
$settings = Auth::user()->settings;
$is_private = Auth::user()->profile->is_private;
$settings['is_private'] = (bool) $is_private;
return view('settings.privacy', compact('settings'));
}
public function privacyStore(Request $request)
{
2019-09-11 06:15:27 +00:00
$settings = $request->user()->settings;
$profile = $request->user()->profile;
2018-09-17 01:45:36 +00:00
$fields = [
'is_private',
'crawlable',
2020-11-18 21:19:02 +00:00
'public_dm',
2018-09-17 01:45:36 +00:00
'show_profile_follower_count',
'show_profile_following_count',
2019-09-11 06:15:27 +00:00
];
2018-09-17 01:45:36 +00:00
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();
}
2019-02-25 06:22:06 +00:00
Cache::forget('profiles:private');
2018-09-17 01:45:36 +00:00
} elseif ($field == 'crawlable') {
if ($form == 'on') {
$settings->{$field} = false;
} else {
$settings->{$field} = true;
}
2020-11-18 21:19:02 +00:00
} elseif ($field == 'public_dm') {
if ($form == 'on') {
$settings->{$field} = true;
} else {
$settings->{$field} = false;
}
2018-09-17 01:45:36 +00:00
} else {
if ($form == 'on') {
$settings->{$field} = true;
} else {
$settings->{$field} = false;
}
}
$settings->save();
}
2019-09-11 06:15:27 +00:00
Cache::forget('profile:settings:' . $profile->id);
Cache::forget('user:account:id:' . $profile->user_id);
Cache::forget('profile:follower_count:' . $profile->id);
Cache::forget('profile:following_count:' . $profile->id);
Cache::forget('profile:embed:' . $profile->id);
Cache::forget('pf:acct:settings:hidden-followers:' . $profile->id);
Cache::forget('pf:acct:settings:hidden-following:' . $profile->id);
2018-09-17 01:45:36 +00:00
return redirect(route('settings.privacy'))->with('status', 'Settings successfully updated!');
}
public function mutedUsers()
{
$pid = Auth::user()->profile->id;
$ids = (new UserFilter())->mutedUserIds($pid);
$users = Profile::whereIn('id', $ids)->simplePaginate(15);
return view('settings.privacy.muted', compact('users'));
}
public function mutedUsersUpdate(Request $request)
{
$this->validate($request, [
'profile_id' => 'required|integer|min:1'
]);
$fid = $request->input('profile_id');
$pid = Auth::user()->profile->id;
DB::transaction(function () use ($fid, $pid) {
$filter = UserFilter::whereUserId($pid)
->whereFilterableId($fid)
->whereFilterableType('App\Profile')
->whereFilterType('mute')
->firstOrFail();
$filter->delete();
});
return redirect()->back();
}
public function blockedUsers()
{
$pid = Auth::user()->profile->id;
$ids = (new UserFilter())->blockedUserIds($pid);
$users = Profile::whereIn('id', $ids)->simplePaginate(15);
return view('settings.privacy.blocked', compact('users'));
}
public function blockedUsersUpdate(Request $request)
{
$this->validate($request, [
'profile_id' => 'required|integer|min:1'
]);
$fid = $request->input('profile_id');
$pid = Auth::user()->profile->id;
DB::transaction(function () use ($fid, $pid) {
$filter = UserFilter::whereUserId($pid)
->whereFilterableId($fid)
->whereFilterableType('App\Profile')
->whereFilterType('block')
->firstOrFail();
$filter->delete();
});
return redirect()->back();
}
public function blockedInstances()
{
2019-01-31 19:54:17 +00:00
$pid = Auth::user()->profile->id;
$filters = UserFilter::whereUserId($pid)
->whereFilterableType('App\Instance')
->whereFilterType('block')
->orderByDesc('id')
->paginate(10);
return view('settings.privacy.blocked-instances', compact('filters'));
}
public function blockedInstanceStore(Request $request)
{
$this->validate($request, [
2019-06-24 05:31:07 +00:00
'domain' => 'required|url|min:1|max:120'
2019-01-31 19:54:17 +00:00
]);
$domain = $request->input('domain');
2019-06-24 05:31:07 +00:00
if(Helpers::validateUrl($domain) == false) {
return abort(400, 'Invalid domain');
}
$domain = parse_url($domain, PHP_URL_HOST);
2019-01-31 19:54:17 +00:00
$instance = Instance::firstOrCreate(['domain' => $domain]);
$filter = new UserFilter;
$filter->user_id = Auth::user()->profile->id;
$filter->filterable_id = $instance->id;
$filter->filterable_type = 'App\Instance';
$filter->filter_type = 'block';
$filter->save();
return response()->json(['msg' => 200]);
}
public function blockedInstanceUnblock(Request $request)
{
$this->validate($request, [
'id' => 'required|integer|min:1'
]);
$pid = Auth::user()->profile->id;
$filter = UserFilter::whereFilterableType('App\Instance')
->whereUserId($pid)
->findOrFail($request->input('id'));
$filter->delete();
return redirect(route('settings.privacy.blocked-instances'));
}
public function blockedKeywords()
{
return view('settings.privacy.blocked-keywords');
2018-09-17 01:45:36 +00:00
}
2019-06-27 01:58:39 +00:00
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 = Auth::user()->profile;
$settings = Auth::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)->delete();
break;
default:
# code...
break;
}
}
$profile->is_private = true;
$settings->show_guests = false;
$settings->show_discover = false;
$settings->save();
$profile->save();
Cache::forget('profiles:private');
return [200];
}
}