Update StoryController, add parental controls support

This commit is contained in:
Daniel Supernault 2024-01-11 05:46:02 -07:00
parent fe30cd25d1
commit 71c148c61e
No known key found for this signature in database
GPG key ID: 23740873EE6F76A1
2 changed files with 645 additions and 614 deletions

View file

@ -29,6 +29,7 @@ use App\Jobs\StoryPipeline\StoryFanout;
use App\Jobs\StoryPipeline\StoryDelete; use App\Jobs\StoryPipeline\StoryDelete;
use ImageOptimizer; use ImageOptimizer;
use App\Models\Conversation; use App\Models\Conversation;
use App\Services\UserRoleService;
class StoryComposeController extends Controller class StoryComposeController extends Controller
{ {
@ -47,7 +48,7 @@ class StoryComposeController extends Controller
]); ]);
$user = $request->user(); $user = $request->user();
abort_if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id), 403, 'Invalid permissions for this action');
$count = Story::whereProfileId($user->profile_id) $count = Story::whereProfileId($user->profile_id)
->whereActive(true) ->whereActive(true)
->where('expires_at', '>', now()) ->where('expires_at', '>', now())
@ -177,6 +178,7 @@ class StoryComposeController extends Controller
$id = $request->input('media_id'); $id = $request->input('media_id');
$user = $request->user(); $user = $request->user();
abort_if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id), 403, 'Invalid permissions for this action');
$story = Story::whereProfileId($user->profile_id) $story = Story::whereProfileId($user->profile_id)
->findOrFail($id); ->findOrFail($id);
@ -218,6 +220,8 @@ class StoryComposeController extends Controller
public function compose(Request $request) public function compose(Request $request)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404);
$user = $request->user();
abort_if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id), 403, 'Invalid permissions for this action');
return view('stories.compose'); return view('stories.compose');
} }
@ -241,6 +245,8 @@ class StoryComposeController extends Controller
'can_react' => 'required|boolean' 'can_react' => 'required|boolean'
]); ]);
$user = $request->user();
abort_if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id), 403, 'Invalid permissions for this action');
$pid = $request->user()->profile_id; $pid = $request->user()->profile_id;
$count = Story::whereProfileId($pid) $count = Story::whereProfileId($pid)
@ -329,6 +335,9 @@ class StoryComposeController extends Controller
'id' => 'required|integer|min:1', 'id' => 'required|integer|min:1',
]); ]);
$user = $request->user();
abort_if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id), 403, 'Invalid permissions for this action');
$pid = $request->user()->profile_id; $pid = $request->user()->profile_id;
$sid = $request->input('id'); $sid = $request->input('id');
$type = $request->input('type'); $type = $request->input('type');
@ -387,7 +396,8 @@ class StoryComposeController extends Controller
]); ]);
$pid = $request->user()->profile_id; $pid = $request->user()->profile_id;
$text = $request->input('reaction'); $text = $request->input('reaction');
$user = $request->user();
abort_if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id), 403, 'Invalid permissions for this action');
$story = Story::findOrFail($request->input('sid')); $story = Story::findOrFail($request->input('sid'));
abort_if(!$story->can_react, 422); abort_if(!$story->can_react, 422);
@ -461,7 +471,8 @@ class StoryComposeController extends Controller
]); ]);
$pid = $request->user()->profile_id; $pid = $request->user()->profile_id;
$text = $request->input('caption'); $text = $request->input('caption');
$user = $request->user();
abort_if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id), 403, 'Invalid permissions for this action');
$story = Story::findOrFail($request->input('sid')); $story = Story::findOrFail($request->input('sid'));
abort_if(!$story->can_reply, 422); abort_if(!$story->can_reply, 422);

View file

@ -28,13 +28,18 @@ use League\Fractal\Serializer\ArraySerializer;
use League\Fractal\Resource\Item; use League\Fractal\Resource\Item;
use App\Transformer\ActivityPub\Verb\StoryVerb; use App\Transformer\ActivityPub\Verb\StoryVerb;
use App\Jobs\StoryPipeline\StoryViewDeliver; use App\Jobs\StoryPipeline\StoryViewDeliver;
use App\Services\UserRoleService;
class StoryController extends StoryComposeController class StoryController extends StoryComposeController
{ {
public function recent(Request $request) public function recent(Request $request)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404);
$pid = $request->user()->profile_id; $user = $request->user();
if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id)) {
return [];
}
$pid = $user->profile_id;
if(config('database.default') == 'pgsql') { if(config('database.default') == 'pgsql') {
$s = Cache::remember('pf:stories:recent-by-id:' . $pid, 900, function() use($pid) { $s = Cache::remember('pf:stories:recent-by-id:' . $pid, 900, function() use($pid) {
@ -114,7 +119,11 @@ class StoryController extends StoryComposeController
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404);
$authed = $request->user()->profile_id; $user = $request->user();
if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id)) {
return [];
}
$authed = $user->profile_id;
$profile = Profile::findOrFail($id); $profile = Profile::findOrFail($id);
if($authed != $profile->id && !FollowerService::follows($authed, $profile->id)) { if($authed != $profile->id && !FollowerService::follows($authed, $profile->id)) {
@ -173,8 +182,11 @@ class StoryController extends StoryComposeController
'id' => 'required|min:1', 'id' => 'required|min:1',
]); ]);
$id = $request->input('id'); $id = $request->input('id');
$user = $request->user();
$authed = $request->user()->profile; if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id)) {
return [];
}
$authed = $user->profile;
$story = Story::with('profile') $story = Story::with('profile')
->findOrFail($id); ->findOrFail($id);
@ -210,7 +222,10 @@ class StoryController extends StoryComposeController
public function exists(Request $request, $id) public function exists(Request $request, $id)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404);
$user = $request->user();
if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id)) {
return response()->json(false);
}
return response()->json(Story::whereProfileId($id) return response()->json(Story::whereProfileId($id)
->whereActive(true) ->whereActive(true)
->exists()); ->exists());
@ -234,6 +249,11 @@ class StoryController extends StoryComposeController
'sid' => 'required|string' 'sid' => 'required|string'
]); ]);
$user = $request->user();
if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id)) {
return response()->json([]);
}
$pid = $request->user()->profile_id; $pid = $request->user()->profile_id;
$sid = $request->input('sid'); $sid = $request->input('sid');