StoryController.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. class StoryController extends Controller
  14. {
  15. public function apiV1Add(Request $request)
  16. {
  17. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  18. $this->validate($request, [
  19. 'file' => function() {
  20. return [
  21. 'required',
  22. 'mimes:image/jpeg,image/png',
  23. 'max:' . config('pixelfed.max_photo_size'),
  24. ];
  25. },
  26. ]);
  27. $user = $request->user();
  28. if(Story::whereProfileId($user->profile_id)->where('expires_at', '>', now())->count() >= Story::MAX_PER_DAY) {
  29. abort(400, 'You have reached your limit for new Stories today.');
  30. }
  31. $photo = $request->file('file');
  32. $path = $this->storePhoto($photo);
  33. $story = new Story();
  34. $story->duration = 3;
  35. $story->profile_id = $user->profile_id;
  36. $story->type = 'photo';
  37. $story->mime = $photo->getMimeType();
  38. $story->path = $path;
  39. $story->local = true;
  40. $story->size = $photo->getClientSize();
  41. $story->expires_at = now()->addHours(24);
  42. $story->save();
  43. return [
  44. 'code' => 200,
  45. 'msg' => 'Successfully added',
  46. 'media_url' => url(Storage::url($story->path))
  47. ];
  48. }
  49. protected function storePhoto($photo)
  50. {
  51. $monthHash = substr(hash('sha1', date('Y').date('m')), 0, 12);
  52. $sid = (string) Str::uuid();
  53. $rid = Str::random(9).'.'.Str::random(9);
  54. $mimes = explode(',', config('pixelfed.media_types'));
  55. if(in_array($photo->getMimeType(), [
  56. 'image/jpeg',
  57. 'image/png'
  58. ]) == false) {
  59. abort(400, 'Invalid media type');
  60. return;
  61. }
  62. $storagePath = "public/_esm.t2/{$monthHash}/{$sid}/{$rid}";
  63. $path = $photo->store($storagePath);
  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. return $path;
  70. }
  71. public function apiV1Delete(Request $request, $id)
  72. {
  73. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  74. $user = $request->user();
  75. $story = Story::whereProfileId($user->profile_id)
  76. ->findOrFail($id);
  77. if(Storage::exists($story->path) == true) {
  78. Storage::delete($story->path);
  79. }
  80. $story->delete();
  81. return [
  82. 'code' => 200,
  83. 'msg' => 'Successfully deleted'
  84. ];
  85. }
  86. public function apiV1Recent(Request $request)
  87. {
  88. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  89. $profile = $request->user()->profile;
  90. $following = $profile->following->pluck('id')->toArray();
  91. $groupBy = config('database.default') == 'pgsql' ? 'id' : 'profile_id';
  92. $stories = Story::with('profile')
  93. ->groupBy($groupBy)
  94. ->whereIn('profile_id', $following)
  95. ->where('expires_at', '>', now())
  96. ->orderByDesc('expires_at')
  97. ->take(9)
  98. ->get()
  99. ->map(function($s, $k) {
  100. return [
  101. 'id' => (string) $s->id,
  102. 'photo' => $s->profile->avatarUrl(),
  103. 'name' => $s->profile->username,
  104. 'link' => $s->profile->url(),
  105. 'lastUpdated' => (int) $s->created_at->format('U'),
  106. 'seen' => $s->seen(),
  107. 'items' => [],
  108. 'pid' => (string) $s->profile->id
  109. ];
  110. });
  111. return response()->json($stories, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  112. }
  113. public function apiV1Fetch(Request $request, $id)
  114. {
  115. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  116. $authed = $request->user()->profile;
  117. $profile = Profile::findOrFail($id);
  118. if($id == $authed->id) {
  119. $publicOnly = true;
  120. } else {
  121. $publicOnly = (bool) $profile->followedBy($authed);
  122. }
  123. $stories = Story::whereProfileId($profile->id)
  124. ->orderBy('expires_at', 'desc')
  125. ->where('expires_at', '>', now())
  126. ->when(!$publicOnly, function($query, $publicOnly) {
  127. return $query->wherePublic(true);
  128. })
  129. ->get()
  130. ->map(function($s, $k) {
  131. return [
  132. 'id' => (string) $s->id,
  133. 'type' => 'photo',
  134. 'length' => 3,
  135. 'src' => url(Storage::url($s->path)),
  136. 'preview' => null,
  137. 'link' => null,
  138. 'linkText' => null,
  139. 'time' => $s->created_at->format('U'),
  140. 'expires_at' => (int) $s->expires_at->format('U'),
  141. 'seen' => $s->seen()
  142. ];
  143. })->toArray();
  144. return response()->json($stories, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  145. }
  146. public function apiV1Item(Request $request, $id)
  147. {
  148. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  149. $authed = $request->user()->profile;
  150. $story = Story::with('profile')
  151. ->where('expires_at', '>', now())
  152. ->findOrFail($id);
  153. $profile = $story->profile;
  154. if($story->profile_id == $authed->id) {
  155. $publicOnly = true;
  156. } else {
  157. $publicOnly = (bool) $profile->followedBy($authed);
  158. }
  159. abort_if(!$publicOnly, 403);
  160. $res = [
  161. 'id' => (string) $story->id,
  162. 'type' => 'photo',
  163. 'length' => 3,
  164. 'src' => url(Storage::url($story->path)),
  165. 'preview' => null,
  166. 'link' => null,
  167. 'linkText' => null,
  168. 'time' => $story->created_at->format('U'),
  169. 'expires_at' => (int) $story->expires_at->format('U'),
  170. 'seen' => $story->seen()
  171. ];
  172. return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  173. }
  174. public function apiV1Profile(Request $request, $id)
  175. {
  176. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  177. $authed = $request->user()->profile;
  178. $profile = Profile::findOrFail($id);
  179. if($id == $authed->id) {
  180. $publicOnly = true;
  181. } else {
  182. $publicOnly = (bool) $profile->followedBy($authed);
  183. }
  184. $stories = Story::whereProfileId($profile->id)
  185. ->orderBy('expires_at')
  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' => $s->id,
  194. 'type' => '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. if(count($stories) == 0) {
  206. return [];
  207. }
  208. $cursor = count($stories) - 1;
  209. $stories = [[
  210. 'id' => (string) $stories[$cursor]['id'],
  211. 'photo' => $profile->avatarUrl(),
  212. 'name' => $profile->username,
  213. 'link' => $profile->url(),
  214. 'lastUpdated' => (int) now()->format('U'),
  215. 'seen' => null,
  216. 'items' => $stories,
  217. 'pid' => (string) $profile->id
  218. ]];
  219. return response()->json($stories, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  220. }
  221. public function apiV1Viewed(Request $request)
  222. {
  223. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  224. $this->validate($request, [
  225. 'id' => 'required|integer|min:1|exists:stories',
  226. ]);
  227. $id = $request->input('id');
  228. $authed = $request->user()->profile;
  229. $story = Story::with('profile')
  230. ->where('expires_at', '>', now())
  231. ->orderByDesc('expires_at')
  232. ->findOrFail($id);
  233. $profile = $story->profile;
  234. if($story->profile_id == $authed->id) {
  235. $publicOnly = true;
  236. } else {
  237. $publicOnly = (bool) $profile->followedBy($authed);
  238. }
  239. abort_if(!$publicOnly, 403);
  240. StoryView::firstOrCreate([
  241. 'story_id' => $id,
  242. 'profile_id' => $authed->id
  243. ]);
  244. return ['code' => 200];
  245. }
  246. public function apiV1Exists(Request $request, $id)
  247. {
  248. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  249. $res = (bool) Story::whereProfileId($id)
  250. ->where('expires_at', '>', now())
  251. ->count();
  252. return response()->json($res);
  253. }
  254. public function apiV1Me(Request $request)
  255. {
  256. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  257. $profile = $request->user()->profile;
  258. $stories = Story::whereProfileId($profile->id)
  259. ->orderBy('expires_at')
  260. ->where('expires_at', '>', now())
  261. ->get()
  262. ->map(function($s, $k) {
  263. return [
  264. 'id' => $s->id,
  265. 'type' => 'photo',
  266. 'length' => 3,
  267. 'src' => url(Storage::url($s->path)),
  268. 'preview' => null,
  269. 'link' => null,
  270. 'linkText' => null,
  271. 'time' => $s->created_at->format('U'),
  272. 'expires_at' => (int) $s->expires_at->format('U'),
  273. 'seen' => true
  274. ];
  275. })->toArray();
  276. $ts = count($stories) ? last($stories)['time'] : null;
  277. $res = [
  278. 'id' => (string) $profile->id,
  279. 'photo' => $profile->avatarUrl(),
  280. 'name' => $profile->username,
  281. 'link' => $profile->url(),
  282. 'lastUpdated' => $ts,
  283. 'seen' => true,
  284. 'items' => $stories
  285. ];
  286. return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  287. }
  288. public function compose(Request $request)
  289. {
  290. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  291. return view('stories.compose');
  292. }
  293. public function iRedirect(Request $request)
  294. {
  295. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  296. $user = $request->user();
  297. abort_if(!$user, 404);
  298. $username = $user->username;
  299. return redirect("/stories/{$username}");
  300. }
  301. }