Example #1
0
/**
 * @param $JSON
 * INFO : API-11 | Status:Completed | Tested : Yes. | Tested in Live : Yes.
 */
function doGetFullMessagesListOfParticularConversation($JSON)
{
    # JSON info's Reading.
    $appUserFacebookID = $JSON->app_user_facebook_id;
    $otherUserFacebookID = $JSON->other_user_facebook_id;
    $conversationThreadID = getConversationThreadID($appUserFacebookID, $otherUserFacebookID);
    # Extra JSON User info's Reading
    $language = $JSON->lang;
    # value can be only 'en' or 'ar'
    $os = $JSON->os;
    # value can be only 'a' or 'i'.
    # Other details
    $currentAppUsingUsersID = getUserIDFromFacebookID($appUserFacebookID);
    # Current app Using User's ID. # 1
    $query = "SELECT * FROM " . TABLE_CONVERSATION_THREADS_INFO . " WHERE id=" . $conversationThreadID;
    $db = new DBCon();
    $result = $db->executeQuery($query);
    if (mysql_num_rows($result) > 0) {
        # Reading all messages exchanged between one conversation , Message's info details Reading.
        $conversationThreadInfo = mysql_fetch_assoc($result);
        $userOneID = $conversationThreadInfo['user_one_id'];
        $userTwoID = $conversationThreadInfo['user_two_id'];
        $otherUserID = $userOneID == $currentAppUsingUsersID ? $userTwoID : $userOneID;
        # 6
        # Current App using Users User Info Read.
        $query = "SELECT facebook_id,profile_picture_url,full_name,flag_online FROM " . TABLE_USER_INFO . " WHERE id={user-id}";
        $query = str_replace("{user-id}", $currentAppUsingUsersID, $query);
        $resultAppUserInfo = $db->executeQuery($query);
        $appUserInfo = mysql_fetch_assoc($resultAppUserInfo);
        $appUserFacebookID = $appUserInfo['facebook_id'];
        # 2
        $appUserProfilePictureURL = $appUserInfo['profile_picture_url'];
        # 3
        $appUserFullName = $appUserInfo['full_name'];
        # 4
        $appUserFlagOnline = $appUserInfo['flag_online'];
        # 5
        # Other Chat users User Info Read.
        $query = "SELECT facebook_id,profile_picture_url,full_name,flag_online FROM " . TABLE_USER_INFO . " WHERE id={user-id}";
        $query = str_replace("{user-id}", $otherUserID, $query);
        $resultOtherUserInfo = $db->executeQuery($query);
        $otherUserInfo = mysql_fetch_assoc($resultOtherUserInfo);
        $otherUserFacebookID = $otherUserInfo['facebook_id'];
        # 7
        $otherUserProfilePictureURL = $otherUserInfo['profile_picture_url'];
        # 8
        $otheUserFullName = $otherUserInfo['full_name'];
        # 9
        $otheUserFlagOnline = $otherUserInfo['flag_online'];
        # 10
        $fullConversationInfo = getFullConversation($conversationThreadID, $currentAppUsingUsersID);
        # Response JSON Creation.
        $responseJSON = array("key" => REQUEST_GET_FULL_MESSAGES, "response" => RESPONSE_SUCCESS, "message" => "Info Read.", "app_user_id" => $currentAppUsingUsersID, "app_user_facebook_id" => $appUserFacebookID, "app_user_profile_picture" => $appUserProfilePictureURL, "app_user_full_name" => $appUserFullName, "app_user_flag_online" => $appUserFlagOnline, "other_user_id" => $otherUserID, "other_user_facebook_id" => $otherUserFacebookID, "other_user_profile_picture" => $otherUserProfilePictureURL, "other_user_full_name" => $otheUserFullName, "other_user_flag_online" => $otheUserFlagOnline, "conversation_info" => $fullConversationInfo);
    } else {
        # Current App using Users User Info Read.
        $query = "SELECT facebook_id,profile_picture_url,full_name,flag_online FROM " . TABLE_USER_INFO . " WHERE id={user-id}";
        $query = str_replace("{user-id}", $currentAppUsingUsersID, $query);
        $resultAppUserInfo = $db->executeQuery($query);
        $appUserInfo = mysql_fetch_assoc($resultAppUserInfo);
        $appUserFacebookID = $appUserInfo['facebook_id'];
        # 2
        $appUserProfilePictureURL = $appUserInfo['profile_picture_url'];
        # 3
        $appUserFullName = $appUserInfo['full_name'];
        # 4
        $appUserFlagOnline = $appUserInfo['flag_online'];
        # 5
        # Other Chat users User Info Read.
        $query = "SELECT facebook_id,profile_picture_url,full_name,flag_online FROM " . TABLE_USER_INFO . " WHERE id={user-id}";
        $query = str_replace("{user-id}", getUserIDFromFacebookID($otherUserFacebookID), $query);
        $resultOtherUserInfo = $db->executeQuery($query);
        $otherUserInfo = mysql_fetch_assoc($resultOtherUserInfo);
        $otherUserFacebookID = $otherUserInfo['facebook_id'];
        # 7
        $otherUserProfilePictureURL = $otherUserInfo['profile_picture_url'];
        # 8
        $otheUserFullName = $otherUserInfo['full_name'];
        # 9
        $otheUserFlagOnline = $otherUserInfo['flag_online'];
        # 10
        # Response JSON Creation.
        $responseJSON = array("key" => REQUEST_GET_FULL_MESSAGES, "response" => RESPONSE_SUCCESS, "message" => "Messages Empty.", "app_user_id" => $currentAppUsingUsersID, "app_user_facebook_id" => $appUserFacebookID, "app_user_profile_picture" => $appUserProfilePictureURL, "app_user_full_name" => $appUserFullName, "app_user_flag_online" => $appUserFlagOnline, "other_user_id" => getUserIDFromFacebookID($otherUserFacebookID), "other_user_facebook_id" => $otherUserFacebookID, "other_user_profile_picture" => $otherUserProfilePictureURL, "other_user_full_name" => $otheUserFullName, "other_user_flag_online" => $otheUserFlagOnline, "conversation_info" => []);
    }
    sendResponseToAppAndExitAPI($responseJSON);
    # Giving JSON Response to Mobile app.
}
Example #2
0
/**
 * @param $JSON
 * INFO : API-9 | Status:Completed | Tested : Yes. | Tested in Live : No.
 * DONE : 1.Add new column in table
 * DONE : 2.Update last updated timestamp in conversation_thread_table
 * DONE : 1.New updates Testing pending.
 * TODO : 2.Push Notification test Pending , currently not working.
 */
