GroupController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. <?php
  2. namespace App\Http\Controllers;
  3. use DB;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Str;
  6. use App\Models\Group;
  7. use App\Models\GroupActivityGraph;
  8. use App\Models\GroupBlock;
  9. use App\Models\GroupCategory;
  10. use App\Models\GroupComment;
  11. use App\Models\GroupEvent;
  12. use App\Models\GroupInteraction;
  13. use App\Models\GroupInvitation;
  14. use App\Models\GroupLimit;
  15. use App\Models\GroupLike;
  16. use App\Models\GroupMember;
  17. use App\Models\GroupPost;
  18. use App\Models\GroupPostHashtag;
  19. use App\Models\GroupReport;
  20. use App\Models\GroupRole;
  21. use App\Models\GroupStore;
  22. use App\Models\Poll;
  23. use App\Follower;
  24. use App\Instance;
  25. use App\Hashtag;
  26. use App\StatusHashtag;
  27. use App\Like;
  28. use App\Media;
  29. use App\Notification;
  30. use App\Profile;
  31. use App\Status;
  32. use App\User;
  33. use App\Util\Lexer\Autolink;
  34. use App\Services\AccountService;
  35. use App\Services\FollowerService;
  36. use App\Services\HashidService;
  37. use App\Services\LikeService;
  38. use App\Services\Groups\GroupCommentService;
  39. use App\Services\Groups\GroupsLikeService;
  40. use App\Services\HashtagService;
  41. use App\Services\GroupService;
  42. use App\Services\GroupFeedService;
  43. use App\Services\GroupPostService;
  44. use App\Services\PollService;
  45. use App\Services\RelationshipService;
  46. use App\Services\StatusService;
  47. use App\Services\UserFilterService;
  48. use Cache;
  49. use Storage;
  50. use Purify;
  51. use App\Jobs\GroupPipeline\LikePipeline;
  52. use App\Jobs\GroupPipeline\UnlikePipeline;
  53. use App\Jobs\ImageOptimizePipeline\ImageOptimize;
  54. use App\Jobs\VideoPipeline\VideoThumbnail;
  55. use App\Jobs\StatusPipeline\StatusDelete;
  56. use App\Jobs\GroupPipeline\GroupCommentPipeline;
  57. use App\Jobs\GroupPipeline\GroupMemberInvite;
  58. use App\Jobs\GroupPipeline\NewStatusPipeline;
  59. use App\Jobs\GroupPipeline\JoinApproved;
  60. use App\Jobs\GroupPipeline\JoinRejected;
  61. use Illuminate\Support\Facades\RateLimiter;
  62. class GroupController extends GroupFederationController
  63. {
  64. public function __construct()
  65. {
  66. // $this->middleware('auth');
  67. }
  68. public function index(Request $request)
  69. {
  70. abort_if(!$request->user(), 404);
  71. return view('layouts.spa');
  72. }
  73. public function home(Request $request)
  74. {
  75. abort_if(!$request->user(), 404);
  76. return view('layouts.spa');
  77. }
  78. public function show(Request $request, $id, $path = false)
  79. {
  80. $group = Group::find($id);
  81. if(!$group || $group->status) {
  82. return response()->view('groups.unavailable')->setStatusCode(404);
  83. }
  84. if($request->wantsJson()) {
  85. return $this->showGroupObject($group);
  86. }
  87. return view('layouts.spa', compact('id', 'path'));
  88. }
  89. public function showStatus(Request $request, $gid, $sid)
  90. {
  91. $group = Group::find($gid);
  92. $pid = optional($request->user())->profile_id ?? false;
  93. if(!$group || $group->status) {
  94. return response()->view('groups.unavailable')->setStatusCode(404);
  95. }
  96. if($group->is_private) {
  97. abort_if(!$request->user(), 404);
  98. abort_if(!$group->isMember($pid), 404);
  99. }
  100. $gp = GroupPost::whereGroupId($gid)
  101. ->findOrFail($sid);
  102. return view('layouts.spa', compact('group', 'gp'));
  103. }
  104. public function getGroup(Request $request, $id)
  105. {
  106. $group = Group::whereNull('status')->findOrFail($id);
  107. $pid = optional($request->user())->profile_id ?? false;
  108. $group = $this->toJson($group, $pid);
  109. return response()->json($group, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  110. }
  111. public function showStatusLikes(Request $request, $id, $sid)
  112. {
  113. $group = Group::findOrFail($id);
  114. $user = $request->user();
  115. $pid = $user->profile_id;
  116. abort_if(!$group->isMember($pid), 404);
  117. $status = GroupPost::whereGroupId($id)->findOrFail($sid);
  118. $likes = GroupLike::whereStatusId($sid)
  119. ->cursorPaginate(10)
  120. ->map(function($l) use($group) {
  121. $account = AccountService::get($l->profile_id);
  122. $account['url'] = "/groups/{$group->id}/user/{$account['id']}";
  123. return $account;
  124. })
  125. ->filter(function($l) {
  126. return $l && isset($l['id']);
  127. })
  128. ->values();
  129. return $likes;
  130. }
  131. public function groupSettings(Request $request, $id)
  132. {
  133. abort_if(!$request->user(), 404);
  134. $group = Group::findOrFail($id);
  135. $pid = $request->user()->profile_id;
  136. abort_if(!$group->isMember($pid), 404);
  137. abort_if(!in_array($group->selfRole($pid), ['founder', 'admin']), 404);
  138. return view('groups.settings', compact('group'));
  139. }
  140. public function joinGroup(Request $request, $id)
  141. {
  142. $group = Group::findOrFail($id);
  143. $pid = $request->user()->profile_id;
  144. abort_if($group->isMember($pid), 404);
  145. if(!$request->user()->is_admin) {
  146. abort_if(GroupService::getRejoinTimeout($group->id, $pid), 422, 'Cannot re-join this group for 24 hours after leaving or cancelling a request to join');
  147. }
  148. $member = new GroupMember;
  149. $member->group_id = $group->id;
  150. $member->profile_id = $pid;
  151. $member->role = 'member';
  152. $member->local_group = true;
  153. $member->local_profile = true;
  154. $member->join_request = $group->is_private;
  155. $member->save();
  156. GroupService::delSelf($group->id, $pid);
  157. GroupService::log(
  158. $group->id,
  159. $pid,
  160. 'group:joined',
  161. null,
  162. GroupMember::class,
  163. $member->id
  164. );
  165. $group = $this->toJson($group, $pid);
  166. return $group;
  167. }
  168. public function updateGroup(Request $request, $id)
  169. {
  170. $this->validate($request, [
  171. 'description' => 'nullable|max:500',
  172. 'membership' => 'required|in:all,local,private',
  173. 'avatar' => 'nullable',
  174. 'header' => 'nullable',
  175. 'discoverable' => 'required',
  176. 'activitypub' => 'required',
  177. 'is_nsfw' => 'required',
  178. 'category' => 'required|string|in:' . implode(',',GroupService::categories())
  179. ]);
  180. $pid = $request->user()->profile_id;
  181. $group = Group::whereProfileId($pid)->findOrFail($id);
  182. $member = GroupMember::whereGroupId($group->id)->whereProfileId($pid)->firstOrFail();
  183. abort_if($member->role != 'founder', 403, 'Invalid group permission');
  184. $metadata = $group->metadata;
  185. $len = $group->is_private ? 12 : 4;
  186. if($request->hasFile('avatar')) {
  187. $avatar = $request->file('avatar');
  188. if($avatar) {
  189. if( isset($metadata['avatar']) &&
  190. isset($metadata['avatar']['path']) &&
  191. Storage::exists($metadata['avatar']['path'])
  192. ) {
  193. Storage::delete($metadata['avatar']['path']);
  194. }
  195. $fileName = 'avatar_' . strtolower(str_random($len)) . '.' . $avatar->extension();
  196. $path = $avatar->storePubliclyAs('public/g/'.$group->id.'/meta', $fileName);
  197. $url = url(Storage::url($path));
  198. $metadata['avatar'] = [
  199. 'path' => $path,
  200. 'url' => $url,
  201. 'updated_at' => now()
  202. ];
  203. }
  204. }
  205. if($request->hasFile('header')) {
  206. $header = $request->file('header');
  207. if($header) {
  208. if( isset($metadata['header']) &&
  209. isset($metadata['header']['path']) &&
  210. Storage::exists($metadata['header']['path'])
  211. ) {
  212. Storage::delete($metadata['header']['path']);
  213. }
  214. $fileName = 'header_' . strtolower(str_random($len)) . '.' . $header->extension();
  215. $path = $header->storePubliclyAs('public/g/'.$group->id.'/meta', $fileName);
  216. $url = url(Storage::url($path));
  217. $metadata['header'] = [
  218. 'path' => $path,
  219. 'url' => $url,
  220. 'updated_at' => now()
  221. ];
  222. }
  223. }
  224. $cat = GroupService::categoryById($group->category_id);
  225. if($request->category !== $cat['name']) {
  226. $group->category_id = GroupCategory::whereName($request->category)->first()->id;
  227. }
  228. $changes = null;
  229. $group->description = e($request->input('description', null));
  230. $group->is_private = $request->input('membership') == 'private';
  231. $group->local_only = $request->input('membership') == 'local';
  232. $group->activitypub = $request->input('activitypub') == "true";
  233. $group->discoverable = $request->input('discoverable') == "true";
  234. $group->is_nsfw = $request->input('is_nsfw') == "true";
  235. $group->metadata = $metadata;
  236. if($group->isDirty()) {
  237. $changes = $group->getDirty();
  238. }
  239. $group->save();
  240. GroupService::log(
  241. $group->id,
  242. $pid,
  243. 'group:settings:updated',
  244. $changes
  245. );
  246. GroupService::del($group->id);
  247. $res = $this->toJson($group, $pid);
  248. return $res;
  249. }
  250. protected function toJson($group, $pid = false)
  251. {
  252. return GroupService::get($group->id, $pid);
  253. }
  254. public function groupLeave(Request $request, $id)
  255. {
  256. abort_if(!$request->user(), 404);
  257. $pid = $request->user()->profile_id;
  258. $group = Group::findOrFail($id);
  259. abort_if($pid == $group->profile_id, 422, 'Cannot leave a group you created');
  260. abort_if(!$group->isMember($pid), 403, 'Not a member of group.');
  261. GroupMember::whereGroupId($group->id)->whereProfileId($pid)->delete();
  262. GroupService::del($group->id);
  263. GroupService::delSelf($group->id, $pid);
  264. GroupService::setRejoinTimeout($group->id, $pid);
  265. return [200];
  266. }
  267. public function cancelJoinRequest(Request $request, $id)
  268. {
  269. abort_if(!$request->user(), 404);
  270. $pid = $request->user()->profile_id;
  271. $group = Group::findOrFail($id);
  272. abort_if($pid == $group->profile_id, 422, 'Cannot leave a group you created');
  273. abort_if($group->isMember($pid), 422, 'Cannot cancel approved join request, please leave group instead.');
  274. GroupMember::whereGroupId($group->id)->whereProfileId($pid)->delete();
  275. GroupService::del($group->id);
  276. GroupService::delSelf($group->id, $pid);
  277. GroupService::setRejoinTimeout($group->id, $pid);
  278. return [200];
  279. }
  280. public function metaBlockSearch(Request $request, $id)
  281. {
  282. abort_if(!$request->user(), 404);
  283. $group = Group::findOrFail($id);
  284. $pid = $request->user()->profile_id;
  285. abort_if(!$group->isMember($pid), 404);
  286. abort_if(!in_array($group->selfRole($pid), ['founder', 'admin']), 404);
  287. $type = $request->input('type');
  288. $item = $request->input('item');
  289. switch($type) {
  290. case 'instance':
  291. $res = Instance::whereDomain($item)->first();
  292. if($res) {
  293. abort_if(GroupBlock::whereGroupId($group->id)->whereInstanceId($res->id)->exists(), 400);
  294. }
  295. break;
  296. case 'user':
  297. $res = Profile::whereUsername($item)->first();
  298. if($res) {
  299. abort_if(GroupBlock::whereGroupId($group->id)->whereProfileId($res->id)->exists(), 400);
  300. }
  301. if($res->user_id != null) {
  302. abort_if(User::whereIsAdmin(true)->whereId($res->user_id)->exists(), 400);
  303. }
  304. break;
  305. }
  306. return response()->json((bool) $res, ($res ? 200 : 404));
  307. }
  308. public function reportCreate(Request $request, $id)
  309. {
  310. abort_if(!$request->user(), 404);
  311. $group = Group::findOrFail($id);
  312. $pid = $request->user()->profile_id;
  313. abort_if(!$group->isMember($pid), 404);
  314. $id = $request->input('id');
  315. $type = $request->input('type');
  316. $types = [
  317. // original 3
  318. 'spam',
  319. 'sensitive',
  320. 'abusive',
  321. // new
  322. 'underage',
  323. 'violence',
  324. 'copyright',
  325. 'impersonation',
  326. 'scam',
  327. 'terrorism'
  328. ];
  329. $gp = GroupPost::whereGroupId($group->id)->find($id);
  330. abort_if(!$gp, 422, 'Cannot report an invalid or deleted post');
  331. abort_if(!in_array($type, $types), 422, 'Invalid report type');
  332. abort_if($gp->profile_id === $pid, 422, 'Cannot report your own post');
  333. abort_if(
  334. GroupReport::whereGroupId($group->id)
  335. ->whereProfileId($pid)
  336. ->whereItemType(GroupPost::class)
  337. ->whereItemId($id)
  338. ->exists(),
  339. 422,
  340. 'You already reported this'
  341. );
  342. $report = new GroupReport();
  343. $report->group_id = $group->id;
  344. $report->profile_id = $pid;
  345. $report->type = $type;
  346. $report->item_type = GroupPost::class;
  347. $report->item_id = $id;
  348. $report->open = true;
  349. $report->save();
  350. GroupService::log(
  351. $group->id,
  352. $pid,
  353. 'group:report:create',
  354. [
  355. 'type' => $type,
  356. 'report_id' => $report->id,
  357. 'status_id' => $gp->status_id,
  358. 'profile_id' => $gp->profile_id,
  359. 'username' => optional(AccountService::get($gp->profile_id))['acct'],
  360. 'gpid' => $gp->id,
  361. 'url' => $gp->url()
  362. ],
  363. GroupReport::class,
  364. $report->id
  365. );
  366. return response([200]);
  367. }
  368. public function reportAction(Request $request, $id)
  369. {
  370. abort_if(!$request->user(), 404);
  371. $group = Group::findOrFail($id);
  372. $pid = $request->user()->profile_id;
  373. abort_if(!$group->isMember($pid), 404);
  374. abort_if(!in_array($group->selfRole($pid), ['founder', 'admin']), 404);
  375. $this->validate($request, [
  376. 'action' => 'required|in:cw,delete,ignore',
  377. 'id' => 'required|string'
  378. ]);
  379. $action = $request->input('action');
  380. $id = $request->input('id');
  381. $report = GroupReport::whereGroupId($group->id)
  382. ->findOrFail($id);
  383. $status = Status::findOrFail($report->item_id);
  384. $gp = GroupPost::whereGroupId($group->id)
  385. ->whereStatusId($status->id)
  386. ->firstOrFail();
  387. switch ($action) {
  388. case 'cw':
  389. $status->is_nsfw = true;
  390. $status->save();
  391. StatusService::del($status->id);
  392. GroupReport::whereGroupId($group->id)
  393. ->whereItemType($report->item_type)
  394. ->whereItemId($report->item_id)
  395. ->update(['open' => false]);
  396. GroupService::log(
  397. $group->id,
  398. $pid,
  399. 'group:moderation:action',
  400. [
  401. 'type' => 'cw',
  402. 'report_id' => $report->id,
  403. 'status_id' => $status->id,
  404. 'profile_id' => $status->profile_id,
  405. 'status_url' => $gp->url()
  406. ],
  407. GroupReport::class,
  408. $report->id
  409. );
  410. return response()->json([200]);
  411. break;
  412. case 'ignore':
  413. GroupReport::whereGroupId($group->id)
  414. ->whereItemType($report->item_type)
  415. ->whereItemId($report->item_id)
  416. ->update(['open' => false]);
  417. GroupService::log(
  418. $group->id,
  419. $pid,
  420. 'group:moderation:action',
  421. [
  422. 'type' => 'ignore',
  423. 'report_id' => $report->id,
  424. 'status_id' => $status->id,
  425. 'profile_id' => $status->profile_id,
  426. 'status_url' => $gp->url()
  427. ],
  428. GroupReport::class,
  429. $report->id
  430. );
  431. return response()->json([200]);
  432. break;
  433. }
  434. }
  435. public function getMemberInteractionLimits(Request $request, $id)
  436. {
  437. abort_if(!$request->user(), 404);
  438. $group = Group::findOrFail($id);
  439. $pid = $request->user()->profile_id;
  440. abort_if(!$group->isMember($pid), 404);
  441. abort_if(!in_array($group->selfRole($pid), ['founder', 'admin']), 404);
  442. $profile_id = $request->input('profile_id');
  443. abort_if(!$group->isMember($profile_id), 404);
  444. $limits = GroupService::getInteractionLimits($group->id, $profile_id);
  445. return response()->json($limits);
  446. }
  447. public function updateMemberInteractionLimits(Request $request, $id)
  448. {
  449. abort_if(!$request->user(), 404);
  450. $group = Group::findOrFail($id);
  451. $pid = $request->user()->profile_id;
  452. abort_if(!$group->isMember($pid), 404);
  453. abort_if(!in_array($group->selfRole($pid), ['founder', 'admin']), 404);
  454. $this->validate($request, [
  455. 'profile_id' => 'required|exists:profiles,id',
  456. 'can_post' => 'required',
  457. 'can_comment' => 'required',
  458. 'can_like' => 'required'
  459. ]);
  460. $member = $request->input('profile_id');
  461. $can_post = $request->input('can_post');
  462. $can_comment = $request->input('can_comment');
  463. $can_like = $request->input('can_like');
  464. $account = AccountService::get($member);
  465. abort_if(!$account, 422, 'Invalid profile');
  466. abort_if(!$group->isMember($member), 422, 'Invalid profile');
  467. $limit = GroupLimit::firstOrCreate([
  468. 'profile_id' => $member,
  469. 'group_id' => $group->id
  470. ]);
  471. if($limit->wasRecentlyCreated) {
  472. abort_if(GroupLimit::whereGroupId($group->id)->count() >= 25, 422, 'limit_reached');
  473. }
  474. $previousLimits = $limit->limits;
  475. $limit->limits = [
  476. 'can_post' => $can_post,
  477. 'can_comment' => $can_comment,
  478. 'can_like' => $can_like
  479. ];
  480. $limit->save();
  481. GroupService::clearInteractionLimits($group->id, $member);
  482. GroupService::log(
  483. $group->id,
  484. $pid,
  485. 'group:member-limits:updated',
  486. [
  487. 'profile_id' => $account['id'],
  488. 'username' => $account['username'],
  489. 'previousLimits' => $previousLimits,
  490. 'newLimits' => $limit->limits
  491. ],
  492. GroupLimit::class,
  493. $limit->id
  494. );
  495. return $request->all();
  496. }
  497. public function showProfile(Request $request, $id, $pid)
  498. {
  499. $group = Group::find($id);
  500. if(!$group || $group->status) {
  501. return response()->view('groups.unavailable')->setStatusCode(404);
  502. }
  503. return view('layouts.spa');
  504. }
  505. public function showProfileByUsername(Request $request, $id, $pid)
  506. {
  507. abort_if(!$request->user(), 404);
  508. if(!$request->user()) {
  509. return redirect("/{$pid}");
  510. }
  511. $group = Group::find($id);
  512. $cid = $request->user()->profile_id;
  513. if(!$group || $group->status) {
  514. return response()->view('groups.unavailable')->setStatusCode(404);
  515. }
  516. if(!$group->isMember($cid)) {
  517. return redirect("/{$pid}");
  518. }
  519. $profile = Profile::whereUsername($pid)->first();
  520. if(!$group->isMember($profile->id)) {
  521. return redirect("/{$pid}");
  522. }
  523. if($profile) {
  524. $url = url("/groups/{$id}/user/{$profile->id}");
  525. return redirect($url);
  526. }
  527. abort(404, 'Invalid username');
  528. }
  529. public function groupInviteLanding(Request $request, $id)
  530. {
  531. abort(404, 'Not yet implemented');
  532. $group = Group::findOrFail($id);
  533. return view('groups.invite', compact('group'));
  534. }
  535. public function groupShortLinkRedirect(Request $request, $hid)
  536. {
  537. $gid = HashidService::decode($hid);
  538. $group = Group::findOrFail($gid);
  539. return redirect($group->url());
  540. }
  541. public function groupInviteClaim(Request $request, $id)
  542. {
  543. $group = GroupService::get($id);
  544. abort_if(!$group || empty($group), 404);
  545. return view('groups.invite-claim', compact('group'));
  546. }
  547. public function groupMemberInviteCheck(Request $request, $id)
  548. {
  549. abort_if(!$request->user(), 404);
  550. $pid = $request->user()->profile_id;
  551. $group = Group::findOrFail($id);
  552. abort_if($group->isMember($pid), 422, 'Already a member');
  553. $exists = GroupInvitation::whereGroupId($id)->whereToProfileId($pid)->exists();
  554. return response()->json([
  555. 'gid' => $id,
  556. 'can_join' => (bool) $exists
  557. ]);
  558. }
  559. public function groupMemberInviteAccept(Request $request, $id)
  560. {
  561. abort_if(!$request->user(), 404);
  562. $pid = $request->user()->profile_id;
  563. $group = Group::findOrFail($id);
  564. abort_if($group->isMember($pid), 422, 'Already a member');
  565. abort_if(!GroupInvitation::whereGroupId($id)->whereToProfileId($pid)->exists(), 422);
  566. $gm = new GroupMember;
  567. $gm->group_id = $id;
  568. $gm->profile_id = $pid;
  569. $gm->role = 'member';
  570. $gm->local_group = $group->local;
  571. $gm->local_profile = true;
  572. $gm->join_request = false;
  573. $gm->save();
  574. GroupInvitation::whereGroupId($id)->whereToProfileId($pid)->delete();
  575. GroupService::del($id);
  576. GroupService::delSelf($id, $pid);
  577. return ['next_url' => $group->url()];
  578. }
  579. public function groupMemberInviteDecline(Request $request, $id)
  580. {
  581. abort_if(!$request->user(), 404);
  582. $pid = $request->user()->profile_id;
  583. $group = Group::findOrFail($id);
  584. abort_if($group->isMember($pid), 422, 'Already a member');
  585. return ['next_url' => '/'];
  586. }
  587. }