Beispiel #1
0
        // this is called in case of gcj from Network manager
        public static ConversationListObject addGroupChatMessage(ConvMessage convMsg, JObject jsonObj)
        {
            ConversationListObject obj = null;

            if (!App.ViewModel.ConvMap.ContainsKey(convMsg.Msisdn)) // represents group is new
            {
                bool success = addMessage(convMsg);
                if (!success)
                {
                    return(null);
                }
                string groupName = GroupManager.Instance.defaultGroupName(convMsg.Msisdn);
                obj = ConversationTableUtils.addGroupConversation(convMsg, groupName);
                App.ViewModel.ConvMap[convMsg.Msisdn] = obj;
                GroupInfo gi = new GroupInfo(convMsg.Msisdn, null, convMsg.GroupParticipant, true);
                GroupTableUtils.addGroupInfo(gi);
            }
            else // add a member to a group
            {
                List <GroupParticipant> existingMembers = null;
                GroupManager.Instance.GroupCache.TryGetValue(convMsg.Msisdn, out existingMembers);
                if (existingMembers == null)
                {
                    return(null);
                }

                obj = App.ViewModel.ConvMap[convMsg.Msisdn];
                GroupInfo gi = GroupTableUtils.getGroupInfoForId(convMsg.Msisdn);

                if (string.IsNullOrEmpty(gi.GroupName)) // no group name is set
                {
                    obj.ContactName = GroupManager.Instance.defaultGroupName(obj.Msisdn);
                }

                if (convMsg.GrpParticipantState == ConvMessage.ParticipantInfoState.MEMBERS_JOINED)
                {
                    string[] vals = convMsg.Message.Split(';');
                    if (vals.Length == 2)
                    {
                        obj.LastMessage = vals[1];
                    }
                    else
                    {
                        obj.LastMessage = convMsg.Message;
                    }
                }
                else
                {
                    obj.LastMessage = convMsg.Message;
                }

                bool success = addMessage(convMsg);
                if (!success)
                {
                    return(null);
                }

                obj.MessageStatus = convMsg.MessageStatus;
                obj.TimeStamp     = convMsg.Timestamp;
                obj.LastMsgId     = convMsg.MessageId;
                ConversationTableUtils.updateConversation(obj);
            }

            return(obj);
        }
