StoryController.php 8.6 KB

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