Exemple #1
0
        public async Task DeleteAllMessages(ChadderConversation conversation)
        {
            ChadderApp.UIHelper.ShowLoading();
            //++await Source.DeleteAllMessages(conversation);
            await Source.DeleteAllMessages();

            ChadderApp.UIHelper.HideLoading();
        }
Exemple #2
0
        public async Task TakeBack(ChadderMessage msg, ChadderConversation conversation)
        {
            ChadderApp.UIHelper.ShowLoading();
            var result = await Source.TakeMessageBack(msg, conversation);

            ChadderApp.UIHelper.HideLoading();
            ShowErrorIfNotOk(result);
        }
Exemple #3
0
        static public ChatFragment OpenChat(ChadderConversation conversation)
        {
            var result = new ChatFragment();

            result.Arguments = new Bundle();
            result.Arguments.PutInt(EXTRA_CONVERSATION_ID, conversation.recordId);
            return(result);
        }
        public async Task <ChadderMessage> SendMessage(string txt, ChadderConversation conversation)
        {
            var msg = ChadderMessage.Create(conversation, db.LocalUser, ChadderMessage.MESSAGE_TYPE.TEXT);

            msg.Body = txt;

            await db.AddMessage(msg, conversation);

            AddPendingMessage(msg);
            return(msg);
        }
 public async Task SetHidden(ChadderConversation conversation)
 {
     try
     {
         conversation.Hidden = true;
         await sqlDB.UpdateAsync(conversation);
     }
     catch (Exception ex)
     {
         Insight.Report(ex);
     }
 }
Exemple #6
0
        public bool SendMessage(string txt, ChadderConversation conversation)
        {
            txt = txt.Trim();

            if (string.IsNullOrWhiteSpace(txt))
            {
                ChadderApp.UIHelper.ShowError("Can't send an empty message!");
                return(false);
            }
            Source.SendMessage(txt, conversation);
            return(true);
        }
Exemple #7
0
 public override void OnCreate(Bundle savedInstanceState)
 {
     base.OnCreate(savedInstanceState);
     if (Arguments.GetString(EXTRA_CONTACT_ID, null) == null)
     {
         _conversation = ChadderUI.Source.db.GetConversation(Arguments.GetInt(EXTRA_CONVERSATION_ID, 0));
     }
     else
     {
         _conversation = ChadderUI.Source.db.GetContactConversation(Arguments.GetString(EXTRA_CONTACT_ID));
     }
     EventHandlers.Add(_conversation, (s, e) =>
     {
         InvalidateData();
     });
 }
        public async Task <ChadderError> TakeAllMessagesBack(ChadderConversation conversation)
        {
            var package = new TakeMessageBackContent();
            var content = await EncryptForUser(package, conversation.Contact);

            var request = new SendPackageParameter()
            {
                UserId = conversation.ContactUserId,
                Data   = content.Serialize()
            };
            var result = await AuthorizedRequest <BasicResponse <string> >(Connection.ChatHub, "SendPackageToUser", request);

            if (result.Error == ChadderError.OK)
            {
                await db.DeleteAllMessages(conversation);
            }
            return(result.Error);
        }
