Beispiel #1
0
        public ConvMessage(ParticipantInfoState participantInfoState, JObject jsonObj)
        {
            string           grpId;
            string           from;
            GroupParticipant gp;

            this.MessageId            = -1;
            this.participantInfoState = participantInfoState;
            this.MessageStatus        = ConvMessage.State.RECEIVED_UNREAD;
            this.Timestamp            = TimeUtils.getCurrentTimeStamp();
            switch (this.participantInfoState)
            {
            case ParticipantInfoState.INTERNATIONAL_USER:
                this.Message        = AppResources.SMS_INDIA;
                this.MetaDataString = jsonObj.ToString(Newtonsoft.Json.Formatting.None);
                break;

            case ParticipantInfoState.GROUP_NAME_CHANGE:
                grpId = (string)jsonObj[HikeConstants.TO];
                from  = (string)jsonObj[HikeConstants.FROM];
                string grpName = (string)jsonObj[HikeConstants.DATA];
                this._groupParticipant = from;
                this._msisdn           = grpId;
                if (from == App.MSISDN)
                {
                    this.Message = string.Format(AppResources.GroupNameChangedByGrpMember_Txt, AppResources.You_Txt, grpName);
                }
                else
                {
                    gp           = GroupManager.Instance.getGroupParticipant(null, from, grpId);
                    this.Message = string.Format(AppResources.GroupNameChangedByGrpMember_Txt, gp.Name, grpName);
                }
                this.MetaDataString = jsonObj.ToString(Newtonsoft.Json.Formatting.None);
                break;

            case ParticipantInfoState.GROUP_PIC_CHANGED:
                grpId = (string)jsonObj[HikeConstants.TO];
                from  = (string)jsonObj[HikeConstants.FROM];
                this._groupParticipant = from;
                this._msisdn           = grpId;
                gp           = GroupManager.Instance.getGroupParticipant(null, from, grpId);
                this.Message = string.Format(AppResources.GroupImgChangedByGrpMember_Txt, gp.Name);
                jsonObj.Remove(HikeConstants.DATA);
                this.MetaDataString = jsonObj.ToString(Newtonsoft.Json.Formatting.None);
                break;

            default:
                this.MetaDataString = jsonObj.ToString(Newtonsoft.Json.Formatting.None);
                break;
            }
        }
Beispiel #2
0
        public ConvMessage(JObject obj, bool isSelfGenerated, bool addedLater)
        {
            // If the message is a group message we get a TO field consisting of the Group ID
            string toVal = obj[HikeConstants.TO].ToString();

            this._msisdn              = (toVal != null) ? (string)obj[HikeConstants.TO] : (string)obj[HikeConstants.FROM]; /*represents msg is coming from another client*/
            this._groupParticipant    = (toVal != null) ? (string)obj[HikeConstants.FROM] : null;
            this.participantInfoState = fromJSON(obj);
            this.metadataJsonString   = obj.ToString(Newtonsoft.Json.Formatting.None);

            if (this.participantInfoState == ParticipantInfoState.MEMBERS_JOINED || this.participantInfoState == ParticipantInfoState.PARTICIPANT_JOINED)
            {
                JArray arr = (JArray)obj[HikeConstants.DATA];
                List <GroupParticipant> addedMembers = null;
                for (int i = 0; i < arr.Count; i++)
                {
                    JObject nameMsisdn = (JObject)arr[i];
                    string  msisdn     = (string)nameMsisdn[HikeConstants.MSISDN];
                    if (msisdn == App.MSISDN)
                    {
                        continue;
                    }
                    bool onhike = true;
                    bool dnd    = true;
                    try
                    {
                        onhike = (bool)nameMsisdn["onhike"];
                    }
                    catch { }
                    try
                    {
                        dnd = (bool)nameMsisdn["dnd"];
                    }
                    catch { }

                    GroupParticipant gp = GroupManager.Instance.getGroupParticipant((string)nameMsisdn[HikeConstants.NAME], msisdn, _msisdn);
                    gp.HasLeft = false;
                    if (!isSelfGenerated) // if you yourself created JSON dont update these as GP is already updated while creating grp.
                    {
                        gp.IsOnHike = onhike;
                        gp.IsDND    = dnd;
                    }
                    if (addedLater)
                    {
                        if (addedMembers == null)
                        {
                            addedMembers = new List <GroupParticipant>(arr.Count);
                        }
                        addedMembers.Add(gp);
                    }
                }
                if (!isSelfGenerated) // when I am group owner chache is already sorted
                {
                    GroupManager.Instance.GroupCache[toVal].Sort();
                }
                if (addedLater)
                {
                    addedMembers.Sort();
                    this._message = GetMsgText(addedMembers, false);
                }
                else
                {
                    this._message = GetMsgText(GroupManager.Instance.GroupCache[toVal], true);
                }
            }

            else if (this.participantInfoState == ParticipantInfoState.GROUP_END)
            {
                this._message = AppResources.GROUP_CHAT_END;
            }
            else if (this.participantInfoState == ParticipantInfoState.PARTICIPANT_LEFT || this.participantInfoState == ParticipantInfoState.INTERNATIONAL_GROUP_USER)// Group member left
            {
                this._groupParticipant = (toVal != null) ? (string)obj[HikeConstants.DATA] : null;
                GroupParticipant gp = GroupManager.Instance.getGroupParticipant(_groupParticipant, _groupParticipant, _msisdn);
                this._message = gp.FirstName + AppResources.USER_LEFT;
                gp.HasLeft    = true;
                gp.IsUsed     = false;
            }

            this._timestamp = TimeUtils.getCurrentTimeStamp();
            if (isSelfGenerated)
            {
                this.MessageStatus = State.UNKNOWN;
            }
            else
            {
                this.MessageStatus = State.RECEIVED_UNREAD;
            }
        }
