Update StoryController, add parental controls support

pull/4862/head
Daniel Supernault 1 year ago
parent fe30cd25d1
commit 71c148c61e
No known key found for this signature in database
GPG Key ID: 23740873EE6F76A1

@ -29,306 +29,315 @@ 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
{ {
public function apiV1Add(Request $request) public function apiV1Add(Request $request)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404);
$this->validate($request, [ $this->validate($request, [
'file' => function() { 'file' => function() {
return [ return [
'required', 'required',
'mimetypes:image/jpeg,image/png,video/mp4', 'mimetypes:image/jpeg,image/png,video/mp4',
'max:' . config_cache('pixelfed.max_photo_size'), 'max:' . config_cache('pixelfed.max_photo_size'),
]; ];
}, },
]); ]);
$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())
->count(); ->count();
if($count >= Story::MAX_PER_DAY) { if($count >= Story::MAX_PER_DAY) {
abort(418, 'You have reached your limit for new Stories today.'); abort(418, 'You have reached your limit for new Stories today.');
} }
$photo = $request->file('file'); $photo = $request->file('file');
$path = $this->storePhoto($photo, $user); $path = $this->storePhoto($photo, $user);
$story = new Story(); $story = new Story();
$story->duration = 3; $story->duration = 3;
$story->profile_id = $user->profile_id; $story->profile_id = $user->profile_id;
$story->type = Str::endsWith($photo->getMimeType(), 'mp4') ? 'video' :'photo'; $story->type = Str::endsWith($photo->getMimeType(), 'mp4') ? 'video' :'photo';
$story->mime = $photo->getMimeType(); $story->mime = $photo->getMimeType();
$story->path = $path; $story->path = $path;
$story->local = true; $story->local = true;
$story->size = $photo->getSize(); $story->size = $photo->getSize();
$story->bearcap_token = str_random(64); $story->bearcap_token = str_random(64);
$story->expires_at = now()->addMinutes(1440); $story->expires_at = now()->addMinutes(1440);
$story->save(); $story->save();
$url = $story->path; $url = $story->path;
$res = [ $res = [
'code' => 200, 'code' => 200,
'msg' => 'Successfully added', 'msg' => 'Successfully added',
'media_id' => (string) $story->id, 'media_id' => (string) $story->id,
'media_url' => url(Storage::url($url)) . '?v=' . time(), 'media_url' => url(Storage::url($url)) . '?v=' . time(),
'media_type' => $story->type 'media_type' => $story->type
]; ];
if($story->type === 'video') { if($story->type === 'video') {
$video = FFMpeg::open($path); $video = FFMpeg::open($path);
$duration = $video->getDurationInSeconds(); $duration = $video->getDurationInSeconds();
$res['media_duration'] = $duration; $res['media_duration'] = $duration;
if($duration > 500) { if($duration > 500) {
Storage::delete($story->path); Storage::delete($story->path);
$story->delete(); $story->delete();
return response()->json([ return response()->json([
'message' => 'Video duration cannot exceed 60 seconds' 'message' => 'Video duration cannot exceed 60 seconds'
], 422); ], 422);
} }
} }
return $res; return $res;
} }
protected function storePhoto($photo, $user) protected function storePhoto($photo, $user)
{ {
$mimes = explode(',', config_cache('pixelfed.media_types')); $mimes = explode(',', config_cache('pixelfed.media_types'));
if(in_array($photo->getMimeType(), [ if(in_array($photo->getMimeType(), [
'image/jpeg', 'image/jpeg',
'image/png', 'image/png',
'video/mp4' 'video/mp4'
]) == false) { ]) == false) {
abort(400, 'Invalid media type'); abort(400, 'Invalid media type');
return; return;
} }
$storagePath = MediaPathService::story($user->profile); $storagePath = MediaPathService::story($user->profile);
$path = $photo->storePubliclyAs($storagePath, Str::random(random_int(2, 12)) . '_' . Str::random(random_int(32, 35)) . '_' . Str::random(random_int(1, 14)) . '.' . $photo->extension()); $path = $photo->storePubliclyAs($storagePath, Str::random(random_int(2, 12)) . '_' . Str::random(random_int(32, 35)) . '_' . Str::random(random_int(1, 14)) . '.' . $photo->extension());
if(in_array($photo->getMimeType(), ['image/jpeg','image/png'])) { if(in_array($photo->getMimeType(), ['image/jpeg','image/png'])) {
$fpath = storage_path('app/' . $path); $fpath = storage_path('app/' . $path);
$img = Intervention::make($fpath); $img = Intervention::make($fpath);
$img->orientate(); $img->orientate();
$img->save($fpath, config_cache('pixelfed.image_quality')); $img->save($fpath, config_cache('pixelfed.image_quality'));
$img->destroy(); $img->destroy();
} }
return $path; return $path;
} }
public function cropPhoto(Request $request) public function cropPhoto(Request $request)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404);
$this->validate($request, [ $this->validate($request, [
'media_id' => 'required|integer|min:1', 'media_id' => 'required|integer|min:1',
'width' => 'required', 'width' => 'required',
'height' => 'required', 'height' => 'required',
'x' => 'required', 'x' => 'required',
'y' => 'required' 'y' => 'required'
]); ]);
$user = $request->user(); $user = $request->user();
$id = $request->input('media_id'); $id = $request->input('media_id');
$width = round($request->input('width')); $width = round($request->input('width'));
$height = round($request->input('height')); $height = round($request->input('height'));
$x = round($request->input('x')); $x = round($request->input('x'));
$y = round($request->input('y')); $y = round($request->input('y'));
$story = Story::whereProfileId($user->profile_id)->findOrFail($id); $story = Story::whereProfileId($user->profile_id)->findOrFail($id);
$path = storage_path('app/' . $story->path); $path = storage_path('app/' . $story->path);
if(!is_file($path)) { if(!is_file($path)) {
abort(400, 'Invalid or missing media.'); abort(400, 'Invalid or missing media.');
} }
if($story->type === 'photo') { if($story->type === 'photo') {
$img = Intervention::make($path); $img = Intervention::make($path);
$img->crop($width, $height, $x, $y); $img->crop($width, $height, $x, $y);
$img->resize(1080, 1920, function ($constraint) { $img->resize(1080, 1920, function ($constraint) {
$constraint->aspectRatio(); $constraint->aspectRatio();
}); });
$img->save($path, config_cache('pixelfed.image_quality')); $img->save($path, config_cache('pixelfed.image_quality'));
} }
return [ return [
'code' => 200, 'code' => 200,
'msg' => 'Successfully cropped', 'msg' => 'Successfully cropped',
]; ];
} }
public function publishStory(Request $request) public function publishStory(Request $request)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404);
$this->validate($request, [ $this->validate($request, [
'media_id' => 'required', 'media_id' => 'required',
'duration' => 'required|integer|min:3|max:120', 'duration' => 'required|integer|min:3|max:120',
'can_reply' => 'required|boolean', 'can_reply' => 'required|boolean',
'can_react' => 'required|boolean' 'can_react' => 'required|boolean'
]); ]);
$id = $request->input('media_id'); $id = $request->input('media_id');
$user = $request->user(); $user = $request->user();
$story = Story::whereProfileId($user->profile_id) abort_if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id), 403, 'Invalid permissions for this action');
->findOrFail($id); $story = Story::whereProfileId($user->profile_id)
->findOrFail($id);
$story->active = true;
$story->duration = $request->input('duration', 10); $story->active = true;
$story->can_reply = $request->input('can_reply'); $story->duration = $request->input('duration', 10);
$story->can_react = $request->input('can_react'); $story->can_reply = $request->input('can_reply');
$story->save(); $story->can_react = $request->input('can_react');
$story->save();
StoryService::delLatest($story->profile_id);
StoryFanout::dispatch($story)->onQueue('story'); StoryService::delLatest($story->profile_id);
StoryService::addRotateQueue($story->id); StoryFanout::dispatch($story)->onQueue('story');
StoryService::addRotateQueue($story->id);
return [
'code' => 200, return [
'msg' => 'Successfully published', 'code' => 200,
]; 'msg' => 'Successfully published',
} ];
}
public function apiV1Delete(Request $request, $id)
{ public function apiV1Delete(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();
$user = $request->user();
$story = Story::whereProfileId($user->profile_id)
->findOrFail($id); $story = Story::whereProfileId($user->profile_id)
$story->active = false; ->findOrFail($id);
$story->save(); $story->active = false;
$story->save();
StoryDelete::dispatch($story)->onQueue('story');
StoryDelete::dispatch($story)->onQueue('story');
return [
'code' => 200, return [
'msg' => 'Successfully deleted' 'code' => 200,
]; 'msg' => 'Successfully deleted'
} ];
}
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);
return view('stories.compose'); $user = $request->user();
} abort_if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id), 403, 'Invalid permissions for this action');
public function createPoll(Request $request) return view('stories.compose');
{ }
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404);
abort_if(!config_cache('instance.polls.enabled'), 404); public function createPoll(Request $request)
{
return $request->all(); abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404);
} abort_if(!config_cache('instance.polls.enabled'), 404);
public function publishStoryPoll(Request $request) return $request->all();
{ }
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404);
public function publishStoryPoll(Request $request)
$this->validate($request, [ {
'question' => 'required|string|min:6|max:140', abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404);
'options' => 'required|array|min:2|max:4',
'can_reply' => 'required|boolean', $this->validate($request, [
'can_react' => 'required|boolean' 'question' => 'required|string|min:6|max:140',
]); 'options' => 'required|array|min:2|max:4',
'can_reply' => 'required|boolean',
$pid = $request->user()->profile_id; 'can_react' => 'required|boolean'
]);
$count = Story::whereProfileId($pid)
->whereActive(true) $user = $request->user();
->where('expires_at', '>', now()) abort_if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id), 403, 'Invalid permissions for this action');
->count(); $pid = $request->user()->profile_id;
if($count >= Story::MAX_PER_DAY) { $count = Story::whereProfileId($pid)
abort(418, 'You have reached your limit for new Stories today.'); ->whereActive(true)
} ->where('expires_at', '>', now())
->count();
$story = new Story;
$story->type = 'poll'; if($count >= Story::MAX_PER_DAY) {
$story->story = json_encode([ abort(418, 'You have reached your limit for new Stories today.');
'question' => $request->input('question'), }
'options' => $request->input('options')
]); $story = new Story;
$story->public = false; $story->type = 'poll';
$story->local = true; $story->story = json_encode([
$story->profile_id = $pid; 'question' => $request->input('question'),
$story->expires_at = now()->addMinutes(1440); 'options' => $request->input('options')
$story->duration = 30; ]);
$story->can_reply = false; $story->public = false;
$story->can_react = false; $story->local = true;
$story->save(); $story->profile_id = $pid;
$story->expires_at = now()->addMinutes(1440);
$poll = new Poll; $story->duration = 30;
$poll->story_id = $story->id; $story->can_reply = false;
$poll->profile_id = $pid; $story->can_react = false;
$poll->poll_options = $request->input('options'); $story->save();
$poll->expires_at = $story->expires_at;
$poll->cached_tallies = collect($poll->poll_options)->map(function($o) { $poll = new Poll;
return 0; $poll->story_id = $story->id;
})->toArray(); $poll->profile_id = $pid;
$poll->save(); $poll->poll_options = $request->input('options');
$poll->expires_at = $story->expires_at;
$story->active = true; $poll->cached_tallies = collect($poll->poll_options)->map(function($o) {
$story->save(); return 0;
})->toArray();
StoryService::delLatest($story->profile_id); $poll->save();
return [ $story->active = true;
'code' => 200, $story->save();
'msg' => 'Successfully published',
]; StoryService::delLatest($story->profile_id);
}
return [
public function storyPollVote(Request $request) 'code' => 200,
{ 'msg' => 'Successfully published',
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); ];
}
$this->validate($request, [
'sid' => 'required', public function storyPollVote(Request $request)
'ci' => 'required|integer|min:0|max:3' {
]); abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404);
$pid = $request->user()->profile_id; $this->validate($request, [
$ci = $request->input('ci'); 'sid' => 'required',
$story = Story::findOrFail($request->input('sid')); 'ci' => 'required|integer|min:0|max:3'
abort_if(!FollowerService::follows($pid, $story->profile_id), 403); ]);
$poll = Poll::whereStoryId($story->id)->firstOrFail();
$pid = $request->user()->profile_id;
$vote = new PollVote; $ci = $request->input('ci');
$vote->profile_id = $pid; $story = Story::findOrFail($request->input('sid'));
$vote->poll_id = $poll->id; abort_if(!FollowerService::follows($pid, $story->profile_id), 403);
$vote->story_id = $story->id; $poll = Poll::whereStoryId($story->id)->firstOrFail();
$vote->status_id = null;
$vote->choice = $ci; $vote = new PollVote;
$vote->save(); $vote->profile_id = $pid;
$vote->poll_id = $poll->id;
$poll->votes_count = $poll->votes_count + 1; $vote->story_id = $story->id;
$poll->cached_tallies = collect($poll->getTallies())->map(function($tally, $key) use($ci) { $vote->status_id = null;
return $ci == $key ? $tally + 1 : $tally; $vote->choice = $ci;
})->toArray(); $vote->save();
$poll->save();
$poll->votes_count = $poll->votes_count + 1;
return 200; $poll->cached_tallies = collect($poll->getTallies())->map(function($tally, $key) use($ci) {
} return $ci == $key ? $tally + 1 : $tally;
})->toArray();
public function storeReport(Request $request) $poll->save();
{
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); return 200;
}
$this->validate($request, [
public function storeReport(Request $request)
{
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404);
$this->validate($request, [
'type' => 'required|alpha_dash', 'type' => 'required|alpha_dash',
'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');
@ -355,17 +364,17 @@ class StoryComposeController extends Controller
abort_if(!FollowerService::follows($pid, $story->profile_id), 422, 'Cannot report a story from an account you do not follow'); abort_if(!FollowerService::follows($pid, $story->profile_id), 422, 'Cannot report a story from an account you do not follow');
if( Report::whereProfileId($pid) if( Report::whereProfileId($pid)
->whereObjectType('App\Story') ->whereObjectType('App\Story')
->whereObjectId($story->id) ->whereObjectId($story->id)
->exists() ->exists()
) { ) {
return response()->json(['error' => [ return response()->json(['error' => [
'code' => 409, 'code' => 409,
'message' => 'Cannot report the same story again' 'message' => 'Cannot report the same story again'
]], 409); ]], 409);
} }
$report = new Report; $report = new Report;
$report->profile_id = $pid; $report->profile_id = $pid;
$report->user_id = $request->user()->id; $report->user_id = $request->user()->id;
$report->object_id = $story->id; $report->object_id = $story->id;
@ -376,149 +385,151 @@ class StoryComposeController extends Controller
$report->save(); $report->save();
return [200]; return [200];
} }
public function react(Request $request) public function react(Request $request)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404);
$this->validate($request, [ $this->validate($request, [
'sid' => 'required', 'sid' => 'required',
'reaction' => 'required|string' 'reaction' => 'required|string'
]); ]);
$pid = $request->user()->profile_id; $pid = $request->user()->profile_id;
$text = $request->input('reaction'); $text = $request->input('reaction');
$user = $request->user();
$story = Story::findOrFail($request->input('sid')); abort_if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id), 403, 'Invalid permissions for this action');
$story = Story::findOrFail($request->input('sid'));
abort_if(!$story->can_react, 422);
abort_if(StoryService::reactCounter($story->id, $pid) >= 5, 422, 'You have already reacted to this story'); abort_if(!$story->can_react, 422);
abort_if(StoryService::reactCounter($story->id, $pid) >= 5, 422, 'You have already reacted to this story');
$status = new Status;
$status->profile_id = $pid; $status = new Status;
$status->type = 'story:reaction'; $status->profile_id = $pid;
$status->caption = $text; $status->type = 'story:reaction';
$status->rendered = $text; $status->caption = $text;
$status->scope = 'direct'; $status->rendered = $text;
$status->visibility = 'direct'; $status->scope = 'direct';
$status->in_reply_to_profile_id = $story->profile_id; $status->visibility = 'direct';
$status->entities = json_encode([ $status->in_reply_to_profile_id = $story->profile_id;
'story_id' => $story->id, $status->entities = json_encode([
'reaction' => $text 'story_id' => $story->id,
]); 'reaction' => $text
$status->save(); ]);
$status->save();
$dm = new DirectMessage;
$dm->to_id = $story->profile_id; $dm = new DirectMessage;
$dm->from_id = $pid; $dm->to_id = $story->profile_id;
$dm->type = 'story:react'; $dm->from_id = $pid;
$dm->status_id = $status->id; $dm->type = 'story:react';
$dm->meta = json_encode([ $dm->status_id = $status->id;
'story_username' => $story->profile->username, $dm->meta = json_encode([
'story_actor_username' => $request->user()->username, 'story_username' => $story->profile->username,
'story_id' => $story->id, 'story_actor_username' => $request->user()->username,
'story_media_url' => url(Storage::url($story->path)), 'story_id' => $story->id,
'reaction' => $text 'story_media_url' => url(Storage::url($story->path)),
]); 'reaction' => $text
$dm->save(); ]);
$dm->save();
Conversation::updateOrInsert(
[ Conversation::updateOrInsert(
'to_id' => $story->profile_id, [
'from_id' => $pid 'to_id' => $story->profile_id,
], 'from_id' => $pid
[ ],
'type' => 'story:react', [
'status_id' => $status->id, 'type' => 'story:react',
'dm_id' => $dm->id, 'status_id' => $status->id,
'is_hidden' => false 'dm_id' => $dm->id,
] 'is_hidden' => false
); ]
);
if($story->local) {
// generate notification if($story->local) {
$n = new Notification; // generate notification
$n->profile_id = $dm->to_id; $n = new Notification;
$n->actor_id = $dm->from_id; $n->profile_id = $dm->to_id;
$n->item_id = $dm->id; $n->actor_id = $dm->from_id;
$n->item_type = 'App\DirectMessage'; $n->item_id = $dm->id;
$n->action = 'story:react'; $n->item_type = 'App\DirectMessage';
$n->save(); $n->action = 'story:react';
} else { $n->save();
StoryReactionDeliver::dispatch($story, $status)->onQueue('story'); } else {
} StoryReactionDeliver::dispatch($story, $status)->onQueue('story');
}
StoryService::reactIncrement($story->id, $pid);
StoryService::reactIncrement($story->id, $pid);
return 200;
} return 200;
}
public function comment(Request $request)
{ public function comment(Request $request)
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); {
$this->validate($request, [ abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404);
'sid' => 'required', $this->validate($request, [
'caption' => 'required|string' 'sid' => 'required',
]); 'caption' => 'required|string'
$pid = $request->user()->profile_id; ]);
$text = $request->input('caption'); $pid = $request->user()->profile_id;
$text = $request->input('caption');
$story = Story::findOrFail($request->input('sid')); $user = $request->user();
abort_if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id), 403, 'Invalid permissions for this action');
abort_if(!$story->can_reply, 422); $story = Story::findOrFail($request->input('sid'));
$status = new Status; abort_if(!$story->can_reply, 422);
$status->type = 'story:reply';
$status->profile_id = $pid; $status = new Status;
$status->caption = $text; $status->type = 'story:reply';
$status->rendered = $text; $status->profile_id = $pid;
$status->scope = 'direct'; $status->caption = $text;
$status->visibility = 'direct'; $status->rendered = $text;
$status->in_reply_to_profile_id = $story->profile_id; $status->scope = 'direct';
$status->entities = json_encode([ $status->visibility = 'direct';
'story_id' => $story->id $status->in_reply_to_profile_id = $story->profile_id;
]); $status->entities = json_encode([
$status->save(); 'story_id' => $story->id
]);
$dm = new DirectMessage; $status->save();
$dm->to_id = $story->profile_id;
$dm->from_id = $pid; $dm = new DirectMessage;
$dm->type = 'story:comment'; $dm->to_id = $story->profile_id;
$dm->status_id = $status->id; $dm->from_id = $pid;
$dm->meta = json_encode([ $dm->type = 'story:comment';
'story_username' => $story->profile->username, $dm->status_id = $status->id;
'story_actor_username' => $request->user()->username, $dm->meta = json_encode([
'story_id' => $story->id, 'story_username' => $story->profile->username,
'story_media_url' => url(Storage::url($story->path)), 'story_actor_username' => $request->user()->username,
'caption' => $text 'story_id' => $story->id,
]); 'story_media_url' => url(Storage::url($story->path)),
$dm->save(); 'caption' => $text
]);
Conversation::updateOrInsert( $dm->save();
[
'to_id' => $story->profile_id, Conversation::updateOrInsert(
'from_id' => $pid [
], 'to_id' => $story->profile_id,
[ 'from_id' => $pid
'type' => 'story:comment', ],
'status_id' => $status->id, [
'dm_id' => $dm->id, 'type' => 'story:comment',
'is_hidden' => false 'status_id' => $status->id,
] 'dm_id' => $dm->id,
); 'is_hidden' => false
]
if($story->local) { );
// generate notification
$n = new Notification; if($story->local) {
$n->profile_id = $dm->to_id; // generate notification
$n->actor_id = $dm->from_id; $n = new Notification;
$n->item_id = $dm->id; $n->profile_id = $dm->to_id;
$n->item_type = 'App\DirectMessage'; $n->actor_id = $dm->from_id;
$n->action = 'story:comment'; $n->item_id = $dm->id;
$n->save(); $n->item_type = 'App\DirectMessage';
} else { $n->action = 'story:comment';
StoryReplyDeliver::dispatch($story, $status)->onQueue('story'); $n->save();
} } else {
StoryReplyDeliver::dispatch($story, $status)->onQueue('story');
return 200; }
}
return 200;
}
} }

