Esempio n. 1
0
        /// <summary>
        /// <para>Preconditions: The associated <see cref="DiscordInterfacer"/> is in a Connected state</para>
        /// </summary>
        public async Task Execute(TagCommandParameters Command)
        {
            Log.Application_.LogVerbose(
                "Processing Tag command for {2} '{0}', Tagged to Taglist '{1}'",
                Command.ItemID, Command.TaglistName,
                Command.isItemAlbum ? "Album" : "Image"
                );
            Task <GalleryItem> TaggedItemTask;

            if (!Command.isItemAlbum)
            {
                TaggedItemTask = Imgur.ReadGalleryImage(Command.ItemID);
            }
            else
            {
                TaggedItemTask = Imgur.ReadGalleryAlbum(Command.ItemID);
            }
            Task <Taglist> LoadTaglistTask = RepositoryTaglists.Load(Command.TaglistName);
            Taglist        SpecifiedTaglist;

            try{
                SpecifiedTaglist = await LoadTaglistTask;
                //Any exceptions from TaggedItemTask will go unobserved if an exception is thrown in this try block
            }catch (EntityNotFoundException) {
                Log.Application_.LogWarning(
                    "The Taglist named '{0}' does not exist, which was specified by a Tag command on the Imgur Gallery Item with ID '{1}' (Comment ID {2:D})",
                    Command.TaglistName, Command.ItemID, Command.HostCommentID
                    );
                return;
            }catch (DataAccessException Error) {
                Log.Application_.LogError(
                    "Error loading Taglist '{1}' while processing Tag command on the Imgur Gallery Item with ID '{0}'; processing of command aborted: {2}",
                    Command.ItemID, Command.TaglistName, Error.Message
                    );
                return;
            }
            GalleryItem TaggedItem;

            try{
                TaggedItem = await TaggedItemTask;
                //Any exceptions from MentionUsersTask will go unobserved if an exception is thrown in this try block
            }catch (ImgurException Error) {
                Log.Application_.LogError("Error acquiring details for Tagged Imgur Gallery Item with ID '{0}'; unable to process Tag command for that item: {1}", Command.ItemID, Error.Message);
                return;
            }
            await Task.WhenAll(
                //Long-running operation, due to delay needed between Imgur Comments
                MentionInterestedUsers(Command, SpecifiedTaglist),
                ArchiveTaggedItem(Command, TaggedItem, SpecifiedTaglist)
                );

            Log.Application_.LogVerbose(
                "Completed processing of Tag command for {2} '{0}' to Taglist '{1}'",
                Command.ItemID, Command.TaglistName,
                Command.isItemAlbum ? "Album" : "Image"
                );
            await Imgur.LogRemainingBandwidth(TraceEventType.Verbose);
        }
Esempio n. 2
0
        //Some exceptions may go unobserved in this method, as not all created Tasks are awaited if problems occur
        /// <summary>
        /// <para>Preconditions: The associated <see cref="DiscordInterfacer"/> of the indirectly-associated <see cref="ProcessTagCommandActivity"/> is in a Connected state</para>
        /// </summary>
        public async Task Execute()
        {
            Log.Application_.LogVerbose("Starting processing of latest Comments");
            Settings        CurrentSettings;
            Task <Settings> CurrentSettingsTask = RepositorySettings.LoadSettings();

            //Robustness — clear the cache in case it hasn't been cleared for some reason
            RepositoryTaglists.ClearCache();

            /*
             * Initialize the Taglist Repository cache, so that subsequent calls to Load
             * by the ProcessCommentActivity SubActivity tasks
             * will efficiently retrieve the cached result
             */
            //TODO Implement proper lazy-loading in CacheingTaglistRepository, so that results are cached upon calls to Load and not just LoadAll
            Task InitializeCacheTask = RepositoryTaglists.LoadAll();

            try{
                CurrentSettings = await CurrentSettingsTask;
            }catch (DataAccessException Error) {
                Log.Application_.LogError("Error loading Settings while pulling latest Comments from Imgur; pull aborted: " + Error.Message);
                return;
            }
            Task <IDictionary <string, IList <IComment> > > NewCommentsTask = Imgur.ReadCommentsSince(
                CurrentSettings.CommentsProcessedUpToInclusive,
                CurrentSettings.CommenterUsernames,
                CurrentSettings.RequestThreshholdPullCommentsPerUser
                );
            IDictionary <string, IList <IComment> > NewComments;

            try{
                NewComments = await NewCommentsTask;
            }catch (ImgurException Error) {
                Log.Application_.LogError("Error pulling latest Comments from Imgur: " + Error.Message);
                return;
            }
            try{
                await InitializeCacheTask;
            }catch (DataAccessException Error) {
                Log.Application_.LogError("Error loading Taglists while pulling Imgur Comments; Comment processing aborted: " + Error.Message);
                return;
            }
            //Execute the sub-activity for each new Comment, keeping track of the latest date–time from all the Comments
            Log.Application_.LogVerbose("Processing latest Comments from {0} total Commenters", NewComments.Count);
            bool           AnyCommentsProcessed = false;
            DateTimeOffset LatestCommentAt      = DateTimeOffset.MinValue;

            foreach (KeyValuePair <string, IList <IComment> > _NewUserComments in NewComments)
            {
                IList <IComment> NewUserComments = _NewUserComments.Value;
                //Process Comments from a particular user, oldest Comment first
                Log.Application_.LogVerbose("Processing latest Comments from Commenter '{0}', total — {1}", _NewUserComments.Key, NewUserComments.Count);
                foreach (IComment NewUserComment in NewUserComments.Reverse())
                {
                    bool CommentsProcessed = await SubActivity.ExecuteIfNew(NewUserComment);

                    if (CommentsProcessed)
                    {
                        AnyCommentsProcessed = true;
                    }
                    if (NewUserComment.DateTime > LatestCommentAt)
                    {
                        LatestCommentAt = NewUserComment.DateTime;
                    }
                }
            }
            RepositoryTaglists.ClearCache();
            //Update and save the latest Comment date–time if it has progressed
            Log.Application_.LogVerbose("Latest date–time of all Comments read — {0:u}; current saved value is {1:u}", LatestCommentAt, CurrentSettings.CommentsProcessedUpToInclusive);
            if (LatestCommentAt > CurrentSettings.CommentsProcessedUpToInclusive)
            {
                Log.Application_.LogVerbose("Updating and saving latest processed Comment date–time");
                CurrentSettings.CommentsProcessedUpToInclusive = LatestCommentAt;
                try{
                    await RepositorySettings.SaveWritableSettings(CurrentSettings);
                }catch (DataAccessException Error) {
                    Log.Application_.LogError(
                        "Unable to save updated Settings with new 'Last Processed Comment date–time' of {0:u}; it would be advisable, though not essential, to update the settings file manually with the updated value. Details: {1}",
                        CurrentSettings.CommentsProcessedUpToInclusive, Error.Message
                        );
                }
            }
            if (AnyCommentsProcessed)
            {
                await Imgur.LogRemainingBandwidth(TraceEventType.Information);
            }
            else
            {
                await Imgur.LogRemainingBandwidth(TraceEventType.Verbose);
            }
        }