diff --git a/app/Http/Controllers/ComposeController.php b/app/Http/Controllers/ComposeController.php index 5aeb18371..3bf0a71da 100644 --- a/app/Http/Controllers/ComposeController.php +++ b/app/Http/Controllers/ComposeController.php @@ -396,7 +396,7 @@ class ComposeController extends Controller ]; }); }); - + $wildcard = config('database.default') === 'pgsql' ? 'ilike' : 'like'; $q = '%'.$raw.'%'; diff --git a/app/Http/Controllers/DirectMessageController.php b/app/Http/Controllers/DirectMessageController.php index ca42bff87..1492d4d88 100644 --- a/app/Http/Controllers/DirectMessageController.php +++ b/app/Http/Controllers/DirectMessageController.php @@ -307,13 +307,15 @@ class DirectMessageController extends Controller $messages = $res->filter(function ($message) { return $message && $message->status; })->map(function ($message) use ($uid) { + $firstMedia = $message->status->media->sortBy('order')->first(); + return [ 'id' => (string) $message->id, 'hidden' => (bool) $message->is_hidden, 'isAuthor' => $uid == $message->from_id, 'type' => $message->type, 'text' => $message->status->caption, - 'media' => $message->status->firstMedia() ? $message->status->firstMedia()->url() : null, + 'media' => $firstMedia ? $firstMedia->url() : null, 'carousel' => MediaService::get($message->status_id), 'created_at' => $message->created_at->format('c'), 'timeAgo' => $message->created_at->diffForHumans(null, null, true), diff --git a/app/Http/Controllers/Groups/GroupsSearchController.php b/app/Http/Controllers/Groups/GroupsSearchController.php index 7c38b23c9..513ed1a56 100644 --- a/app/Http/Controllers/Groups/GroupsSearchController.php +++ b/app/Http/Controllers/Groups/GroupsSearchController.php @@ -42,24 +42,27 @@ class GroupsSearchController extends Controller 'Invite limit reached' ); - $profiles = collect($uid) - ->map(function ($u) { - return Profile::find($u); - }) - ->filter(function ($u) use ($pid) { - return $u && - $u->id != $pid && - isset($u->id) && - Follower::whereFollowingId($pid) - ->whereProfileId($u->id) - ->exists(); - }) - ->filter(function ($u) use ($group, $pid) { - return GroupInvitation::whereGroupId($group->id) - ->whereFromProfileId($pid) - ->whereToProfileId($u->id) - ->exists() == false; - }) + $candidateIds = collect($uid) + ->filter(fn ($u) => $u != $pid) + ->unique() + ->values(); + + $profiles = Profile::whereIn('id', $candidateIds)->get(); + + $followedIds = Follower::whereFollowingId($pid) + ->whereIn('profile_id', $profiles->pluck('id')) + ->pluck('profile_id') + ->all(); + + $alreadyInvitedIds = GroupInvitation::whereGroupId($group->id) + ->whereFromProfileId($pid) + ->whereIn('to_profile_id', $profiles->pluck('id')) + ->pluck('to_profile_id') + ->all(); + + $profiles + ->filter(fn ($u) => in_array($u->id, $followedIds)) + ->filter(fn ($u) => ! in_array($u->id, $alreadyInvitedIds)) ->each(function ($u) use ($gid, $pid) { $gi = new GroupInvitation; $gi->group_id = $gid; diff --git a/app/Services/TrendingHashtagService.php b/app/Services/TrendingHashtagService.php index 9d23c2d9c..824d19b62 100644 --- a/app/Services/TrendingHashtagService.php +++ b/app/Services/TrendingHashtagService.php @@ -63,27 +63,32 @@ class TrendingHashtagService $skipIds = array_merge(self::getBannedHashtags(), self::getNonTrendingHashtags(), self::getNsfwHashtags()); return Cache::remember(self::CACHE_KEY, config('trending.hashtags.ttl'), function () use ($minId, $skipIds) { - return StatusHashtag::select('hashtag_id', DB::raw('count(*) as total')) + $trending = StatusHashtag::select('hashtag_id', DB::raw('count(*) as total')) ->whereNotIn('hashtag_id', $skipIds) ->where('id', '>', $minId) ->groupBy('hashtag_id') ->orderBy('total', 'desc') ->take(config('trending.hashtags.limit')) + ->get(); + + $hashtags = Hashtag::whereIn('id', $trending->pluck('hashtag_id')) ->get() - ->map(function ($h) { - $hashtag = Hashtag::find($h->hashtag_id); - if (! $hashtag) { - return; - } - - return [ - 'id' => $h->hashtag_id, - 'total' => $h->total, - 'name' => '#'.$hashtag->name, - 'hashtag' => $hashtag->name, - 'url' => $hashtag->url(), - ]; - }) + ->keyBy('id'); + + return $trending->map(function ($h) use ($hashtags) { + $hashtag = $hashtags->get($h->hashtag_id); + if (! $hashtag) { + return; + } + + return [ + 'id' => $h->hashtag_id, + 'total' => $h->total, + 'name' => '#'.$hashtag->name, + 'hashtag' => $hashtag->name, + 'url' => $hashtag->url(), + ]; + }) ->filter() ->values(); }); diff --git a/tests/Feature/ComposeControllerTest.php b/tests/Feature/ComposeControllerTest.php index fea56f494..88880219b 100644 --- a/tests/Feature/ComposeControllerTest.php +++ b/tests/Feature/ComposeControllerTest.php @@ -2,10 +2,10 @@ namespace Tests\Feature; -use Illuminate\Support\Facades\DB; +use App\Models\User; use Database\Factories\ProfileFactory; -use App\User; use Illuminate\Foundation\Testing\RefreshDatabase; +use Illuminate\Support\Facades\DB; use PHPUnit\Framework\Attributes\Test; use Tests\TestCase; @@ -71,4 +71,4 @@ class ComposeControllerTest extends TestCase 'country' => 'Philippines', ]); } -} \ No newline at end of file +}