StoryController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. $groupBy = config('database.default') == 'pgsql' ? 'id' : 'profile_id';
  85. $stories = Story::with('profile')
  86. ->whereIn('profile_id', $following)
  87. ->where('expires_at', '>', now())
  88. ->groupBy($groupBy)
  89. ->orderByDesc('expires_at')
  90. ->take(9)
  91. ->get()
  92. ->map(function($s, $k) {
  93. return [
  94. 'id' => (string) $s->id,
  95. 'photo' => $s->profile->avatarUrl(),
  96. 'name' => $s->profile->username,
  97. 'link' => $s->profile->url(),
  98. 'lastUpdated' => (int) $s->created_at->format('U'),
  99. 'seen' => $s->seen(),
  100. 'items' => [],
  101. 'pid' => (string) $s->profile->id
  102. ];
  103. });
  104. return response()->json($stories, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  105. }
  106. public function apiV1Fetch(Request $request, $id)
  107. {
  108. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  109. $authed = $request->user()->profile;
  110. $profile = Profile::findOrFail($id);
  111. if($id == $authed->id) {
  112. $publicOnly = true;
  113. } else {
  114. $publicOnly = (bool) $profile->followedBy($authed);
  115. }
  116. $stories = Story::whereProfileId($profile->id)
  117. ->orderBy('expires_at', 'desc')
  118. ->where('expires_at', '>', now())
  119. ->when(!$publicOnly, function($query, $publicOnly) {
  120. return $query->wherePublic(true);
  121. })
  122. ->get()
  123. ->map(function($s, $k) {
  124. return [
  125. 'id' => (string) $s->id,
  126. 'type' => 'photo',
  127. 'length' => 3,
  128. 'src' => url(Storage::url($s->path)),
  129. 'preview' => null,
  130. 'link' => null,
  131. 'linkText' => null,
  132. 'time' => $s->created_at->format('U'),
  133. 'expires_at' => (int) $s->expires_at->format('U'),
  134. 'seen' => $s->seen()
  135. ];
  136. })->toArray();
  137. return response()->json($stories, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  138. }
  139. public function apiV1Profile(Request $request, $id)
  140. {
  141. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  142. $authed = $request->user()->profile;
  143. $profile = Profile::findOrFail($id);
  144. if($id == $authed->id) {
  145. $publicOnly = true;
  146. } else {
  147. $publicOnly = (bool) $profile->followedBy($authed);
  148. }
  149. $stories = Story::whereProfileId($profile->id)
  150. ->orderBy('expires_at')
  151. ->where('expires_at', '>', now())
  152. ->when(!$publicOnly, function($query, $publicOnly) {
  153. return $query->wherePublic(true);
  154. })
  155. ->get()
  156. ->map(function($s, $k) {
  157. return [
  158. 'id' => $s->id,
  159. 'type' => 'photo',
  160. 'length' => 3,
  161. 'src' => url(Storage::url($s->path)),
  162. 'preview' => null,
  163. 'link' => null,
  164. 'linkText' => null,
  165. 'time' => $s->created_at->format('U'),
  166. 'expires_at' => (int) $s->expires_at->format('U'),
  167. 'seen' => $s->seen()
  168. ];
  169. })->toArray();
  170. if(count($stories) == 0) {
  171. return [];
  172. }
  173. $cursor = count($stories) - 1;
  174. $stories = [[
  175. 'id' => (string) $stories[$cursor]['id'],
  176. 'photo' => $profile->avatarUrl(),
  177. 'name' => $profile->username,
  178. 'link' => $profile->url(),
  179. 'lastUpdated' => (int) now()->format('U'),
  180. 'seen' => null,
  181. 'items' => $stories,
  182. 'pid' => (string) $profile->id
  183. ]];
  184. return response()->json($stories, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  185. }
  186. public function apiV1Viewed(Request $request)
  187. {
  188. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  189. $this->validate($request, [
  190. 'id' => 'required|integer|min:1|exists:stories',
  191. ]);
  192. StoryView::firstOrCreate([
  193. 'story_id' => $request->input('id'),
  194. 'profile_id' => $request->user()->profile_id
  195. ]);
  196. return ['code' => 200];
  197. }
  198. public function compose(Request $request)
  199. {
  200. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  201. return view('stories.compose');
  202. }
  203. public function apiV1Exists(Request $request, $id)
  204. {
  205. abort_if(!config('instance.stories.enabled'), 404);
  206. $res = (bool) Story::whereProfileId($id)
  207. ->where('expires_at', '>', now())
  208. ->count();
  209. return response()->json($res);
  210. }
  211. public function iRedirect(Request $request)
  212. {
  213. $user = $request->user();
  214. abort_if(!$user, 404);
  215. $username = $user->username;
  216. return redirect("/stories/{$username}");
  217. }
  218. }