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