public function getInterlocutorsIds($userId) { if (empty($userId) or !is_numeric($userId)) { throw new InvalidArgumentException("Invalid userId specified!"); } $filter = new ChatSessionFilter(); $filter->setEitherUserId($userId); $chatSessions = $this->getChatSessions($filter, $userId); $interlocutorIds = array(); foreach ($chatSessions as $chatSession) { array_push($interlocutorIds, $chatSession->interlocutorUser->userId); } return $interlocutorIds; }
public function run() { $runInterval = ConfigManager::getConfig("Comet", "Comet")->AuxConfig->runInterval; // New Invitations by Last Id $invitationFilter = new ChatInvitationsFilter(); $invitationFilter->setEitherUserId(Reg::get('usr')->getId()); $invitationFilter->setIdGreater($this->lastInvId); $invitations = Reg::get('chatInvMgr')->getInvitations($invitationFilter); $invitationsToMe = array(); $myInvitations = array(); $needToWait = false; foreach ($invitations as $invitation) { if ($invitation->inviterUser->userId == Reg::get('usr')->getId()) { if ($invitation->status == ChatInvitationManager::STATUS_DECLINED) { $this->addMessage(sprintf(CHAT_INVITATION_REJECT, $invitation->invitedUser->userName)); } array_push($myInvitations, $invitation); } else { array_push($invitationsToMe, $invitation); } if ($invitation->status != ChatInvitationManager::STATUS_NEW) { $needToWait = true; } } if ($needToWait) { // Wait for defined inteval, because maybe some other tabs(threads) need // to grab same info from db before our thread will delete it usleep($runInterval * 1000000); } $this->setMyInvitations($myInvitations); $this->setInvitationsToMe($invitationsToMe); // Open Invitations for UI Sync $openInvitationFilter = new ChatInvitationsFilter(); $openInvitationFilter->setEitherUserId(Reg::get('usr')->getId()); $openInvitationFilter->setInvitationStatus(ChatInvitationManager::STATUS_NEW); $openInvitations = Reg::get('chatInvMgr')->getInvitations($openInvitationFilter); $this->openMyInvitations = array(); $this->openInvitationsToMe = array(); foreach ($openInvitations as $invitation) { if ($invitation->inviterUser->userId == Reg::get('usr')->getId()) { array_push($this->openMyInvitations, $invitation); } else { array_push($this->openInvitationsToMe, $invitation); } } // Chat Sessions $sessionFilter = new ChatSessionFilter(); $sessionFilter->setEitherUserId(Reg::get('usr')->getId()); $chatSessions = Reg::get('chatSessMgr')->getChatSessions($sessionFilter, Reg::get('usr')->getId()); $this->openSessions = array(); $interlocutorIds = ChatSessionManager::getInterlocutorsFromSessions($chatSessions); if (!empty($interlocutorIds)) { $messageFilter = new ChatMessageFilter(); $messageFilter->setAllMessagesWithInterlocutors(Reg::get('usr')->getId(), $interlocutorIds); $messageFilter->setOrderDatetimeAsc(); $messageFilter->setMessageIdGreater($this->lastId); $chatMessages = Reg::get('chatMsgMgr')->getChatMessages($messageFilter); Reg::get('chatMsgMgr')->parseMessagesForSmilies($chatMessages); ChatSessionManager::fillSessionsWithMessages($chatSessions, $chatMessages); foreach ($chatSessions as $chatSession) { // Init variables $interlocutorId = $chatSession->interlocutorUser->userId; $interlocutorExtra = Reg::get('um')->getUserExtra($interlocutorId, array('online')); $interlocutorOnlineStatus = $interlocutorExtra['online']; // Check if interlocutor is offline if ($chatSession->closed == ChatSessionManager::CLOSED_STATUS_NO and $interlocutorOnlineStatus == DatingClubUserManagement::STATE_ONLINE_OFFLINE) { Reg::get('chatSessMgr')->closeSession($chatSession, $chatSession->interlocutorUser, ChatSessionManager::CLOSED_REASON_OFFLINE); $this->addChatSession($chatSession); $this->addMessage(sprintf(CHAT_INTER_GONE_OFFLINE, $chatSession->interlocutorUser->userName)); } // Check maybe interlocuter closed the chat if ($chatSession->closed == ChatSessionManager::CLOSED_STATUS_YES) { // Chat closed not by me if ($chatSession->closedBy != Reg::get('usr')->getId()) { if ($chatSession->closedReason == ChatSessionManager::CLOSED_REASON_CLOSE) { $this->addMessage(sprintf(CHAT_INTER_CLOSED_CHAT, $chatSession->interlocutorUser->userName)); } elseif ($chatSession->closedReason == ChatSessionManager::CLOSED_REASON_MONEY) { $this->addMessage(sprintf(CHAT_CLOSED_INTER_HAVE_NO_MONEY, $chatSession->interlocutorUser->userName)); } elseif ($chatSession->closedReason == ChatSessionManager::CLOSED_REASON_SYNC_UI) { $this->addMessage(sprintf(CHAT_CLOSED_UNEXPECTED_ERROR, $chatSession->interlocutorUser->userName)); } } $this->addChatSession($chatSession); // Wait for defined inteval, because maybe some other tabs(threads) need // to grab same info from db before our thread will delete it usleep($runInterval * 1000000); } // Check for new messages if (count($chatSession->messages)) { $this->addChatSession($chatSession); } // Payment part // Do not handle payment for closed sessions if ($chatSession->closed == ChatSessionManager::CLOSED_STATUS_NO) { if (ChatPayment::isTimeToPay($chatSession->chatterUser, $chatSession->interlocutorUser)) { session_start(); if (ChatPayment::isTimeToPay($chatSession->chatterUser, $chatSession->interlocutorUser)) { if (pay($interlocutorId, DatingClubPayment::ACTION_CHAT, null, false)) { ChatPayment::madePayment($chatSession->chatterUser, $chatSession->interlocutorUser); $this->setMoney(Reg::get('usr')->money); } else { // If user have no money close all sessions and give appropriate message. Reg::get('chatSessMgr')->closeSession($chatSession, $chatSession->chatterUser, ChatSessionManager::CLOSED_REASON_MONEY); $this->addChatSession($chatSession); $this->addMessage(sprintf(CHAT_I_HAVE_NO_MONEY, SITE_PATH . 'recharge')); } } session_write_close(); } } // Open Sessions for UI Sync if ($chatSession->closed == ChatSessionManager::CLOSED_STATUS_NO) { array_push($this->openSessions, $chatSession->interlocutorUser->userId); } } } }