protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            try
            {
                conversation = new ConversationModel();

                //(App.Current as App).SecondPageObject = null;
                progressIndicator = SystemTray.ProgressIndicator;
                progressIndicator = new ProgressIndicator();
                SystemTray.SetProgressIndicator(this, progressIndicator);
                progressIndicator.IsIndeterminate = true;
                progressIndicator.Text = "Pulling Messages...";
                if (SettingsMobile.Instance.User == null)
                {
                    SqlFactory fact = new SqlFactory();
                    SettingsMobile.Instance.User = fact.GetProfile();
                }
                conversation.GroupMessageId = Convert.ToInt64(this.NavigationContext.QueryString["mid"]);
                PullTopic();

                _timer = new DispatcherTimer();
                _timer.Interval = TimeSpan.FromSeconds(20);
                _timer.Tick += (s, eee) => Tick();
                _timer.Start();
            }
            catch (Exception exception)
            {
                ErrorHandler.Save(exception, MobileTypeEnum.WP8);
            }

        }
        void ReturnMessageTopic(ConversationModel model)
        {
            try
            {
                if (conversation.Messages.Count < model.Messages.Count)
                {
                    Dispatcher.BeginInvoke(delegate
                    {
                        try
                        {
                            topicName.Title = conversation.Title;
                            MessageList.ItemsSource = conversation.Messages;
                            RecipientsList.ItemsSource = conversation.Recipients;
                            MessageList.ScrollTo(conversation.Messages.LastOrDefault());
                        }
                        catch (Exception exception)
                        {
                            ErrorHandler.Save(exception, MobileTypeEnum.WP8);
                        }
                    });
                }
                conversation = model;

                Dispatcher.BeginInvoke(delegate
                  {
                      progressIndicator.IsVisible = false;
                  });
            }
            catch (Exception exception)
            {
                ErrorHandler.Save(exception, MobileTypeEnum.WP8);
            }
        }
 public static ConversationModel ReplyToMessage(ConversationModel message)
 {
     var response = Network.SendPackage(Network.ConvertObjectToStream(message), MobileConfig.MEMBER_MESSAGES_REPLY_URL);
     var stream = response.GetResponseStream();
     StreamReader read = new StreamReader(stream);
     string json = read.ReadToEnd();
     return Json.DeserializeObject<ConversationModel>(json);
 }
 public static ConversationModel SendNewMessage(ConversationModel message)
 {
     HttpWebResponse response = null;
     if (message.MessageTypeEnum == MessageTypeEnum.Regular)
         response = Network.SendPackage(Network.ConvertObjectToStream(message), MobileConfig.MEMBER_MESSAGES_NEW_URL);
     else if (message.MessageTypeEnum == MessageTypeEnum.Text)
         response = Network.SendPackage(Network.ConvertObjectToStream(message), MobileConfig.MEMBER_MESSAGES_NEW_TEXT_URL);
     var stream = response.GetResponseStream();
     StreamReader read = new StreamReader(stream);
     string json = read.ReadToEnd();
     return Json.DeserializeObject<ConversationModel>(json);
 }
        private void SendPost_Click(object sender, EventArgs e)
        {
            progressIndicator.Text = "Sending Message...";
            progressIndicator.IsVisible = true;
            try
            {
                ConversationModel con = new ConversationModel();
                if (Regular.IsChecked.GetValueOrDefault())
                    con.MessageTypeEnum = MessageTypeEnum.Regular;
                else if (Text.IsChecked.GetValueOrDefault())
                    con.MessageTypeEnum = MessageTypeEnum.Text;
                con.MemberId = SettingsMobile.Instance.User.MemberId;
                con.Message = MessageInput.Text;
                con.Title = Title.Text;
                con.UserId = SettingsMobile.Instance.User.LoginId;
                con.FromId = SettingsMobile.Instance.User.MemberId;
                foreach (var id in Names)
                {
                    long idOfGroup = 0;
                    Guid tempId = new Guid();
                    if (Int64.TryParse(id.Id, out idOfGroup))
                    {
                        con.GroupIds.Add(idOfGroup);
                    }
                    else if (Guid.TryParse(id.Id, out tempId))
                    {
                        con.Recipients.Add(new MemberDisplayMessage() { MemberId = tempId });
                    }
                }
                con.SendEmailForMessage = SendEmail.IsChecked.GetValueOrDefault();

                Task.Run(new Action(() =>
                {
                    try
                    {
                        var m = MessagesMobile.SendNewMessage(con);

                        (App.Current as App).SecondPageObject = conversationModel;
                        Dispatcher.BeginInvoke(delegate
                                                {
                                                    progressIndicator.IsVisible = false;
                                                    ToastPrompt t = new ToastPrompt
                                                    {
                                                        Title = "Message Sent",
                                                        TextOrientation = System.Windows.Controls.Orientation.Vertical

                                                    };
                                                    t.Show();
                                                    NavigationService.Navigate(new Uri("/View/MyLeague/MyLeague.xaml", UriKind.RelativeOrAbsolute));
                                                });
                    }
                    catch (Exception exception)
                    {
                        ErrorHandler.Save(exception, MobileTypeEnum.WP8);
                    }
                }));

            }
            catch (Exception exception)
            {
                ErrorHandler.Save(exception, MobileTypeEnum.WP8);
            }
        }
Пример #6
0
        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;
        }
Пример #7
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();
        }
Пример #8
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;
        }
Пример #9
0
        public static bool CreateNewTextMessageForGroup(ConversationModel con)
        {
            try
            {
                string conversationLink = ServerConfig.VIEW_MESSAGE_CONVERSATION;
                var member = MemberCache.GetMemberDisplay(con.MemberId);
                string ownerName = String.Empty;
                ownerName = member.DerbyName;
                var leagueMembers = MemberCache.GetCurrentLeagueMembers(con.MemberId);
                var tempGroups = MemberCache.GetLeagueGroupsOfMember();
                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
                    {
                        var tempMem = leagueMembers.Where(x => x.MemberId == mem.MemberId).FirstOrDefault();
                        if (tempMem != null)
                        {
                            var emailData = new Dictionary<string, string>
                                        {
                                                                           { "name",tempMem.DerbyName},
                                                                           { "fromName",ownerName},
                                            { "body",ownerName+":"+ con.Message},
                                            { "setupLink",ServerConfig.MEMBER_SETTINGS_URL},

                                        };
                            if (tempMem.IsCarrierVerified)
                            {
                                if (!String.IsNullOrEmpty(tempMem.PhoneNumber))
                                    EmailServer.EmailServer.SendEmail(ServerConfig.DEFAULT_EMAIL_MESSAGES, ServerConfig.DEFAULT_EMAIL_FROM_NAME, ServerConfig.TEXT_MESSAGE_EMAIL, tempMem.PhoneNumber, emailData, EmailServer.EmailServerLayoutsEnum.TextMessage);
                            }
                            else
                            {
                                if (!String.IsNullOrEmpty(tempMem.UserName))
                                    EmailServer.EmailServer.SendEmail(ServerConfig.DEFAULT_EMAIL_MESSAGES, ServerConfig.DEFAULT_EMAIL_FROM_NAME, tempMem.UserName, "Text Message From " + ownerName, emailData, EmailServer.EmailServerLayoutsEnum.TextMessageNotVerified);
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        ErrorDatabaseManager.AddException(exception, exception.GetType());
                    }
                }

                return true;
            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType());
            }
            return false;
        }
Пример #10
0
        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;
        }