예제 #1
0
        public string ConstructDefaultMessage(
            WatchedCategory category,
            CategoryWatcherChannel categoryChannel,
            IReadOnlyCollection <CategoryItem> items,
            bool isNew,
            bool describeEmptySet)
        {
            var destination  = categoryChannel.Channel.Name;
            var pluralString = this.responder.GetMessagePart(
                $"catwatcher.item.{category.Keyword}.plural",
                destination);

            if (pluralString == null)
            {
                pluralString = this.responder.GetMessagePart(
                    $"catwatcher.item.default.plural",
                    destination);
            }

            if (items.Any())
            {
                var textItems = new List <string>();

                foreach (var item in items)
                {
                    // Display an http URL to the page, if desired
                    var urlData = string.Empty;
                    if (categoryChannel.ShowLink)
                    {
                        var pageUrl = this.linkerService.ConvertWikilinkToUrl(destination, item.Title);
                        urlData = " " + this.urlShorteningService.Shorten(pageUrl);
                    }

                    // show the waiting time for the request
                    var waitTimeData = string.Empty;
                    if (categoryChannel.ShowWaitTime)
                    {
                        var waitingTime = DateTime.Now - item.InsertTime;

                        if (waitingTime >= new TimeSpan(0, 0, 0, categoryChannel.MinWaitTime))
                        {
                            var waitTimeFormat = string.Format(
                                " (waiting {{0:{0}hh\\:mm}})",
                                waitingTime.TotalDays > 1 ? "d\\d\\ " : string.Empty);

                            waitTimeData = string.Format(waitTimeFormat, waitingTime);
                        }
                    }

                    textItems.Add(string.Format("[[{0}]]{1}{2}", item.Title, urlData, waitTimeData));
                }

                if (items.Count == 1)
                {
                    pluralString = this.responder.GetMessagePart(
                        $"catwatcher.item.{category.Keyword}.singular",
                        destination);

                    if (pluralString == null)
                    {
                        pluralString = this.responder.GetMessagePart(
                            $"catwatcher.item.default.singular",
                            destination);
                    }
                }

                object[] messageParams =
                {
                    items.Count,
                    pluralString,
                    string.Join(" , ", textItems)
                };

                var keySuffix = "hasitems";
                if (isNew)
                {
                    keySuffix = "newitems";
                }

                var message = this.responder.GetMessagePart(
                    $"catwatcher.item.{category.Keyword}.{keySuffix}",
                    destination, messageParams);

                if (message == null)
                {
                    message = this.responder.GetMessagePart(
                        $"catwatcher.item.default.{keySuffix}",
                        destination, messageParams);
                }

                return(message);
            }

            if (!describeEmptySet)
            {
                return(null);
            }

            var noitems = this.responder.GetMessagePart(
                $"catwatcher.item.{category.Keyword}.noitems",
                destination,
                pluralString
                );

            if (noitems == null)
            {
                noitems = this.responder.GetMessagePart(
                    $"catwatcher.item.default.noitems",
                    destination, pluralString);
            }

            return(noitems);
        }
        public void ForceUpdate(string key, Channel destination)
        {
            this.Logger.DebugFormat("Force-update was triggered for {0} in {1}", key, destination.Name);

            // Locks!
            this.timerSemaphore.WaitOne();

            try
            {
                var watcher = this.helperService.WatchedCategories.FirstOrDefault(x => x.Keyword == key);
                if (watcher == null)
                {
                    throw new ArgumentOutOfRangeException("key");
                }

                this.Logger.DebugFormat("Found watcher {0} for {1}", key, watcher.Category);

                var channel = watcher.Channels.FirstOrDefault(x => x.Channel == destination);
                if (channel == null)
                {
                    this.Logger.DebugFormat("Faking channelconfig for {0}", destination);

                    channel = new CategoryWatcherChannel
                    {
                        AlertForAdditions = false,
                        AlertForRemovals  = false,
                        Channel           = destination,
                        MinWaitTime       = 3600,
                        ShowWaitTime      = true,
                        ShowLink          = true,
                        SleepTime         = 10000,
                        Watcher           = watcher
                    };
                }

                this.helperService.UpdateCategoryItems(watcher);

                var message = this.helperService.ConstructDefaultMessage(
                    watcher,
                    channel,
                    watcher.CategoryItems.ToList(),
                    false,
                    true);

                if (message != null)
                {
                    this.ircClient.SendMessage(destination.Name, message);
                }
            }
            catch (WebException ex)
            {
                this.ircClient.SendMessage(destination.Name, "Could not retrieve category items due to Wikimedia API error");
                this.Logger.Warn("Error during API fetch", ex);
            }
            catch (Exception ex)
            {
                this.Logger.ErrorFormat(ex, "Error encountered updating catwatcher for {0}", key);
                throw;
            }
            finally
            {
                this.timerSemaphore.Release();
            }
        }