public void SendEmailsForPost(Post post, Channel sourceChannel) { var message = new MailMessage(); message.From = new MailAddress(_fromEmail, "Info Hub"); message.Subject = string.Format("New Infohub message for '{0}'", sourceChannel.Name); message.Body = _GetEmailText(post); message.BodyEncoding = Encoding.UTF8; foreach (var user in sourceChannel.Users) { if (user.User.EmailNotificationEnabled) message.Bcc.Add(user.User.Email); } _client.SendAsync(message, null); }
private static string _GetEmailText(Post post) { var emailBody = new StringBuilder(); emailBody.AppendLine(string.Format("From: {0}", post.ByUser.Name)); emailBody.AppendLine(string.Format("Channel: {0}", post.Channel.Name)); emailBody.AppendLine(); emailBody.AppendLine(); emailBody.AppendLine(post.Content); if (!string.IsNullOrEmpty(post.UrlForFurtherInfos)) { emailBody.AppendLine(); emailBody.AppendLine(string.Format("Further info: {0}", post.UrlForFurtherInfos)); } return emailBody.ToString(); }
private PostForUser _GetPostForUser(Post post, User user, Channel sourceChannel) { var postForUser = _dbContext.PostsForUser.SingleOrDefault(x => x.Post.Id == post.Id && x.User.Id == user.Id && x.SourceChannel.Id == sourceChannel.Id); if (postForUser == null) { postForUser = new PostForUser { Post = post, User = user, WasRead = false, SourceChannel = sourceChannel }; _dbContext.PostsForUser.Add(postForUser); } return postForUser; }
private void _NotifySubscribersForPostUpdate(Post post) { var postViewModel = Mapper.Map<PostViewModel>(post); var postHtml = RenderPartialViewToString("ViewPost", postViewModel); var clients = Hub.GetClients<NewsFeed>(); clients[post.Channel.Name].updatePost(post.Id, postHtml); }
public ActionResult CreatePost(int channel, string message, string additionalInfo) { var sourceChannel = _dbContext.Channels.Single(x => x.Id == channel); var post = new Post { ByUser = CurrentUser, Channel = sourceChannel, Content = message, Date = DateTime.UtcNow, UrlForFurtherInfos = additionalInfo }; _dbContext.Posts.Add(post); foreach (var subscribedChannelByUser in sourceChannel.Users) _GetPostForUser(post, subscribedChannelByUser.User, sourceChannel); _dbContext.SaveChanges(); _emailNotifier.SendEmailsForPost(post, sourceChannel); var postViewModel = Mapper.Map<PostViewModel>(post); var postHtml = RenderPartialViewToString("ViewPost", postViewModel); var clients = Hub.GetClients<NewsFeed>(); clients[sourceChannel.Name].addPost(postHtml); return Json(sourceChannel.Name); }