OCA\OcSms\Controller\SmsController::getConversation PHP 메소드

getConversation() 공개 메소드

public getConversation ( $phoneNumber, integer $lastDate ) : OCP\AppFramework\Http\JSONResponse
$phoneNumber
$lastDate integer
리턴 OCP\AppFramework\Http\JSONResponse
    public function getConversation($phoneNumber, $lastDate = 0)
    {
        $contacts = $this->contactCache->getContacts();
        $iContacts = $this->contactCache->getInvertedContacts();
        $contactName = "";
        // Cache country because of loops
        $configuredCountry = $this->configMapper->getCountry();
        $fmtPN = PhoneNumberFormatter::format($configuredCountry, $phoneNumber);
        if (isset($contacts[$fmtPN])) {
            $contactName = $contacts[$fmtPN];
        }
        $messages = array();
        $phoneNumbers = array();
        $msgCount = 0;
        // Contact resolved
        if ($contactName != "" && isset($iContacts[$contactName])) {
            // forall numbers in iContacts
            foreach ($iContacts[$contactName] as $cnumber) {
                $messages = $messages + $this->smsMapper->getAllMessagesForPhoneNumber($this->userId, $cnumber, $configuredCountry, $lastDate);
                $msgCount += $this->smsMapper->countMessagesForPhoneNumber($this->userId, $cnumber, $configuredCountry);
                $phoneNumbers[] = PhoneNumberFormatter::format($configuredCountry, $cnumber);
            }
        } else {
            $messages = $this->smsMapper->getAllMessagesForPhoneNumber($this->userId, $phoneNumber, $configuredCountry, $lastDate);
            $msgCount = $this->smsMapper->countMessagesForPhoneNumber($this->userId, $phoneNumber, $configuredCountry);
            $phoneNumbers[] = PhoneNumberFormatter::format($configuredCountry, $phoneNumber);
        }
        // Order by id (date)
        ksort($messages);
        $msgLimit = $this->configMapper->getMessageLimit();
        // Only load the last 500 messages
        $messages = array_slice($messages, -$msgLimit, $msgLimit, true);
        // Set the last read message for the conversation (all phone numbers)
        if (count($messages) > 0) {
            $maxDate = max(array_keys($messages));
            for ($i = 0; $i < count($phoneNumbers); $i++) {
                $this->convStateMapper->setLast($this->userId, $phoneNumbers[$i], $maxDate);
            }
        }
        // @ TODO: filter correctly
        return new JSONResponse(array("conversation" => $messages, "contactName" => $contactName, "phoneNumbers" => $phoneNumbers, "msgCount" => $msgCount));
    }