StoryController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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\Story;
  8. use App\StoryView;
  9. use App\Services\StoryService;
  10. use Cache, Storage;
  11. use Image as Intervention;
  12. use App\Services\FollowerService;
  13. use App\Services\MediaPathService;
  14. class StoryController extends Controller
  15. {
  16. public function apiV1Add(Request $request)
  17. {
  18. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  19. $this->validate($request, [
  20. 'file' => function() {
  21. return [
  22. 'required',
  23. 'mimes:image/jpeg,image/png,video/mp4',
  24. 'max:' . config('pixelfed.max_photo_size'),
  25. ];
  26. },
  27. ]);
  28. $user = $request->user();
  29. if(Story::whereProfileId($user->profile_id)->where('expires_at', '>', now())->count() >= Story::MAX_PER_DAY) {
  30. abort(400, 'You have reached your limit for new Stories today.');
  31. }
  32. $photo = $request->file('file');
  33. $path = $this->storePhoto($photo, $user);
  34. $story = new Story();
  35. $story->duration = 3;
  36. $story->profile_id = $user->profile_id;
  37. $story->type = Str::endsWith($photo->getMimeType(), 'mp4') ? 'video' :'photo';
  38. $story->mime = $photo->getMimeType();
  39. $story->path = $path;
  40. $story->local = true;
  41. $story->size = $photo->getSize();
  42. $story->save();
  43. return [
  44. 'code' => 200,
  45. 'msg' => 'Successfully added',
  46. 'media_id' => (string) $story->id,
  47. 'media_url' => url(Storage::url($story->path))
  48. ];
  49. }
  50. protected function storePhoto($photo, $user)
  51. {
  52. $mimes = explode(',', config('pixelfed.media_types'));
  53. if(in_array($photo->getMimeType(), [
  54. 'image/jpeg',
  55. 'image/png',
  56. 'video/mp4'
  57. ]) == false) {
  58. abort(400, 'Invalid media type');
  59. return;
  60. }
  61. $storagePath = MediaPathService::story($user->profile);
  62. $path = $photo->store($storagePath);
  63. if(in_array($photo->getMimeType(), ['image/jpeg','image/png'])) {
  64. $fpath = storage_path('app/' . $path);
  65. $img = Intervention::make($fpath);
  66. $img->orientate();
  67. $img->save($fpath, config('pixelfed.image_quality'));
  68. $img->destroy();
  69. }
  70. return $path;
  71. }
  72. public function cropPhoto(Request $request)
  73. {
  74. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  75. $this->validate($request, [
  76. 'media_id' => 'required|integer|min:1',
  77. 'width' => 'required',
  78. 'height' => 'required',
  79. 'x' => 'required',
  80. 'y' => 'required'
  81. ]);
  82. $user = $request->user();
  83. $id = $request->input('media_id');
  84. $width = round($request->input('width'));
  85. $height = round($request->input('height'));
  86. $x = round($request->input('x'));
  87. $y = round($request->input('y'));
  88. $story = Story::whereProfileId($user->profile_id)->findOrFail($id);
  89. $path = storage_path('app/' . $story->path);
  90. if(!is_file($path)) {
  91. abort(400, 'Invalid or missing media.');
  92. }
  93. $img = Intervention::make($path);
  94. $img->crop($width, $height, $x, $y);
  95. $img->save($path, config('pixelfed.image_quality'));
  96. return [
  97. 'code' => 200,
  98. 'msg' => 'Successfully cropped',
  99. ];
  100. }
  101. public function publishStory(Request $request)
  102. {
  103. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  104. $this->validate($request, [
  105. 'media_id' => 'required',
  106. 'duration' => 'required|integer|min:3|max:10'
  107. ]);
  108. $id = $request->input('media_id');
  109. $user = $request->user();
  110. $story = Story::whereProfileId($user->profile_id)
  111. ->findOrFail($id);
  112. $story->active = true;
  113. $story->duration = $request->input('duration', 10);
  114. $story->expires_at = now()->addHours(24);
  115. $story->save();
  116. return [
  117. 'code' => 200,
  118. 'msg' => 'Successfully published',
  119. ];
  120. }
  121. public function apiV1Delete(Request $request, $id)
  122. {
  123. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  124. $user = $request->user();
  125. $story = Story::whereProfileId($user->profile_id)
  126. ->findOrFail($id);
  127. if(Storage::exists($story->path) == true) {
  128. Storage::delete($story->path);
  129. }
  130. $story->delete();
  131. return [
  132. 'code' => 200,
  133. 'msg' => 'Successfully deleted'
  134. ];
  135. }
  136. public function apiV1Recent(Request $request)
  137. {
  138. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  139. $profile = $request->user()->profile;
  140. $following = $profile->following->pluck('id')->toArray();
  141. if(config('database.default') == 'pgsql') {
  142. $db = Story::with('profile')
  143. ->whereActive(true)
  144. ->whereIn('profile_id', $following)
  145. ->where('expires_at', '>', now())
  146. ->distinct('profile_id')
  147. ->take(9)
  148. ->get();
  149. } else {
  150. $db = Story::with('profile')
  151. ->whereActive(true)
  152. ->whereIn('profile_id', $following)
  153. ->where('created_at', '>', now()->subDay())
  154. ->orderByDesc('expires_at')
  155. ->groupBy('profile_id')
  156. ->take(9)
  157. ->get();
  158. }
  159. $stories = $db->map(function($s, $k) {
  160. return [
  161. 'id' => (string) $s->id,
  162. 'photo' => $s->profile->avatarUrl(),
  163. 'name' => $s->profile->username,
  164. 'link' => $s->profile->url(),
  165. 'lastUpdated' => (int) $s->created_at->format('U'),
  166. 'seen' => $s->seen(),
  167. 'items' => [],
  168. 'pid' => (string) $s->profile->id
  169. ];
  170. });
  171. return response()->json($stories, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  172. }
  173. public function apiV1Fetch(Request $request, $id)
  174. {
  175. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  176. $authed = $request->user()->profile;
  177. $profile = Profile::findOrFail($id);
  178. if($id == $authed->id) {
  179. $publicOnly = true;
  180. } else {
  181. $publicOnly = (bool) $profile->followedBy($authed);
  182. }
  183. $stories = Story::whereProfileId($profile->id)
  184. ->whereActive(true)
  185. ->orderBy('expires_at', 'desc')
  186. ->where('expires_at', '>', now())
  187. ->when(!$publicOnly, function($query, $publicOnly) {
  188. return $query->wherePublic(true);
  189. })
  190. ->get()
  191. ->map(function($s, $k) {
  192. return [
  193. 'id' => (string) $s->id,
  194. 'type' => Str::endsWith($s->path, '.mp4') ? 'video' :'photo',
  195. 'length' => 3,
  196. 'src' => url(Storage::url($s->path)),
  197. 'preview' => null,
  198. 'link' => null,
  199. 'linkText' => null,
  200. 'time' => $s->created_at->format('U'),
  201. 'expires_at' => (int) $s->expires_at->format('U'),
  202. 'seen' => $s->seen()
  203. ];
  204. })->toArray();
  205. return response()->json($stories, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  206. }
  207. public function apiV1Item(Request $request, $id)
  208. {
  209. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  210. $authed = $request->user()->profile;
  211. $story = Story::with('profile')
  212. ->whereActive(true)
  213. ->where('expires_at', '>', now())
  214. ->findOrFail($id);
  215. $profile = $story->profile;
  216. if($story->profile_id == $authed->id) {
  217. $publicOnly = true;
  218. } else {
  219. $publicOnly = (bool) $profile->followedBy($authed);
  220. }
  221. abort_if(!$publicOnly, 403);
  222. $res = [
  223. 'id' => (string) $story->id,
  224. 'type' => Str::endsWith($story->path, '.mp4') ? 'video' :'photo',
  225. 'length' => 10,
  226. 'src' => url(Storage::url($story->path)),
  227. 'preview' => null,
  228. 'link' => null,
  229. 'linkText' => null,
  230. 'time' => $story->created_at->format('U'),
  231. 'expires_at' => (int) $story->expires_at->format('U'),
  232. 'seen' => $story->seen()
  233. ];
  234. return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  235. }
  236. public function apiV1Profile(Request $request, $id)
  237. {
  238. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  239. $authed = $request->user()->profile;
  240. $profile = Profile::findOrFail($id);
  241. if($id == $authed->id) {
  242. $publicOnly = true;
  243. } else {
  244. $publicOnly = (bool) $profile->followedBy($authed);
  245. }
  246. $stories = Story::whereProfileId($profile->id)
  247. ->whereActive(true)
  248. ->orderBy('expires_at')
  249. ->where('expires_at', '>', now())
  250. ->when(!$publicOnly, function($query, $publicOnly) {
  251. return $query->wherePublic(true);
  252. })
  253. ->get()
  254. ->map(function($s, $k) {
  255. return [
  256. 'id' => $s->id,
  257. 'type' => Str::endsWith($s->path, '.mp4') ? 'video' :'photo',
  258. 'length' => 10,
  259. 'src' => url(Storage::url($s->path)),
  260. 'preview' => null,
  261. 'link' => null,
  262. 'linkText' => null,
  263. 'time' => $s->created_at->format('U'),
  264. 'expires_at' => (int) $s->expires_at->format('U'),
  265. 'seen' => $s->seen()
  266. ];
  267. })->toArray();
  268. if(count($stories) == 0) {
  269. return [];
  270. }
  271. $cursor = count($stories) - 1;
  272. $stories = [[
  273. 'id' => (string) $stories[$cursor]['id'],
  274. 'photo' => $profile->avatarUrl(),
  275. 'name' => $profile->username,
  276. 'link' => $profile->url(),
  277. 'lastUpdated' => (int) now()->format('U'),
  278. 'seen' => null,
  279. 'items' => $stories,
  280. 'pid' => (string) $profile->id
  281. ]];
  282. return response()->json($stories, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  283. }
  284. public function apiV1Viewed(Request $request)
  285. {
  286. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  287. $this->validate($request, [
  288. 'id' => 'required|integer|min:1|exists:stories',
  289. ]);
  290. $id = $request->input('id');
  291. $authed = $request->user()->profile;
  292. $story = Story::with('profile')
  293. ->where('expires_at', '>', now())
  294. ->orderByDesc('expires_at')
  295. ->findOrFail($id);
  296. $profile = $story->profile;
  297. if($story->profile_id == $authed->id) {
  298. return [];
  299. }
  300. $publicOnly = (bool) $profile->followedBy($authed);
  301. abort_if(!$publicOnly, 403);
  302. StoryView::firstOrCreate([
  303. 'story_id' => $id,
  304. 'profile_id' => $authed->id
  305. ]);
  306. $story->view_count = $story->view_count + 1;
  307. $story->save();
  308. return ['code' => 200];
  309. }
  310. public function apiV1Exists(Request $request, $id)
  311. {
  312. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  313. $res = (bool) Story::whereProfileId($id)
  314. ->whereActive(true)
  315. ->where('expires_at', '>', now())
  316. ->count();
  317. return response()->json($res);
  318. }
  319. public function apiV1Me(Request $request)
  320. {
  321. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  322. $profile = $request->user()->profile;
  323. $stories = Story::whereProfileId($profile->id)
  324. ->whereActive(true)
  325. ->orderBy('expires_at')
  326. ->where('expires_at', '>', now())
  327. ->get()
  328. ->map(function($s, $k) {
  329. return [
  330. 'id' => $s->id,
  331. 'type' => Str::endsWith($s->path, '.mp4') ? 'video' :'photo',
  332. 'length' => 3,
  333. 'src' => url(Storage::url($s->path)),
  334. 'preview' => null,
  335. 'link' => null,
  336. 'linkText' => null,
  337. 'time' => $s->created_at->format('U'),
  338. 'expires_at' => (int) $s->expires_at->format('U'),
  339. 'seen' => true
  340. ];
  341. })->toArray();
  342. $ts = count($stories) ? last($stories)['time'] : null;
  343. $res = [
  344. 'id' => (string) $profile->id,
  345. 'photo' => $profile->avatarUrl(),
  346. 'name' => $profile->username,
  347. 'link' => $profile->url(),
  348. 'lastUpdated' => $ts,
  349. 'seen' => true,
  350. 'items' => $stories
  351. ];
  352. return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  353. }
  354. public function compose(Request $request)
  355. {
  356. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  357. return view('stories.compose');
  358. }
  359. public function iRedirect(Request $request)
  360. {
  361. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  362. $user = $request->user();
  363. abort_if(!$user, 404);
  364. $username = $user->username;
  365. return redirect("/stories/{$username}");
  366. }
  367. }