StoryController.php 9.3 KB

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