Exemple #9
0
        public virtual async Task ProcessTakeBack(TakeMessageBackContent content, string fromId, string toId)
        {
            ChadderConversation conversation = null;

            if (fromId == db.LocalUser.UserId)
            {
                conversation = db.GetContactConversation(toId);
            }
            else
            {
                conversation = db.GetContactConversation(fromId);
            }
            if (content.MessageIds.Count == 0)
            {
                foreach (var msg in conversation.Messages)
                {
                    if (msg.UserId == fromId)
                    {
                        await db.DeleteMessage(msg, conversation);
                    }
                }
            }
            else
            {
                foreach (var id in content.MessageIds)
                {
                    var msg = conversation.Messages.FirstOrDefault(i => i.MessageId == id);
                    if (msg != null)
                    {
                        if (msg.Sender.UserId == fromId)
                        {
                            await db.DeleteMessage(msg, conversation);
                        }
                        else
                        {
                            Insight.Track(string.Format("User {0} unauthorized take back", fromId));
                        }
                    }
                }
            }
        }
        public async void SendPicture(byte[] data, ChadderConversation conversation)
        {
            var record = new ChadderSQLPicture()
            {
                ToBeUploaded = true,
                Bin          = data,
                PictureId    = Guid.NewGuid().ToString()// Temporary
            };

            await sqlDB.InsertAsync(record);

            var picture = await db.LoadPicture(record, true);

            var msg = ChadderMessage.Create(conversation, db.LocalUser, ChadderMessage.MESSAGE_TYPE.PICTURE);

            msg.PictureId = record.PictureId;
            msg.Picture   = picture;

            await db.AddMessage(msg, conversation);

            AddPendingMessage(msg);
        }
Exemple #11
0
 public async Task TakeAllBack(ChadderConversation conversation)
 {
 }
 public ChatAdapter(Activity context, ChadderConversation conversation, ListView listView)
     : base(context, 0, conversation.Messages)
 {
     _conversation = conversation;
     this.lstView  = listView;
 }
Exemple #13
0
 public Task SetHidden(ChadderConversation conversation)
 {
     return(Source.SetHidden(conversation));
 }
Exemple #14
0
 public bool SendPicture(byte[] data, ChadderConversation conversation)
 {
     Source.SendPicture(data, conversation);
     return(true);
 }
        protected async Task <ChadderError> SendMessageToServer(ChadderMessage msg, ChadderConversation conversation)
        {
            try
            {
                BasicMessage package = null;
                if (msg.Type == ChadderMessage.MESSAGE_TYPE.TEXT)
                {
                    package = new TextMessage()
                    {
                        Body = msg.Body
                    };
                }
                else if (msg.Type == ChadderMessage.MESSAGE_TYPE.PICTURE)
                {
                    if (msg.Picture.ToBeUploaded)
                    {
                        var record = await sqlDB.GetPicture(msg.Picture.RecordId);

                        var presult = await UploadPicture(record);

                        if (presult.Error == ChadderError.OK)
                        {
                            msg.PictureId = record.PictureId;
                            await sqlDB.UpdateAsync(msg);
                        }
                        else
                        {
                            return(presult.Error);
                        }
                    }
                    package = new ImageMessage()
                    {
                        PictureId = msg.PictureId
                    };
                }
                else
                {
                    Insight.Track("Invalid Message Type in SendMessageToServer");
                    return(ChadderError.INVALID_INPUT);
                }
                package.Id         = msg.MessageId;
                package.Group      = null;
                package.TimeSent   = msg.TimeSent.Value;
                package.Expiration = msg.Expiration.Value;

                var content = await EncryptForUser(package, conversation.Contact);

                var request = new SendPackageParameter()
                {
                    UserId = conversation.ContactUserId,
                    Data   = content.Serialize()
                };
                var result = await AuthorizedRequest <BasicResponse <string> >(Connection.ChatHub, "SendPackageToUser", request);

                if (result.Error == ChadderError.OK)
                {
                    msg.Status      = ChadderMessage.MESSAGE_STATUS.SENT;
                    msg.TimeServer  = result.Time;
                    msg.ReferenceId = result.Extra;
                    await sqlDB.UpdateAsync(msg);
                }
                return(result.Error);
            }
            catch (Exception ex)
            {
                Insight.Report(ex);
                return(ChadderError.GENERAL_EXCEPTION);
            }
        }
Exemple #16
0
 public async Task DeleteMessage(ChadderMessage msg, ChadderConversation conversation)
 {
     //++await Source.DeleteMessage(msg, conversation);
     await Source.DeleteAllMessages();
 }
