StoryComposeController.php 17 KB

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