Ejemplo n.º 1
0
        public static bool CreateNewMessageForGroup(ConversationModel con)
        {
            try
            {
                string conversationLink = ServerConfig.VIEW_MESSAGE_CONVERSATION;
                var dc = new ManagementContext();
                DataModels.Messages.Message m = new DataModels.Messages.Message();
                m.FromUser = dc.Members.Where(x => x.MemberId == con.MemberId).FirstOrDefault();
                m.MessageText = con.Message;
                dc.Message.Add(m);
                dc.SaveChanges();

                string ownerName = String.Empty;
                string messageId = String.Empty;

                if (m.FromUser != null)
                    ownerName = m.FromUser.DerbyName;

                DataModels.Messages.GroupMessage group = new DataModels.Messages.GroupMessage();
                group.Messages.Add(m);
                group.TitleOfMessage = con.Title;
                group.GroupOwnerId = con.FromId;
                group.LastModified = DateTime.UtcNow;
                var fed = dc.Federations.Where(x => x.FederationId == con.FromId).FirstOrDefault();
                if (fed != null)
                    ownerName = fed.Name;
                else
                {
                    var leag = dc.Leagues.Where(x => x.LeagueId == con.FromId).FirstOrDefault();
                    if (leag != null)
                        ownerName = leag.Name;
                }
                dc.GroupMessages.Add(group);
                dc.SaveChanges();
                messageId = group.GroupId.ToString();
                var tempGroups = MemberCache.GetLeagueGroupsOfMember(con.FromId);
                //add all members of the groups
                foreach (var g in con.GroupIds)
                {
                    var tempG = tempGroups.Where(x => x.Id == g).FirstOrDefault();
                    if (tempG != null)
                    {
                        foreach (var mTemp in tempG.GroupMembers)
                        {
                            if (con.Recipients.Where(x => x.MemberId == mTemp.MemberId).FirstOrDefault() == null)
                                con.Recipients.Add(new MemberDisplayMessage() { MemberId = mTemp.MemberId });
                        }
                    }
                }
                foreach (var mem in con.Recipients)
                {
                    try
                    {
                        DataModels.Messages.MessageInbox inbox = new DataModels.Messages.MessageInbox();
                        inbox.Message = m;
                        inbox.ToUser = dc.Members.Where(x => x.MemberId == mem.MemberId).FirstOrDefault();
                        inbox.MessageReadDateTime = DateTime.UtcNow;
                        inbox.NotifiedEmailDateTime = DateTime.UtcNow;
                        if (inbox.ToUser.MemberId == m.FromUser.MemberId)
                            inbox.IsRead = true;
                        else
                            MemberCache.AddMessageCountToCache(+1, mem.MemberId);

                        DataModels.Messages.MessageRecipient recipient = new DataModels.Messages.MessageRecipient();
                        recipient.Group = group;
                        recipient.Recipient = inbox.ToUser;
                        group.Recipients.Add(recipient);

                        if (con.SendEmailForMessage && inbox.ToUser.AspNetUserId != new Guid())
                        {

                            var emailData = new Dictionary<string, string>
                                        {
                                            { "derbyname", inbox.ToUser.DerbyName }, 
                                            { "FromUserName", ownerName }, 
                                            { "messageBody", HtmlSanitize.ReplaceCarriageReturnsWithBreaks( m.MessageText )},
                                            { "viewConversationLink", conversationLink +  messageId}
                                        };

                            var user = System.Web.Security.Membership.GetUser((object)inbox.ToUser.AspNetUserId);
                            if (user != null)
                            {
                                inbox.UserNotifiedViaEmail = true;
                                EmailServer.EmailServer.SendEmail(ServerConfig.DEFAULT_EMAIL_MESSAGES, ServerConfig.DEFAULT_EMAIL_FROM_NAME, user.UserName, EmailServer.EmailServer.DEFAULT_SUBJECT + " New Message From " + ownerName, emailData, EmailServer.EmailServerLayoutsEnum.SendMessageToUserFromOtherUser);
                            }

                        }
                        dc.MessageInbox.Add(inbox);
                    }
                    catch (Exception exception)
                    {
                        ErrorDatabaseManager.AddException(exception, exception.GetType());
                    }
                }

                int c = dc.SaveChanges();

                MobileNotificationFactory fact = new MobileNotificationFactory();
                fact.Initialize("New Message", m.MessageText, Mobile.Enums.NotificationTypeEnum.Message)
                    .AddId(group.GroupId)
                    .AddMembers(con.Recipients.Select(x => x.MemberId).ToList())
                    .SendNotifications();

                return c > 0;
            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType());
            }
            return false;
        }
Ejemplo n.º 2
0
        public static void AddNewMessageToGroup(long groupId, Guid ownerMemberId, string message)
        {
            try
            {
                var dc = new ManagementContext();
                DataModels.Messages.Message m = new DataModels.Messages.Message();
                m.FromUser = dc.Members.Where(x => x.MemberId == ownerMemberId).FirstOrDefault();
                m.MessageText = message;
                dc.Message.Add(m);

                var grp = dc.GroupMessages.Where(x => x.GroupId == groupId).FirstOrDefault();
                grp.LastModified = DateTime.UtcNow;
                grp.Messages.Add(m);
                dc.SaveChanges();

                var recips = grp.Recipients.Where(x => x.IsRemovedFromGroup == false);
                foreach (var mem in recips)
                {
                    DataModels.Messages.MessageInbox inbox = new DataModels.Messages.MessageInbox();
                    inbox.Message = m;
                    inbox.ToUser = dc.Members.Where(x => x.MemberId == mem.Recipient.MemberId).FirstOrDefault();
                    if (mem.Recipient.MemberId == ownerMemberId)
                        inbox.IsRead = true;
                    else
                        MemberCache.AddMessageCountToCache(+1, mem.Recipient.MemberId);
                    inbox.MessageReadDateTime = DateTime.UtcNow;
                    inbox.NotifiedEmailDateTime = DateTime.UtcNow;
                    dc.MessageInbox.Add(inbox);

                }
                dc.SaveChanges();
            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType());
            }
        }