StoryController.php 11 KB

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