예제 #1
0
        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();
        }
        void ReturnMessageModel(MessageModel model)
        {
            try
            {
                messageModel = model;

                Dispatcher.BeginInvoke(delegate
                {

                    if (messageModel != null)
                    {
                        MessagesList.ItemsSource = model.Conversations;
                    }
                    progressIndicator.IsVisible = false;
                    isLoading = false;
                });
            }
            catch (Exception exception)
            {
                ErrorHandler.Save(exception, MobileTypeEnum.WP8);
            }
        }
예제 #3
0
        public static MessageModel GetMessagesForOwner(GroupOwnerTypeEnum ownerType, Guid memId, int page, int count)
        {
            MessageModel mess = new MessageModel();
            try
            {
                mess.IdOfEntity = memId;
                mess.OwnerType = ownerType;

                var dc = new ManagementContext();
                GetGroupMessages(memId, mess, dc, page, count);
                //this is the conversation with my self.  I don't know ye why this shows up talking to one self.
                var personalConvo = mess.Conversations.Where(x => x.ConversationWithUser == memId).FirstOrDefault();
                if (personalConvo != null)
                    mess.Conversations.Remove(personalConvo);
            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType());
            }
            return mess;
        }
        public static int EmailMembersAboutMessagesWaitingInInbox()
        {
            int emailsSent = 0;
            try
            {
                var dc = new ManagementContext();
                int type = Convert.ToInt32(TaskTypeEnum.EmailMembersThatHaveMessagesSittingInInbox);
                var emailTask = dc.AutomatedTasks.Where(x => x.TaskIdForDescription == type).FirstOrDefault();

                if (emailTask == null)
                {
                    TaskForRunning newTask = new TaskForRunning();
                    newTask.FirstRun = DateTime.UtcNow;
                    newTask.HoursBetweenEachRunOfTask = HOURS_BETWEEN_EMAILS_FOR_MESSAGES_IN_INBOX;
                    newTask.LastRun = DateTime.UtcNow;
                    newTask.TaskIdForDescription = type;
                    dc.AutomatedTasks.Add(newTask);
                    dc.SaveChanges();
                }
                else
                {
                    emailTask.HoursBetweenEachRunOfTask = HOURS_BETWEEN_EMAILS_FOR_MESSAGES_IN_INBOX;
                    if (emailTask.LastRun.AddHours(HOURS_BETWEEN_EMAILS_FOR_MESSAGES_IN_INBOX) < DateTime.UtcNow)
                    {
                        List<Guid> membersToEmail = new List<Guid>();
                        MessageModel mess = new MessageModel();

                        emailTask.LastRun = DateTime.UtcNow;

                        var to = (from xx in dc.MessageInbox.Include("Message").Include("Message.FromUser").Include("ToUser")
                                  where xx.UserNotifiedViaEmail == false
                                  where xx.IsRead == false
                                  select xx).ToList();

                        foreach (var message in to)
                        {
                            message.UserNotifiedViaEmail = true;
                            message.NotifiedEmailDateTime = DateTime.UtcNow;
                            try
                            {
                                MessageSingleModel ms = new MessageSingleModel();
                                ms.FromId = message.Message.FromUser.MemberId;
                                ms.MessageText = RDN.Portable.Util.Strings.StringExt.HtmlDecode(message.Message.MessageText);
                                membersToEmail.Add(message.ToUser.MemberId);
                                if (ms.MessageText.Length > 20)
                                    ms.MessageText = ms.MessageText.Remove(20);
                                var convo = mess.Conversations.Where(x => x.ConversationWithUser == ms.FromId && x.OwnerUserId == message.ToUser.MemberId).FirstOrDefault();
                                if (convo != null)
                                {
                                    convo.Messages.Add(ms);
                                }
                                else
                                {
                                    ConversationModel con = new ConversationModel();
                                    con.ConversationWithUser = ms.FromId;
                                    con.FromName = message.Message.FromUser.DerbyName;
                                    con.OwnerUserId = message.ToUser.MemberId;
                                    con.Messages.Add(ms);
                                    mess.Conversations.Add(con);
                                }
                            }
                            catch (Exception exception)
                            {
                                ErrorDatabaseManager.AddException(exception, exception.GetType());
                            }
                        }
                        dc.SaveChanges();
                        membersToEmail = membersToEmail.Distinct().ToList();

                        var members = dc.Members.Where(x => x.AspNetUserId != null && x.AspNetUserId != new Guid()).Where(x => membersToEmail.Contains(x.MemberId)).ToList();

                        foreach (var mem in members)
                        {
                            string membersWhoSentThisMemberAMessage = String.Empty;
                            foreach (var conv in mess.Conversations.Where(x => x.OwnerUserId == mem.MemberId))
                            {
                                membersWhoSentThisMemberAMessage += conv.FromName;
                                membersWhoSentThisMemberAMessage += "<br/>";
                            }

                            try
                            {
                                var user = System.Web.Security.Membership.GetUser((object)mem.AspNetUserId);
                                var emailData = new Dictionary<string, string>
                                        {
                                            { "derbyname",mem.DerbyName}, 
                                            { "membersWhoSentMessages", membersWhoSentThisMemberAMessage}, 
                                            { "viewConversationLink",ServerConfig.VIEW_MESSAGES_INBOX_MEMBER + mem.MemberId.ToString().Replace("-","") }
                                        };

                                EmailServer.EmailServer.SendEmail(ServerConfig.DEFAULT_EMAIL, ServerConfig.DEFAULT_EMAIL_FROM_NAME, user.UserName, EmailServer.EmailServer.DEFAULT_SUBJECT + " New Messages", emailData, EmailServer.EmailServerLayoutsEnum.SendLatestConversationsThreadToUser);
                                emailsSent += 1;
                            }
                            catch (Exception exception)
                            {
                                Error.ErrorDatabaseManager.AddException(exception, exception.GetType());
                            }
                        }
                        dc.SaveChanges();
                    }
                }
            }
            catch (Exception exception)
            {
                Error.ErrorDatabaseManager.AddException(exception, exception.GetType());
            }
            return emailsSent;
        }
        public ActionResult GetMessages(string mid, string uid, string t, string p, string c)
        {
            MessageModel mess = new MessageModel();

            try
            {
                var mem = MemberCache.GetMemberDisplay(new Guid(mid));
                if (new Guid(uid) == mem.UserId)
                {
                    mess = Messages.GetMessagesForOwner((GroupOwnerTypeEnum)Enum.Parse(typeof(GroupOwnerTypeEnum), t.ToLower()), mem.MemberId, Convert.ToInt32(p), Convert.ToInt32(c));
                    mess.IsSuccessful = true;
                    return Json(mess, JsonRequestBehavior.AllowGet);
                }
            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType(), additionalInformation: "/type:" + t);
            }
            return Json(new MessageModel() { IsSuccessful = false }, JsonRequestBehavior.AllowGet);
        }
        public ActionResult TextMessageNew(string mid, string uid, string type)
        {
            MessageModel model = new MessageModel();
            try
            {
                var mem = MemberCache.GetMemberDisplay(new Guid(mid));
                if (new Guid(uid) == mem.UserId)
                {
                    model.OwnerType = (GroupOwnerTypeEnum)Enum.Parse(typeof(GroupOwnerTypeEnum), type);
                    model.IdOfEntity = mem.MemberId;
                    model.SendEmailForMessage = true;
                    model.IsCarrierVerified = mem.IsCarrierVerified;

                    if (model.OwnerType == GroupOwnerTypeEnum.member)
                        model.Recipients = Messages.GetConnectedMembersOfMember(mem.MemberId);
                    else
                        model.Recipients = Messages.GetConnectedMembersOfGroup(mem.MemberId);
                }
            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType());
            }
            return Json(model, JsonRequestBehavior.AllowGet);
        }
 public ActionResult MessageNew(string mid, string uid, string type)
 {
     MessageModel model = new MessageModel();
     try
     {
         var mem = MemberCache.GetMemberDisplay(new Guid(mid));
         if (new Guid(uid) == mem.UserId)
         {
             model.OwnerType = (GroupOwnerTypeEnum)Enum.Parse(typeof(GroupOwnerTypeEnum), type);
             model.IdOfEntity = mem.MemberId;
             model.SendEmailForMessage = true;
             if (model.OwnerType == GroupOwnerTypeEnum.member)
                 model.Recipients = Messages.GetConnectedMembersOfMember(mem.MemberId);
             else if (model.OwnerType == GroupOwnerTypeEnum.shop)
             {
                 model.Recipients = Messages.GetConnectedShopRecipient(mem.MemberId);
             }
             else if (model.OwnerType == GroupOwnerTypeEnum.jobboard)
             {
                 model.Recipients = new List<MemberDisplayBasic>();
                 model.Recipients.Add(mem);
             }
             else if (model.OwnerType == GroupOwnerTypeEnum.league)
             {
                 model.Recipients = Messages.GetConnectedLeagueRecipient(mem.MemberId);
             }
             else if (model.OwnerType == GroupOwnerTypeEnum.paywall)
             {
                 model.Recipients = Messages.GetConnectedShopRecipient(mem.MemberId);
             }
             else if (model.OwnerType == GroupOwnerTypeEnum.calevent)
             {
                 model.Recipients = Messages.GetConnectedCalEventRecipient(mem.MemberId);
             }
             else
                 model.Recipients = Messages.GetConnectedMembersOfGroup(mem.MemberId);
         }
     }
     catch (Exception exception)
     {
         ErrorDatabaseManager.AddException(exception, exception.GetType());
     }
     return Json(model, JsonRequestBehavior.AllowGet);
 }