Update StoryController

This commit is contained in:
Daniel Supernault 2020-01-10 19:45:46 -07:00
parent 5a764473f2
commit d403e0c615
No known key found for this signature in database
GPG Key ID: 0DEF1C662C9033F7
1 changed files with 83 additions and 82 deletions

View File

@ -26,7 +26,7 @@ class StoryController extends Controller
abort_if(!config('instance.stories.enabled') || !$request->user(), 404); abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
$this->validate($request, [ $this->validate($request, [
'file.*' => function() { 'file' => function() {
return [ return [
'required', 'required',
'mimes:image/jpeg,image/png', 'mimes:image/jpeg,image/png',
@ -37,35 +37,36 @@ class StoryController extends Controller
$user = $request->user(); $user = $request->user();
if(Story::whereProfileId($user->profile_id)->where('expires_at', '>', now())->count() >= Story::MAX_PER_DAY) { if(Story::whereProfileId($user->profile_id)->where('expires_at', '>', now())->count() >= Story::MAX_PER_DAY) {
abort(400, 'You have reached your limit for new Stories today.'); abort(400, 'You have reached your limit for new Stories today.');
} }
$story = new Story(); $monthHash = substr(hash('sha1', date('Y').date('m')), 0, 12);
$story->duration = 3; $sid = Str::uuid();
$story->profile_id = $user->profile_id; $rid = Str::random(6).'.'.Str::random(9);
$story->expires_at = now()->addHours(24);
$story->save();
$monthHash = substr(hash('sha1', date('Y').date('m')), 0, 12); $photo = $request->file('file');
$rid = Str::random(6).'.'.Str::random(9);
$photo = $request->file('file'); $mimes = explode(',', config('pixelfed.media_types'));
if(in_array($photo->getMimeType(), [
'image/jpeg',
'image/png'
]) == false) {
abort(400, 'Invalid media type');
return;
}
$mimes = explode(',', config('pixelfed.media_types')); $storagePath = "public/_esm.t1/{$monthHash}/{$sid}/{$rid}";
if(in_array($photo->getMimeType(), [
'image/jpeg',
'image/png'
]) == false) {
abort(400, 'Invalid media type');
return;
}
$storagePath = "public/_esm.t1/{$monthHash}/{$story->id}/{$rid}";
$path = $photo->store($storagePath); $path = $photo->store($storagePath);
$story = new Story();
$story->duration = 3;
$story->profile_id = $user->profile_id;
$story->type = 'photo';
$story->mime = $photo->getMimeType();
$story->path = $path; $story->path = $path;
$story->local = true; $story->local = true;
$story->size = $photo->getClientSize();
$story->expires_at = now()->addHours(24); $story->expires_at = now()->addHours(24);
$story->save(); $story->save();
@ -83,7 +84,7 @@ class StoryController extends Controller
$user = $request->user(); $user = $request->user();
$story = Story::whereProfileId($user->profile_id) $story = Story::whereProfileId($user->profile_id)
->findOrFail($id); ->findOrFail($id);
if(Storage::exists($story->path) == true) { if(Storage::exists($story->path) == true) {
Storage::delete($story->path); Storage::delete($story->path);
@ -105,23 +106,23 @@ class StoryController extends Controller
$following = FollowerService::build()->profile($profile)->following(); $following = FollowerService::build()->profile($profile)->following();
$stories = Story::with('profile') $stories = Story::with('profile')
->whereIn('profile_id', $following) ->whereIn('profile_id', $following)
->where('expires_at', '>', now()) ->where('expires_at', '>', now())
->groupBy('profile_id') ->groupBy('profile_id')
->orderByDesc('expires_at') ->orderByDesc('expires_at')
->take(9) ->take(9)
->get() ->get()
->map(function($s, $k) { ->map(function($s, $k) {
return [ return [
'id' => (string) $s->id, 'id' => (string) $s->id,
'photo' => $s->profile->avatarUrl(), 'photo' => $s->profile->avatarUrl(),
'name' => $s->profile->username, 'name' => $s->profile->username,
'link' => $s->profile->url(), 'link' => $s->profile->url(),
'lastUpdated' => (int) $s->created_at->format('U'), 'lastUpdated' => (int) $s->created_at->format('U'),
'seen' => $s->seen(), 'seen' => $s->seen(),
'items' => [], 'items' => [],
'pid' => (string) $s->profile->id 'pid' => (string) $s->profile->id
]; ];
}); });
return response()->json($stories, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES); return response()->json($stories, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
@ -140,26 +141,26 @@ class StoryController extends Controller
} }
$stories = Story::whereProfileId($id) $stories = Story::whereProfileId($id)
->orderBy('expires_at', 'desc') ->orderBy('expires_at', 'desc')
->where('expires_at', '>', now()) ->where('expires_at', '>', now())
->when(!$publicOnly, function($query, $publicOnly) { ->when(!$publicOnly, function($query, $publicOnly) {
return $query->wherePublic(true); return $query->wherePublic(true);
}) })
->get() ->get()
->map(function($s, $k) { ->map(function($s, $k) {
return [ return [
'id' => (string) $s->id, 'id' => (string) $s->id,
'type' => 'photo', 'type' => 'photo',
'length' => 3, 'length' => 3,
'src' => url(Storage::url($s->path)), 'src' => url(Storage::url($s->path)),
'preview' => null, 'preview' => null,
'link' => null, 'link' => null,
'linkText' => null, 'linkText' => null,
'time' => $s->created_at->format('U'), 'time' => $s->created_at->format('U'),
'expires_at' => (int) $s->expires_at->format('U'), 'expires_at' => (int) $s->expires_at->format('U'),
'seen' => $s->seen() 'seen' => $s->seen()
]; ];
})->toArray(); })->toArray();
return response()->json($stories, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES); return response()->json($stories, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
} }
@ -177,26 +178,26 @@ class StoryController extends Controller
} }
$stories = Story::whereProfileId($profile->id) $stories = Story::whereProfileId($profile->id)
->orderBy('expires_at') ->orderBy('expires_at')
->where('expires_at', '>', now()) ->where('expires_at', '>', now())
->when(!$publicOnly, function($query, $publicOnly) { ->when(!$publicOnly, function($query, $publicOnly) {
return $query->wherePublic(true); return $query->wherePublic(true);
}) })
->get() ->get()
->map(function($s, $k) { ->map(function($s, $k) {
return [ return [
'id' => $s->id, 'id' => $s->id,
'type' => 'photo', 'type' => 'photo',
'length' => 3, 'length' => 3,
'src' => url(Storage::url($s->path)), 'src' => url(Storage::url($s->path)),
'preview' => null, 'preview' => null,
'link' => null, 'link' => null,
'linkText' => null, 'linkText' => null,
'time' => $s->created_at->format('U'), 'time' => $s->created_at->format('U'),
'expires_at' => (int) $s->expires_at->format('U'), 'expires_at' => (int) $s->expires_at->format('U'),
'seen' => $s->seen() 'seen' => $s->seen()
]; ];
})->toArray(); })->toArray();
if(count($stories) == 0) { if(count($stories) == 0) {
return []; return [];
} }
@ -241,8 +242,8 @@ class StoryController extends Controller
abort_if(!config('instance.stories.enabled'), 404); abort_if(!config('instance.stories.enabled'), 404);
$res = (bool) Story::whereProfileId($id) $res = (bool) Story::whereProfileId($id)
->where('expires_at', '>', now()) ->where('expires_at', '>', now())
->count(); ->count();
return response()->json($res); return response()->json($res);
} }