1
0

StoryController.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. if(config('database.default') == 'pgsql') {
  92. $db = Story::with('profile')
  93. ->whereIn('profile_id', $following)
  94. ->where('expires_at', '>', now())
  95. ->distinct('profile_id')
  96. ->take(9)
  97. ->get();
  98. } else {
  99. $db = Story::with('profile')
  100. ->whereIn('profile_id', $following)
  101. ->where('expires_at', '>', now())
  102. ->orderByDesc('expires_at')
  103. ->groupBy('profile_id')
  104. ->take(9)
  105. ->get();
  106. }
  107. $stories = $db->map(function($s, $k) {
  108. return [
  109. 'id' => (string) $s->id,
  110. 'photo' => $s->profile->avatarUrl(),
  111. 'name' => $s->profile->username,
  112. 'link' => $s->profile->url(),
  113. 'lastUpdated' => (int) $s->created_at->format('U'),
  114. 'seen' => $s->seen(),
  115. 'items' => [],
  116. 'pid' => (string) $s->profile->id
  117. ];
  118. });
  119. return response()->json($stories, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  120. }
  121. public function apiV1Fetch(Request $request, $id)
  122. {
  123. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  124. $authed = $request->user()->profile;
  125. $profile = Profile::findOrFail($id);
  126. if($id == $authed->id) {
  127. $publicOnly = true;
  128. } else {
  129. $publicOnly = (bool) $profile->followedBy($authed);
  130. }
  131. $stories = Story::whereProfileId($profile->id)
  132. ->orderBy('expires_at', 'desc')
  133. ->where('expires_at', '>', now())
  134. ->when(!$publicOnly, function($query, $publicOnly) {
  135. return $query->wherePublic(true);
  136. })
  137. ->get()
  138. ->map(function($s, $k) {
  139. return [
  140. 'id' => (string) $s->id,
  141. 'type' => 'photo',
  142. 'length' => 3,
  143. 'src' => url(Storage::url($s->path)),
  144. 'preview' => null,
  145. 'link' => null,
  146. 'linkText' => null,
  147. 'time' => $s->created_at->format('U'),
  148. 'expires_at' => (int) $s->expires_at->format('U'),
  149. 'seen' => $s->seen()
  150. ];
  151. })->toArray();
  152. return response()->json($stories, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  153. }
  154. public function apiV1Item(Request $request, $id)
  155. {
  156. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  157. $authed = $request->user()->profile;
  158. $story = Story::with('profile')
  159. ->where('expires_at', '>', now())
  160. ->findOrFail($id);
  161. $profile = $story->profile;
  162. if($story->profile_id == $authed->id) {
  163. $publicOnly = true;
  164. } else {
  165. $publicOnly = (bool) $profile->followedBy($authed);
  166. }
  167. abort_if(!$publicOnly, 403);
  168. $res = [
  169. 'id' => (string) $story->id,
  170. 'type' => 'photo',
  171. 'length' => 3,
  172. 'src' => url(Storage::url($story->path)),
  173. 'preview' => null,
  174. 'link' => null,
  175. 'linkText' => null,
  176. 'time' => $story->created_at->format('U'),
  177. 'expires_at' => (int) $story->expires_at->format('U'),
  178. 'seen' => $story->seen()
  179. ];
  180. return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  181. }
  182. public function apiV1Profile(Request $request, $id)
  183. {
  184. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  185. $authed = $request->user()->profile;
  186. $profile = Profile::findOrFail($id);
  187. if($id == $authed->id) {
  188. $publicOnly = true;
  189. } else {
  190. $publicOnly = (bool) $profile->followedBy($authed);
  191. }
  192. $stories = Story::whereProfileId($profile->id)
  193. ->orderBy('expires_at')
  194. ->where('expires_at', '>', now())
  195. ->when(!$publicOnly, function($query, $publicOnly) {
  196. return $query->wherePublic(true);
  197. })
  198. ->get()
  199. ->map(function($s, $k) {
  200. return [
  201. 'id' => $s->id,
  202. 'type' => 'photo',
  203. 'length' => 3,
  204. 'src' => url(Storage::url($s->path)),
  205. 'preview' => null,
  206. 'link' => null,
  207. 'linkText' => null,
  208. 'time' => $s->created_at->format('U'),
  209. 'expires_at' => (int) $s->expires_at->format('U'),
  210. 'seen' => $s->seen()
  211. ];
  212. })->toArray();
  213. if(count($stories) == 0) {
  214. return [];
  215. }
  216. $cursor = count($stories) - 1;
  217. $stories = [[
  218. 'id' => (string) $stories[$cursor]['id'],
  219. 'photo' => $profile->avatarUrl(),
  220. 'name' => $profile->username,
  221. 'link' => $profile->url(),
  222. 'lastUpdated' => (int) now()->format('U'),
  223. 'seen' => null,
  224. 'items' => $stories,
  225. 'pid' => (string) $profile->id
  226. ]];
  227. return response()->json($stories, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  228. }
  229. public function apiV1Viewed(Request $request)
  230. {
  231. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  232. $this->validate($request, [
  233. 'id' => 'required|integer|min:1|exists:stories',
  234. ]);
  235. $id = $request->input('id');
  236. $authed = $request->user()->profile;
  237. $story = Story::with('profile')
  238. ->where('expires_at', '>', now())
  239. ->orderByDesc('expires_at')
  240. ->findOrFail($id);
  241. $profile = $story->profile;
  242. if($story->profile_id == $authed->id) {
  243. $publicOnly = true;
  244. } else {
  245. $publicOnly = (bool) $profile->followedBy($authed);
  246. }
  247. abort_if(!$publicOnly, 403);
  248. StoryView::firstOrCreate([
  249. 'story_id' => $id,
  250. 'profile_id' => $authed->id
  251. ]);
  252. return ['code' => 200];
  253. }
  254. public function apiV1Exists(Request $request, $id)
  255. {
  256. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  257. $res = (bool) Story::whereProfileId($id)
  258. ->where('expires_at', '>', now())
  259. ->count();
  260. return response()->json($res);
  261. }
  262. public function apiV1Me(Request $request)
  263. {
  264. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  265. $profile = $request->user()->profile;
  266. $stories = Story::whereProfileId($profile->id)
  267. ->orderBy('expires_at')
  268. ->where('expires_at', '>', now())
  269. ->get()
  270. ->map(function($s, $k) {
  271. return [
  272. 'id' => $s->id,
  273. 'type' => 'photo',
  274. 'length' => 3,
  275. 'src' => url(Storage::url($s->path)),
  276. 'preview' => null,
  277. 'link' => null,
  278. 'linkText' => null,
  279. 'time' => $s->created_at->format('U'),
  280. 'expires_at' => (int) $s->expires_at->format('U'),
  281. 'seen' => true
  282. ];
  283. })->toArray();
  284. $ts = count($stories) ? last($stories)['time'] : null;
  285. $res = [
  286. 'id' => (string) $profile->id,
  287. 'photo' => $profile->avatarUrl(),
  288. 'name' => $profile->username,
  289. 'link' => $profile->url(),
  290. 'lastUpdated' => $ts,
  291. 'seen' => true,
  292. 'items' => $stories
  293. ];
  294. return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  295. }
  296. public function compose(Request $request)
  297. {
  298. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  299. return view('stories.compose');
  300. }
  301. public function iRedirect(Request $request)
  302. {
  303. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  304. $user = $request->user();
  305. abort_if(!$user, 404);
  306. $username = $user->username;
  307. return redirect("/stories/{$username}");
  308. }
  309. }