Update stories config, use config_cache

This commit is contained in:
Daniel Supernault 2024-03-12 04:15:05 -06:00
parent ce228f7fa4
commit d1adb109de
No known key found for this signature in database
GPG key ID: 23740873EE6F76A1
4 changed files with 223 additions and 217 deletions

View file

@ -2,54 +2,56 @@
namespace App\Http\Controllers\Stories; namespace App\Http\Controllers\Stories;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Storage;
use App\Models\Conversation;
use App\DirectMessage; use App\DirectMessage;
use App\Notification; use App\Http\Controllers\Controller;
use App\Story; use App\Http\Resources\StoryView as StoryViewResource;
use App\Status;
use App\StoryView;
use App\Jobs\StoryPipeline\StoryDelete; use App\Jobs\StoryPipeline\StoryDelete;
use App\Jobs\StoryPipeline\StoryFanout; use App\Jobs\StoryPipeline\StoryFanout;
use App\Jobs\StoryPipeline\StoryReplyDeliver; use App\Jobs\StoryPipeline\StoryReplyDeliver;
use App\Jobs\StoryPipeline\StoryViewDeliver; use App\Jobs\StoryPipeline\StoryViewDeliver;
use App\Models\Conversation;
use App\Notification;
use App\Services\AccountService; use App\Services\AccountService;
use App\Services\MediaPathService; use App\Services\MediaPathService;
use App\Services\StoryService; use App\Services\StoryService;
use App\Http\Resources\StoryView as StoryViewResource; use App\Status;
use App\Story;
use App\StoryView;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
class StoryApiV1Controller extends Controller class StoryApiV1Controller extends Controller
{ {
const RECENT_KEY = 'pf:stories:recent-by-id:'; const RECENT_KEY = 'pf:stories:recent-by-id:';
const RECENT_TTL = 300; const RECENT_TTL = 300;
public function carousel(Request $request) public function carousel(Request $request)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
$pid = $request->user()->profile_id; $pid = $request->user()->profile_id;
if(config('database.default') == 'pgsql') { if (config('database.default') == 'pgsql') {
$s = Cache::remember(self::RECENT_KEY . $pid, self::RECENT_TTL, function() use($pid) { $s = Cache::remember(self::RECENT_KEY.$pid, self::RECENT_TTL, function () use ($pid) {
return Story::select('stories.*', 'followers.following_id') return Story::select('stories.*', 'followers.following_id')
->leftJoin('followers', 'followers.following_id', 'stories.profile_id') ->leftJoin('followers', 'followers.following_id', 'stories.profile_id')
->where('followers.profile_id', $pid) ->where('followers.profile_id', $pid)
->where('stories.active', true) ->where('stories.active', true)
->map(function($s) { ->map(function ($s) {
$r = new \StdClass; $r = new \StdClass;
$r->id = $s->id; $r->id = $s->id;
$r->profile_id = $s->profile_id; $r->profile_id = $s->profile_id;
$r->type = $s->type; $r->type = $s->type;
$r->path = $s->path; $r->path = $s->path;
return $r; return $r;
}) })
->unique('profile_id'); ->unique('profile_id');
}); });
} else { } else {
$s = Cache::remember(self::RECENT_KEY . $pid, self::RECENT_TTL, function() use($pid) { $s = Cache::remember(self::RECENT_KEY.$pid, self::RECENT_TTL, function () use ($pid) {
return Story::select('stories.*', 'followers.following_id') return Story::select('stories.*', 'followers.following_id')
->leftJoin('followers', 'followers.following_id', 'stories.profile_id') ->leftJoin('followers', 'followers.following_id', 'stories.profile_id')
->where('followers.profile_id', $pid) ->where('followers.profile_id', $pid)
@ -59,9 +61,9 @@ class StoryApiV1Controller extends Controller
}); });
} }
$nodes = $s->map(function($s) use($pid) { $nodes = $s->map(function ($s) use ($pid) {
$profile = AccountService::get($s->profile_id, true); $profile = AccountService::get($s->profile_id, true);
if(!$profile || !isset($profile['id'])) { if (! $profile || ! isset($profile['id'])) {
return false; return false;
} }
@ -72,50 +74,51 @@ class StoryApiV1Controller extends Controller
'src' => url(Storage::url($s->path)), 'src' => url(Storage::url($s->path)),
'duration' => $s->duration ?? 3, 'duration' => $s->duration ?? 3,
'seen' => StoryService::hasSeen($pid, $s->id), 'seen' => StoryService::hasSeen($pid, $s->id),
'created_at' => $s->created_at->format('c') 'created_at' => $s->created_at->format('c'),
]; ];
}) })
->filter() ->filter()
->groupBy('pid') ->groupBy('pid')
->map(function($item) use($pid) { ->map(function ($item) use ($pid) {
$profile = AccountService::get($item[0]['pid'], true); $profile = AccountService::get($item[0]['pid'], true);
$url = $profile['local'] ? url("/stories/{$profile['username']}") : $url = $profile['local'] ? url("/stories/{$profile['username']}") :
url("/i/rs/{$profile['id']}"); url("/i/rs/{$profile['id']}");
return [
'id' => 'pfs:' . $profile['id'], return [
'user' => [ 'id' => 'pfs:'.$profile['id'],
'id' => (string) $profile['id'], 'user' => [
'username' => $profile['username'], 'id' => (string) $profile['id'],
'username_acct' => $profile['acct'], 'username' => $profile['username'],
'avatar' => $profile['avatar'], 'username_acct' => $profile['acct'],
'local' => $profile['local'], 'avatar' => $profile['avatar'],
'is_author' => $profile['id'] == $pid 'local' => $profile['local'],
], 'is_author' => $profile['id'] == $pid,
'nodes' => $item, ],
'url' => $url, 'nodes' => $item,
'seen' => StoryService::hasSeen($pid, StoryService::latest($profile['id'])), 'url' => $url,
]; 'seen' => StoryService::hasSeen($pid, StoryService::latest($profile['id'])),
}) ];
->sortBy('seen') })
->values(); ->sortBy('seen')
->values();
$res = [ $res = [
'self' => [], 'self' => [],
'nodes' => $nodes, 'nodes' => $nodes,
]; ];
if(Story::whereProfileId($pid)->whereActive(true)->exists()) { if (Story::whereProfileId($pid)->whereActive(true)->exists()) {
$selfStories = Story::whereProfileId($pid) $selfStories = Story::whereProfileId($pid)
->whereActive(true) ->whereActive(true)
->get() ->get()
->map(function($s) use($pid) { ->map(function ($s) {
return [ return [
'id' => (string) $s->id, 'id' => (string) $s->id,
'type' => $s->type, 'type' => $s->type,
'src' => url(Storage::url($s->path)), 'src' => url(Storage::url($s->path)),
'duration' => $s->duration, 'duration' => $s->duration,
'seen' => true, 'seen' => true,
'created_at' => $s->created_at->format('c') 'created_at' => $s->created_at->format('c'),
]; ];
}) })
->sortBy('id') ->sortBy('id')
@ -127,38 +130,40 @@ class StoryApiV1Controller extends Controller
'username' => $selfProfile['acct'], 'username' => $selfProfile['acct'],
'avatar' => $selfProfile['avatar'], 'avatar' => $selfProfile['avatar'],
'local' => $selfProfile['local'], 'local' => $selfProfile['local'],
'is_author' => true 'is_author' => true,
], ],
'nodes' => $selfStories, 'nodes' => $selfStories,
]; ];
} }
return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
return response()->json($res, 200, [], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
} }
public function selfCarousel(Request $request) public function selfCarousel(Request $request)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
$pid = $request->user()->profile_id; $pid = $request->user()->profile_id;
if(config('database.default') == 'pgsql') { if (config('database.default') == 'pgsql') {
$s = Cache::remember(self::RECENT_KEY . $pid, self::RECENT_TTL, function() use($pid) { $s = Cache::remember(self::RECENT_KEY.$pid, self::RECENT_TTL, function () use ($pid) {
return Story::select('stories.*', 'followers.following_id') return Story::select('stories.*', 'followers.following_id')
->leftJoin('followers', 'followers.following_id', 'stories.profile_id') ->leftJoin('followers', 'followers.following_id', 'stories.profile_id')
->where('followers.profile_id', $pid) ->where('followers.profile_id', $pid)
->where('stories.active', true) ->where('stories.active', true)
->map(function($s) { ->map(function ($s) {
$r = new \StdClass; $r = new \StdClass;
$r->id = $s->id; $r->id = $s->id;
$r->profile_id = $s->profile_id; $r->profile_id = $s->profile_id;
$r->type = $s->type; $r->type = $s->type;
$r->path = $s->path; $r->path = $s->path;
return $r; return $r;
}) })
->unique('profile_id'); ->unique('profile_id');
}); });
} else { } else {
$s = Cache::remember(self::RECENT_KEY . $pid, self::RECENT_TTL, function() use($pid) { $s = Cache::remember(self::RECENT_KEY.$pid, self::RECENT_TTL, function () use ($pid) {
return Story::select('stories.*', 'followers.following_id') return Story::select('stories.*', 'followers.following_id')
->leftJoin('followers', 'followers.following_id', 'stories.profile_id') ->leftJoin('followers', 'followers.following_id', 'stories.profile_id')
->where('followers.profile_id', $pid) ->where('followers.profile_id', $pid)
@ -168,9 +173,9 @@ class StoryApiV1Controller extends Controller
}); });
} }
$nodes = $s->map(function($s) use($pid) { $nodes = $s->map(function ($s) use ($pid) {
$profile = AccountService::get($s->profile_id, true); $profile = AccountService::get($s->profile_id, true);
if(!$profile || !isset($profile['id'])) { if (! $profile || ! isset($profile['id'])) {
return false; return false;
} }
@ -181,32 +186,33 @@ class StoryApiV1Controller extends Controller
'src' => url(Storage::url($s->path)), 'src' => url(Storage::url($s->path)),
'duration' => $s->duration ?? 3, 'duration' => $s->duration ?? 3,
'seen' => StoryService::hasSeen($pid, $s->id), 'seen' => StoryService::hasSeen($pid, $s->id),
'created_at' => $s->created_at->format('c') 'created_at' => $s->created_at->format('c'),
]; ];
}) })
->filter() ->filter()
->groupBy('pid') ->groupBy('pid')
->map(function($item) use($pid) { ->map(function ($item) use ($pid) {
$profile = AccountService::get($item[0]['pid'], true); $profile = AccountService::get($item[0]['pid'], true);
$url = $profile['local'] ? url("/stories/{$profile['username']}") : $url = $profile['local'] ? url("/stories/{$profile['username']}") :
url("/i/rs/{$profile['id']}"); url("/i/rs/{$profile['id']}");
return [
'id' => 'pfs:' . $profile['id'], return [
'user' => [ 'id' => 'pfs:'.$profile['id'],
'id' => (string) $profile['id'], 'user' => [
'username' => $profile['username'], 'id' => (string) $profile['id'],
'username_acct' => $profile['acct'], 'username' => $profile['username'],
'avatar' => $profile['avatar'], 'username_acct' => $profile['acct'],
'local' => $profile['local'], 'avatar' => $profile['avatar'],
'is_author' => $profile['id'] == $pid 'local' => $profile['local'],
], 'is_author' => $profile['id'] == $pid,
'nodes' => $item, ],
'url' => $url, 'nodes' => $item,
'seen' => StoryService::hasSeen($pid, StoryService::latest($profile['id'])), 'url' => $url,
]; 'seen' => StoryService::hasSeen($pid, StoryService::latest($profile['id'])),
}) ];
->sortBy('seen') })
->values(); ->sortBy('seen')
->values();
$selfProfile = AccountService::get($pid, true); $selfProfile = AccountService::get($pid, true);
$res = [ $res = [
@ -216,7 +222,7 @@ class StoryApiV1Controller extends Controller
'username' => $selfProfile['acct'], 'username' => $selfProfile['acct'],
'avatar' => $selfProfile['avatar'], 'avatar' => $selfProfile['avatar'],
'local' => $selfProfile['local'], 'local' => $selfProfile['local'],
'is_author' => true 'is_author' => true,
], ],
'nodes' => [], 'nodes' => [],
@ -224,40 +230,41 @@ class StoryApiV1Controller extends Controller
'nodes' => $nodes, 'nodes' => $nodes,
]; ];
if(Story::whereProfileId($pid)->whereActive(true)->exists()) { if (Story::whereProfileId($pid)->whereActive(true)->exists()) {
$selfStories = Story::whereProfileId($pid) $selfStories = Story::whereProfileId($pid)
->whereActive(true) ->whereActive(true)
->get() ->get()
->map(function($s) use($pid) { ->map(function ($s) {
return [ return [
'id' => (string) $s->id, 'id' => (string) $s->id,
'type' => $s->type, 'type' => $s->type,
'src' => url(Storage::url($s->path)), 'src' => url(Storage::url($s->path)),
'duration' => $s->duration, 'duration' => $s->duration,
'seen' => true, 'seen' => true,
'created_at' => $s->created_at->format('c') 'created_at' => $s->created_at->format('c'),
]; ];
}) })
->sortBy('id') ->sortBy('id')
->values(); ->values();
$res['self']['nodes'] = $selfStories; $res['self']['nodes'] = $selfStories;
} }
return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
return response()->json($res, 200, [], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
} }
public function add(Request $request) public function add(Request $request)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(! (bool) 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'),
]; ];
}, },
'duration' => 'sometimes|integer|min:0|max:30' 'duration' => 'sometimes|integer|min:0|max:30',
]); ]);
$user = $request->user(); $user = $request->user();
@ -267,7 +274,7 @@ class StoryApiV1Controller extends Controller
->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.');
} }
@ -277,7 +284,7 @@ class StoryApiV1Controller extends Controller
$story = new Story(); $story = new Story();
$story->duration = $request->input('duration', 3); $story->duration = $request->input('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;
@ -290,10 +297,10 @@ class StoryApiV1Controller extends Controller
$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,
]; ];
return $res; return $res;
@ -301,13 +308,13 @@ class StoryApiV1Controller extends Controller
public function publish(Request $request) public function publish(Request $request)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
$this->validate($request, [ $this->validate($request, [
'media_id' => 'required', 'media_id' => 'required',
'duration' => 'required|integer|min:0|max:30', 'duration' => 'required|integer|min:0|max:30',
'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');
@ -327,13 +334,13 @@ class StoryApiV1Controller extends Controller
return [ return [
'code' => 200, 'code' => 200,
'msg' => 'Successfully published', 'msg' => 'Successfully published',
]; ];
} }
public function delete(Request $request, $id) public function delete(Request $request, $id)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
$user = $request->user(); $user = $request->user();
@ -346,16 +353,16 @@ class StoryApiV1Controller extends Controller
return [ return [
'code' => 200, 'code' => 200,
'msg' => 'Successfully deleted' 'msg' => 'Successfully deleted',
]; ];
} }
public function viewed(Request $request) public function viewed(Request $request)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
$this->validate($request, [ $this->validate($request, [
'id' => 'required|min:1', 'id' => 'required|min:1',
]); ]);
$id = $request->input('id'); $id = $request->input('id');
@ -367,44 +374,45 @@ class StoryApiV1Controller extends Controller
$profile = $story->profile; $profile = $story->profile;
if($story->profile_id == $authed->id) { if ($story->profile_id == $authed->id) {
return []; return [];
} }
$publicOnly = (bool) $profile->followedBy($authed); $publicOnly = (bool) $profile->followedBy($authed);
abort_if(!$publicOnly, 403); abort_if(! $publicOnly, 403);
$v = StoryView::firstOrCreate([ $v = StoryView::firstOrCreate([
'story_id' => $id, 'story_id' => $id,
'profile_id' => $authed->id 'profile_id' => $authed->id,
]); ]);
if($v->wasRecentlyCreated) { if ($v->wasRecentlyCreated) {
Story::findOrFail($story->id)->increment('view_count'); Story::findOrFail($story->id)->increment('view_count');
if($story->local == false) { if ($story->local == false) {
StoryViewDeliver::dispatch($story, $authed)->onQueue('story'); StoryViewDeliver::dispatch($story, $authed)->onQueue('story');
} }
} }
Cache::forget('stories:recent:by_id:' . $authed->id); Cache::forget('stories:recent:by_id:'.$authed->id);
StoryService::addSeen($authed->id, $story->id); StoryService::addSeen($authed->id, $story->id);
return ['code' => 200]; return ['code' => 200];
} }
public function comment(Request $request) public function comment(Request $request)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
$this->validate($request, [ $this->validate($request, [
'sid' => 'required', 'sid' => 'required',
'caption' => 'required|string' 'caption' => 'required|string',
]); ]);
$pid = $request->user()->profile_id; $pid = $request->user()->profile_id;
$text = $request->input('caption'); $text = $request->input('caption');
$story = Story::findOrFail($request->input('sid')); $story = Story::findOrFail($request->input('sid'));
abort_if(!$story->can_reply, 422); abort_if(! $story->can_reply, 422);
$status = new Status; $status = new Status;
$status->type = 'story:reply'; $status->type = 'story:reply';
@ -415,7 +423,7 @@ class StoryApiV1Controller extends Controller
$status->visibility = 'direct'; $status->visibility = 'direct';
$status->in_reply_to_profile_id = $story->profile_id; $status->in_reply_to_profile_id = $story->profile_id;
$status->entities = json_encode([ $status->entities = json_encode([
'story_id' => $story->id 'story_id' => $story->id,
]); ]);
$status->save(); $status->save();
@ -429,24 +437,24 @@ class StoryApiV1Controller extends Controller
'story_actor_username' => $request->user()->username, 'story_actor_username' => $request->user()->username,
'story_id' => $story->id, 'story_id' => $story->id,
'story_media_url' => url(Storage::url($story->path)), 'story_media_url' => url(Storage::url($story->path)),
'caption' => $text 'caption' => $text,
]); ]);
$dm->save(); $dm->save();
Conversation::updateOrInsert( Conversation::updateOrInsert(
[ [
'to_id' => $story->profile_id, 'to_id' => $story->profile_id,
'from_id' => $pid 'from_id' => $pid,
], ],
[ [
'type' => 'story:comment', 'type' => 'story:comment',
'status_id' => $status->id, 'status_id' => $status->id,
'dm_id' => $dm->id, 'dm_id' => $dm->id,
'is_hidden' => false 'is_hidden' => false,
] ]
); );
if($story->local) { if ($story->local) {
$n = new Notification; $n = new Notification;
$n->profile_id = $dm->to_id; $n->profile_id = $dm->to_id;
$n->actor_id = $dm->from_id; $n->actor_id = $dm->from_id;
@ -460,33 +468,35 @@ class StoryApiV1Controller extends Controller
return [ return [
'code' => 200, 'code' => 200,
'msg' => 'Sent!' 'msg' => 'Sent!',
]; ];
} }
protected function storeMedia($photo, $user) protected function storeMedia($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());
return $path; return $path;
} }
public function viewers(Request $request) public function viewers(Request $request)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
$this->validate($request, [ $this->validate($request, [
'sid' => 'required|string|min:1|max:50' 'sid' => 'required|string|min:1|max:50',
]); ]);
$pid = $request->user()->profile_id; $pid = $request->user()->profile_id;

