Exemplo n.º 1
0
        public async Task UpdateNmStoreRecordAsync(NmStoreModel newModel)
        {
            using (var dbContext = new GmailBotDbContext())
            {
                var existModel = await dbContext.NmStore
                                 .Where(nmStore => nmStore.Id == newModel.Id)
                                 .Include(nmStore => nmStore.To)
                                 .Include(nmStore => nmStore.Cc)
                                 .Include(nmStore => nmStore.Bcc)
                                 .Include(nmStore => nmStore.File)
                                 .SingleOrDefaultAsync();

                if (existModel == null)
                {
                    return;
                }
                // Update
                dbContext.Entry(existModel).CurrentValues.SetValues(newModel);
                // Delete
                dbContext.To.RemoveRange(existModel.To.Except(newModel.To, new IdEqualityComparer <ToModel>()));
                dbContext.Cc.RemoveRange(existModel.Cc.Except(newModel.Cc, new IdEqualityComparer <CcModel>()));
                dbContext.Bcc.RemoveRange(existModel.Bcc.Except(newModel.Bcc, new IdEqualityComparer <BccModel>()));
                dbContext.File.RemoveRange(existModel.File.Except(newModel.File, new IdEqualityComparer <FileModel>()));

                UpdateAdress(dbContext, newModel.To, existModel.To);
                UpdateAdress(dbContext, newModel.Cc, existModel.Cc);
                UpdateAdress(dbContext, newModel.Bcc, existModel.Bcc);
                UpdateFile(dbContext, newModel.File, existModel.File);
                await dbContext.SaveChangesAsync();
            }
        }
Exemplo n.º 2
0
        public static void ComposeNmStateModel(NmStoreModel model, FormattedMessage formattedMessage)
        {
            model.NullInspect(nameof(model));
            formattedMessage.NullInspect(nameof(formattedMessage));

            formattedMessage.To?.ForEach(to => { model.To.Add(new ToModel {
                    Email = to.Email, Name = to.Name
                }); });
            formattedMessage.Cc?.ForEach(cc => { model.Cc.Add(new CcModel {
                    Email = cc.Email, Name = cc.Name
                }); });
            formattedMessage.Bcc?.ForEach(bcc => { model.Bcc.Add(new BccModel {
                    Email = bcc.Email, Name = bcc.Name
                }); });
            if (formattedMessage.HasAttachments)
            {
                foreach (var attach in formattedMessage.Attachments)
                {
                    model.File.Add(new FileModel
                    {
                        AttachId     = attach.Id,
                        OriginalName = attach.FileName
                    });
                }
            }
            model.Subject = formattedMessage.Subject;
            if (formattedMessage.Body != null)
            {
                model.Message = GetTextPlainMessage(formattedMessage.Body);
            }
        }
Exemplo n.º 3
0
 private void UpdateNmStoreModel(NmStoreModel model, Message message)
 {
     if (message.GetType() == typeof(TextMessage))
     {
         model.Message = (message as TextMessage)?.Text;
     }
     else if (message.GetType() == typeof(DocumentMessage))
     {
         model.Message = (message as DocumentMessage)?.Document.FileName;
         model.Message = (message as DocumentMessage)?.Document.FileId;
     }
 }
Exemplo n.º 4
0
        public void RemoveNmStore(NmStoreModel model)
        {
            model.NullInspect(nameof(model));

            using (var dbContext = new GmailBotDbContext())
            {
                var existModel = dbContext.NmStore
                                 .Where(nmStore => nmStore.Id == model.Id)
                                 .Include(nmStore => nmStore.To)
                                 .Include(nmStore => nmStore.Cc)
                                 .Include(nmStore => nmStore.Bcc)
                                 .Include(nmStore => nmStore.File)
                                 .SingleOrDefault();
                if (existModel == null)
                {
                    return;
                }

                dbContext.NmStore.Remove(existModel);
                dbContext.SaveChanges();
            }
        }
Exemplo n.º 5
0
        public SendKeyboard CreateKeyboard(SendKeyboardState state, NmStoreModel model = null, string draftId = "")
        {
            SendKeyboard keyboard;

            switch (state)
            {
            case SendKeyboardState.Init:
                keyboard = new InitKeyboard(null);
                break;

            case SendKeyboardState.Store:
                keyboard = new StoreKeyboard(model);
                break;

            case SendKeyboardState.Continue:
                keyboard = new InitKeyboard(model);
                break;

            case SendKeyboardState.Drafted:
            case SendKeyboardState.SentWithError:
                keyboard = new DraftedKeyboard(model, draftId);
                break;

            case SendKeyboardState.SentSuccessful:
                keyboard = null;
                break;

            default:
                return(null);
            }
            if (keyboard == null)
            {
                return(null);
            }
            keyboard.CreateInlineKeyboard();
            return(keyboard);
        }
