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,34 +2,35 @@
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\Notification;
use App\Story;
use App\Status;
use App\StoryView;
use App\Http\Controllers\Controller;
use App\Http\Resources\StoryView as StoryViewResource;
use App\Jobs\StoryPipeline\StoryDelete;
use App\Jobs\StoryPipeline\StoryFanout;
use App\Jobs\StoryPipeline\StoryReplyDeliver;
use App\Jobs\StoryPipeline\StoryViewDeliver;
use App\Models\Conversation;
use App\Notification;
use App\Services\AccountService;
use App\Services\MediaPathService;
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
{
const RECENT_KEY = 'pf:stories:recent-by-id:';
const RECENT_TTL = 300;
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;
if (config('database.default') == 'pgsql') {
@ -44,6 +45,7 @@ class StoryApiV1Controller extends Controller
$r->profile_id = $s->profile_id;
$r->type = $s->type;
$r->path = $s->path;
return $r;
})
->unique('profile_id');
@ -72,7 +74,7 @@ class StoryApiV1Controller extends Controller
'src' => url(Storage::url($s->path)),
'duration' => $s->duration ?? 3,
'seen' => StoryService::hasSeen($pid, $s->id),
'created_at' => $s->created_at->format('c')
'created_at' => $s->created_at->format('c'),
];
})
->filter()
@ -81,6 +83,7 @@ class StoryApiV1Controller extends Controller
$profile = AccountService::get($item[0]['pid'], true);
$url = $profile['local'] ? url("/stories/{$profile['username']}") :
url("/i/rs/{$profile['id']}");
return [
'id' => 'pfs:'.$profile['id'],
'user' => [
@ -89,7 +92,7 @@ class StoryApiV1Controller extends Controller
'username_acct' => $profile['acct'],
'avatar' => $profile['avatar'],
'local' => $profile['local'],
'is_author' => $profile['id'] == $pid
'is_author' => $profile['id'] == $pid,
],
'nodes' => $item,
'url' => $url,
@ -108,14 +111,14 @@ class StoryApiV1Controller extends Controller
$selfStories = Story::whereProfileId($pid)
->whereActive(true)
->get()
->map(function($s) use($pid) {
->map(function ($s) {
return [
'id' => (string) $s->id,
'type' => $s->type,
'src' => url(Storage::url($s->path)),
'duration' => $s->duration,
'seen' => true,
'created_at' => $s->created_at->format('c')
'created_at' => $s->created_at->format('c'),
];
})
->sortBy('id')
@ -127,18 +130,19 @@ class StoryApiV1Controller extends Controller
'username' => $selfProfile['acct'],
'avatar' => $selfProfile['avatar'],
'local' => $selfProfile['local'],
'is_author' => true
'is_author' => true,
],
'nodes' => $selfStories,
];
}
return response()->json($res, 200, [], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
}
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;
if (config('database.default') == 'pgsql') {
@ -153,6 +157,7 @@ class StoryApiV1Controller extends Controller
$r->profile_id = $s->profile_id;
$r->type = $s->type;
$r->path = $s->path;
return $r;
})
->unique('profile_id');
@ -181,7 +186,7 @@ class StoryApiV1Controller extends Controller
'src' => url(Storage::url($s->path)),
'duration' => $s->duration ?? 3,
'seen' => StoryService::hasSeen($pid, $s->id),
'created_at' => $s->created_at->format('c')
'created_at' => $s->created_at->format('c'),
];
})
->filter()
@ -190,6 +195,7 @@ class StoryApiV1Controller extends Controller
$profile = AccountService::get($item[0]['pid'], true);
$url = $profile['local'] ? url("/stories/{$profile['username']}") :
url("/i/rs/{$profile['id']}");
return [
'id' => 'pfs:'.$profile['id'],
'user' => [
@ -198,7 +204,7 @@ class StoryApiV1Controller extends Controller
'username_acct' => $profile['acct'],
'avatar' => $profile['avatar'],
'local' => $profile['local'],
'is_author' => $profile['id'] == $pid
'is_author' => $profile['id'] == $pid,
],
'nodes' => $item,
'url' => $url,
@ -216,7 +222,7 @@ class StoryApiV1Controller extends Controller
'username' => $selfProfile['acct'],
'avatar' => $selfProfile['avatar'],
'local' => $selfProfile['local'],
'is_author' => true
'is_author' => true,
],
'nodes' => [],
@ -228,26 +234,27 @@ class StoryApiV1Controller extends Controller
$selfStories = Story::whereProfileId($pid)
->whereActive(true)
->get()
->map(function($s) use($pid) {
->map(function ($s) {
return [
'id' => (string) $s->id,
'type' => $s->type,
'src' => url(Storage::url($s->path)),
'duration' => $s->duration,
'seen' => true,
'created_at' => $s->created_at->format('c')
'created_at' => $s->created_at->format('c'),
];
})
->sortBy('id')
->values();
$res['self']['nodes'] = $selfStories;
}
return response()->json($res, 200, [], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
}
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, [
'file' => function () {
@ -257,7 +264,7 @@ class StoryApiV1Controller extends Controller
'max:'.config_cache('pixelfed.max_photo_size'),
];
},
'duration' => 'sometimes|integer|min:0|max:30'
'duration' => 'sometimes|integer|min:0|max:30',
]);
$user = $request->user();
@ -293,7 +300,7 @@ class StoryApiV1Controller extends Controller
'msg' => 'Successfully added',
'media_id' => (string) $story->id,
'media_url' => url(Storage::url($url)).'?v='.time(),
'media_type' => $story->type
'media_type' => $story->type,
];
return $res;
@ -301,13 +308,13 @@ class StoryApiV1Controller extends Controller
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, [
'media_id' => 'required',
'duration' => 'required|integer|min:0|max:30',
'can_reply' => 'required|boolean',
'can_react' => 'required|boolean'
'can_react' => 'required|boolean',
]);
$id = $request->input('media_id');
@ -333,7 +340,7 @@ class StoryApiV1Controller extends Controller
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();
@ -346,13 +353,13 @@ class StoryApiV1Controller extends Controller
return [
'code' => 200,
'msg' => 'Successfully deleted'
'msg' => 'Successfully deleted',
];
}
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, [
'id' => 'required|min:1',
@ -376,7 +383,7 @@ class StoryApiV1Controller extends Controller
$v = StoryView::firstOrCreate([
'story_id' => $id,
'profile_id' => $authed->id
'profile_id' => $authed->id,
]);
if ($v->wasRecentlyCreated) {
@ -389,15 +396,16 @@ class StoryApiV1Controller extends Controller
Cache::forget('stories:recent:by_id:'.$authed->id);
StoryService::addSeen($authed->id, $story->id);
return ['code' => 200];
}
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, [
'sid' => 'required',
'caption' => 'required|string'
'caption' => 'required|string',
]);
$pid = $request->user()->profile_id;
$text = $request->input('caption');
@ -415,7 +423,7 @@ class StoryApiV1Controller extends Controller
$status->visibility = 'direct';
$status->in_reply_to_profile_id = $story->profile_id;
$status->entities = json_encode([
'story_id' => $story->id
'story_id' => $story->id,
]);
$status->save();
@ -429,20 +437,20 @@ class StoryApiV1Controller extends Controller
'story_actor_username' => $request->user()->username,
'story_id' => $story->id,
'story_media_url' => url(Storage::url($story->path)),
'caption' => $text
'caption' => $text,
]);
$dm->save();
Conversation::updateOrInsert(
[
'to_id' => $story->profile_id,
'from_id' => $pid
'from_id' => $pid,
],
[
'type' => 'story:comment',
'status_id' => $status->id,
'dm_id' => $dm->id,
'is_hidden' => false
'is_hidden' => false,
]
);
@ -460,7 +468,7 @@ class StoryApiV1Controller extends Controller
return [
'code' => 200,
'msg' => 'Sent!'
'msg' => 'Sent!',
];
}
@ -470,23 +478,25 @@ class StoryApiV1Controller extends Controller
if (in_array($photo->getMimeType(), [
'image/jpeg',
'image/png',
'video/mp4'
'video/mp4',
]) == false) {
abort(400, 'Invalid media type');
return;
}
$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());
return $path;
}
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, [
'sid' => 'required|string|min:1|max:50'
'sid' => 'required|string|min:1|max:50',
]);
$pid = $request->user()->profile_id;