@ -28,288 +28,308 @@ 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)) {
if(config('database.default') == 'pgsql') { return [];
$s = Cache::remember('pf:stories:recent-by-id:' . $pid, 900, function() use($pid) { }
return Story::select('stories.*', 'followers.following_id') $pid = $user->profile_id;
->leftJoin('followers', 'followers.following_id', 'stories.profile_id')
->where('followers.profile_id', $pid) if(config('database.default') == 'pgsql') {
->where('stories.active', true) $s = Cache::remember('pf:stories:recent-by-id:' . $pid, 900, function() use($pid) {
->get() return Story::select('stories.*', 'followers.following_id')
->map(function($s) { ->leftJoin('followers', 'followers.following_id', 'stories.profile_id')
$r = new \StdClass; ->where('followers.profile_id', $pid)
$r->id = $s->id; ->where('stories.active', true)
$r->profile_id = $s->profile_id; ->get()
$r->type = $s->type; ->map(function($s) {
$r->path = $s->path; $r = new \StdClass;
return $r; $r->id = $s->id;
}) $r->profile_id = $s->profile_id;
->unique('profile_id'); $r->type = $s->type;
}); $r->path = $s->path;
return $r;
} else { })
$s = Cache::remember('pf:stories:recent-by-id:' . $pid, 900, function() use($pid) { ->unique('profile_id');
return Story::select('stories.*', 'followers.following_id') });
->leftJoin('followers', 'followers.following_id', 'stories.profile_id')
->where('followers.profile_id', $pid) } else {
->where('stories.active', true) $s = Cache::remember('pf:stories:recent-by-id:' . $pid, 900, function() use($pid) {
->groupBy('followers.following_id') return Story::select('stories.*', 'followers.following_id')
->orderByDesc('id') ->leftJoin('followers', 'followers.following_id', 'stories.profile_id')
->get(); ->where('followers.profile_id', $pid)
}); ->where('stories.active', true)
} ->groupBy('followers.following_id')
->orderByDesc('id')
$self = Cache::remember('pf:stories:recent-self:' . $pid, 21600, function() use($pid) { ->get();
return Story::whereProfileId($pid) });
->whereActive(true) }
->orderByDesc('id')
->limit(1) $self = Cache::remember('pf:stories:recent-self:' . $pid, 21600, function() use($pid) {
->get() return Story::whereProfileId($pid)
->map(function($s) use($pid) { ->whereActive(true)
$r = new \StdClass; ->orderByDesc('id')
$r->id = $s->id; ->limit(1)
$r->profile_id = $pid; ->get()
$r->type = $s->type; ->map(function($s) use($pid) {
$r->path = $s->path; $r = new \StdClass;
return $r; $r->id = $s->id;
}); $r->profile_id = $pid;
}); $r->type = $s->type;
$r->path = $s->path;
if($self->count()) { return $r;
$s->prepend($self->first()); });
} });
$res = $s->map(function($s) use($pid) { if($self->count()) {
$profile = AccountService::get($s->profile_id); $s->prepend($self->first());
$url = $profile['local'] ? url("/stories/{$profile['username']}") : }
url("/i/rs/{$profile['id']}");
return [ $res = $s->map(function($s) use($pid) {
'pid' => $profile['id'], $profile = AccountService::get($s->profile_id);
'avatar' => $profile['avatar'], $url = $profile['local'] ? url("/stories/{$profile['username']}") :
'local' => $profile['local'], url("/i/rs/{$profile['id']}");
'username' => $profile['acct'], return [
'latest' => [ 'pid' => $profile['id'],
'id' => $s->id, 'avatar' => $profile['avatar'],
'type' => $s->type, 'local' => $profile['local'],
'preview_url' => url(Storage::url($s->path)) 'username' => $profile['acct'],
], 'latest' => [
'url' => $url, 'id' => $s->id,
'seen' => StoryService::hasSeen($pid, StoryService::latest($s->profile_id)), 'type' => $s->type,
'sid' => $s->id 'preview_url' => url(Storage::url($s->path))
]; ],
}) 'url' => $url,
->sortBy('seen') 'seen' => StoryService::hasSeen($pid, StoryService::latest($s->profile_id)),
->values(); 'sid' => $s->id
return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES); ];
} })
->sortBy('seen')
public function profile(Request $request, $id) ->values();
{ return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); }
$authed = $request->user()->profile_id; public function profile(Request $request, $id)
$profile = Profile::findOrFail($id); {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404);
if($authed != $profile->id && !FollowerService::follows($authed, $profile->id)) {
return abort([], 403); $user = $request->user();
} if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id)) {
return [];
$stories = Story::whereProfileId($profile->id) }
->whereActive(true) $authed = $user->profile_id;
->orderBy('expires_at') $profile = Profile::findOrFail($id);
->get()
->map(function($s, $k) use($authed) { if($authed != $profile->id && !FollowerService::follows($authed, $profile->id)) {
$seen = StoryService::hasSeen($authed, $s->id); return abort([], 403);
$res = [ }
'id' => (string) $s->id,
'type' => $s->type, $stories = Story::whereProfileId($profile->id)
'duration' => $s->duration, ->whereActive(true)
'src' => url(Storage::url($s->path)), ->orderBy('expires_at')
'created_at' => $s->created_at->toAtomString(), ->get()
'expires_at' => $s->expires_at->toAtomString(), ->map(function($s, $k) use($authed) {
'view_count' => ($authed == $s->profile_id) ? ($s->view_count ?? 0) : null, $seen = StoryService::hasSeen($authed, $s->id);
'seen' => $seen, $res = [
'progress' => $seen ? 100 : 0, 'id' => (string) $s->id,
'can_reply' => (bool) $s->can_reply, 'type' => $s->type,
'can_react' => (bool) $s->can_react 'duration' => $s->duration,
]; 'src' => url(Storage::url($s->path)),
'created_at' => $s->created_at->toAtomString(),
if($s->type == 'poll') { 'expires_at' => $s->expires_at->toAtomString(),
$res['question'] = json_decode($s->story, true)['question']; 'view_count' => ($authed == $s->profile_id) ? ($s->view_count ?? 0) : null,
$res['options'] = json_decode($s->story, true)['options']; 'seen' => $seen,
$res['voted'] = PollService::votedStory($s->id, $authed); 'progress' => $seen ? 100 : 0,
if($res['voted']) { 'can_reply' => (bool) $s->can_reply,
$res['voted_index'] = PollService::storyChoice($s->id, $authed); 'can_react' => (bool) $s->can_react
} ];
}
if($s->type == 'poll') {
return $res; $res['question'] = json_decode($s->story, true)['question'];
})->toArray(); $res['options'] = json_decode($s->story, true)['options'];
if(count($stories) == 0) { $res['voted'] = PollService::votedStory($s->id, $authed);
return []; if($res['voted']) {
} $res['voted_index'] = PollService::storyChoice($s->id, $authed);
$cursor = count($stories) - 1; }
$stories = [[ }
'id' => (string) $stories[$cursor]['id'],
'nodes' => $stories, return $res;
'account' => AccountService::get($profile->id), })->toArray();
'pid' => (string) $profile->id if(count($stories) == 0) {
]]; return [];
return response()->json($stories, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES); }
} $cursor = count($stories) - 1;
$stories = [[
public function viewed(Request $request) 'id' => (string) $stories[$cursor]['id'],
{ 'nodes' => $stories,
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); 'account' => AccountService::get($profile->id),
'pid' => (string) $profile->id
$this->validate($request, [ ]];
'id' => 'required|min:1', return response()->json($stories, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
]); }
$id = $request->input('id');
public function viewed(Request $request)
$authed = $request->user()->profile; {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404);
$story = Story::with('profile')
->findOrFail($id); $this->validate($request, [
$exp = $story->expires_at; 'id' => 'required|min:1',
]);
$profile = $story->profile; $id = $request->input('id');
$user = $request->user();
if($story->profile_id == $authed->id) { if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id)) {
return []; return [];
} }
$authed = $user->profile;
$publicOnly = (bool) $profile->followedBy($authed);
abort_if(!$publicOnly, 403); $story = Story::with('profile')
->findOrFail($id);
$v = StoryView::firstOrCreate([ $exp = $story->expires_at;
'story_id' => $id,
'profile_id' => $authed->id $profile = $story->profile;
]);
if($story->profile_id == $authed->id) {
if($v->wasRecentlyCreated) { return [];
Story::findOrFail($story->id)->increment('view_count'); }
if($story->local == false) { $publicOnly = (bool) $profile->followedBy($authed);
StoryViewDeliver::dispatch($story, $authed)->onQueue('story'); abort_if(!$publicOnly, 403);
}
} $v = StoryView::firstOrCreate([
'story_id' => $id,
Cache::forget('stories:recent:by_id:' . $authed->id); 'profile_id' => $authed->id
StoryService::addSeen($authed->id, $story->id); ]);
return ['code' => 200];
} if($v->wasRecentlyCreated) {
Story::findOrFail($story->id)->increment('view_count');
public function exists(Request $request, $id)
{ if($story->local == false) {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); StoryViewDeliver::dispatch($story, $authed)->onQueue('story');
}
return response()->json(Story::whereProfileId($id) }
->whereActive(true)
->exists()); Cache::forget('stories:recent:by_id:' . $authed->id);
} StoryService::addSeen($authed->id, $story->id);
return ['code' => 200];
public function iRedirect(Request $request) }
{
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); public function exists(Request $request, $id)
{
$user = $request->user(); abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404);
abort_if(!$user, 404); $user = $request->user();
$username = $user->username; if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id)) {
return redirect("/stories/{$username}"); return response()->json(false);
} }
return response()->json(Story::whereProfileId($id)
public function viewers(Request $request) ->whereActive(true)
{ ->exists());
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); }
$this->validate($request, [ public function iRedirect(Request $request)
'sid' => 'required|string' {
]); abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404);
$pid = $request->user()->profile_id; $user = $request->user();
$sid = $request->input('sid'); abort_if(!$user, 404);
$username = $user->username;
$story = Story::whereProfileId($pid) return redirect("/stories/{$username}");
->whereActive(true) }
->findOrFail($sid);
public function viewers(Request $request)
$viewers = StoryView::whereStoryId($story->id) {
->latest() abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404);
->simplePaginate(10)
->map(function($view) { $this->validate($request, [
return AccountService::get($view->profile_id); 'sid' => 'required|string'
}) ]);
->values();
$user = $request->user();
return response()->json($viewers, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES); if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id)) {
} return response()->json([]);
}
public function remoteStory(Request $request, $id)
{ $pid = $request->user()->profile_id;
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); $sid = $request->input('sid');
$profile = Profile::findOrFail($id); $story = Story::whereProfileId($pid)
if($profile->user_id != null || $profile->domain == null) { ->whereActive(true)
return redirect('/stories/' . $profile->username); ->findOrFail($sid);
}
$pid = $profile->id; $viewers = StoryView::whereStoryId($story->id)
return view('stories.show_remote', compact('pid')); ->latest()
} ->simplePaginate(10)
->map(function($view) {
public function pollResults(Request $request) return AccountService::get($view->profile_id);
{ })
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); ->values();
$this->validate($request, [ return response()->json($viewers, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
'sid' => 'required|string' }
]);
public function remoteStory(Request $request, $id)
$pid = $request->user()->profile_id; {
$sid = $request->input('sid'); abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404);
$story = Story::whereProfileId($pid) $profile = Profile::findOrFail($id);
->whereActive(true) if($profile->user_id != null || $profile->domain == null) {
->findOrFail($sid); return redirect('/stories/' . $profile->username);
}
return PollService::storyResults($sid); $pid = $profile->id;
} return view('stories.show_remote', compact('pid'));
}
public function getActivityObject(Request $request, $username, $id)
{ public function pollResults(Request $request)
abort_if(!config_cache('instance.stories.enabled'), 404); {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404);
if(!$request->wantsJson()) {
return redirect('/stories/' . $username); $this->validate($request, [
} 'sid' => 'required|string'
]);
abort_if(!$request->hasHeader('Authorization'), 404);
$pid = $request->user()->profile_id;
$profile = Profile::whereUsername($username)->whereNull('domain')->firstOrFail(); $sid = $request->input('sid');
$story = Story::whereActive(true)->whereProfileId($profile->id)->findOrFail($id);
$story = Story::whereProfileId($pid)
abort_if($story->bearcap_token == null, 404); ->whereActive(true)
abort_if(now()->gt($story->expires_at), 404); ->findOrFail($sid);
$token = substr($request->header('Authorization'), 7);
abort_if(hash_equals($story->bearcap_token, $token) === false, 404); return PollService::storyResults($sid);
abort_if($story->created_at->lt(now()->subMinutes(20)), 404); }
$fractal = new Manager(); public function getActivityObject(Request $request, $username, $id)
$fractal->setSerializer(new ArraySerializer()); {
$resource = new Item($story, new StoryVerb()); abort_if(!config_cache('instance.stories.enabled'), 404);
$res = $fractal->createData($resource)->toArray();
return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES); if(!$request->wantsJson()) {
} return redirect('/stories/' . $username);
}
public function showSystemStory()
{ abort_if(!$request->hasHeader('Authorization'), 404);
// return view('stories.system');
} $profile = Profile::whereUsername($username)->whereNull('domain')->firstOrFail();
$story = Story::whereActive(true)->whereProfileId($profile->id)->findOrFail($id);
abort_if($story->bearcap_token == null, 404);
abort_if(now()->gt($story->expires_at), 404);
$token = substr($request->header('Authorization'), 7);
abort_if(hash_equals($story->bearcap_token, $token) === false, 404);
abort_if($story->created_at->lt(now()->subMinutes(20)), 404);
$fractal = new Manager();
$fractal->setSerializer(new ArraySerializer());
$resource = new Item($story, new StoryVerb());
$res = $fractal->createData($resource)->toArray();
return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
}
public function showSystemStory()
{
// return view('stories.system');
}
} }

Loading…
Cancel
Save