StoryController.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 construct()
  16. {
  17. $this->middleware('auth');
  18. }
  19. public function apiV1Add(Request $request)
  20. {
  21. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  22. $this->validate($request, [
  23. 'file' => function() {
  24. return [
  25. 'required',
  26. 'mimes:image/jpeg,image/png',
  27. 'max:' . config('pixelfed.max_photo_size'),
  28. ];
  29. },
  30. ]);
  31. $user = $request->user();
  32. if(Story::whereProfileId($user->profile_id)->where('expires_at', '>', now())->count() >= Story::MAX_PER_DAY) {
  33. abort(400, 'You have reached your limit for new Stories today.');
  34. }
  35. $photo = $request->file('file');
  36. $path = $this->storePhoto($photo);
  37. $story = new Story();
  38. $story->duration = 3;
  39. $story->profile_id = $user->profile_id;
  40. $story->type = 'photo';
  41. $story->mime = $photo->getMimeType();
  42. $story->path = $path;
  43. $story->local = true;
  44. $story->size = $photo->getClientSize();
  45. $story->expires_at = now()->addHours(24);
  46. $story->save();
  47. return [
  48. 'code' => 200,
  49. 'msg' => 'Successfully added',
  50. 'media_url' => url(Storage::url($story->path))
  51. ];
  52. }
  53. protected function storePhoto($photo)
  54. {
  55. $monthHash = substr(hash('sha1', date('Y').date('m')), 0, 12);
  56. $sid = Str::uuid();
  57. $rid = Str::random(6).'.'.Str::random(9);
  58. $mimes = explode(',', config('pixelfed.media_types'));
  59. if(in_array($photo->getMimeType(), [
  60. 'image/jpeg',
  61. 'image/png'
  62. ]) == false) {
  63. abort(400, 'Invalid media type');
  64. return;
  65. }
  66. $storagePath = "public/_esm.t1/{$monthHash}/{$sid}/{$rid}";
  67. $path = $photo->store($storagePath);
  68. $fpath = storage_path('app/' . $path);
  69. $img = Intervention::make($fpath);
  70. $img->orientate();
  71. $img->save($fpath, config('pixelfed.image_quality'));
  72. $img->destroy();
  73. return $path;
  74. }
  75. public function apiV1Delete(Request $request, $id)
  76. {
  77. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  78. $user = $request->user();
  79. $story = Story::whereProfileId($user->profile_id)
  80. ->findOrFail($id);
  81. if(Storage::exists($story->path) == true) {
  82. Storage::delete($story->path);
  83. }
  84. $story->delete();
  85. return [
  86. 'code' => 200,
  87. 'msg' => 'Successfully deleted'
  88. ];
  89. }
  90. public function apiV1Recent(Request $request)
  91. {
  92. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  93. $profile = $request->user()->profile;
  94. $following = $profile->following->pluck('id')->toArray();
  95. $groupBy = config('database.default') == 'pgsql' ? 'id' : 'profile_id';
  96. $stories = Story::with('profile')
  97. ->groupBy($groupBy)
  98. ->whereIn('profile_id', $following)
  99. ->where('expires_at', '>', now())
  100. ->orderByDesc('expires_at')
  101. ->take(9)
  102. ->get()
  103. ->map(function($s, $k) {
  104. return [
  105. 'id' => (string) $s->id,
  106. 'photo' => $s->profile->avatarUrl(),
  107. 'name' => $s->profile->username,
  108. 'link' => $s->profile->url(),
  109. 'lastUpdated' => (int) $s->created_at->format('U'),
  110. 'seen' => $s->seen(),
  111. 'items' => [],
  112. 'pid' => (string) $s->profile->id
  113. ];
  114. });
  115. return response()->json($stories, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  116. }
  117. public function apiV1Fetch(Request $request, $id)
  118. {
  119. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  120. $authed = $request->user()->profile;
  121. $profile = Profile::findOrFail($id);
  122. if($id == $authed->id) {
  123. $publicOnly = true;
  124. } else {
  125. $publicOnly = (bool) $profile->followedBy($authed);
  126. }
  127. $stories = Story::whereProfileId($profile->id)
  128. ->orderBy('expires_at', 'desc')
  129. ->where('expires_at', '>', now())
  130. ->when(!$publicOnly, function($query, $publicOnly) {
  131. return $query->wherePublic(true);
  132. })
  133. ->get()
  134. ->map(function($s, $k) {
  135. return [
  136. 'id' => (string) $s->id,
  137. 'type' => 'photo',
  138. 'length' => 3,
  139. 'src' => url(Storage::url($s->path)),
  140. 'preview' => null,
  141. 'link' => null,
  142. 'linkText' => null,
  143. 'time' => $s->created_at->format('U'),
  144. 'expires_at' => (int) $s->expires_at->format('U'),
  145. 'seen' => $s->seen()
  146. ];
  147. })->toArray();
  148. return response()->json($stories, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  149. }
  150. public function apiV1Profile(Request $request, $id)
  151. {
  152. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  153. $authed = $request->user()->profile;
  154. $profile = Profile::findOrFail($id);
  155. if($id == $authed->id) {
  156. $publicOnly = true;
  157. } else {
  158. $publicOnly = (bool) $profile->followedBy($authed);
  159. }
  160. $stories = Story::whereProfileId($profile->id)
  161. ->orderBy('expires_at')
  162. ->where('expires_at', '>', now())
  163. ->when(!$publicOnly, function($query, $publicOnly) {
  164. return $query->wherePublic(true);
  165. })
  166. ->get()
  167. ->map(function($s, $k) {
  168. return [
  169. 'id' => $s->id,
  170. 'type' => 'photo',
  171. 'length' => 3,
  172. 'src' => url(Storage::url($s->path)),
  173. 'preview' => null,
  174. 'link' => null,
  175. 'linkText' => null,
  176. 'time' => $s->created_at->format('U'),
  177. 'expires_at' => (int) $s->expires_at->format('U'),
  178. 'seen' => $s->seen()
  179. ];
  180. })->toArray();
  181. if(count($stories) == 0) {
  182. return [];
  183. }
  184. $cursor = count($stories) - 1;
  185. $stories = [[
  186. 'id' => (string) $stories[$cursor]['id'],
  187. 'photo' => $profile->avatarUrl(),
  188. 'name' => $profile->username,
  189. 'link' => $profile->url(),
  190. 'lastUpdated' => (int) now()->format('U'),
  191. 'seen' => null,
  192. 'items' => $stories,
  193. 'pid' => (string) $profile->id
  194. ]];
  195. return response()->json($stories, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  196. }
  197. public function apiV1Viewed(Request $request)
  198. {
  199. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  200. $this->validate($request, [
  201. 'id' => 'required|integer|min:1|exists:stories',
  202. ]);
  203. StoryView::firstOrCreate([
  204. 'story_id' => $request->input('id'),
  205. 'profile_id' => $request->user()->profile_id
  206. ]);
  207. return ['code' => 200];
  208. }
  209. public function compose(Request $request)
  210. {
  211. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  212. return view('stories.compose');
  213. }
  214. public function apiV1Exists(Request $request, $id)
  215. {
  216. abort_if(!config('instance.stories.enabled'), 404);
  217. $res = (bool) Story::whereProfileId($id)
  218. ->where('expires_at', '>', now())
  219. ->count();
  220. return response()->json($res);
  221. }
  222. public function iRedirect(Request $request)
  223. {
  224. $user = $request->user();
  225. abort_if(!$user, 404);
  226. $username = $user->username;
  227. return redirect("/stories/{$username}");
  228. }
  229. }