private void CheckNotificationsForUser(UserToken user)
        {
            var service = new LightTwitterClient(SensitiveData.ConsumerToken, SensitiveData.ConsumerSecret, user.Key, user.Secret);

            if (user.Preferences.MentionsPreferences != NotificationType.None)
            {
                Interlocked.Increment(ref requestsPending);
                service.ListMentions(10, (statuses, response) =>
                {
                    ReceiveTweetObjects(TweetType.Mention, user, statuses, response);
                    if (Interlocked.Decrement(ref requestsPending) == 0)
                        notificationsWaitHandle.Set();
                });
            }

            if (user.Preferences.MessagesPreferences != NotificationType.None)
            {
                Interlocked.Increment(ref requestsPending);
                service.ListMessages(10, (statuses, response) =>
                {
                    ReceiveTweetObjects(TweetType.Message, user, statuses, response);
                    if (Interlocked.Decrement(ref requestsPending) == 0)
                        notificationsWaitHandle.Set();
                });
            }
        }
Exemple #2
0
        private void CheckNotificationsForUser(UserToken user)
        {
            var service = new LightTwitterClient(SensitiveData.ConsumerToken, SensitiveData.ConsumerSecret, user.Key, user.Secret);
            if (user.Preferences.MentionsPreferences == NotificationType.TileAndToast
                || user.Preferences.MentionsPreferences == NotificationType.Tile)
            {
                SignalThreadStart();

                service.ListMentions(10, (statuses, response) =>
                {
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    GC.Collect();
                    WriteMemUsage("Received mentions");
                    if (statuses == null || response.StatusCode != System.Net.HttpStatusCode.OK)
                    {
                        WriteMemUsage("Mentions: exit with error " + response.StatusDescription);
                        SignalThreadEnd();
                        return;
                    }

                    var CheckDate = DateSync.GetLastCheckDate();
                    var ToastDate = DateSync.GetLastToastNotificationDate();

                    if (CheckDate > ToastDate)
                        ToastDate = CheckDate;

                    var newStatuses = statuses.Where(item =>
                    {
                        return TwitterObjectIsOlderThan(item, CheckDate);
                    });

                    var newToastStatuses = statuses.Where(item =>
                    {
                        return TwitterObjectIsOlderThan(item, ToastDate);
                    });

                    if (newStatuses.Count() > 0)
                    {
                        newMentions = true;

                        if (newStatuses.Count() == 1)
                        {
                            string userstring = "";
                            if (newStatuses.FirstOrDefault().TryGetProperty("user", out userstring))
                                new TwitterObject(userstring).TryGetProperty("screen_name", out from);
                            else
                                from = "no_name";
                        }

                        if (!usersWithNotifications.Contains(user.ScreenName))
                            usersWithNotifications.Add(user.ScreenName);

                        notifications += newStatuses.Count();

                        if (user.Preferences.MentionsPreferences == NotificationType.TileAndToast && newToastStatuses.Count() > 1)
                        {
                            string toastFrom, userstring = "";
                            if (newToastStatuses.FirstOrDefault().TryGetProperty("user", out userstring))
                                new TwitterObject(userstring).TryGetProperty("screen_name", out toastFrom);
                            else
                                toastFrom = "no_name";

                            CreateToast("mention", newToastStatuses.Count(), toastFrom, user.ScreenName);
                            DateSync.WriteLastToastNotificationDate(DateTime.Now.ToUniversalTime());
                        }

                        BuildTile();

                    }

                    WriteMemUsage("Mentions: Exit with " + newStatuses.Count().ToString() + " new statuses.");
                    SignalThreadEnd();
                });
            }
            if (user.Preferences.MessagesPreferences == NotificationType.TileAndToast
                || user.Preferences.MessagesPreferences == NotificationType.Tile)
            {
                SignalThreadStart();

                service.ListMessages(10, (statuses, response) =>
                {
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    GC.Collect();
                    WriteMemUsage("Received messages");

                    if (statuses == null || response.StatusCode != System.Net.HttpStatusCode.OK)
                    {
                        WriteMemUsage("Messages: exit with error " + response.StatusDescription);
                        SignalThreadEnd();
                        return;
                    }

                    var CheckDate = DateSync.GetLastCheckDate();
                    var ToastDate = DateSync.GetLastToastNotificationDate();

                    var newStatuses = statuses.Where(item =>
                    {
                        return TwitterObjectIsOlderThan(item, CheckDate);
                    });

                    var newToastStatuses = statuses.Where(item =>
                    {
                        return TwitterObjectIsOlderThan(item, ToastDate);
                    });

                    if (newStatuses.Count() > 0)
                    {
                        newMessages = true;

                        if (newStatuses.Count() == 1)
                            newStatuses.FirstOrDefault().TryGetProperty("sender_screen_name", out from);

                        if (!usersWithNotifications.Contains(user.ScreenName))
                            usersWithNotifications.Add(user.ScreenName);

                        notifications += newStatuses.Count();

                        if (user.Preferences.MessagesPreferences == NotificationType.TileAndToast && newToastStatuses.Count() > 0)
                        {
                            string toastFrom;
                            newStatuses.FirstOrDefault().TryGetProperty("sender_screen_name", out toastFrom);

                            CreateToast("message", newToastStatuses.Count(), toastFrom, user.ScreenName);
                            DateSync.WriteLastToastNotificationDate(DateTime.Now.ToUniversalTime());
                        }

                        BuildTile();

                    }

                    WriteMemUsage("Messages: Exit with " + newStatuses.Count().ToString() + " new statuses.");
                    SignalThreadEnd();
                });
            }
        }