1
0
Fork 0
pixelfed/app/Http/Controllers/StoryController.php

285 lines
7.8 KiB
PHP
Raw Normal View History

2018-08-26 19:21:58 +00:00
<?php
namespace App\Http\Controllers;
2019-01-31 20:28:30 +00:00
use Illuminate\Http\Request;
2020-01-07 06:31:51 +00:00
use Illuminate\Support\Str;
use App\DirectMessage;
use App\Follower;
use App\Notification;
2020-01-07 06:31:51 +00:00
use App\Media;
use App\Profile;
use App\Status;
2020-01-07 06:31:51 +00:00
use App\Story;
use App\StoryView;
use App\Services\PollService;
use App\Services\ProfileService;
2020-01-07 06:31:51 +00:00
use App\Services\StoryService;
use Cache, Storage;
use Image as Intervention;
use App\Services\AccountService;
2020-01-07 06:31:51 +00:00
use App\Services\FollowerService;
use App\Services\MediaPathService;
2021-04-30 05:58:06 +00:00
use FFMpeg;
use FFMpeg\Coordinate\Dimension;
use FFMpeg\Format\Video\X264;
use League\Fractal\Manager;
use League\Fractal\Serializer\ArraySerializer;
use League\Fractal\Resource\Item;
use App\Transformer\ActivityPub\Verb\StoryVerb;
use App\Jobs\StoryPipeline\StoryViewDeliver;
2019-01-31 20:28:30 +00:00
class StoryController extends StoryComposeController
2018-08-26 19:21:58 +00:00
{
public function recent(Request $request)
2020-01-07 06:31:51 +00:00
{
2021-05-12 00:30:32 +00:00
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404);
$pid = $request->user()->profile_id;
if(config('database.default') == 'pgsql') {
$s = Story::select('stories.*', 'followers.following_id')
->leftJoin('followers', 'followers.following_id', 'stories.profile_id')
->where('followers.profile_id', $pid)
->where('stories.active', true)
->get()
->map(function($s) {
$r = new \StdClass;
$r->id = $s->id;
$r->profile_id = $s->profile_id;
return $r;
})
->unique('profile_id');
} else {
$s = Story::select('stories.*', 'followers.following_id')
->leftJoin('followers', 'followers.following_id', 'stories.profile_id')
->where('followers.profile_id', $pid)
->where('stories.active', true)
->groupBy('followers.following_id')
->orderByDesc('id')
->get();
}
2020-02-17 07:00:33 +00:00
$res = $s->map(function($s) use($pid) {
$profile = AccountService::get($s->profile_id);
$url = $profile['local'] ? url("/stories/{$profile['username']}") :
url("/i/rs/{$profile['id']}");
2020-01-11 02:45:46 +00:00
return [
'pid' => $profile['id'],
'avatar' => $profile['avatar'],
'local' => $profile['local'],
'username' => $profile['acct'],
'url' => $url,
'seen' => StoryService::hasSeen($pid, StoryService::latest($s->profile_id)),
'sid' => $s->id
2020-01-11 02:45:46 +00:00
];
})
->sortBy('seen')
->values();
2020-02-05 06:18:51 +00:00
return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
}
public function profile(Request $request, $id)
2020-01-07 06:31:51 +00:00
{
2021-05-12 00:30:32 +00:00
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404);
2020-01-07 06:31:51 +00:00
$authed = $request->user()->profile_id;
2020-01-07 06:31:51 +00:00
$profile = Profile::findOrFail($id);
if($authed != $profile->id && !FollowerService::follows($authed, $profile->id)) {
return [];
2020-01-07 06:31:51 +00:00
}
$stories = Story::whereProfileId($profile->id)
->whereActive(true)
2020-01-11 02:45:46 +00:00
->orderBy('expires_at')
->get()
->map(function($s, $k) use($authed) {
$seen = StoryService::hasSeen($authed, $s->id);
$res = [
'id' => (string) $s->id,
'type' => $s->type,
'duration' => $s->duration,
2020-01-11 02:45:46 +00:00
'src' => url(Storage::url($s->path)),
'created_at' => $s->created_at->toAtomString(),
'expires_at' => $s->expires_at->toAtomString(),
'view_count' => ($authed == $s->profile_id) ? ($s->view_count ?? 0) : null,
'seen' => $seen,
'progress' => $seen ? 100 : 0,
'can_reply' => (bool) $s->can_reply,
'can_react' => (bool) $s->can_react
2020-01-11 02:45:46 +00:00
];
if($s->type == 'poll') {
$res['question'] = json_decode($s->story, true)['question'];
$res['options'] = json_decode($s->story, true)['options'];
$res['voted'] = PollService::votedStory($s->id, $authed);
if($res['voted']) {
$res['voted_index'] = PollService::storyChoice($s->id, $authed);
}
}
return $res;
2020-01-11 02:45:46 +00:00
})->toArray();
2020-01-07 06:31:51 +00:00
if(count($stories) == 0) {
return [];
}
$cursor = count($stories) - 1;
$stories = [[
'id' => (string) $stories[$cursor]['id'],
'nodes' => $stories,
'account' => AccountService::get($profile->id),
2020-01-07 06:31:51 +00:00
'pid' => (string) $profile->id
]];
return response()->json($stories, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
}
public function viewed(Request $request)
2020-01-07 06:31:51 +00:00
{
2021-05-12 00:30:32 +00:00
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404);
2020-01-07 06:31:51 +00:00
$this->validate($request, [
'id' => 'required|min:1',
2020-01-07 06:31:51 +00:00
]);
2020-02-05 06:18:51 +00:00
$id = $request->input('id');
2020-02-05 06:18:51 +00:00
$authed = $request->user()->profile;
2020-02-05 06:18:51 +00:00
$story = Story::with('profile')
->findOrFail($id);
$exp = $story->expires_at;
2020-02-05 06:18:51 +00:00
$profile = $story->profile;
2020-02-05 06:18:51 +00:00
if($story->profile_id == $authed->id) {
return [];
2020-02-05 06:18:51 +00:00
}
$publicOnly = (bool) $profile->followedBy($authed);
2020-02-05 06:18:51 +00:00
abort_if(!$publicOnly, 403);
2020-01-07 06:31:51 +00:00
$v = StoryView::firstOrCreate([
2020-02-05 06:18:51 +00:00
'story_id' => $id,
'profile_id' => $authed->id
2020-01-07 06:31:51 +00:00
]);
if($v->wasRecentlyCreated) {
Story::findOrFail($story->id)->increment('view_count');
if($story->local == false) {
StoryViewDeliver::dispatch($story, $authed)->onQueue('story');
}
}
Cache::forget('stories:recent:by_id:' . $authed->id);
StoryService::addSeen($authed->id, $story->id);
2020-01-07 06:31:51 +00:00
return ['code' => 200];
}
public function exists(Request $request, $id)
2020-01-07 06:31:51 +00:00
{
2021-05-12 00:30:32 +00:00
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404);
2020-01-07 06:31:51 +00:00
return response()->json(Story::whereProfileId($id)
->whereActive(true)
->exists());
}
public function iRedirect(Request $request)
{
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404);
2020-01-07 06:31:51 +00:00
$user = $request->user();
abort_if(!$user, 404);
$username = $user->username;
return redirect("/stories/{$username}");
2020-01-07 06:31:51 +00:00
}
public function viewers(Request $request)
2020-02-05 06:18:51 +00:00
{
2021-05-12 00:30:32 +00:00
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404);
2020-02-05 06:18:51 +00:00
$this->validate($request, [
'sid' => 'required|string'
]);
$pid = $request->user()->profile_id;
$sid = $request->input('sid');
$story = Story::whereProfileId($pid)
->whereActive(true)
->findOrFail($sid);
2020-02-05 06:18:51 +00:00
$viewers = StoryView::whereStoryId($story->id)
->latest()
->simplePaginate(10)
->map(function($view) {
return AccountService::get($view->profile_id);
})
->values();
return response()->json($viewers, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
2020-02-05 06:18:51 +00:00
}
public function remoteStory(Request $request, $id)
2020-02-05 06:18:51 +00:00
{
2021-05-12 00:30:32 +00:00
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404);
$profile = Profile::findOrFail($id);
if($profile->user_id != null || $profile->domain == null) {
return redirect('/stories/' . $profile->username);
}
$pid = $profile->id;
return view('stories.show_remote', compact('pid'));
2020-02-05 06:18:51 +00:00
}
public function pollResults(Request $request)
2019-01-31 20:28:30 +00:00
{
2021-05-12 00:30:32 +00:00
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404);
2020-02-05 06:18:51 +00:00
$this->validate($request, [
'sid' => 'required|string'
]);
$pid = $request->user()->profile_id;
$sid = $request->input('sid');
$story = Story::whereProfileId($pid)
->whereActive(true)
->findOrFail($sid);
return PollService::storyResults($sid);
}
public function getActivityObject(Request $request, $username, $id)
{
abort_if(!config_cache('instance.stories.enabled'), 404);
if(!$request->wantsJson()) {
return redirect('/stories/' . $username);
}
abort_if(!$request->hasHeader('Authorization'), 404);
$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');
2019-01-31 20:28:30 +00:00
}
2018-08-28 03:07:36 +00:00
}