StoryComposeController.php 13 KB

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