StoryController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Str;
  5. use App\DirectMessage;
  6. use App\Follower;
  7. use App\Notification;
  8. use App\Media;
  9. use App\Profile;
  10. use App\Status;
  11. use App\Story;
  12. use App\StoryView;
  13. use App\Services\PollService;
  14. use App\Services\ProfileService;
  15. use App\Services\StoryService;
  16. use Cache, Storage;
  17. use Image as Intervention;
  18. use App\Services\AccountService;
  19. use App\Services\FollowerService;
  20. use App\Services\MediaPathService;
  21. use FFMpeg;
  22. use FFMpeg\Coordinate\Dimension;
  23. use FFMpeg\Format\Video\X264;
  24. use League\Fractal\Manager;
  25. use League\Fractal\Serializer\ArraySerializer;
  26. use League\Fractal\Resource\Item;
  27. use App\Transformer\ActivityPub\Verb\StoryVerb;
  28. use App\Jobs\StoryPipeline\StoryViewDeliver;
  29. use App\Services\UserRoleService;
  30. class StoryController extends StoryComposeController
  31. {
  32. public function recent(Request $request)
  33. {
  34. abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404);
  35. $user = $request->user();
  36. if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id)) {
  37. return [];
  38. }
  39. $pid = $user->profile_id;
  40. if(config('database.default') == 'pgsql') {
  41. $s = Cache::remember('pf:stories:recent-by-id:' . $pid, 900, function() use($pid) {
  42. return Story::select('stories.*', 'followers.following_id')
  43. ->leftJoin('followers', 'followers.following_id', 'stories.profile_id')
  44. ->where('followers.profile_id', $pid)
  45. ->where('stories.active', true)
  46. ->get()
  47. ->map(function($s) {
  48. $r = new \StdClass;
  49. $r->id = $s->id;
  50. $r->profile_id = $s->profile_id;
  51. $r->type = $s->type;
  52. $r->path = $s->path;
  53. return $r;
  54. })
  55. ->unique('profile_id');
  56. });
  57. } else {
  58. $s = Cache::remember('pf:stories:recent-by-id:' . $pid, 900, function() use($pid) {
  59. return Story::select('stories.*', 'followers.following_id')
  60. ->leftJoin('followers', 'followers.following_id', 'stories.profile_id')
  61. ->where('followers.profile_id', $pid)
  62. ->where('stories.active', true)
  63. ->groupBy('followers.following_id')
  64. ->orderByDesc('id')
  65. ->get();
  66. });
  67. }
  68. $self = Cache::remember('pf:stories:recent-self:' . $pid, 21600, function() use($pid) {
  69. return Story::whereProfileId($pid)
  70. ->whereActive(true)
  71. ->orderByDesc('id')
  72. ->limit(1)
  73. ->get()
  74. ->map(function($s) use($pid) {
  75. $r = new \StdClass;
  76. $r->id = $s->id;
  77. $r->profile_id = $pid;
  78. $r->type = $s->type;
  79. $r->path = $s->path;
  80. return $r;
  81. });
  82. });
  83. if($self->count()) {
  84. $s->prepend($self->first());
  85. }
  86. $res = $s->map(function($s) use($pid) {
  87. $profile = AccountService::get($s->profile_id);
  88. $url = $profile['local'] ? url("/stories/{$profile['username']}") :
  89. url("/i/rs/{$profile['id']}");
  90. return [
  91. 'pid' => $profile['id'],
  92. 'avatar' => $profile['avatar'],
  93. 'local' => $profile['local'],
  94. 'username' => $profile['acct'],
  95. 'latest' => [
  96. 'id' => $s->id,
  97. 'type' => $s->type,
  98. 'preview_url' => url(Storage::url($s->path))
  99. ],
  100. 'url' => $url,
  101. 'seen' => StoryService::hasSeen($pid, StoryService::latest($s->profile_id)),
  102. 'sid' => $s->id
  103. ];
  104. })
  105. ->sortBy('seen')
  106. ->values();
  107. return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  108. }
  109. public function profile(Request $request, $id)
  110. {
  111. abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404);
  112. $user = $request->user();
  113. if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id)) {
  114. return [];
  115. }
  116. $authed = $user->profile_id;
  117. $profile = Profile::findOrFail($id);
  118. if($authed != $profile->id && !FollowerService::follows($authed, $profile->id)) {
  119. return abort([], 403);
  120. }
  121. $stories = Story::whereProfileId($profile->id)
  122. ->whereActive(true)
  123. ->orderBy('expires_at')
  124. ->get()
  125. ->map(function($s, $k) use($authed) {
  126. $seen = StoryService::hasSeen($authed, $s->id);
  127. $res = [
  128. 'id' => (string) $s->id,
  129. 'type' => $s->type,
  130. 'duration' => $s->duration,
  131. 'src' => url(Storage::url($s->path)),
  132. 'created_at' => $s->created_at->toAtomString(),
  133. 'expires_at' => $s->expires_at->toAtomString(),
  134. 'view_count' => ($authed == $s->profile_id) ? ($s->view_count ?? 0) : null,
  135. 'seen' => $seen,
  136. 'progress' => $seen ? 100 : 0,
  137. 'can_reply' => (bool) $s->can_reply,
  138. 'can_react' => (bool) $s->can_react
  139. ];
  140. if($s->type == 'poll') {
  141. $res['question'] = json_decode($s->story, true)['question'];
  142. $res['options'] = json_decode($s->story, true)['options'];
  143. $res['voted'] = PollService::votedStory($s->id, $authed);
  144. if($res['voted']) {
  145. $res['voted_index'] = PollService::storyChoice($s->id, $authed);
  146. }
  147. }
  148. return $res;
  149. })->toArray();
  150. if(count($stories) == 0) {
  151. return [];
  152. }
  153. $cursor = count($stories) - 1;
  154. $stories = [[
  155. 'id' => (string) $stories[$cursor]['id'],
  156. 'nodes' => $stories,
  157. 'account' => AccountService::get($profile->id),
  158. 'pid' => (string) $profile->id
  159. ]];
  160. return response()->json($stories, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  161. }
  162. public function viewed(Request $request)
  163. {
  164. abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404);
  165. $this->validate($request, [
  166. 'id' => 'required|min:1',
  167. ]);
  168. $id = $request->input('id');
  169. $user = $request->user();
  170. if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id)) {
  171. return [];
  172. }
  173. $authed = $user->profile;
  174. $story = Story::with('profile')
  175. ->findOrFail($id);
  176. $exp = $story->expires_at;
  177. $profile = $story->profile;
  178. if($story->profile_id == $authed->id) {
  179. return [];
  180. }
  181. $publicOnly = (bool) $profile->followedBy($authed);
  182. abort_if(!$publicOnly, 403);
  183. $v = StoryView::firstOrCreate([
  184. 'story_id' => $id,
  185. 'profile_id' => $authed->id
  186. ]);
  187. if($v->wasRecentlyCreated) {
  188. Story::findOrFail($story->id)->increment('view_count');
  189. if($story->local == false) {
  190. StoryViewDeliver::dispatch($story, $authed)->onQueue('story');
  191. }
  192. }
  193. Cache::forget('stories:recent:by_id:' . $authed->id);
  194. StoryService::addSeen($authed->id, $story->id);
  195. return ['code' => 200];
  196. }
  197. public function exists(Request $request, $id)
  198. {
  199. abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404);
  200. $user = $request->user();
  201. if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id)) {
  202. return response()->json(false);
  203. }
  204. return response()->json(Story::whereProfileId($id)
  205. ->whereActive(true)
  206. ->exists());
  207. }
  208. public function iRedirect(Request $request)
  209. {
  210. abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404);
  211. $user = $request->user();
  212. abort_if(!$user, 404);
  213. $username = $user->username;
  214. return redirect("/stories/{$username}");
  215. }
  216. public function viewers(Request $request)
  217. {
  218. abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404);
  219. $this->validate($request, [
  220. 'sid' => 'required|string'
  221. ]);
  222. $user = $request->user();
  223. if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id)) {
  224. return response()->json([]);
  225. }
  226. $pid = $request->user()->profile_id;
  227. $sid = $request->input('sid');
  228. $story = Story::whereProfileId($pid)
  229. ->whereActive(true)
  230. ->findOrFail($sid);
  231. $viewers = StoryView::whereStoryId($story->id)
  232. ->latest()
  233. ->simplePaginate(10)
  234. ->map(function($view) {
  235. return AccountService::get($view->profile_id);
  236. })
  237. ->values();
  238. return response()->json($viewers, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  239. }
  240. public function remoteStory(Request $request, $id)
  241. {
  242. abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404);
  243. $profile = Profile::findOrFail($id);
  244. if($profile->user_id != null || $profile->domain == null) {
  245. return redirect('/stories/' . $profile->username);
  246. }
  247. $pid = $profile->id;
  248. return view('stories.show_remote', compact('pid'));
  249. }
  250. public function pollResults(Request $request)
  251. {
  252. abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404);
  253. $this->validate($request, [
  254. 'sid' => 'required|string'
  255. ]);
  256. $pid = $request->user()->profile_id;
  257. $sid = $request->input('sid');
  258. $story = Story::whereProfileId($pid)
  259. ->whereActive(true)
  260. ->findOrFail($sid);
  261. return PollService::storyResults($sid);
  262. }
  263. public function getActivityObject(Request $request, $username, $id)
  264. {
  265. abort_if(!config_cache('instance.stories.enabled'), 404);
  266. if(!$request->wantsJson()) {
  267. return redirect('/stories/' . $username);
  268. }
  269. abort_if(!$request->hasHeader('Authorization'), 404);
  270. $profile = Profile::whereUsername($username)->whereNull('domain')->firstOrFail();
  271. $story = Story::whereActive(true)->whereProfileId($profile->id)->findOrFail($id);
  272. abort_if($story->bearcap_token == null, 404);
  273. abort_if(now()->gt($story->expires_at), 404);
  274. $token = substr($request->header('Authorization'), 7);
  275. abort_if(hash_equals($story->bearcap_token, $token) === false, 404);
  276. abort_if($story->created_at->lt(now()->subMinutes(20)), 404);
  277. $fractal = new Manager();
  278. $fractal->setSerializer(new ArraySerializer());
  279. $resource = new Item($story, new StoryVerb());
  280. $res = $fractal->createData($resource)->toArray();
  281. return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  282. }
  283. public function showSystemStory()
  284. {
  285. // return view('stories.system');
  286. }
  287. }