diff --git a/application/controllers/ApiV1ContactgroupsController.php b/application/controllers/ApiV1ContactgroupsController.php index b68fedbf..59f54f8f 100644 --- a/application/controllers/ApiV1ContactgroupsController.php +++ b/application/controllers/ApiV1ContactgroupsController.php @@ -105,10 +105,7 @@ function (Filter\Condition $condition) { $this->httpNotFound('Contactgroup not found'); } - $users = $this->fetchUserIdentifiers($result->contactgroup_id); - if ($users) { - $result->users = $users; - } + $result->users = $this->fetchUserIdentifiers($result->contactgroup_id); unset($result->contactgroup_id); $results[] = $result; @@ -137,10 +134,7 @@ function (Filter\Condition $condition) { do { /** @var stdClass $row */ foreach ($res as $i => $row) { - $users = $this->fetchUserIdentifiers($row->contactgroup_id); - if ($users) { - $row->users = $users; - } + $row->users = $this->fetchUserIdentifiers($row->contactgroup_id); if ($i > 0 || $offset !== 0) { echo ",\n"; @@ -260,11 +254,11 @@ function (Filter\Condition $condition) { * * @param int $contactgroupId * - * @return ?string[] + * @return string[] */ - private function fetchUserIdentifiers(int $contactgroupId): ?array + private function fetchUserIdentifiers(int $contactgroupId): array { - $users = Database::get()->fetchCol( + return Database::get()->fetchCol( (new Select()) ->from('contactgroup_member cgm') ->columns('co.external_uuid') @@ -272,8 +266,6 @@ private function fetchUserIdentifiers(int $contactgroupId): ?array ->where(['cgm.contactgroup_id = ?' => $contactgroupId]) ->groupBy('co.external_uuid') ); - - return $users ?: null; } /** diff --git a/application/controllers/ApiV1ContactsController.php b/application/controllers/ApiV1ContactsController.php index 20386511..96f41717 100644 --- a/application/controllers/ApiV1ContactsController.php +++ b/application/controllers/ApiV1ContactsController.php @@ -114,19 +114,8 @@ function (Filter\Condition $condition) { $this->httpNotFound('Contact not found'); } - if ($result->username === null) { - unset($result->username); - } - - $groups = $this->fetchGroupIdentifiers($result->contact_id); - if ($groups) { - $result->groups = $groups; - } - - $addresses = $this->fetchContactAddresses($result->contact_id); - if ($addresses) { - $result->addresses = $addresses; - } + $result->groups = $this->fetchGroupIdentifiers($result->contact_id); + $result->addresses = $this->fetchContactAddresses($result->contact_id); unset($result->contact_id); $results[] = $result; @@ -155,19 +144,8 @@ function (Filter\Condition $condition) { do { /** @var stdClass $row */ foreach ($res as $i => $row) { - if ($row->username === null) { - unset($row->username); - } - - $groups = $this->fetchGroupIdentifiers($row->contact_id); - if ($groups) { - $row->groups = $groups; - } - - $addresses = $this->fetchContactAddresses($row->contact_id); - if ($addresses) { - $row->addresses = $addresses; - } + $row->groups = $this->fetchGroupIdentifiers($row->contact_id); + $row->addresses = $this->fetchContactAddresses($row->contact_id); if ($i > 0 || $offset !== 0) { echo ",\n"; @@ -326,9 +304,9 @@ private function getChannelId(string $channelName): int * * @param int $contactId * - * @return ?string + * @return string */ - private function fetchContactAddresses(int $contactId): ?string + private function fetchContactAddresses(int $contactId): string { /** @var array $addresses */ $addresses = Database::get()->fetchPairs( @@ -338,11 +316,7 @@ private function fetchContactAddresses(int $contactId): ?string ->where(['contact_id = ?' => $contactId]) ); - if (! empty($addresses)) { - return json_encode($addresses) ?: null; - } - - return null; + return Json::sanitize($addresses, JSON_FORCE_OBJECT); } /** @@ -350,11 +324,11 @@ private function fetchContactAddresses(int $contactId): ?string * * @param int $contactId * - * @return ?string[] + * @return string[] */ - private function fetchGroupIdentifiers(int $contactId): ?array + private function fetchGroupIdentifiers(int $contactId): array { - $groups = Database::get()->fetchCol( + return Database::get()->fetchCol( (new Select()) ->from('contactgroup_member cgm') ->columns('cg.external_uuid') @@ -362,8 +336,6 @@ private function fetchGroupIdentifiers(int $contactId): ?array ->where(['cgm.contact_id = ?' => $contactId]) ->groupBy('cg.external_uuid') ); - - return $groups ?: null; } /**