diff --git a/main/inc/ajax/message.ajax.php b/main/inc/ajax/message.ajax.php index 0378d832a1..d5288c42c4 100755 --- a/main/inc/ajax/message.ajax.php +++ b/main/inc/ajax/message.ajax.php @@ -37,35 +37,7 @@ switch ($action) { break; case 'get_count_message': $userId = api_get_user_id(); - $invitations = []; - // Setting notifications - $count_unread_message = 0; - if (api_get_setting('allow_message_tool') === 'true') { - // get count unread message and total invitations - $count_unread_message = MessageManager::getCountNewMessagesFromDB($userId); - } - - if (api_get_setting('allow_social_tool') === 'true') { - $number_of_new_messages_of_friend = SocialManager::get_message_number_invitation_by_user_id( - $userId - ); - $usergroup = new UserGroup(); - $group_pending_invitations = $usergroup->get_groups_by_user( - $userId, - GROUP_USER_PERMISSION_PENDING_INVITATION, - false - ); - if (!empty($group_pending_invitations)) { - $group_pending_invitations = count($group_pending_invitations); - } else { - $group_pending_invitations = 0; - } - $invitations = [ - 'ms_friends' => $number_of_new_messages_of_friend, - 'ms_groups' => $group_pending_invitations, - 'ms_inbox' => $count_unread_message, - ]; - } + $invitations = MessageManager::getMessagesCountForUser($userId); header('Content-type:application/json'); echo json_encode($invitations); break; diff --git a/main/inc/lib/message.lib.php b/main/inc/lib/message.lib.php index b95ec0fd9f..937b79877f 100755 --- a/main/inc/lib/message.lib.php +++ b/main/inc/lib/message.lib.php @@ -3157,4 +3157,40 @@ class MessageManager return (int) $row['count']; } + + public static function getMessagesCountForUser(int $userId): array + { + // Setting notifications + $countUnreadMessage = 0; + + if (api_get_setting('allow_message_tool') === 'true') { + // get count unread message and total invitations + $countUnreadMessage = MessageManager::getCountNewMessagesFromDB($userId); + } + + if (api_get_setting('allow_social_tool') === 'true') { + $numberOfNewMessagesOfFriend = SocialManager::get_message_number_invitation_by_user_id( + $userId + ); + $usergroup = new UserGroup(); + $groupPendingInvitations = $usergroup->get_groups_by_user( + $userId, + GROUP_USER_PERMISSION_PENDING_INVITATION + ); + + if (!empty($groupPendingInvitations)) { + $groupPendingInvitations = count($groupPendingInvitations); + } else { + $groupPendingInvitations = 0; + } + + return [ + 'ms_friends' => $numberOfNewMessagesOfFriend, + 'ms_groups' => $groupPendingInvitations, + 'ms_inbox' => $countUnreadMessage, + ]; + } + + return []; + } } diff --git a/main/inc/lib/webservices/Rest.php b/main/inc/lib/webservices/Rest.php index 5e7fc607fb..3a2be02745 100644 --- a/main/inc/lib/webservices/Rest.php +++ b/main/inc/lib/webservices/Rest.php @@ -79,6 +79,7 @@ class Rest extends WebService const UPDATE_CONDITION_ACCEPTED = 'update_condition_accepted'; const LOGOUT = 'logout'; const DELETE_USER = 'delete_user'; + const GET_COUNT_NEW_MESSAGES = 'get_count_new_messages'; /** * @var Session diff --git a/main/webservices/api/v2.php b/main/webservices/api/v2.php index 11212cb470..f0c392e969 100644 --- a/main/webservices/api/v2.php +++ b/main/webservices/api/v2.php @@ -457,10 +457,15 @@ try { $result = UserManager::delete_user($_REQUEST['user_id']); $restResponse->setData(['status' => $result]); break; + case Rest::LOGOUT: $restApi->logout(); $restResponse->setData(['status' => true]); break; - case Rest::LOGOUT: + case Rest::GET_COUNT_NEW_MESSAGES: + $restResponse->setData( + MessageManager::getMessagesCountForUser($restApi->getUser()->getId()) + ); + break; default: throw new Exception(get_lang('InvalidAction')); }