StoryController.php 7.9 KB

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