function doSendChatMessage($JSON)
{
    # Multi part POST Requests reading.
    $senderFacebookID = $_POST['sender_facebook_id'];
    $receiverFacebookID = $_POST['receiver_facebook_id'];
    $messageType = $_POST['message_type'];
    $givenFoodID = $_POST['given_food_id'];
    if ($messageType == MESSAGE_TEXT) {
        # Get Text - Message
        $message = $_POST['message'];
    } else {
        # Get Image - Message, Save Image in Server
        $fileIndexName = "message";
        # index name used for File in Multipart Request.
        $message = getUniqueRandomeFileName($fileIndexName);
        #
        $statusFileSave = saveFileInUploadsDIR($fileIndexName, $message);
        # If Image file is not Saved in server , Send Failed response to app and Exit.
        if (!$statusFileSave) {
            $responseJSON = array("key" => REQUEST_SEND_CHAT_MESSAGE, "response" => RESPONSE_FAILED, "message" => "Message not Sent, File save Failed.");
            sendResponseToAppAndExitAPI($responseJSON);
            # Giving JSON Response to Mobile app.
        }
    }
    $statusConversationThreadCreated = isConversationThreadCreated($senderFacebookID, $receiverFacebookID, $givenFoodID);
    if (!$statusConversationThreadCreated) {
        # create conversation_thread,get thread ID, add message in messages_info with thread ID.
        $messageSentTimestamp = time();
        # Query creation
        $query = "INSERT INTO " . TABLE_CONVERSATION_THREADS_INFO . "(user_one_id,user_two_id,timestamp_created,flag_active,timestamp_last_message,selected_given_food_id) VALUES('{user-one-id}','{user-two-id}','{timestamp-created}',{flag-active},'{timestamp-last-message}',{selected-given-food-id})";
        $query = str_replace("{user-one-id}", getUserIDFromFacebookID($senderFacebookID), $query);
        $query = str_replace("{user-two-id}", getUserIDFromFacebookID($receiverFacebookID), $query);
        $query = str_replace("{timestamp-created}", time(), $query);
        $query = str_replace("{flag-active}", YES, $query);
        $query = str_replace("{timestamp-last-message}", $messageSentTimestamp, $query);
        $query = str_replace("{selected-given-food-id}", $givenFoodID, $query);
        $db = new DBCon();
        $result = $db->executeQuery($query);
        if ($result) {
            # Add message in messages_info with created Thread ID.
            $createdThreadID = $db->getLastInsertedID();
            # Query creation.
            $query = "INSERT INTO " . TABLE_MESSAGES_INFO . "(message,type,sender_user_id,conversation_thread_id,timestamp_sent,flag_active)\n                    VALUES('{message}',{type},{sender-user-id},{conversation-thread-id},'{timestamp-sent}',{flag-active})";
            $query = str_replace("{message}", $message, $query);
            $query = str_replace("{type}", $messageType, $query);
            $query = str_replace("{sender-user-id}", getUserIDFromFacebookID($senderFacebookID), $query);
            $query = str_replace("{conversation-thread-id}", $createdThreadID, $query);
            $query = str_replace("{timestamp-sent}", $messageSentTimestamp, $query);
            $query = str_replace("{flag-active}", YES, $query);
            $db = new DBCon();
            $result = $db->executeQuery($query);
            if ($result) {
                $statusPushNotificationSent = sendPushNotificationToUser($receiverFacebookID, $senderFacebookID, $message);
                if ($statusPushNotificationSent) {
                    $responseJSON = array("key" => REQUEST_SEND_CHAT_MESSAGE, "response" => RESPONSE_SUCCESS, "message" => "Thread created / Push notification / Message Sent Successfully completed.");
                } else {
                    $responseJSON = array("key" => REQUEST_SEND_CHAT_MESSAGE, "response" => RESPONSE_FAILED, "message" => "Thread created /  Message Sent / Push notification Failed");
                }
            } else {
                $responseJSON = array("key" => REQUEST_SEND_CHAT_MESSAGE, "response" => RESPONSE_FAILED, "message" => "Message not Sent - Full , Message add Failed.");
            }
        } else {
            $responseJSON = array("key" => REQUEST_SEND_CHAT_MESSAGE, "response" => RESPONSE_FAILED, "message" => "Message not Sent, Conversation thread creation Failed.");
        }
    } else {
        # Get thread ID, Update last message timestamp, Add message in messages_info with thread ID.
        $alreadyCreatedConversationThreadID = getConversationThreadID($senderFacebookID, $receiverFacebookID, $givenFoodID);
        $messageSentTimestamp = time();
        $statusConversationThreadLastMessageTimeUpdated = updateConversationThreadLastMessageTimeUpdate($alreadyCreatedConversationThreadID, $messageSentTimestamp);
        if ($statusConversationThreadLastMessageTimeUpdated) {
            # Query creation.
            $query = "INSERT INTO " . TABLE_MESSAGES_INFO . "(message,type,sender_user_id,conversation_thread_id,timestamp_sent,flag_active)\n                        VALUES('{message}',{type},{sender-user-id},{conversation-thread-id},'{timestamp-sent}',{flag-active})";
            $query = str_replace("{message}", $message, $query);
            $query = str_replace("{type}", $messageType, $query);
            $query = str_replace("{sender-user-id}", getUserIDFromFacebookID($senderFacebookID), $query);
            $query = str_replace("{conversation-thread-id}", $alreadyCreatedConversationThreadID, $query);
            $query = str_replace("{timestamp-sent}", $messageSentTimestamp, $query);
            $query = str_replace("{flag-active}", YES, $query);
            $db = new DBCon();
            $result = $db->executeQuery($query);
            if ($result) {
                $statusPushNotificationSent = sendPushNotificationToUser($receiverFacebookID, $senderFacebookID, $message);
                if ($statusPushNotificationSent) {
                    $responseJSON = array("key" => REQUEST_SEND_CHAT_MESSAGE, "response" => RESPONSE_SUCCESS, "message" => "Push notification sent / Message Sent Successfully completed.");
                } else {
                    $responseJSON = array("key" => REQUEST_SEND_CHAT_MESSAGE, "response" => RESPONSE_FAILED, "message" => "Message Sent / Push notification Failed ");
                }
            } else {
                $responseJSON = array("key" => REQUEST_SEND_CHAT_MESSAGE, "response" => RESPONSE_FAILED, "message" => "Message not Sent - Half , Message add Failed.");
            }
        } else {
            $responseJSON = array("key" => REQUEST_SEND_CHAT_MESSAGE, "response" => RESPONSE_FAILED, "message" => "Message not Sent - Conversation Thread Last message update Failed");
        }
    }
    sendResponseToAppAndExitAPI($responseJSON);
    # Giving JSON Response to Mobile app.
}