private static void GetGroupMessages(Guid ownerId, MessageModel mess, ManagementContext dc, int page, int take) { //TODO: add Take(50) a few days from now. var to = (from xx in dc.GroupMessages where xx.IsDeleted == false where xx.Recipients.Where(z => z.IsRemovedFromGroup == false).Select(x => x.Recipient.MemberId).Contains(ownerId) select xx).AsParallel().OrderByDescending(x => x.LastModified).Skip(page * take).Take(take).ToList(); List<RDN.Library.DataModels.Messages.GroupMessage> groups = new List<DataModels.Messages.GroupMessage>(); //foreach (var recipient in to) foreach (var group in to) { try { //var group = recipient.Group; //var messages = recipient.Group.Messages; foreach (var message in group.Messages) { MessageSingleModel ms = new MessageSingleModel(); if (message.FromUser == null) { ms.FromId = new Guid(); } else { ms.FromId = message.FromUser.MemberId; ms.FromName = message.FromUser.DerbyName; } ms.MessageCreated = message.Created; ms.MessageId = message.MessageId; if (!String.IsNullOrEmpty(message.MessageText)) { ms.MessageText = RDN.Portable.Util.Strings.StringExt.HtmlDecode(message.MessageText); if (ms.MessageText.Length > 20) ms.MessageText = ms.MessageText.Remove(20); } var convo = mess.Conversations.Where(x => x.GroupMessageId == group.GroupId).FirstOrDefault(); if (convo == null) { ConversationModel con = new ConversationModel(); con.CanDelete = true; if (message.FromUser != null) { con.FromName = message.FromUser.DerbyName; con.FromId = message.FromUser.MemberId; } con.LastPostDate = ms.MessageCreated; if (message.MessagesInbox.Where(x => x.ToUser.MemberId == ownerId).FirstOrDefault() == null) con.IsConversationRead = true; con.GroupMessageId = group.GroupId; if (!String.IsNullOrEmpty(group.TitleOfMessage)) con.Title = group.TitleOfMessage; else con.Title = "Message"; if (message.FromUser != null) { con.LastPostBy = message.FromUser.DerbyName; } var recips = group.Recipients.Where(x => x.IsRemovedFromGroup == false); foreach (var rec in recips) { MemberDisplayMessage mem = new MemberDisplayMessage(); mem.DerbyName = rec.Recipient.DerbyName; mem.MemberId = rec.Recipient.MemberId; mem.UserId = rec.Recipient.AspNetUserId; var photo = rec.Recipient.Photos.FirstOrDefault(); if (photo != null) mem.ThumbUrl = photo.ImageUrlThumb; con.Recipients.Add(mem); } con.Messages.Add(ms); mess.Conversations.Add(con); } else { if (ms.MessageCreated > convo.LastPostDate) { convo.LastPostBy = ms.FromName; convo.LastPostDate = ms.MessageCreated; } else { convo.FromId = ms.FromId; convo.FromName = ms.FromName; } if (message.MessagesInbox.Where(x => x.ToUser.MemberId == ownerId).FirstOrDefault() != null) convo.IsConversationRead = false; else convo.IsConversationRead = true; convo.Messages.Add(ms); } groups.Add(group); } } catch (Exception exception) { ErrorDatabaseManager.AddException(exception, exception.GetType()); } } mess.Conversations = mess.Conversations.OrderByDescending(x => x.LastPostDate).ToList(); }
public static ConversationModel GetConversationFromGroup(long groupId, Guid memId) { ConversationModel con = new ConversationModel(); try { con.GroupMessageId = groupId; con.OwnerUserId = memId; var toMem = MemberCache.GetMemberDisplay(memId); con.FromName = toMem.DerbyName; var dc = new ManagementContext(); var gro = (from xx in dc.GroupMessages.Include("Messages") where xx.GroupId == groupId where xx.IsDeleted == false select new { xx.TitleOfMessage, Messages = (from yy in xx.Messages select new { yy.FromUser, yy.Created, yy.MessageId, yy.MessageText, yy.MessagesInbox }).OrderBy(z => z.MessageId), Recipients = xx.Recipients.Where(x => x.IsRemovedFromGroup == false), }).AsParallel().FirstOrDefault(); if (gro == null) return null; foreach (var user in gro.Recipients) { MemberDisplayMessage mem = new MemberDisplayMessage(); mem.DerbyName = user.Recipient.DerbyName; mem.Firstname = user.Recipient.Firstname; mem.LastName = user.Recipient.Lastname; if (user.Recipient.Photos.OrderByDescending(x => x.Created).FirstOrDefault() != null) mem.ThumbUrl = user.Recipient.Photos.OrderByDescending(x => x.Created).FirstOrDefault().ImageUrlThumb; mem.MemberId = user.Recipient.MemberId; mem.UserId = user.Recipient.AspNetUserId; con.Recipients.Add(mem); } con.Title = gro.TitleOfMessage; foreach (var message in gro.Messages) { try { MessageSingleModel ms = new MessageSingleModel(); ms.FromId = message.FromUser.MemberId; ms.FromName = message.FromUser.DerbyName; if (message.FromUser.Photos.OrderByDescending(x => x.Created).FirstOrDefault() != null) ms.ThumbUrl = message.FromUser.Photos.OrderByDescending(x => x.Created).FirstOrDefault().ImageUrlThumb; ms.MessageCreated = message.Created; ms.MessageId = message.MessageId; foreach (var inbox in message.MessagesInbox) { var user = con.Recipients.Where(x => x.MemberId == inbox.ToUser.MemberId).FirstOrDefault(); user.HasNotReadConversation = true; } if (!String.IsNullOrEmpty(message.MessageText)) { ms.MessageText = message.MessageText; ms.MessageTextHtml = message.MessageText; } if (ms.MessageCreated > con.LastPostDate) { con.LastPostDate = ms.MessageCreated; //con.IsConversationRead = ms.IsMessageRead; con.LastPostBy = ms.FromName; } if (con.Messages.Where(x => x.MessageId == ms.MessageId).FirstOrDefault() == null) con.Messages.Add(ms); } catch (Exception exception) { ErrorDatabaseManager.AddException(exception, exception.GetType()); } } } catch (Exception exception) { ErrorDatabaseManager.AddException(exception, exception.GetType()); } return con; }
public ActionResult CreateNewTextMessage(MessageModel model) { try { RDN.Portable.Classes.Controls.Message.ConversationModel mess = new Portable.Classes.Controls.Message.ConversationModel(); List<Guid> listOfGuids = new List<Guid>(); List<long> listOfGroupIds = new List<long>(); mess.MemberId = model.OwnerId; mess.FromId = model.OwnerId; mess.Message = model.MessageTextWriting; listOfGuids.Add(RDN.Library.Classes.Account.User.GetMemberId()); mess.OwnerType = model.OwnerType; if (!String.IsNullOrEmpty(model.ToMemberIds)) { foreach (string guid in model.ToMemberIds.Split(',')) { Guid temp = new Guid(); bool didWork = Guid.TryParse(guid, out temp); if (didWork) listOfGuids.Add(temp); } } if (!String.IsNullOrEmpty(model.ToMemberNames)) { foreach (string guid in model.ToMemberNames.Split(',')) { Guid temp = new Guid(); bool didWork = Guid.TryParse(guid, out temp); if (didWork) listOfGuids.Add(temp); } } if (!String.IsNullOrEmpty(model.ToGroupIds)) { foreach (string guid in model.ToGroupIds.Split(',')) { long temp = new long(); if (Int64.TryParse(guid, out temp)) listOfGroupIds.Add(temp); } } listOfGuids = listOfGuids.Distinct().ToList(); foreach (var guid in listOfGuids) { MemberDisplayMessage mem = new MemberDisplayMessage(); mem.MemberId = guid; mess.Recipients.Add(mem); } listOfGroupIds = listOfGroupIds.Distinct().ToList(); foreach (var guid in listOfGroupIds) { mess.GroupIds.Add(guid); } Messages.CreateNewTextMessageForGroup(mess); return Redirect("~/messages/" + GroupOwnerTypeEnum.member.ToString() + "/" + model.OwnerId.ToString().Replace("-", "") + "?u=" + SiteMessagesEnum.ms.ToString()); } catch (Exception exception) { ErrorDatabaseManager.AddException(exception, exception.GetType()); } return Redirect("~/messages/" + GroupOwnerTypeEnum.member.ToString() + "/" + model.OwnerId.ToString().Replace("-", "") + "?u=" + SiteMessagesEnum.mns.ToString()); }