View file

@ -2,40 +2,33 @@
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\Notification;
use App\Status;
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\StoryDelete;
use App\Jobs\StoryPipeline\StoryFanout;
use App\Jobs\StoryPipeline\StoryReactionDeliver;
use App\Jobs\StoryPipeline\StoryReplyDeliver;
use App\Jobs\StoryPipeline\StoryFanout;
use App\Jobs\StoryPipeline\StoryDelete;
use ImageOptimizer;
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\Status;
use App\Story;
use FFMpeg;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Image as Intervention;
use Storage;
class StoryComposeController extends Controller
{
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, [
'file' => function () {
@ -80,7 +73,7 @@ class StoryComposeController extends Controller
'msg' => 'Successfully added',
'media_id' => (string) $story->id,
'media_url' => url(Storage::url($url)).'?v='.time(),
'media_type' => $story->type
'media_type' => $story->type,
];
if ($story->type === 'video') {
@ -90,8 +83,9 @@ class StoryComposeController extends Controller
if ($duration > 500) {
Storage::delete($story->path);
$story->delete();
return response()->json([
'message' => 'Video duration cannot exceed 60 seconds'
'message' => 'Video duration cannot exceed 60 seconds',
], 422);
}
}
@ -105,9 +99,10 @@ class StoryComposeController extends Controller
if (in_array($photo->getMimeType(), [
'image/jpeg',
'image/png',
'video/mp4'
'video/mp4',
]) == false) {
abort(400, 'Invalid media type');
return;
}
@ -120,19 +115,20 @@ class StoryComposeController extends Controller
$img->save($fpath, config_cache('pixelfed.image_quality'));
$img->destroy();
}
return $path;
}
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, [
'media_id' => 'required|integer|min:1',
'width' => 'required',
'height' => 'required',
'x' => 'required',
'y' => 'required'
'y' => 'required',
]);
$user = $request->user();
@ -167,13 +163,13 @@ class StoryComposeController extends Controller
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, [
'media_id' => 'required',
'duration' => 'required|integer|min:3|max:120',
'can_reply' => 'required|boolean',
'can_react' => 'required|boolean'
'can_react' => 'required|boolean',
]);
$id = $request->input('media_id');
@ -200,7 +196,7 @@ class StoryComposeController extends Controller
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();
@ -213,13 +209,13 @@ class StoryComposeController extends Controller
return [
'code' => 200,
'msg' => 'Successfully deleted'
'msg' => 'Successfully deleted',
];
}
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();
abort_if($user->has_roles && ! UserRoleService::can('can-use-stories', $user->id), 403, 'Invalid permissions for this action');
@ -228,7 +224,7 @@ class StoryComposeController extends Controller
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);
return $request->all();
@ -236,13 +232,13 @@ class StoryComposeController extends Controller
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, [
'question' => 'required|string|min:6|max:140',
'options' => 'required|array|min:2|max:4',
'can_reply' => 'required|boolean',
'can_react' => 'required|boolean'
'can_react' => 'required|boolean',
]);
$user = $request->user();
@ -262,7 +258,7 @@ class StoryComposeController extends Controller
$story->type = 'poll';
$story->story = json_encode([
'question' => $request->input('question'),
'options' => $request->input('options')
'options' => $request->input('options'),
]);
$story->public = false;
$story->local = true;
@ -296,11 +292,11 @@ class StoryComposeController extends Controller
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, [
'sid' => 'required',
'ci' => 'required|integer|min:0|max:3'
'ci' => 'required|integer|min:0|max:3',
]);
$pid = $request->user()->profile_id;
@ -328,7 +324,7 @@ class StoryComposeController extends Controller
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, [
'type' => 'required|alpha_dash',
@ -353,7 +349,7 @@ class StoryComposeController extends Controller
'copyright',
'impersonation',
'scam',
'terrorism'
'terrorism',
];
abort_if(! in_array($type, $types), 422, 'Invalid story report type');
@ -370,7 +366,7 @@ class StoryComposeController extends Controller
) {
return response()->json(['error' => [
'code' => 409,
'message' => 'Cannot report the same story again'
'message' => 'Cannot report the same story again',
]], 409);
}
@ -389,10 +385,10 @@ class StoryComposeController extends Controller
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, [
'sid' => 'required',
'reaction' => 'required|string'
'reaction' => 'required|string',
]);
$pid = $request->user()->profile_id;
$text = $request->input('reaction');
@ -413,7 +409,7 @@ class StoryComposeController extends Controller
$status->in_reply_to_profile_id = $story->profile_id;
$status->entities = json_encode([
'story_id' => $story->id,
'reaction' => $text
'reaction' => $text,
]);
$status->save();
@ -427,20 +423,20 @@ class StoryComposeController extends Controller
'story_actor_username' => $request->user()->username,
'story_id' => $story->id,
'story_media_url' => url(Storage::url($story->path)),
'reaction' => $text
'reaction' => $text,
]);
$dm->save();
Conversation::updateOrInsert(
[
'to_id' => $story->profile_id,
'from_id' => $pid
'from_id' => $pid,
],
[
'type' => 'story:react',
'status_id' => $status->id,
'dm_id' => $dm->id,
'is_hidden' => false
'is_hidden' => false,
]
);
@ -464,10 +460,10 @@ class StoryComposeController extends Controller
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, [
'sid' => 'required',
'caption' => 'required|string'
'caption' => 'required|string',
]);
$pid = $request->user()->profile_id;
$text = $request->input('caption');
@ -486,7 +482,7 @@ class StoryComposeController extends Controller
$status->visibility = 'direct';
$status->in_reply_to_profile_id = $story->profile_id;
$status->entities = json_encode([
'story_id' => $story->id
'story_id' => $story->id,
]);
$status->save();
@ -500,20 +496,20 @@ class StoryComposeController extends Controller
'story_actor_username' => $request->user()->username,
'story_id' => $story->id,
'story_media_url' => url(Storage::url($story->path)),
'caption' => $text
'caption' => $text,
]);
$dm->save();
Conversation::updateOrInsert(
[
'to_id' => $story->profile_id,
'from_id' => $pid
'from_id' => $pid,
],
[
'type' => 'story:comment',
'status_id' => $status->id,
'dm_id' => $dm->id,
'is_hidden' => false
'is_hidden' => false,
]
);

View file

@ -545,7 +545,7 @@
<tr>
<td><span class="badge badge-primary">INSTANCE</span></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>
<td><span class="badge badge-primary">INSTANCE</span></td>

View file

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