StoryController.php 7.8 KB

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