View file

@ -2,59 +2,52 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use App\Media;
use App\Profile;
use App\Report;
use App\DirectMessage; use App\DirectMessage;
use App\Notification; use App\Jobs\StoryPipeline\StoryDelete;
use App\Status; use App\Jobs\StoryPipeline\StoryFanout;
use App\Story;
use App\StoryView;
use App\Models\Poll;
use App\Models\PollVote;
use App\Services\ProfileService;
use App\Services\StoryService;
use Cache, Storage;
use Image as Intervention;
use App\Services\FollowerService;
use App\Services\MediaPathService;
use FFMpeg;
use FFMpeg\Coordinate\Dimension;
use FFMpeg\Format\Video\X264;
use App\Jobs\StoryPipeline\StoryReactionDeliver; use App\Jobs\StoryPipeline\StoryReactionDeliver;
use App\Jobs\StoryPipeline\StoryReplyDeliver; use App\Jobs\StoryPipeline\StoryReplyDeliver;
use App\Jobs\StoryPipeline\StoryFanout;
use App\Jobs\StoryPipeline\StoryDelete;
use ImageOptimizer;
use App\Models\Conversation; use App\Models\Conversation;
use App\Models\Poll;
use App\Models\PollVote;
use App\Notification;
use App\Report;
use App\Services\FollowerService;
use App\Services\MediaPathService;
use App\Services\StoryService;
use App\Services\UserRoleService; use App\Services\UserRoleService;
use App\Status;
use App\Story;
use FFMpeg;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Image as Intervention;
use Storage;
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(! (bool) 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'); 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.');
} }
@ -64,7 +57,7 @@ class StoryComposeController extends Controller
$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;
@ -77,21 +70,22 @@ class StoryComposeController extends Controller
$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);
} }
} }
@ -102,37 +96,39 @@ class StoryComposeController extends Controller
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(! (bool) 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();
@ -144,13 +140,13 @@ class StoryComposeController extends Controller
$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) {
@ -161,24 +157,24 @@ class StoryComposeController extends Controller
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(! (bool) 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();
abort_if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id), 403, 'Invalid permissions for this action'); 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);
@ -194,13 +190,13 @@ class StoryComposeController extends Controller
return [ return [
'code' => 200, 'code' => 200,
'msg' => 'Successfully published', '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(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
$user = $request->user(); $user = $request->user();
@ -213,40 +209,40 @@ class StoryComposeController extends Controller
return [ return [
'code' => 200, 'code' => 200,
'msg' => 'Successfully deleted' '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(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
$user = $request->user(); $user = $request->user();
abort_if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id), 403, 'Invalid permissions for this action'); 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');
} }
public function createPoll(Request $request) public function createPoll(Request $request)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
abort_if(!config_cache('instance.polls.enabled'), 404); abort_if(! config_cache('instance.polls.enabled'), 404);
return $request->all(); return $request->all();
} }
public function publishStoryPoll(Request $request) public function publishStoryPoll(Request $request)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
$this->validate($request, [ $this->validate($request, [
'question' => 'required|string|min:6|max:140', 'question' => 'required|string|min:6|max:140',
'options' => 'required|array|min:2|max:4', 'options' => 'required|array|min:2|max:4',
'can_reply' => 'required|boolean', 'can_reply' => 'required|boolean',
'can_react' => 'required|boolean' 'can_react' => 'required|boolean',
]); ]);
$user = $request->user(); $user = $request->user();
abort_if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id), 403, 'Invalid permissions for this action'); 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)
@ -254,7 +250,7 @@ class StoryComposeController extends Controller
->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.');
} }
@ -262,7 +258,7 @@ class StoryComposeController extends Controller
$story->type = 'poll'; $story->type = 'poll';
$story->story = json_encode([ $story->story = json_encode([
'question' => $request->input('question'), 'question' => $request->input('question'),
'options' => $request->input('options') 'options' => $request->input('options'),
]); ]);
$story->public = false; $story->public = false;
$story->local = true; $story->local = true;
@ -278,7 +274,7 @@ class StoryComposeController extends Controller
$poll->profile_id = $pid; $poll->profile_id = $pid;
$poll->poll_options = $request->input('options'); $poll->poll_options = $request->input('options');
$poll->expires_at = $story->expires_at; $poll->expires_at = $story->expires_at;
$poll->cached_tallies = collect($poll->poll_options)->map(function($o) { $poll->cached_tallies = collect($poll->poll_options)->map(function ($o) {
return 0; return 0;
})->toArray(); })->toArray();
$poll->save(); $poll->save();
@ -290,23 +286,23 @@ class StoryComposeController extends Controller
return [ return [
'code' => 200, 'code' => 200,
'msg' => 'Successfully published', 'msg' => 'Successfully published',
]; ];
} }
public function storyPollVote(Request $request) public function storyPollVote(Request $request)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
$this->validate($request, [ $this->validate($request, [
'sid' => 'required', 'sid' => 'required',
'ci' => 'required|integer|min:0|max:3' 'ci' => 'required|integer|min:0|max:3',
]); ]);
$pid = $request->user()->profile_id; $pid = $request->user()->profile_id;
$ci = $request->input('ci'); $ci = $request->input('ci');
$story = Story::findOrFail($request->input('sid')); $story = Story::findOrFail($request->input('sid'));
abort_if(!FollowerService::follows($pid, $story->profile_id), 403); abort_if(! FollowerService::follows($pid, $story->profile_id), 403);
$poll = Poll::whereStoryId($story->id)->firstOrFail(); $poll = Poll::whereStoryId($story->id)->firstOrFail();
$vote = new PollVote; $vote = new PollVote;
@ -318,7 +314,7 @@ class StoryComposeController extends Controller
$vote->save(); $vote->save();
$poll->votes_count = $poll->votes_count + 1; $poll->votes_count = $poll->votes_count + 1;
$poll->cached_tallies = collect($poll->getTallies())->map(function($tally, $key) use($ci) { $poll->cached_tallies = collect($poll->getTallies())->map(function ($tally, $key) use ($ci) {
return $ci == $key ? $tally + 1 : $tally; return $ci == $key ? $tally + 1 : $tally;
})->toArray(); })->toArray();
$poll->save(); $poll->save();
@ -328,15 +324,15 @@ class StoryComposeController extends Controller
public function storeReport(Request $request) public function storeReport(Request $request)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
$this->validate($request, [ $this->validate($request, [
'type' => 'required|alpha_dash', 'type' => 'required|alpha_dash',
'id' => 'required|integer|min:1', 'id' => 'required|integer|min:1',
]); ]);
$user = $request->user(); $user = $request->user();
abort_if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id), 403, 'Invalid permissions for this action'); 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');
@ -353,24 +349,24 @@ class StoryComposeController extends Controller
'copyright', 'copyright',
'impersonation', 'impersonation',
'scam', 'scam',
'terrorism' 'terrorism',
]; ];
abort_if(!in_array($type, $types), 422, 'Invalid story report type'); abort_if(! in_array($type, $types), 422, 'Invalid story report type');
$story = Story::findOrFail($sid); $story = Story::findOrFail($sid);
abort_if($story->profile_id == $pid, 422, 'Cannot report your own story'); abort_if($story->profile_id == $pid, 422, 'Cannot report your own story');
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);
} }
@ -389,18 +385,18 @@ class StoryComposeController extends Controller
public function react(Request $request) public function react(Request $request)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(! (bool) 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(); $user = $request->user();
abort_if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id), 403, 'Invalid permissions for this action'); 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);
abort_if(StoryService::reactCounter($story->id, $pid) >= 5, 422, 'You have already reacted to this story'); abort_if(StoryService::reactCounter($story->id, $pid) >= 5, 422, 'You have already reacted to this story');
$status = new Status; $status = new Status;
@ -413,7 +409,7 @@ class StoryComposeController extends Controller
$status->in_reply_to_profile_id = $story->profile_id; $status->in_reply_to_profile_id = $story->profile_id;
$status->entities = json_encode([ $status->entities = json_encode([
'story_id' => $story->id, 'story_id' => $story->id,
'reaction' => $text 'reaction' => $text,
]); ]);
$status->save(); $status->save();
@ -427,24 +423,24 @@ class StoryComposeController extends Controller
'story_actor_username' => $request->user()->username, 'story_actor_username' => $request->user()->username,
'story_id' => $story->id, 'story_id' => $story->id,
'story_media_url' => url(Storage::url($story->path)), 'story_media_url' => url(Storage::url($story->path)),
'reaction' => $text 'reaction' => $text,
]); ]);
$dm->save(); $dm->save();
Conversation::updateOrInsert( Conversation::updateOrInsert(
[ [
'to_id' => $story->profile_id, 'to_id' => $story->profile_id,
'from_id' => $pid 'from_id' => $pid,
], ],
[ [
'type' => 'story:react', 'type' => 'story:react',
'status_id' => $status->id, 'status_id' => $status->id,
'dm_id' => $dm->id, 'dm_id' => $dm->id,
'is_hidden' => false 'is_hidden' => false,
] ]
); );
if($story->local) { if ($story->local) {
// generate notification // generate notification
$n = new Notification; $n = new Notification;
$n->profile_id = $dm->to_id; $n->profile_id = $dm->to_id;
@ -464,18 +460,18 @@ class StoryComposeController extends Controller
public function comment(Request $request) public function comment(Request $request)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
$this->validate($request, [ $this->validate($request, [
'sid' => 'required', 'sid' => 'required',
'caption' => 'required|string' 'caption' => 'required|string',
]); ]);
$pid = $request->user()->profile_id; $pid = $request->user()->profile_id;
$text = $request->input('caption'); $text = $request->input('caption');
$user = $request->user(); $user = $request->user();
abort_if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id), 403, 'Invalid permissions for this action'); 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);
$status = new Status; $status = new Status;
$status->type = 'story:reply'; $status->type = 'story:reply';
@ -486,7 +482,7 @@ class StoryComposeController extends Controller
$status->visibility = 'direct'; $status->visibility = 'direct';
$status->in_reply_to_profile_id = $story->profile_id; $status->in_reply_to_profile_id = $story->profile_id;
$status->entities = json_encode([ $status->entities = json_encode([
'story_id' => $story->id 'story_id' => $story->id,
]); ]);
$status->save(); $status->save();
@ -500,24 +496,24 @@ class StoryComposeController extends Controller
'story_actor_username' => $request->user()->username, 'story_actor_username' => $request->user()->username,
'story_id' => $story->id, 'story_id' => $story->id,
'story_media_url' => url(Storage::url($story->path)), 'story_media_url' => url(Storage::url($story->path)),
'caption' => $text 'caption' => $text,
]); ]);
$dm->save(); $dm->save();
Conversation::updateOrInsert( Conversation::updateOrInsert(
[ [
'to_id' => $story->profile_id, 'to_id' => $story->profile_id,
'from_id' => $pid 'from_id' => $pid,
], ],
[ [
'type' => 'story:comment', 'type' => 'story:comment',
'status_id' => $status->id, 'status_id' => $status->id,
'dm_id' => $dm->id, 'dm_id' => $dm->id,
'is_hidden' => false 'is_hidden' => false,
] ]
); );
if($story->local) { if ($story->local) {
// generate notification // generate notification
$n = new Notification; $n = new Notification;
$n->profile_id = $dm->to_id; $n->profile_id = $dm->to_id;

View file

@ -545,7 +545,7 @@
<tr> <tr>
<td><span class="badge badge-primary">INSTANCE</span></td> <td><span class="badge badge-primary">INSTANCE</span></td>
<td><strong>STORIES_ENABLED</strong></td> <td><strong>STORIES_ENABLED</strong></td>
<td><span>{{config_cache('instance.stories.enabled') ? '✅ true' : '❌ false' }}</span></td> <td><span>{{(bool) config_cache('instance.stories.enabled') ? '✅ true' : '❌ false' }}</span></td>
</tr> </tr>
<tr> <tr>
<td><span class="badge badge-primary">INSTANCE</span></td> <td><span class="badge badge-primary">INSTANCE</span></td>

View file

@ -105,7 +105,7 @@
{{__('navmenu.discover')}} {{__('navmenu.discover')}}
</a> </a>
@if(config_cache('instance.stories.enabled')) @if((bool) config_cache('instance.stories.enabled'))
<a class="dropdown-item lead" href="/i/stories/new"> <a class="dropdown-item lead" href="/i/stories/new">
<span style="width: 50px;margin-right:14px;"> <span style="width: 50px;margin-right:14px;">
<span class="fal fa-history text-lighter fa-lg"></span> <span class="fal fa-history text-lighter fa-lg"></span>