StoryController.php 7.9 KB

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