Beispiel #2
0
        public static ConversationListObject addChatMessage(ConvMessage convMsg, bool isNewGroup)
        {
            if (convMsg == null)
            {
                return(null);
            }
            ConversationListObject obj = null;

            if (!App.ViewModel.ConvMap.ContainsKey(convMsg.Msisdn))
            {
                if (Utils.isGroupConversation(convMsg.Msisdn) && !isNewGroup) // if its a group chat msg and group does not exist , simply ignore msg.
                {
                    return(null);
                }

                obj = ConversationTableUtils.addConversation(convMsg, isNewGroup);
                App.ViewModel.ConvMap.Add(convMsg.Msisdn, obj);
            }
            else
            {
                obj = App.ViewModel.ConvMap[convMsg.Msisdn];
                #region PARTICIPANT_JOINED
                if (convMsg.GrpParticipantState == ConvMessage.ParticipantInfoState.PARTICIPANT_JOINED)
                {
                    obj.LastMessage = convMsg.Message;

                    GroupInfo gi = GroupTableUtils.getGroupInfoForId(convMsg.Msisdn);
                    if (gi == null)
                    {
                        return(null);
                    }
                    if (string.IsNullOrEmpty(gi.GroupName)) // no group name is set
                    {
                        obj.ContactName = GroupManager.Instance.defaultGroupName(convMsg.Msisdn);
                    }
                }
                #endregion
                #region PARTICIPANT_LEFT
                else if (convMsg.GrpParticipantState == ConvMessage.ParticipantInfoState.PARTICIPANT_LEFT || convMsg.GrpParticipantState == ConvMessage.ParticipantInfoState.INTERNATIONAL_GROUP_USER)
                {
                    obj.LastMessage = convMsg.Message;
                    GroupInfo gi = GroupTableUtils.getGroupInfoForId(convMsg.Msisdn);
                    if (gi == null)
                    {
                        return(null);
                    }
                    if (string.IsNullOrEmpty(gi.GroupName)) // no group name is set
                    {
                        obj.ContactName = GroupManager.Instance.defaultGroupName(convMsg.Msisdn);
                    }
                }
                #endregion
                #region GROUP_JOINED_OR_WAITING
                else if (convMsg.GrpParticipantState == ConvMessage.ParticipantInfoState.GROUP_JOINED_OR_WAITING) // shows invite msg
                {
                    string[]      vals = Utils.splitUserJoinedMessage(convMsg.Message);
                    List <string> waitingParticipants = null;
                    for (int i = 0; i < vals.Length; i++)
                    {
                        string[] vars = vals[i].Split(HikeConstants.DELIMITERS, StringSplitOptions.RemoveEmptyEntries); // msisdn:0 or msisdn:1

                        // every participant is either on DND or not on DND
                        GroupParticipant gp = GroupManager.Instance.getGroupParticipant(null, vars[0], convMsg.Msisdn);
                        if (vars[1] == "0") // DND USER and not OPTED IN
                        {
                            if (waitingParticipants == null)
                            {
                                waitingParticipants = new List <string>();
                            }
                            waitingParticipants.Add(gp.FirstName);
                        }
                    }
                    if (waitingParticipants != null && waitingParticipants.Count > 0) // show waiting msg
                    {
                        StringBuilder msgText = new StringBuilder();
                        if (waitingParticipants.Count == 1)
                        {
                            msgText.Append(waitingParticipants[0]);
                        }
                        else if (waitingParticipants.Count == 2)
                        {
                            msgText.Append(waitingParticipants[0] + AppResources.And_txt + waitingParticipants[1]);
                        }
                        else
                        {
                            for (int i = 0; i < waitingParticipants.Count; i++)
                            {
                                msgText.Append(waitingParticipants[0]);
                                if (i == waitingParticipants.Count - 2)
                                {
                                    msgText.Append(AppResources.And_txt);
                                }
                                else if (i < waitingParticipants.Count - 2)
                                {
                                    msgText.Append(",");
                                }
                            }
                        }
                        obj.LastMessage = string.Format(AppResources.WAITING_TO_JOIN, msgText.ToString());
                    }
                    else
                    {
                        string[]         vars = vals[vals.Length - 1].Split(':');
                        GroupParticipant gp   = GroupManager.Instance.getGroupParticipant(null, vars[0], convMsg.Msisdn);
                        string           text = AppResources.USER_JOINED_GROUP_CHAT;
                        obj.LastMessage = gp.FirstName + text;
                    }
                }
                #endregion
                #region USER_OPT_IN
                else if (convMsg.GrpParticipantState == ConvMessage.ParticipantInfoState.USER_OPT_IN)
                {
                    if (Utils.isGroupConversation(obj.Msisdn))
                    {
                        GroupParticipant gp = GroupManager.Instance.getGroupParticipant(null, convMsg.Message, obj.Msisdn);
                        obj.LastMessage = gp.FirstName + AppResources.USER_JOINED_GROUP_CHAT;
                    }
                    else
                    {
                        obj.LastMessage = obj.NameToShow + AppResources.USER_OPTED_IN_MSG;
                    }
                    convMsg.Message = obj.LastMessage;
                }
                #endregion
                #region CREDITS_GAINED
                else if (convMsg.GrpParticipantState == ConvMessage.ParticipantInfoState.CREDITS_GAINED)
                {
                    obj.LastMessage = convMsg.Message;
                }
                #endregion
                #region DND_USER
                else if (convMsg.GrpParticipantState == ConvMessage.ParticipantInfoState.DND_USER)
                {
                    obj.LastMessage = string.Format(AppResources.DND_USER, obj.NameToShow);
                    convMsg.Message = obj.LastMessage;
                }
                #endregion
                #region USER_JOINED
                else if (convMsg.GrpParticipantState == ConvMessage.ParticipantInfoState.USER_JOINED)
                {
                    if (Utils.isGroupConversation(obj.Msisdn))
                    {
                        GroupParticipant gp = GroupManager.Instance.getGroupParticipant(null, convMsg.Message, obj.Msisdn);
                        obj.LastMessage = string.Format(AppResources.USER_JOINED_HIKE, gp.FirstName);
                    }
                    else // 1-1 chat
                    {
                        obj.LastMessage = string.Format(AppResources.USER_JOINED_HIKE, obj.NameToShow);
                    }
                    convMsg.Message = obj.LastMessage;
                }
                #endregion \
                #region GROUP NAME/PIC CHANGED
                else if (convMsg.GrpParticipantState == ConvMessage.ParticipantInfoState.GROUP_NAME_CHANGE || convMsg.GrpParticipantState == ConvMessage.ParticipantInfoState.GROUP_PIC_CHANGED)
                {
                    obj.LastMessage = convMsg.Message;
                }
                #endregion
                #region NO_INFO
                else if (convMsg.GrpParticipantState == ConvMessage.ParticipantInfoState.NO_INFO)
                {
                    //convMsg.GroupParticipant is null means message sent by urself
                    if (convMsg.GroupParticipant != null && Utils.isGroupConversation(convMsg.Msisdn))
                    {
                        GroupParticipant gp = GroupManager.Instance.getGroupParticipant(null, convMsg.GroupParticipant, convMsg.Msisdn);
                        obj.LastMessage = gp != null ? (gp.FirstName + "- " + convMsg.Message) : convMsg.Message;
                    }
                    else
                    {
                        obj.LastMessage = convMsg.Message;
                    }
                }
                #endregion
                #region OTHER MSGS
                else
                {
                    obj.LastMessage = convMsg.Message;
                }
                #endregion

                Stopwatch st1     = Stopwatch.StartNew();
                bool      success = addMessage(convMsg);
                if (!success)
                {
                    return(null);
                }
                st1.Stop();
                long msec1 = st1.ElapsedMilliseconds;
                Debug.WriteLine("Time to add chat msg : {0}", msec1);

                obj.MessageStatus = convMsg.MessageStatus;
                obj.TimeStamp     = convMsg.Timestamp;
                obj.LastMsgId     = convMsg.MessageId;
                Stopwatch st = Stopwatch.StartNew();
                ConversationTableUtils.updateConversation(obj);
                st.Stop();
                long msec = st.ElapsedMilliseconds;
                Debug.WriteLine("Time to update conversation  : {0}", msec);
            }

            return(obj);
        }
        public void onEventReceived(string type, object obj)
        {
            #region MESSAGE_SENT
            if (HikePubSub.MESSAGE_SENT == type)
            {
                object[]    vals        = (object[])obj;
                ConvMessage convMessage = (ConvMessage)vals[0];

                bool                   isNewGroup = (bool)vals[1];
                SentChatBubble         chatBubble = (SentChatBubble)vals[2];
                ConversationListObject convObj    = MessagesTableUtils.addChatMessage(convMessage, isNewGroup);
                if (convObj == null)
                {
                    return;
                }
                if (chatBubble != null)
                {
                    chatBubble.MessageId = convMessage.MessageId;
                }

                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    if (chatBubble != null)
                    {
                        addSentMessageToMsgMap(chatBubble);
                    }

                    if (convObj.ConvBoxObj == null)
                    {
                        convObj.ConvBoxObj = new ConversationBox(convObj);
                        if (App.ViewModel.ConversationListPage != null)
                        {
                            ContextMenuService.SetContextMenu(convObj.ConvBoxObj, App.ViewModel.ConversationListPage.createConversationContextMenu(convObj));
                        }
                    }
                    else if (App.ViewModel.MessageListPageCollection.Contains(convObj.ConvBoxObj))//cannot use convMap here because object has pushed to map but not to ui
                    {
                        App.ViewModel.MessageListPageCollection.Remove(convObj.ConvBoxObj);
                    }

                    App.ViewModel.MessageListPageCollection.Insert(0, convObj.ConvBoxObj);

                    if (!isNewGroup)
                    {
                        mPubSub.publish(HikePubSub.MQTT_PUBLISH, convMessage.serialize(convMessage.IsSms ? false : true));
                    }
                });
                //if (!isNewGroup)
                //    mPubSub.publish(HikePubSub.MQTT_PUBLISH, convMessage.serialize(convMessage.IsSms ? false : true));
            }
            #endregion
            #region FORWARD_ATTACHMENT
            else if (HikePubSub.FORWARD_ATTACHMENT == type)
            {
                object[]       vals           = (object[])obj;
                ConvMessage    convMessage    = (ConvMessage)vals[0];
                string         sourceFilePath = (string)vals[1];
                SentChatBubble chatBubble     = (SentChatBubble)vals[2];

                ConversationListObject convObj = MessagesTableUtils.addChatMessage(convMessage, false);
                chatBubble.MessageId = convMessage.MessageId;

                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    addSentMessageToMsgMap(chatBubble);

                    if (convObj.ConvBoxObj == null)
                    {
                        convObj.ConvBoxObj = new ConversationBox(convObj);
                        if (App.ViewModel.ConversationListPage != null)
                        {
                            ContextMenuService.SetContextMenu(convObj.ConvBoxObj, App.ViewModel.ConversationListPage.createConversationContextMenu(convObj));
                        }
                    }
                    else if (App.ViewModel.MessageListPageCollection.Contains(convObj.ConvBoxObj))//cannot use convMap here because object has pushed to map but not to ui
                    {
                        App.ViewModel.MessageListPageCollection.Remove(convObj.ConvBoxObj);
                    }

                    App.ViewModel.MessageListPageCollection.Insert(0, convObj.ConvBoxObj);
                    //forward attachment message
                    string destinationFilePath = HikeConstants.FILES_BYTE_LOCATION + "/" + convMessage.Msisdn + "/" + convMessage.MessageId;
                    //while writing in iso, we write it as failed and then revert to started
                    MiscDBUtil.saveAttachmentObject(convMessage.FileAttachment, convMessage.Msisdn, convMessage.MessageId);

                    //since, Location & Contact has required info in metadata string, no need to use raw files
                    if (!convMessage.FileAttachment.ContentType.Contains(HikeConstants.CT_CONTACT) &&
                        !convMessage.FileAttachment.ContentType.Contains(HikeConstants.LOCATION))
                    {
                        MiscDBUtil.copyFileInIsolatedStorage(sourceFilePath, destinationFilePath);
                    }
                    mPubSub.publish(HikePubSub.MQTT_PUBLISH, convMessage.serialize(true));
                });
            }
            #endregion
            #region ATTACHMENT_SEND
            else if (HikePubSub.ATTACHMENT_SENT == type)
            {
                object[]       vals        = (object[])obj;
                ConvMessage    convMessage = (ConvMessage)vals[0];
                byte[]         fileBytes   = (byte[])vals[1];
                SentChatBubble chatBubble  = (SentChatBubble)vals[2];

                //In case of sending attachments, here message state should be unknown instead of sent_unconfirmed
                //convMessage.MessageStatus = ConvMessage.State.SENT_UNCONFIRMED;
                ConversationListObject convObj = MessagesTableUtils.addChatMessage(convMessage, false);

                // in case of db failure convObj returned will be null
                if (convObj == null)
                {
                    return;
                }
                chatBubble.MessageId = convMessage.MessageId;

                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    addSentMessageToMsgMap(chatBubble);
                    if (convObj.ConvBoxObj == null)
                    {
                        convObj.ConvBoxObj = new ConversationBox(convObj);
                        if (App.ViewModel.ConversationListPage != null)
                        {
                            ContextMenuService.SetContextMenu(convObj.ConvBoxObj, App.ViewModel.ConversationListPage.createConversationContextMenu(convObj));
                        }
                    }
                    else if (App.ViewModel.MessageListPageCollection.Contains(convObj.ConvBoxObj))
                    {
                        App.ViewModel.MessageListPageCollection.Remove(convObj.ConvBoxObj);
                    }
                    App.ViewModel.MessageListPageCollection.Insert(0, convObj.ConvBoxObj);
                    //send attachment message (new attachment - upload case)
                    MessagesTableUtils.addUploadingOrDownloadingMessage(convMessage.MessageId, chatBubble);
                    convMessage.FileAttachment.FileState = Attachment.AttachmentState.FAILED_OR_NOT_STARTED;
                    MiscDBUtil.saveAttachmentObject(convMessage.FileAttachment, convMessage.Msisdn, convMessage.MessageId);
                    convMessage.FileAttachment.FileState = Attachment.AttachmentState.STARTED;

                    AccountUtils.postUploadPhotoFunction finalCallbackForUploadFile = new AccountUtils.postUploadPhotoFunction(uploadFileCallback);
                    if (!convMessage.FileAttachment.ContentType.Contains(HikeConstants.CT_CONTACT))
                    {
                        MiscDBUtil.storeFileInIsolatedStorage(HikeConstants.FILES_BYTE_LOCATION + "/" + convMessage.Msisdn + "/" +
                                                              Convert.ToString(convMessage.MessageId), fileBytes);
                    }
                    AccountUtils.uploadFile(fileBytes, finalCallbackForUploadFile, convMessage, chatBubble);
                });
            }
            #endregion
            #region ATTACHMENT_RESEND_OR_FORWARD
            else if (HikePubSub.ATTACHMENT_RESEND == type)
            {
                object[]       vals        = (object[])obj;
                ConvMessage    convMessage = (ConvMessage)vals[0];
                SentChatBubble chatBubble  = (SentChatBubble)vals[1];
                byte[]         fileBytes;
                if (convMessage.FileAttachment.ContentType.Contains(HikeConstants.CT_CONTACT))
                {
                    fileBytes = Encoding.UTF8.GetBytes(convMessage.MetaDataString);
                }
                else
                {
                    MiscDBUtil.readFileFromIsolatedStorage(HikeConstants.FILES_BYTE_LOCATION + "/" + convMessage.Msisdn + "/" +
                                                           Convert.ToString(convMessage.MessageId), out fileBytes);
                }
                AccountUtils.postUploadPhotoFunction finalCallbackForUploadFile = new AccountUtils.postUploadPhotoFunction(uploadFileCallback);
                AccountUtils.uploadFile(fileBytes, finalCallbackForUploadFile, convMessage, chatBubble);
            }
            #endregion
            #region MESSAGE_RECEIVED_READ
            else if (HikePubSub.MESSAGE_RECEIVED_READ == type)  // represents event when a msg is read by this user
            {
                long[] ids = (long[])obj;
                updateDbBatch(ids, (int)ConvMessage.State.RECEIVED_READ);
            }
            #endregion
            #region MESSAGE_DELETED
            else if (HikePubSub.MESSAGE_DELETED == type)
            {
                object[] o = (object[])obj;

                long msgId = (long)o[0];
                MessagesTableUtils.deleteMessage(msgId); // delete msg with given msgId from messages table

                ConversationListObject c = (ConversationListObject)o[1];
                bool delConv             = (bool)o[2];
                if (delConv)
                {
                    // delete the conversation from DB.
                    ConversationTableUtils.deleteConversation(c.Msisdn);
                    //ConversationTableUtils.saveConvObjectList();
                }
                else
                {
                    //update conversation
                    ConversationTableUtils.updateConversation(c);
                }
                // TODO :: persistence.removeMessage(msgId);
            }
            #endregion
            #region BLOCK_USER
            else if (HikePubSub.BLOCK_USER == type)
            {
                string msisdn = (string)obj;
                UsersTableUtils.block(msisdn);
                JObject blockObj = blockUnblockSerialize("b", msisdn);
                mPubSub.publish(HikePubSub.MQTT_PUBLISH, blockObj);
            }
            #endregion
            #region UNBLOCK_USER
            else if (HikePubSub.UNBLOCK_USER == type)
            {
                string msisdn = (string)obj;
                UsersTableUtils.unblock(msisdn);
                JObject unblockObj = blockUnblockSerialize("ub", msisdn);
                mPubSub.publish(HikePubSub.MQTT_PUBLISH, unblockObj);
            }
            #endregion
            #region ADD_OR_UPDATE_PROFILE
            else if (HikePubSub.ADD_OR_UPDATE_PROFILE == type)
            {
                object[] vals           = (object[])obj;
                string   msisdn         = (string)vals[0];
                byte[]   fullViewBytes  = (byte[])vals[1];
                byte[]   thumbnailBytes = (byte[])vals[2];
                if (Utils.isGroupConversation(msisdn))
                {
                    string grpId = msisdn.Replace(":", "_");
                    MiscDBUtil.saveAvatarImage(grpId + HikeConstants.FULL_VIEW_IMAGE_PREFIX, fullViewBytes, false);
                    MiscDBUtil.saveAvatarImage(grpId, thumbnailBytes, false);
                }
                else
                {
                    MiscDBUtil.saveAvatarImage(HikeConstants.MY_PROFILE_PIC, thumbnailBytes, false);
                }
            }
            #endregion
            #region GROUP LEFT
            else if (HikePubSub.GROUP_LEFT == type)
            {
                /*
                 * 1. Delete conversation with this groupId
                 * 2. Delete ConvMessages
                 * 3. Delete GroupInfo
                 * 4. Delete GroupMembers
                 */
                string groupId = (string)obj;
                ConversationTableUtils.deleteConversation(groupId);
                //ConversationTableUtils.saveConvObjectList();
                MessagesTableUtils.deleteAllMessagesForMsisdn(groupId);
                GroupTableUtils.deleteGroupWithId(groupId);
                GroupManager.Instance.GroupCache.Remove(groupId);
                GroupManager.Instance.DeleteGroup(groupId);
            }
            #endregion
            #region BLOCK GROUP OWNER
            else if (HikePubSub.BLOCK_GROUPOWNER == type)
            {
                string groupOwner = (string)obj;
                UsersTableUtils.block(groupOwner);
                JObject blockObj = blockUnblockSerialize("b", groupOwner);
                mPubSub.publish(HikePubSub.MQTT_PUBLISH, blockObj);
            }
            #endregion
            #region UNBLOCK GROUP OWNER
            else if (HikePubSub.UNBLOCK_GROUPOWNER == type)
            {
                string groupOwner = (string)obj;
                UsersTableUtils.unblock(groupOwner);
                JObject unblockObj = blockUnblockSerialize("ub", groupOwner);
                mPubSub.publish(HikePubSub.MQTT_PUBLISH, unblockObj);
            }
            #endregion
            #region DELETE CONVERSATION
            else if (HikePubSub.DELETE_CONVERSATION == type)
            {
                string convMsisdn = (string)obj;
                if (Utils.isGroupConversation(convMsisdn))                 // if Group Conversation delete groups too
                {
                    GroupTableUtils.deleteGroupWithId(convMsisdn);         // remove entry from Group Table
                    GroupManager.Instance.GroupCache.Remove(convMsisdn);
                    GroupManager.Instance.DeleteGroup(convMsisdn);         // delete the group file
                }
                MessagesTableUtils.deleteAllMessagesForMsisdn(convMsisdn); //removed all chat messages for this msisdn
                ConversationTableUtils.deleteConversation(convMsisdn);     // removed entry from conversation table
                //ConversationTableUtils.saveConvObjectList();
                MiscDBUtil.deleteMsisdnData(convMsisdn);
            }
            #endregion
        }