Exemplo n.º 6
0
        private string BuildNewMailMessage(NmStoreModel model, SendKeyboardState state)
        {
            string headerText;

            switch (state)
            {
            case SendKeyboardState.Init:
            case SendKeyboardState.Continue:
                headerText = _newMessageMainText;
                break;

            case SendKeyboardState.SentSuccessful:
                headerText = _newMessageSuccessfulSentText;
                break;

            default:
                headerText = "";
                break;
            }
            var message = new StringBuilder(headerText);

            if (model == null)
            {
                message.AppendLine(_newMessageTipText);
                return(message.ToString());
            }
            var iterFunc = new Action <StringBuilder, List <string>, string>((builder, collection, label) =>
            {
                if (collection == null || !collection.Any())
                {
                    return;
                }
                builder.Append($"<b>{label}:</b> ");
                collection.IndexEach((item, i) =>
                {
                    builder.Append($"<code>{Path.GetFileName(item)}</code>"); //! GetFileName
                    if (i < collection.Count - 1)
                    {
                        builder.Append(", ");
                    }
                });
            });

            message.AppendLine();
            iterFunc(message, model.To.Select(a => a.Email).ToList(), "To");
            message.AppendLine();
            iterFunc(message, model.Cc.Select(a => a.Email).ToList(), "Cc");
            message.AppendLine();
            iterFunc(message, model.Bcc.Select(a => a.Email).ToList(), "Bcc");
            message.AppendLine();
            if (model.Subject != null)
            {
                message.AppendLine($"<b>Subject:</b> {model.Subject}");
            }
            if (model.Message != null)
            {
                message.AppendLine("<b>Message:</b>");
                message.AppendLine(model.Message);
            }
            message.AppendLine();
            iterFunc(message, model.File.Select(f => f.OriginalName).ToList(), $"{Emoji.PAPER_CLIP}Attachments"); //Emoji probable cause of error, because it will be send inside <b> tag
            return(message.ToString());
        }
Exemplo n.º 7
0
        public async Task UpdateNewMailMessage(string chatId, SendKeyboardState state, NmStoreModel model, string draftId = "", string errorMessage = "Unidentified error")
        {
            var    keyboard = _sendKeyboardFactory.CreateKeyboard(state, model, draftId);
            string message  = null;

            switch (state)
            {
            case SendKeyboardState.Drafted:
                message = _restoreFromDraftMessageText;
                break;

            case SendKeyboardState.SentSuccessful:
                message = BuildNewMailMessage(model, state);
                break;

            case SendKeyboardState.SentWithError:
                message = $"<b>Error while sending a message:</b>{Environment.NewLine}{errorMessage}";
                break;

            default:
                message = BuildNewMailMessage(model, state);
                break;
            }

            await _telegramMethods.EditMessageText(message, chatId, model.MessageId.ToString(), null, ParseMode.Html, true, keyboard);
        }
Exemplo n.º 8
0
        public async Task <TextMessage> SpecifyNewMailMessage(string chatId, SendKeyboardState state, NmStoreModel model = null)
        {
            var keyboard = _sendKeyboardFactory.CreateKeyboard(state, model);
            var message  = BuildNewMailMessage(model, state);

            return(await _telegramMethods.SendMessage(chatId, message, ParseMode.Html, true, false, null, keyboard));
        }
Exemplo n.º 9
0
 protected SendKeyboard(NmStoreModel model)
 {
     Model = model;
 }
Exemplo n.º 10
0
 public StoreKeyboard(NmStoreModel model) : base(model)
 {
 }
Exemplo n.º 11
0
        private async Task <Draft> SaveDraftMailServer(Service service, NmStoreModel nmModel, List <IUserInfo> sender = null)
        {
            nmModel.NullInspect(nameof(nmModel));
            List <FileStream> streams = new List <FileStream>();
            Draft             draft;

            try
            {
                var downloadAndOpenFiles = new Func <string, Task>(async messageId =>
                {
                    if (nmModel.File != null)
                    {
                        var teleramFiles = await DownloadFilesFromTelegramStore(nmModel.File);
                        teleramFiles.ForEach(f => streams.Add(File.OpenRead(f)));
                        if (messageId == null)
                        {
                            return;
                        }

                        var gmailFiles = await DownloadFilesFromGmailStore(service, messageId, nmModel.File);
                        gmailFiles.ForEach(f => streams.Add(File.OpenRead(f)));
                    }
                });

                if (string.IsNullOrEmpty(nmModel.DraftId))
                {
                    await downloadAndOpenFiles(null);

                    var body = Methods.CreateNewDraftBody(nmModel.Subject, nmModel.Message,
                                                          nmModel.To.ToList <IUserInfo>(),
                                                          nmModel.Cc.ToList <IUserInfo>(),
                                                          nmModel.Bcc.ToList <IUserInfo>(),
                                                          sender, streams);
                    draft = await Methods.CreateDraft(body, service.From);
                }
                else
                {
                    draft = await Methods.GetDraft(service.From, nmModel.DraftId);
                    await downloadAndOpenFiles(draft.Message.Id);

                    var body = Methods.AddToDraftBody(draft, nmModel.Subject, nmModel.Message,
                                                      nmModel.To.ToList <IUserInfo>(),
                                                      nmModel.Cc.ToList <IUserInfo>(),
                                                      nmModel.Bcc.ToList <IUserInfo>(),
                                                      sender, streams);
                    draft = await Methods.UpdateDraft(body, service.From, draft.Id);
                }
                if (draft == null)
                {
                    await _botActions.SendSaveMessageAsDraftError(service.From);
                }
            }
            finally
            {
                foreach (var stream in streams)
                {
                    stream.Close();
                    var dir   = Path.GetDirectoryName(stream.Name);
                    var dInfo = new DirectoryInfo(dir);
                    if (dInfo.Exists)
                    {
                        dInfo.Delete(true);
                    }
                }
            }
            return(draft);
        }
Exemplo n.º 12
0
 public InitKeyboard(NmStoreModel model) : base(model)
 {
 }
Exemplo n.º 13
0
 public DraftedKeyboard(NmStoreModel model, string draftId) : base(model)
 {
     DraftId = draftId;
 }