StoryComposeController.php 14 KB

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