GroupService.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <?php
  2. namespace App\Services;
  3. use App\Profile;
  4. use App\Models\Group;
  5. use App\Models\GroupCategory;
  6. use App\Models\GroupMember;
  7. use App\Models\GroupPost;
  8. use App\Models\GroupInteraction;
  9. use App\Models\GroupLimit;
  10. use App\Util\ActivityPub\Helpers;
  11. use Cache;
  12. use Purify;
  13. use App\Http\Resources\Groups\GroupResource;
  14. class GroupService
  15. {
  16. const CACHE_KEY = 'pf:services:groups:';
  17. protected static function key($name)
  18. {
  19. return self::CACHE_KEY . $name;
  20. }
  21. public static function get($id, $pid = false)
  22. {
  23. $res = Cache::remember(
  24. self::key($id),
  25. 1209600,
  26. function() use($id, $pid) {
  27. $group = (new Group)->withoutRelations()->whereNull('status')->find($id);
  28. if(!$group) {
  29. return null;
  30. }
  31. $admin = $group->profile_id ? AccountService::get($group->profile_id) : null;
  32. return [
  33. 'id' => (string) $group->id,
  34. 'name' => $group->name,
  35. 'description' => $group->description,
  36. 'short_description' => str_limit(strip_tags($group->description), 120),
  37. 'category' => self::categoryById($group->category_id),
  38. 'local' => (bool) $group->local,
  39. 'url' => $group->url(),
  40. 'shorturl' => url('/g/'.HashidService::encode($group->id)),
  41. 'membership' => $group->getMembershipType(),
  42. 'member_count' => $group->members()->whereJoinRequest(false)->count(),
  43. 'verified' => false,
  44. 'self' => null,
  45. 'admin' => $admin,
  46. 'config' => [
  47. 'recommended' => (bool) $group->recommended,
  48. 'discoverable' => (bool) $group->discoverable,
  49. 'activitypub' => (bool) $group->activitypub,
  50. 'is_nsfw' => (bool) $group->is_nsfw,
  51. 'dms' => (bool) $group->dms
  52. ],
  53. 'metadata' => $group->metadata,
  54. 'created_at' => $group->created_at->toAtomString(),
  55. ];
  56. }
  57. );
  58. if($pid) {
  59. $res['self'] = self::getSelf($id, $pid);
  60. }
  61. return $res;
  62. }
  63. public static function del($id)
  64. {
  65. Cache::forget('ap:groups:object:' . $id);
  66. return Cache::forget(self::key($id));
  67. }
  68. public static function getSelf($gid, $pid)
  69. {
  70. return Cache::remember(
  71. self::key('self:gid-' . $gid . ':pid-' . $pid),
  72. 3600,
  73. function() use($gid, $pid) {
  74. $group = Group::find($gid);
  75. if(!$gid || !$pid) {
  76. return [
  77. 'is_member' => false,
  78. 'role' => null,
  79. 'is_requested' => null
  80. ];
  81. }
  82. return [
  83. 'is_member' => $group->isMember($pid),
  84. 'role' => $group->selfRole($pid),
  85. 'is_requested' => optional($group->members()->whereProfileId($pid)->first())->join_request ?? false
  86. ];
  87. }
  88. );
  89. }
  90. public static function delSelf($gid, $pid)
  91. {
  92. Cache::forget(self::key("is_member:{$gid}:{$pid}"));
  93. return Cache::forget(self::key('self:gid-' . $gid . ':pid-' . $pid));
  94. }
  95. public static function sidToGid($gid, $pid)
  96. {
  97. return Cache::remember(self::key('s2gid:' . $gid . ':' . $pid), 3600, function() use($gid, $pid) {
  98. return optional(GroupPost::whereGroupId($gid)->whereStatusId($pid)->first())->id;
  99. });
  100. }
  101. public static function membershipsByPid($pid)
  102. {
  103. return Cache::remember(self::key("mbpid:{$pid}"), 3600, function() use($pid) {
  104. return GroupMember::whereProfileId($pid)->pluck('group_id');
  105. });
  106. }
  107. public static function config()
  108. {
  109. return [
  110. 'enabled' => config('exp.gps') ?? false,
  111. 'limits' => [
  112. 'group' => [
  113. 'max' => 999,
  114. 'federation' => false,
  115. ],
  116. 'user' => [
  117. 'create' => [
  118. 'new' => true,
  119. 'max' => 10
  120. ],
  121. 'join' => [
  122. 'max' => 10
  123. ],
  124. 'invite' => [
  125. 'max' => 20
  126. ]
  127. ]
  128. ],
  129. 'guest' => [
  130. 'public' => false
  131. ]
  132. ];
  133. }
  134. public static function fetchRemote($url)
  135. {
  136. // todo: refactor this demo
  137. $res = Helpers::fetchFromUrl($url);
  138. if(!$res || !isset($res['type']) || $res['type'] != 'Group') {
  139. return false;
  140. }
  141. $group = Group::whereRemoteUrl($url)->first();
  142. if($group) {
  143. return $group;
  144. }
  145. $group = new Group;
  146. $group->remote_url = $res['url'];
  147. $group->name = $res['name'];
  148. $group->inbox_url = $res['inbox'];
  149. $group->metadata = [
  150. 'header' => [
  151. 'url' => $res['icon']['image']['url']
  152. ]
  153. ];
  154. $group->description = Purify::clean($res['summary']);
  155. $group->local = false;
  156. $group->save();
  157. return $group->url();
  158. }
  159. public static function log(
  160. string $groupId,
  161. string $profileId,
  162. string $type = null,
  163. array $meta = null,
  164. string $itemType = null,
  165. string $itemId = null
  166. )
  167. {
  168. // todo: truncate (some) metadata after XX days in cron/queue
  169. $log = new GroupInteraction;
  170. $log->group_id = $groupId;
  171. $log->profile_id = $profileId;
  172. $log->type = $type;
  173. $log->item_type = $itemType;
  174. $log->item_id = $itemId;
  175. $log->metadata = $meta;
  176. $log->save();
  177. }
  178. public static function getRejoinTimeout($gid, $pid)
  179. {
  180. $key = self::key('rejoin-timeout:gid-' . $gid . ':pid-' . $pid);
  181. return Cache::has($key);
  182. }
  183. public static function setRejoinTimeout($gid, $pid)
  184. {
  185. // todo: allow group admins to manually remove timeout
  186. $key = self::key('rejoin-timeout:gid-' . $gid . ':pid-' . $pid);
  187. return Cache::put($key, 1, 86400);
  188. }
  189. public static function getMemberInboxes($id)
  190. {
  191. // todo: cache this, maybe add join/leave methods to this service to handle cache invalidation
  192. $group = (new Group)->withoutRelations()->findOrFail($id);
  193. if(!$group->local) {
  194. return [];
  195. }
  196. $members = GroupMember::whereGroupId($id)->whereLocalProfile(false)->pluck('profile_id');
  197. return Profile::find($members)->map(function($u) {
  198. return $u->sharedInbox ?? $u->inbox_url;
  199. })->toArray();
  200. }
  201. public static function getInteractionLimits($gid, $pid)
  202. {
  203. return Cache::remember(self::key(":il:{$gid}:{$pid}"), 3600, function() use($gid, $pid) {
  204. $limit = GroupLimit::whereGroupId($gid)->whereProfileId($pid)->first();
  205. if(!$limit) {
  206. return [
  207. 'limits' => [
  208. 'can_post' => true,
  209. 'can_comment' => true,
  210. 'can_like' => true
  211. ],
  212. 'updated_at' => null
  213. ];
  214. }
  215. return [
  216. 'limits' => $limit->limits,
  217. 'updated_at' => $limit->updated_at->format('c')
  218. ];
  219. });
  220. }
  221. public static function clearInteractionLimits($gid, $pid)
  222. {
  223. return Cache::forget(self::key(":il:{$gid}:{$pid}"));
  224. }
  225. public static function canPost($gid, $pid)
  226. {
  227. $limits = self::getInteractionLimits($gid, $pid);
  228. if($limits) {
  229. return (bool) $limits['limits']['can_post'];
  230. } else {
  231. return true;
  232. }
  233. }
  234. public static function canComment($gid, $pid)
  235. {
  236. $limits = self::getInteractionLimits($gid, $pid);
  237. if($limits) {
  238. return (bool) $limits['limits']['can_comment'];
  239. } else {
  240. return true;
  241. }
  242. }
  243. public static function canLike($gid, $pid)
  244. {
  245. $limits = self::getInteractionLimits($gid, $pid);
  246. if($limits) {
  247. return (bool) $limits['limits']['can_like'];
  248. } else {
  249. return true;
  250. }
  251. }
  252. public static function categories($onlyActive = true)
  253. {
  254. return Cache::remember(self::key(':categories'), 2678400, function() use($onlyActive) {
  255. return GroupCategory::when($onlyActive, function($q, $onlyActive) {
  256. return $q->whereActive(true);
  257. })
  258. ->orderBy('order')
  259. ->pluck('name')
  260. ->toArray();
  261. });
  262. }
  263. public static function categoryById($id)
  264. {
  265. return Cache::remember(self::key(':categorybyid:'.$id), 2678400, function() use($id) {
  266. $category = GroupCategory::find($id);
  267. if($category) {
  268. return [
  269. 'name' => $category->name,
  270. 'url' => url("/groups/explore/category/{$category->slug}")
  271. ];
  272. }
  273. return false;
  274. });
  275. }
  276. public static function isMember($gid = false, $pid = false)
  277. {
  278. if(!$gid || !$pid) {
  279. return false;
  280. }
  281. $key = self::key("is_member:{$gid}:{$pid}");
  282. return Cache::remember($key, 3600, function() use($gid, $pid) {
  283. return GroupMember::whereGroupId($gid)
  284. ->whereProfileId($pid)
  285. ->whereJoinRequest(false)
  286. ->exists();
  287. });
  288. }
  289. public static function mutualGroups($cid = false, $pid = false, $exclude = [])
  290. {
  291. if(!$cid || !$pid) {
  292. return [
  293. 'count' => 0,
  294. 'groups' => []
  295. ];
  296. }
  297. $self = self::membershipsByPid($cid);
  298. $user = self::membershipsByPid($pid);
  299. if(!$self->count() || !$user->count()) {
  300. return [
  301. 'count' => 0,
  302. 'groups' => []
  303. ];
  304. }
  305. $intersect = $self->intersect($user);
  306. $count = $intersect->count();
  307. $groups = $intersect
  308. ->values()
  309. ->filter(function($id) use($exclude) {
  310. return !in_array($id, $exclude);
  311. })
  312. ->shuffle()
  313. ->take(1)
  314. ->map(function($id) {
  315. return self::get($id);
  316. });
  317. return [
  318. 'count' => $count,
  319. 'groups' => $groups
  320. ];
  321. }
  322. }