StoryController.php 11 KB

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