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); }
// 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); }