Exemple #17
0
        public virtual async Task <bool> ProcessPackage(Package package)
        {
            var content    = Content.Deserialize(package.Data);
            var identifier = await content.Find <ECDH>(this);

            string fromId = null;
            string toId   = null;

            if (identifier != null)
            {
                fromId = identifier.SourceId;
                toId   = identifier.TargetId;
            }
            if (content == null)
            {
                return(false);
            }
            // If multiple layer of encryption were to be supported there should be a loop around this section
            if (content is IContentWrapper) // Decrypt section
            {
                content = await(content as IContentWrapper).GetContent(this);
            }

            // This has to be the clear content
            if (content is UserPublicKey)
            {
                if (package.OwnerId != null)
                {
                    Insight.Track("Non-server trying to update client id");
                }
                else
                {
                    await ProcessUserPublicKey(content as UserPublicKey);
                }
            }
            else if (content is DevicePublicKey)
            {
                if (package.OwnerId != null)
                {
                    Insight.Track("Non-server trying to update device id");
                    return(true);
                }
                var device = db.GetDevice((content as DevicePublicKey).DeviceId);
                var pbk    = (content as DevicePublicKey).PublicKeyData;
                if (device == null)
                {
                    if (pbk != null)
                    {
                        device = new ChadderUserDevice()
                        {
                            DeviceId          = (content as DevicePublicKey).DeviceId,
                            PublicKeyBookData = pbk,
                            Name          = "Temp",
                            HasUserKey    = false,
                            CurrentDevice = false
                        };
                        await db.AddDevice(device);
                    }
                }
                else
                {
                    if (pbk == null)
                    {
                        db.LocalUser.Devices.Remove(device);
                        await sqlDB.DeleteAsync(device);
                    }
                    else
                    {
                        device.PublicKeyBookData = pbk;
                        await sqlDB.UpdateAsync(device);
                    }
                }
            }
            else if (content is PairDeviceContent)
            {
                if (package.OwnerId != db.LocalUser.UserId)
                {
                    Insight.Track("Not this user trying to pair device");
                    return(true);
                }
                db.LocalUser.PrivateKeyBookData = (content as PairDeviceContent).Book.Serialize();
                await sqlDB.UpdateAsync(db.LocalUser);

                return(true);
            }
            else if (content is TakeMessageBackContent)
            {
                await ProcessTakeBack(content as TakeMessageBackContent, fromId, toId);
            }
            else if (content is BasicMessage) // Process section
            {
                var msg = content as BasicMessage;
                ChadderConversation conversation = null;
                if (fromId == db.LocalUser.UserId)
                {
                    conversation = db.GetContactConversation(toId);
                }
                else
                {
                    conversation = db.GetContactConversation(package.OwnerId);
                }
                if (conversation.Messages.FirstOrDefault(i => i.MessageId == msg.Id) == null)
                {
                    var record = new ChadderMessage()
                    {
                        MessageId      = msg.Id,
                        Type           = ChadderMessage.MESSAGE_TYPE.TEXT,
                        Status         = ChadderMessage.MESSAGE_STATUS.SENT,
                        Expiration     = msg.Expiration,
                        TimeSent       = msg.TimeSent,
                        TimeReceived   = DateTime.UtcNow,
                        TimeServer     = package.Time,
                        MyMessage      = false,
                        ConversationId = conversation.recordId,
                        Sender         = conversation.Contact,
                    };

                    if (conversation.ContactUserId != fromId)
                    {
                        record.MyMessage = true;
                        record.Sender    = db.LocalUser;
                    }
                    record.UserId = record.Sender.UserId;

                    if (content is TextMessage)
                    {
                        record.Body = (content as TextMessage).Body;
                    }

                    await db.AddMessage(record, conversation);
                }
                else
                {
                    Insight.Track("Repeated Message GUID");
                }
            }
            else
            {
                return(false);
            }
            return(true);
        }