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