StoryController.php 6.2 KB

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