Beispiel #3
0
        public ConvMessage(JObject obj)
        {
            try
            {
                bool    isFileTransfer;
                JObject metadataObject = null;
                JToken  val            = null;
                obj.TryGetValue(HikeConstants.TO, out val);

                JToken metadataToken = null;
                try
                {
                    obj[HikeConstants.DATA].ToObject <JObject>().TryGetValue(HikeConstants.METADATA, out metadataToken);
                }
                catch { }

                if (metadataToken != null)
                {
                    metadataObject = JObject.FromObject(metadataToken);
                    JToken filesToken = null;
                    isFileTransfer = metadataObject.TryGetValue("files", out filesToken);
                    if (isFileTransfer)
                    {
                        JArray  files      = metadataObject["files"].ToObject <JArray>();
                        JObject fileObject = files[0].ToObject <JObject>();

                        JToken fileName;
                        JToken fileKey;
                        JToken thumbnail;
                        JToken contentType;

                        fileObject.TryGetValue(HikeConstants.FILE_CONTENT_TYPE, out contentType);
                        fileObject.TryGetValue(HikeConstants.FILE_NAME, out fileName);
                        fileObject.TryGetValue(HikeConstants.FILE_KEY, out fileKey);
                        fileObject.TryGetValue(HikeConstants.FILE_THUMBNAIL, out thumbnail);
                        this.HasAttachment = true;

                        byte[] base64Decoded = null;
                        if (thumbnail != null)
                        {
                            base64Decoded = System.Convert.FromBase64String(thumbnail.ToString());
                        }
                        this.FileAttachment = new Attachment(fileName == null ? "" : fileName.ToString(), fileKey == null ? "" : fileKey.ToString(), base64Decoded,
                                                             contentType.ToString(), Attachment.AttachmentState.FAILED_OR_NOT_STARTED);
                        if (contentType.ToString().Contains(HikeConstants.LOCATION))
                        {
                            JObject locationFile = new JObject();
                            locationFile[HikeConstants.LATITUDE]         = fileObject[HikeConstants.LATITUDE];
                            locationFile[HikeConstants.LONGITUDE]        = fileObject[HikeConstants.LONGITUDE];
                            locationFile[HikeConstants.ZOOM_LEVEL]       = fileObject[HikeConstants.ZOOM_LEVEL];
                            locationFile[HikeConstants.LOCATION_ADDRESS] = fileObject[HikeConstants.LOCATION_ADDRESS];
                            this.MetaDataString = locationFile.ToString(Newtonsoft.Json.Formatting.None);
                        }

                        if (contentType.ToString().Contains(HikeConstants.CONTACT))
                        {
                            this.MetaDataString = fileObject.ToString(Newtonsoft.Json.Formatting.None);
                        }
                    }
                    else
                    {
                        metadataJsonString = metadataObject.ToString(Newtonsoft.Json.Formatting.None);
                    }
                }
                participantInfoState = fromJSON(metadataObject);
                if (val != null) // represents group message
                {
                    _msisdn           = val.ToString();
                    _groupParticipant = (string)obj[HikeConstants.FROM];
                }
                else
                {
                    _msisdn           = (string)obj[HikeConstants.FROM]; /*represents msg is coming from another client or system msg*/
                    _groupParticipant = null;
                }

                JObject data = (JObject)obj[HikeConstants.DATA];
                JToken  msg;

                if (data.TryGetValue(HikeConstants.SMS_MESSAGE, out msg)) // if sms
                {
                    _message = msg.ToString();
                    _isSms   = true;
                }
                else       // if not sms
                {
                    _isSms = false;
                    if (this.HasAttachment)
                    {
                        string messageText = "";
                        if (this.FileAttachment.ContentType.Contains(HikeConstants.IMAGE))
                        {
                            messageText = AppResources.Image_Txt;
                        }
                        else if (this.FileAttachment.ContentType.Contains(HikeConstants.AUDIO))
                        {
                            messageText = AppResources.Audio_Txt;
                        }
                        else if (this.FileAttachment.ContentType.Contains(HikeConstants.VIDEO))
                        {
                            messageText = AppResources.Video_Txt;
                        }
                        else if (this.FileAttachment.ContentType.Contains(HikeConstants.LOCATION))
                        {
                            messageText = AppResources.Location_Txt;
                        }
                        else if (this.FileAttachment.ContentType.Contains(HikeConstants.CT_CONTACT))
                        {
                            messageText = AppResources.ContactTransfer_Text;
                        }
                        this._message = messageText;
                    }
                    else
                    {
                        if (participantInfoState == ParticipantInfoState.INTERNATIONAL_USER)
                        {
                            _message = AppResources.SMS_Works_Only_In_India_Txt;
                        }
                        else
                        {
                            _message = (string)data[HikeConstants.HIKE_MESSAGE];
                        }
                    }
                }
                if (data.TryGetValue("poke", out msg)) // if sms
                {
                    metadataJsonString = "{poke: true}";
                }

                //JToken ts = null;
                //if (data.TryGetValue(HikeConstants.TIMESTAMP, out ts))
                _timestamp = TimeUtils.getCurrentTimeStamp();

                /* prevent us from receiving a message from the future */

                long now = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds / 1000;
                this.Timestamp = (this.Timestamp > now) ? now : this.Timestamp;

                /* if we're deserialized an object from json, it's always unread */
                this.MessageStatus = State.RECEIVED_UNREAD;
                this._messageId    = -1;
                string mappedMsgID = (string)data[HikeConstants.MESSAGE_ID];
                this.MappedMessageId = System.Int64.Parse(mappedMsgID);
            }
            catch (Exception e)
            {
                throw new Exception("Error in parsing json");
            }
        }