StoryController.php 7.3 KB

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