StoryComposeController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\DirectMessage;
  4. use App\Jobs\StoryPipeline\StoryDelete;
  5. use App\Jobs\StoryPipeline\StoryFanout;
  6. use App\Jobs\StoryPipeline\StoryReactionDeliver;
  7. use App\Jobs\StoryPipeline\StoryReplyDeliver;
  8. use App\Models\Conversation;
  9. use App\Models\Poll;
  10. use App\Models\PollVote;
  11. use App\Notification;
  12. use App\Report;
  13. use App\Services\FollowerService;
  14. use App\Services\MediaPathService;
  15. use App\Services\StoryService;
  16. use App\Services\UserRoleService;
  17. use App\Status;
  18. use App\Story;
  19. use FFMpeg;
  20. use Illuminate\Http\Request;
  21. use Illuminate\Support\Str;
  22. use Image as Intervention;
  23. use Storage;
  24. class StoryComposeController extends Controller
  25. {
  26. public function apiV1Add(Request $request)
  27. {
  28. abort_if(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
  29. $this->validate($request, [
  30. 'file' => function () {
  31. return [
  32. 'required',
  33. 'mimetypes:image/jpeg,image/png,video/mp4',
  34. 'max:'.config_cache('pixelfed.max_photo_size'),
  35. ];
  36. },
  37. ]);
  38. $user = $request->user();
  39. abort_if($user->has_roles && ! UserRoleService::can('can-use-stories', $user->id), 403, 'Invalid permissions for this action');
  40. $count = Story::whereProfileId($user->profile_id)
  41. ->whereActive(true)
  42. ->where('expires_at', '>', now())
  43. ->count();
  44. if ($count >= Story::MAX_PER_DAY) {
  45. abort(418, 'You have reached your limit for new Stories today.');
  46. }
  47. $photo = $request->file('file');
  48. $path = $this->storePhoto($photo, $user);
  49. $story = new Story();
  50. $story->duration = 3;
  51. $story->profile_id = $user->profile_id;
  52. $story->type = Str::endsWith($photo->getMimeType(), 'mp4') ? 'video' : 'photo';
  53. $story->mime = $photo->getMimeType();
  54. $story->path = $path;
  55. $story->local = true;
  56. $story->size = $photo->getSize();
  57. $story->bearcap_token = str_random(64);
  58. $story->expires_at = now()->addMinutes(1440);
  59. $story->save();
  60. $url = $story->path;
  61. $res = [
  62. 'code' => 200,
  63. 'msg' => 'Successfully added',
  64. 'media_id' => (string) $story->id,
  65. 'media_url' => url(Storage::url($url)).'?v='.time(),
  66. 'media_type' => $story->type,
  67. ];
  68. if ($story->type === 'video') {
  69. $video = FFMpeg::open($path);
  70. $duration = $video->getDurationInSeconds();
  71. $res['media_duration'] = $duration;
  72. if ($duration > 500) {
  73. Storage::delete($story->path);
  74. $story->delete();
  75. return response()->json([
  76. 'message' => 'Video duration cannot exceed 60 seconds',
  77. ], 422);
  78. }
  79. }
  80. return $res;
  81. }
  82. protected function storePhoto($photo, $user)
  83. {
  84. $mimes = explode(',', config_cache('pixelfed.media_types'));
  85. if (in_array($photo->getMimeType(), [
  86. 'image/jpeg',
  87. 'image/png',
  88. 'video/mp4',
  89. ]) == false) {
  90. abort(400, 'Invalid media type');
  91. return;
  92. }
  93. $storagePath = MediaPathService::story($user->profile);
  94. $path = $photo->storePubliclyAs($storagePath, Str::random(random_int(2, 12)).'_'.Str::random(random_int(32, 35)).'_'.Str::random(random_int(1, 14)).'.'.$photo->extension());
  95. if (in_array($photo->getMimeType(), ['image/jpeg', 'image/png'])) {
  96. $fpath = storage_path('app/'.$path);
  97. $img = Intervention::make($fpath);
  98. $img->orientate();
  99. $img->save($fpath, config_cache('pixelfed.image_quality'));
  100. $img->destroy();
  101. }
  102. return $path;
  103. }
  104. public function cropPhoto(Request $request)
  105. {
  106. abort_if(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
  107. $this->validate($request, [
  108. 'media_id' => 'required|integer|min:1',
  109. 'width' => 'required',
  110. 'height' => 'required',
  111. 'x' => 'required',
  112. 'y' => 'required',
  113. ]);
  114. $user = $request->user();
  115. $id = $request->input('media_id');
  116. $width = round($request->input('width'));
  117. $height = round($request->input('height'));
  118. $x = round($request->input('x'));
  119. $y = round($request->input('y'));
  120. $story = Story::whereProfileId($user->profile_id)->findOrFail($id);
  121. $path = storage_path('app/'.$story->path);
  122. if (! is_file($path)) {
  123. abort(400, 'Invalid or missing media.');
  124. }
  125. if ($story->type === 'photo') {
  126. $img = Intervention::make($path);
  127. $img->crop($width, $height, $x, $y);
  128. $img->resize(1080, 1920, function ($constraint) {
  129. $constraint->aspectRatio();
  130. });
  131. $img->save($path, config_cache('pixelfed.image_quality'));
  132. }
  133. return [
  134. 'code' => 200,
  135. 'msg' => 'Successfully cropped',
  136. ];
  137. }
  138. public function publishStory(Request $request)
  139. {
  140. abort_if(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
  141. $this->validate($request, [
  142. 'media_id' => 'required',
  143. 'duration' => 'required|integer|min:3|max:120',
  144. 'can_reply' => 'required|boolean',
  145. 'can_react' => 'required|boolean',
  146. ]);
  147. $id = $request->input('media_id');
  148. $user = $request->user();
  149. abort_if($user->has_roles && ! UserRoleService::can('can-use-stories', $user->id), 403, 'Invalid permissions for this action');
  150. $story = Story::whereProfileId($user->profile_id)
  151. ->findOrFail($id);
  152. $story->active = true;
  153. $story->duration = $request->input('duration', 10);
  154. $story->can_reply = $request->input('can_reply');
  155. $story->can_react = $request->input('can_react');
  156. $story->save();
  157. StoryService::delLatest($story->profile_id);
  158. StoryFanout::dispatch($story)->onQueue('story');
  159. StoryService::addRotateQueue($story->id);
  160. return [
  161. 'code' => 200,
  162. 'msg' => 'Successfully published',
  163. ];
  164. }
  165. public function apiV1Delete(Request $request, $id)
  166. {
  167. abort_if(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
  168. $user = $request->user();
  169. $story = Story::whereProfileId($user->profile_id)
  170. ->findOrFail($id);
  171. $story->active = false;
  172. $story->save();
  173. StoryDelete::dispatch($story)->onQueue('story');
  174. return [
  175. 'code' => 200,
  176. 'msg' => 'Successfully deleted',
  177. ];
  178. }
  179. public function compose(Request $request)
  180. {
  181. abort_if(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
  182. $user = $request->user();
  183. abort_if($user->has_roles && ! UserRoleService::can('can-use-stories', $user->id), 403, 'Invalid permissions for this action');
  184. return view('stories.compose');
  185. }
  186. public function createPoll(Request $request)
  187. {
  188. abort_if(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
  189. abort_if(! config_cache('instance.polls.enabled'), 404);
  190. return $request->all();
  191. }
  192. public function publishStoryPoll(Request $request)
  193. {
  194. abort_if(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
  195. $this->validate($request, [
  196. 'question' => 'required|string|min:6|max:140',
  197. 'options' => 'required|array|min:2|max:4',
  198. 'can_reply' => 'required|boolean',
  199. 'can_react' => 'required|boolean',
  200. ]);
  201. $user = $request->user();
  202. abort_if($user->has_roles && ! UserRoleService::can('can-use-stories', $user->id), 403, 'Invalid permissions for this action');
  203. $pid = $request->user()->profile_id;
  204. $count = Story::whereProfileId($pid)
  205. ->whereActive(true)
  206. ->where('expires_at', '>', now())
  207. ->count();
  208. if ($count >= Story::MAX_PER_DAY) {
  209. abort(418, 'You have reached your limit for new Stories today.');
  210. }
  211. $story = new Story;
  212. $story->type = 'poll';
  213. $story->story = json_encode([
  214. 'question' => $request->input('question'),
  215. 'options' => $request->input('options'),
  216. ]);
  217. $story->public = false;
  218. $story->local = true;
  219. $story->profile_id = $pid;
  220. $story->expires_at = now()->addMinutes(1440);
  221. $story->duration = 30;
  222. $story->can_reply = false;
  223. $story->can_react = false;
  224. $story->save();
  225. $poll = new Poll;
  226. $poll->story_id = $story->id;
  227. $poll->profile_id = $pid;
  228. $poll->poll_options = $request->input('options');
  229. $poll->expires_at = $story->expires_at;
  230. $poll->cached_tallies = collect($poll->poll_options)->map(function ($o) {
  231. return 0;
  232. })->toArray();
  233. $poll->save();
  234. $story->active = true;
  235. $story->save();
  236. StoryService::delLatest($story->profile_id);
  237. return [
  238. 'code' => 200,
  239. 'msg' => 'Successfully published',
  240. ];
  241. }
  242. public function storyPollVote(Request $request)
  243. {
  244. abort_if(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
  245. $this->validate($request, [
  246. 'sid' => 'required',
  247. 'ci' => 'required|integer|min:0|max:3',
  248. ]);
  249. $pid = $request->user()->profile_id;
  250. $ci = $request->input('ci');
  251. $story = Story::findOrFail($request->input('sid'));
  252. abort_if(! FollowerService::follows($pid, $story->profile_id), 403);
  253. $poll = Poll::whereStoryId($story->id)->firstOrFail();
  254. $vote = new PollVote;
  255. $vote->profile_id = $pid;
  256. $vote->poll_id = $poll->id;
  257. $vote->story_id = $story->id;
  258. $vote->status_id = null;
  259. $vote->choice = $ci;
  260. $vote->save();
  261. $poll->votes_count = $poll->votes_count + 1;
  262. $poll->cached_tallies = collect($poll->getTallies())->map(function ($tally, $key) use ($ci) {
  263. return $ci == $key ? $tally + 1 : $tally;
  264. })->toArray();
  265. $poll->save();
  266. return 200;
  267. }
  268. public function storeReport(Request $request)
  269. {
  270. abort_if(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
  271. $this->validate($request, [
  272. 'type' => 'required|alpha_dash',
  273. 'id' => 'required|integer|min:1',
  274. ]);
  275. $user = $request->user();
  276. abort_if($user->has_roles && ! UserRoleService::can('can-use-stories', $user->id), 403, 'Invalid permissions for this action');
  277. $pid = $request->user()->profile_id;
  278. $sid = $request->input('id');
  279. $type = $request->input('type');
  280. $types = [
  281. // original 3
  282. 'spam',
  283. 'sensitive',
  284. 'abusive',
  285. // new
  286. 'underage',
  287. 'copyright',
  288. 'impersonation',
  289. 'scam',
  290. 'terrorism',
  291. ];
  292. abort_if(! in_array($type, $types), 422, 'Invalid story report type');
  293. $story = Story::findOrFail($sid);
  294. abort_if($story->profile_id == $pid, 422, 'Cannot report your own story');
  295. abort_if(! FollowerService::follows($pid, $story->profile_id), 422, 'Cannot report a story from an account you do not follow');
  296. if (Report::whereProfileId($pid)
  297. ->whereObjectType('App\Story')
  298. ->whereObjectId($story->id)
  299. ->exists()
  300. ) {
  301. return response()->json(['error' => [
  302. 'code' => 409,
  303. 'message' => 'Cannot report the same story again',
  304. ]], 409);
  305. }
  306. $report = new Report;
  307. $report->profile_id = $pid;
  308. $report->user_id = $request->user()->id;
  309. $report->object_id = $story->id;
  310. $report->object_type = 'App\Story';
  311. $report->reported_profile_id = $story->profile_id;
  312. $report->type = $type;
  313. $report->message = null;
  314. $report->save();
  315. return [200];
  316. }
  317. public function react(Request $request)
  318. {
  319. abort_if(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
  320. $this->validate($request, [
  321. 'sid' => 'required',
  322. 'reaction' => 'required|string',
  323. ]);
  324. $pid = $request->user()->profile_id;
  325. $text = $request->input('reaction');
  326. $user = $request->user();
  327. abort_if($user->has_roles && ! UserRoleService::can('can-use-stories', $user->id), 403, 'Invalid permissions for this action');
  328. $story = Story::findOrFail($request->input('sid'));
  329. abort_if(! $story->can_react, 422);
  330. abort_if(StoryService::reactCounter($story->id, $pid) >= 5, 422, 'You have already reacted to this story');
  331. $status = new Status;
  332. $status->profile_id = $pid;
  333. $status->type = 'story:reaction';
  334. $status->caption = $text;
  335. $status->rendered = $text;
  336. $status->scope = 'direct';
  337. $status->visibility = 'direct';
  338. $status->in_reply_to_profile_id = $story->profile_id;
  339. $status->entities = json_encode([
  340. 'story_id' => $story->id,
  341. 'reaction' => $text,
  342. ]);
  343. $status->save();
  344. $dm = new DirectMessage;
  345. $dm->to_id = $story->profile_id;
  346. $dm->from_id = $pid;
  347. $dm->type = 'story:react';
  348. $dm->status_id = $status->id;
  349. $dm->meta = json_encode([
  350. 'story_username' => $story->profile->username,
  351. 'story_actor_username' => $request->user()->username,
  352. 'story_id' => $story->id,
  353. 'story_media_url' => url(Storage::url($story->path)),
  354. 'reaction' => $text,
  355. ]);
  356. $dm->save();
  357. Conversation::updateOrInsert(
  358. [
  359. 'to_id' => $story->profile_id,
  360. 'from_id' => $pid,
  361. ],
  362. [
  363. 'type' => 'story:react',
  364. 'status_id' => $status->id,
  365. 'dm_id' => $dm->id,
  366. 'is_hidden' => false,
  367. ]
  368. );
  369. if ($story->local) {
  370. // generate notification
  371. $n = new Notification;
  372. $n->profile_id = $dm->to_id;
  373. $n->actor_id = $dm->from_id;
  374. $n->item_id = $dm->id;
  375. $n->item_type = 'App\DirectMessage';
  376. $n->action = 'story:react';
  377. $n->save();
  378. } else {
  379. StoryReactionDeliver::dispatch($story, $status)->onQueue('story');
  380. }
  381. StoryService::reactIncrement($story->id, $pid);
  382. return 200;
  383. }
  384. public function comment(Request $request)
  385. {
  386. abort_if(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
  387. $this->validate($request, [
  388. 'sid' => 'required',
  389. 'caption' => 'required|string',
  390. ]);
  391. $pid = $request->user()->profile_id;
  392. $text = $request->input('caption');
  393. $user = $request->user();
  394. abort_if($user->has_roles && ! UserRoleService::can('can-use-stories', $user->id), 403, 'Invalid permissions for this action');
  395. $story = Story::findOrFail($request->input('sid'));
  396. abort_if(! $story->can_reply, 422);
  397. $status = new Status;
  398. $status->type = 'story:reply';
  399. $status->profile_id = $pid;
  400. $status->caption = $text;
  401. $status->rendered = $text;
  402. $status->scope = 'direct';
  403. $status->visibility = 'direct';
  404. $status->in_reply_to_profile_id = $story->profile_id;
  405. $status->entities = json_encode([
  406. 'story_id' => $story->id,
  407. ]);
  408. $status->save();
  409. $dm = new DirectMessage;
  410. $dm->to_id = $story->profile_id;
  411. $dm->from_id = $pid;
  412. $dm->type = 'story:comment';
  413. $dm->status_id = $status->id;
  414. $dm->meta = json_encode([
  415. 'story_username' => $story->profile->username,
  416. 'story_actor_username' => $request->user()->username,
  417. 'story_id' => $story->id,
  418. 'story_media_url' => url(Storage::url($story->path)),
  419. 'caption' => $text,
  420. ]);
  421. $dm->save();
  422. Conversation::updateOrInsert(
  423. [
  424. 'to_id' => $story->profile_id,
  425. 'from_id' => $pid,
  426. ],
  427. [
  428. 'type' => 'story:comment',
  429. 'status_id' => $status->id,
  430. 'dm_id' => $dm->id,
  431. 'is_hidden' => false,
  432. ]
  433. );
  434. if ($story->local) {
  435. // generate notification
  436. $n = new Notification;
  437. $n->profile_id = $dm->to_id;
  438. $n->actor_id = $dm->from_id;
  439. $n->item_id = $dm->id;
  440. $n->item_type = 'App\DirectMessage';
  441. $n->action = 'story:comment';
  442. $n->save();
  443. } else {
  444. StoryReplyDeliver::dispatch($story, $status)->onQueue('story');
  445. }
  446. return 200;
  447. }
  448. }