Files
timebank-cc-public/app/Http/Livewire/WireChat/Chat/Group/AddMembers.php
T

77 lines
2.5 KiB
PHP

<?php
namespace App\Http\Livewire\WireChat\Chat\Group;
use App\Models\User;
use Namu\WireChat\Facades\WireChat;
use Namu\WireChat\Livewire\Chat\Group\AddMembers as BaseAddMembers;
use Namu\WireChat\Livewire\Chat\Group\Info;
class AddMembers extends BaseAddMembers
{
/**
* Override save to dispatch the refreshed participant count after persisting members.
* The base save() dispatches before the DB count is re-queried; we re-query to ensure
* the Info header always shows the correct number.
*/
public function save()
{
parent::save();
// Re-query the actual participant count from DB and re-dispatch so Info updates correctly
$actualCount = $this->conversation->participants()->count();
$this->dispatch('participantsCountUpdated', $actualCount)->to(Info::class);
}
/**
* Add all linked users of the active Organization, Bank, or Admin profile
* to the selected members list, up to the group member limit.
*/
public function addLinkedMembers(): void
{
$activeProfile = getActiveProfile();
if (!$activeProfile) {
return;
}
\App\Helpers\ProfileAuthorizationHelper::authorize($activeProfile);
$activeProfileType = getActiveProfileType();
if (!in_array($activeProfileType, ['Organization', 'Bank', 'Admin'])) {
return;
}
$relationMethod = $activeProfileType === 'Bank' ? 'managers' : 'users';
$linkedUsers = $activeProfile->$relationMethod()
->select('users.id')
->get();
foreach ($linkedUsers as $linkedUser) {
// Stop if we've hit the group member limit
if ($this->newTotalCount >= WireChat::maxGroupMembers()) {
$this->dispatch('show-member-limit-error');
break;
}
$user = User::find($linkedUser->id);
if (!$user) {
continue;
}
// Skip if already a participant in the conversation
if ($user->belongsToConversation($this->conversation)) {
continue;
}
// Skip if already selected
if ($this->selectedMembers->contains(fn ($m) => $m->id == $user->id && get_class($m) == get_class($user))) {
continue;
}
$this->selectedMembers->push($user);
$this->newTotalCount = count($this->selectedMembers) + $this->exitingMembersCount;
}
}
}