예제 #1
0
        /// <summary>
        /// Method called when someone executes a custom command.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">An <see cref="OnChatCommandReceivedArgs"/> object.</param>
        private async void CustomCommand_OnCustomCommand(object sender, OnChatCommandReceivedArgs e)
        {
            //TODO: Refactor Mongo
            CommandDocument commandDocument = await GetCustomCommandAsync(e.Command.ChatMessage.RoomId, e.Command.CommandText).ConfigureAwait(false);

            if (!(commandDocument is null))
            {
                if (!await Permission.Can(e.Command, false, 0).ConfigureAwait(false))
                {
                    return;
                }

                TwitchLibClient.Instance.SendMessage(e.Command.ChatMessage.RoomId, commandDocument.Response);
            }
        }
예제 #2
0
        public bool CreateImpression(AuthUser operatorUser, User targetUser, string text)
        {
            if (operatorUser == null || Permission.Can(operatorUser, SpacePermissionSet.Action.UseImpression) == false)
            {
                return(false);
            }

            if (targetUser == null || Permission.Can(targetUser, SpacePermissionSet.Action.UseImpression) == false)
            {
                return(false);
            }

            if (text == null)
            {
                ThrowError(new ImpressionTextEmptyError("text"));
                return(false);
            }

            text = text.Trim();

            if (ValidateText(text) == false)
            {
                return(false);
            }

            bool succeed = ImpressionDao.Instance.CreateImpression(operatorUser.UserID, targetUser.UserID, text, AllSettings.Current.ImpressionSettings.TimeLimit);

            if (succeed)
            {
                Notify notify = new ImpressionNotify(operatorUser.UserID, targetUser.UserID);
                NotifyBO.Instance.AddNotify(operatorUser, notify);
                RemoveCachedTargetUserImpressionRecordsTotalCount(targetUser.UserID);
            }

            return(succeed);
        }
예제 #3
0
        private async void CustomCommand_OnRemoveCommand(object sender, OnChatCommandReceivedArgs e)
        {
            if (!await Permission.Can(e.Command, false, 2).ConfigureAwait(false))
            {
                return;
            }

            Match commandMatch = this._commandRemoveRegex.Match(e.Command.ArgumentsAsString);

            if (commandMatch.Success)
            {
                string command = commandMatch.Groups["command"].Value.ToLowerInvariant();

                if (await PluginManager.DoesCustomChatCommandExistAsync(e.Command.ChatMessage.RoomId, command).ConfigureAwait(false))
                {
                    IMongoCollection <CommandDocument> collection = DatabaseClient.Instance.MongoDatabase.GetCollection <CommandDocument>(CommandDocument.CollectionName);

                    FilterDefinition <CommandDocument> filter = Builders <CommandDocument> .Filter.Where(c => c.ChannelId == e.Command.ChatMessage.RoomId && !c.IsWhisperCommand && c.Command == command);

                    _ = await collection.DeleteOneAsync(filter).ConfigureAwait(false);

                    //TODO: Unregister custom permission

                    // Command removed.
                    TwitchLibClient.Instance.SendMessage(e.Command.ChatMessage.Channel,
                                                         await I18n.Instance.GetAndFormatWithAsync("CustomCommand", "RemoveSuccess", e.Command.ChatMessage.RoomId,
                                                                                                   new
                    {
                        CommandPrefix = PluginManager.Instance.ChatCommandIdentifier,
                        Command       = command,
                        User          = e.Command.ChatMessage.Username,
                        Sender        = e.Command.ChatMessage.Username,
                        e.Command.ChatMessage.DisplayName
                    },
                                                                                                   "@{DisplayName}, the command {CommandPrefix}{Command} has been removed.").ConfigureAwait(false));
                }
                else
                {
                    // Command doesn't exist.
                    TwitchLibClient.Instance.SendMessage(e.Command.ChatMessage.Channel,
                                                         await I18n.Instance.GetAndFormatWithAsync("CustomCommand", "RemoveNotExists", e.Command.ChatMessage.RoomId,
                                                                                                   new
                    {
                        CommandPrefix = PluginManager.Instance.ChatCommandIdentifier,
                        Command       = command,
                        User          = e.Command.ChatMessage.Username,
                        Sender        = e.Command.ChatMessage.Username,
                        e.Command.ChatMessage.DisplayName
                    },
                                                                                                   "@{DisplayName}, the command {CommandPrefix}{Command} cannot be removed as it doesn't exist.").ConfigureAwait(false));
                }
            }
            else
            {
                // Wrong syntax.
                TwitchLibClient.Instance.SendMessage(e.Command.ChatMessage.Channel,
                                                     await I18n.Instance.GetAndFormatWithAsync("CustomCommand", "RemoveUsage", e.Command.ChatMessage.RoomId,
                                                                                               new
                {
                    CommandPrefix = PluginManager.Instance.ChatCommandIdentifier,
                    User          = e.Command.ChatMessage.Username,
                    Sender        = e.Command.ChatMessage.Username,
                    e.Command.ChatMessage.DisplayName
                },
                                                                                               "@{DisplayName}, Removes a custom command. Usage: {CommandPrefix}command remove {CommandPrefix}[command]").ConfigureAwait(false));
            }
        }
예제 #4
0
        private async void CustomCommand_OnEditCommand(object sender, OnChatCommandReceivedArgs e)
        {
            if (!await Permission.Can(e.Command, false, 2).ConfigureAwait(false))
            {
                return;
            }

            Match commandMatch = this._commandAddEditRegex.Match(e.Command.ArgumentsAsString);

            if (commandMatch.Success)
            {
                string command = commandMatch.Groups["command"].Value.ToLowerInvariant();

                if (await PluginManager.DoesCustomChatCommandExistAsync(e.Command.ChatMessage.RoomId, command).ConfigureAwait(false))
                {
                    UserLevels userLevel = GetUserLevelFromTag(commandMatch.Groups["userlevel"].Value);
                    string     response  = commandMatch.Groups["response"].Value;

                    IMongoCollection <CommandDocument> collection = DatabaseClient.Instance.MongoDatabase.GetCollection <CommandDocument>(CommandDocument.CollectionName);

                    // Update the document.
                    UpdateDefinition <CommandDocument> update = Builders <CommandDocument> .Update.Set(c => c.Response, response);

                    FilterDefinition <CommandDocument> filter = Builders <CommandDocument> .Filter.Where(c => c.ChannelId == e.Command.ChatMessage.RoomId && !c.IsWhisperCommand && c.Command == command);

                    // Update permission if the user specified it.
                    if (!string.IsNullOrEmpty(commandMatch.Groups["userlevel"].Value))
                    {
                        _ = update.Set(c => c.UserLevel, userLevel);
                    }

                    _ = await collection.UpdateOneAsync(filter, update).ConfigureAwait(false);

                    // Command modified.
                    TwitchLibClient.Instance.SendMessage(e.Command.ChatMessage.Channel,
                                                         await I18n.Instance.GetAndFormatWithAsync("CustomCommand", "ModifySuccess", e.Command.ChatMessage.RoomId,
                                                                                                   new
                    {
                        CommandPrefix = PluginManager.Instance.ChatCommandIdentifier,
                        Command       = command,
                        Response      = response,
                        User          = e.Command.ChatMessage.Username,
                        Sender        = e.Command.ChatMessage.Username,
                        e.Command.ChatMessage.DisplayName
                    },
                                                                                                   "@{DisplayName}, the command {CommandPrefix}{Command} has been modified with the response: {Response}").ConfigureAwait(false));
                }
                else
                {
                    // Command does not exist.
                    TwitchLibClient.Instance.SendMessage(e.Command.ChatMessage.Channel,
                                                         await I18n.Instance.GetAndFormatWithAsync("CustomCommand", "ModifyNotExists", e.Command.ChatMessage.RoomId,
                                                                                                   new
                    {
                        CommandPrefix = PluginManager.Instance.ChatCommandIdentifier,
                        Command       = command,
                        User          = e.Command.ChatMessage.Username,
                        Sender        = e.Command.ChatMessage.Username,
                        e.Command.ChatMessage.DisplayName
                    },
                                                                                                   "@{DisplayName}, the command {CommandPrefix}{Command} cannot be modified as it doesn't exist.").ConfigureAwait(false));
                }
            }
            else
            {
                // Wrong syntax.
                TwitchLibClient.Instance.SendMessage(e.Command.ChatMessage.Channel,
                                                     await I18n.Instance.GetAndFormatWithAsync("CustomCommand", "ModifyUsage", e.Command.ChatMessage.RoomId,
                                                                                               new
                {
                    CommandPrefix = PluginManager.Instance.ChatCommandIdentifier,
                    User          = e.Command.ChatMessage.Username,
                    Sender        = e.Command.ChatMessage.Username,
                    e.Command.ChatMessage.DisplayName
                },
                                                                                               "@{DisplayName}, Modifies a command. Usage: {CommandPrefix}command modify {CommandPrefix}[command] [permission (optional)] [response]").ConfigureAwait(false));
            }
        }
예제 #5
0
        private async void CustomCommand_OnAddCommand(object sender, OnChatCommandReceivedArgs e)
        {
            if (!await Permission.Can(e.Command, false, 2).ConfigureAwait(false))
            {
                return;
            }

            Match commandMatch = this._commandAddEditRegex.Match(e.Command.ArgumentsAsString);

            if (commandMatch.Success)
            {
                string command = commandMatch.Groups["command"].Value.ToLowerInvariant();

                if (!await PluginManager.DoesCustomChatCommandExistAsync(e.Command.ChatMessage.RoomId, command).ConfigureAwait(false))
                {
                    UserLevels userLevel = GetUserLevelFromTag(commandMatch.Groups["userlevel"].Value);
                    string     response  = commandMatch.Groups["response"].Value;

                    // TODO: Register the custom role

                    IMongoCollection <CommandDocument> collection = DatabaseClient.Instance.MongoDatabase.GetCollection <CommandDocument>(CommandDocument.CollectionName);

                    CommandDocument commandDocument = new CommandDocument
                    {
                        ChannelId = e.Command.ChatMessage.RoomId,
                        Command   = command,
                        Response  = response,
                        UserLevel = userLevel
                    };

                    await collection.InsertOneAsync(commandDocument).ConfigureAwait(false);

                    // Command added.
                    TwitchLibClient.Instance.SendMessage(e.Command.ChatMessage.Channel,
                                                         await I18n.Instance.GetAndFormatWithAsync("CustomCommand", "AddSuccess", e.Command.ChatMessage.RoomId,
                                                                                                   new
                    {
                        CommandPrefix = PluginManager.Instance.ChatCommandIdentifier,
                        Command       = command,
                        Response      = response,
                        User          = e.Command.ChatMessage.Username,
                        Sender        = e.Command.ChatMessage.Username,
                        e.Command.ChatMessage.DisplayName
                    },
                                                                                                   "@{DisplayName}, the command {CommandPrefix}{Command} has been added with the response: {Response}").ConfigureAwait(false));
                }
                else
                {
                    // Command exists.
                    TwitchLibClient.Instance.SendMessage(e.Command.ChatMessage.Channel,
                                                         await I18n.Instance.GetAndFormatWithAsync("CustomCommand", "AddAlreadyExists", e.Command.ChatMessage.RoomId,
                                                                                                   new
                    {
                        CommandPrefix = PluginManager.Instance.ChatCommandIdentifier,
                        Command       = command,
                        User          = e.Command.ChatMessage.Username,
                        Sender        = e.Command.ChatMessage.Username,
                        e.Command.ChatMessage.DisplayName
                    },
                                                                                                   "@{DisplayName}, the command {CommandPrefix}{Command} cannot be added as it already exists.").ConfigureAwait(false));
                }
            }
            else
            {
                // Wrong syntax.
                TwitchLibClient.Instance.SendMessage(e.Command.ChatMessage.Channel,
                                                     await I18n.Instance.GetAndFormatWithAsync("CustomCommand", "AddUsage", e.Command.ChatMessage.RoomId,
                                                                                               new
                {
                    CommandPrefix = PluginManager.Instance.ChatCommandIdentifier,
                    User          = e.Command.ChatMessage.Username,
                    Sender        = e.Command.ChatMessage.Username,
                    e.Command.ChatMessage.DisplayName
                },
                                                                                               "@{DisplayName}, Adds a custom command. Usage: {CommandPrefix}command add {CommandPrefix}[command] [permission (optional)] [response]").ConfigureAwait(false));
            }
        }
예제 #6
0
 public void ShouldAllowWhenPermissionsMatch()
 {
     Assert.IsTrue(permission.Can("TestPermission"));
 }
예제 #7
0
        /// <summary>
        /// 添加评论 并更新缓存
        /// </summary>
        /// <param name="userID">评论者ID</param>
        /// <param name="targetID">被评论的记录、日志、相册的ID</param>
        /// <param name="commentID">被评论的评论ID</param>
        /// <param name="type">评论类型 记录、日志、相册</param>
        /// <param name="content"></param>
        /// <param name="createIP"></param>
        /// <returns></returns>
        public bool AddComment(AuthUser operatorUser, int targetID, int commentID, CommentType type, string content, string createIP, bool isReply, int replyTargetUserID, out int newCommentId, out string newCommentContent)
        {
            Notify notify;

            newCommentId      = 0;
            newCommentContent = string.Empty;

            if (Permission.Can(operatorUser, SpacePermissionSet.Action.AddComment) == false)
            {
                ThrowError <NoPermissionAddCommentError>(new NoPermissionAddCommentError());
                return(false);
            }

            if (targetID < 0)
            {
                ThrowError(new InvalidParamError("targetID"));
                return(false);
            }

            if (commentID < 0)
            {
                ThrowError(new InvalidParamError("commentID"));
                return(false);
            }

            if (string.IsNullOrEmpty(content))
            {
                ThrowError(new EmptyCommentError("content"));
                return(false);
            }

            if (StringUtil.GetByteCount(content) > Consts.Comment_Length)
            {
                ThrowError(new InvalidCommentLengthError("content", content, Consts.Comment_Length));
                return(false);
            }



            int commentTargetUserID = GetCommentTargetUserID(targetID, type);

            if (FriendBO.Instance.MyInBlacklist(operatorUser.UserID, commentTargetUserID))
            {
                ThrowError(new PrivacyError());
                return(false);
            }

            bool isApproved = true;

            //string reverter, version;

            //content = AllSettings.Current.ContentKeywordSettings.ReplaceKeywords.Replace(content, out version, out reverter);

            CommentPointType commentPointType = CommentPointType.AddApprovedComment;


            KeywordReplaceRegulation keywordReg = AllSettings.Current.ContentKeywordSettings.ReplaceKeywords;

            content = keywordReg.Replace(content);

            string keyword = null;

            if (AllSettings.Current.ContentKeywordSettings.BannedKeywords.IsMatch(content, out keyword))
            {
                Context.ThrowError(new KeywordBannedError("content", keyword));
                return(false);
            }


            if (AllSettings.Current.ContentKeywordSettings.ApprovedKeywords.IsMatch(content))
            {
                isApproved       = false;
                commentPointType = CommentPointType.AddNoApprovedComment;
            }

            if (StringUtil.GetByteCount(content) > Consts.Comment_Length)
            {
                Context.ThrowError(new InvalidCommentLengthError("content", content, Consts.Comment_Length));
                return(false);
            }

            int targetUserID = 0;

            int tempCommentID = 0;

            content           = CommentUbbParser.ParseForSave(content);
            newCommentContent = content;

            bool success = CommentPointAction.Instance.UpdateUserPoint(operatorUser.UserID, commentPointType, delegate(PointActionManager.TryUpdateUserPointState state)
            {
                if (state != PointActionManager.TryUpdateUserPointState.CheckSucceed)
                {
                    return(false);
                }
                else
                {
                    //添加评论并发送动态与通知
                    switch (type)
                    {
                    case CommentType.Board:
                        SimpleUser user = UserBO.Instance.GetSimpleUser(targetID);

                        if (user == null)
                        {
                            ThrowError(new UserNotExistsError("targetID", targetID));
                            return(false);
                        }

                        CommentDao.Instance.AddComment(operatorUser.UserID, targetID, type, isApproved, content, /* reverter, */ createIP, out targetUserID, out tempCommentID);
                        FeedBO.Instance.CreateLeveMessageFeed(operatorUser.UserID, targetID);

                        if (isReply && operatorUser.UserID != replyTargetUserID)
                        {
                            notify        = new BoardCommentNotify(operatorUser.UserID, tempCommentID, isReply, targetID);
                            notify.UserID = replyTargetUserID;
                            NotifyBO.Instance.AddNotify(operatorUser, notify);
                        }
                        else if (targetID != operatorUser.UserID)
                        {
                            notify        = new BoardCommentNotify(operatorUser.UserID, tempCommentID, false, targetID);
                            notify.UserID = targetID;
                            NotifyBO.Instance.AddNotify(operatorUser, notify);
                        }

                        CacheUtil.RemoveBySearch(string.Format(cacheKey_List_Space, targetID, type));
                        break;

                    case CommentType.Blog:
                        BlogArticle article = BlogBO.Instance.GetBlogArticle(targetID);

                        if (article == null)
                        {
                            ThrowError(new NotExistsAlbumError(targetID));
                            return(false);
                        }

                        if (!article.EnableComment)
                        {
                            ThrowError(new PrivacyError());
                            return(false);
                        }
                        else
                        {
                            if (article.UserID != operatorUser.UserID)       //如果不是作者评论自己的 给作者加积分
                            {
                                CommentDao.Instance.AddComment(operatorUser.UserID, targetID, type, isApproved, content, /* reverter, */ createIP, out targetUserID, out tempCommentID);

                                success = BlogPointAction.Instance.UpdateUserPoint(article.UserID, BlogPointType.ArticleWasCommented);

                                if (success == false)
                                {
                                    return(false);
                                }
                            }
                            else
                            {
                                CommentDao.Instance.AddComment(operatorUser.UserID, targetID, type, isApproved, content, /* reverter, */ createIP, out targetUserID, out tempCommentID);
                            }
                        }

                        FeedBO.Instance.CreatBlogCommentFeed(operatorUser.UserID, article.UserID, targetID, article.Subject);

                        if (isReply && operatorUser.UserID != replyTargetUserID)
                        {
                            notify        = new BlogCommentNotify(operatorUser.UserID, article.Subject, targetID, tempCommentID, isReply, article.UserID);
                            notify.UserID = replyTargetUserID;
                            NotifyBO.Instance.AddNotify(operatorUser, notify);
                        }
                        else if (article.UserID != operatorUser.UserID)
                        {
                            notify        = new BlogCommentNotify(operatorUser.UserID, article.Subject, targetID, tempCommentID, false, article.UserID);
                            notify.UserID = article.UserID;
                            NotifyBO.Instance.AddNotify(operatorUser, notify);
                        }
                        break;

                    case CommentType.Doing:
                        Doing doing = DoingBO.Instance.GetDoing(targetID);

                        if (doing == null)
                        {
                            ThrowError(new NotExistsDoingError(targetID));
                            return(false);
                        }

                        CommentDao.Instance.AddComment(operatorUser.UserID, targetID, type, isApproved, content, /* reverter, */ createIP, out targetUserID, out tempCommentID);

                        if (isReply && operatorUser.UserID != replyTargetUserID)
                        {
                            notify        = new DoingPostNotify(operatorUser.UserID, doing.Content, doing.ID, tempCommentID, isReply, doing.UserID);
                            notify.UserID = replyTargetUserID;
                            NotifyBO.Instance.AddNotify(operatorUser, notify);
                        }
                        else if (doing.UserID != operatorUser.UserID)
                        {
                            notify        = new DoingPostNotify(operatorUser.UserID, doing.Content, doing.ID, tempCommentID, false, doing.UserID);
                            notify.UserID = doing.UserID;
                            NotifyBO.Instance.AddNotify(operatorUser, notify);
                        }
                        DoingBO.Instance.ClearCachedEveryoneData();
                        break;

                    case CommentType.Share:
                        Share share = ShareBO.Instance.GetUserShare(targetID);

                        if (share == null)
                        {
                            ThrowError(new NotExistsShareError(targetID));
                            return(false);
                        }

                        if (share.UserID != operatorUser.UserID)       //如果不是分享作者评论自己的分享 给分享作者加积分
                        {
                            CommentDao.Instance.AddComment(operatorUser.UserID, targetID, type, isApproved, content, /* reverter, */ createIP, out targetUserID, out tempCommentID);
                            success = SharePointAction.Instance.UpdateUserPoint(share.UserID, SharePointType.ShareWasCommeted);

                            if (!success)
                            {
                                return(false);
                            }
                        }
                        else
                        {
                            CommentDao.Instance.AddComment(operatorUser.UserID, targetID, type, isApproved, content, /* reverter, */ createIP, out targetUserID, out tempCommentID);
                        }

                        ShareBO.Instance.ClearCachedEveryoneData();

                        FeedBO.Instance.CreatShareCommentFeed(operatorUser.UserID, share.UserID, targetID, share.Type);

                        if (isReply && operatorUser.UserID != replyTargetUserID)
                        {
                            notify        = new SharePostNotify(operatorUser.UserID, targetID, tempCommentID, isReply, share.UserID);
                            notify.UserID = replyTargetUserID;
                            NotifyBO.Instance.AddNotify(operatorUser, notify);
                        }
                        else if (share.UserID != operatorUser.UserID)
                        {
                            notify        = new SharePostNotify(operatorUser.UserID, targetID, tempCommentID, false, share.UserID);
                            notify.UserID = share.UserID;
                            NotifyBO.Instance.AddNotify(operatorUser, notify);
                        }

                        break;

                    case CommentType.Photo:
                        Photo photo = AlbumBO.Instance.GetPhoto(targetID);

                        if (photo == null)
                        {
                            ThrowError(new NotExistsPhotoError(targetID));
                            return(false);
                        }

                        CommentDao.Instance.AddComment(operatorUser.UserID, targetID, type, isApproved, content, /* reverter, */ createIP, out targetUserID, out tempCommentID);
                        FeedBO.Instance.CreatPictureCommentFeed(operatorUser.UserID, targetUserID, targetID, photo.ThumbSrc, photo.Name, content);

                        if (isReply && operatorUser.UserID != replyTargetUserID)
                        {
                            notify        = new PhotoCommentNotify(operatorUser.UserID, targetID, photo.Name, tempCommentID, isReply, photo.UserID);
                            notify.UserID = replyTargetUserID;

                            NotifyBO.Instance.AddNotify(operatorUser, notify);
                        }
                        else if (photo.UserID != operatorUser.UserID)
                        {
                            notify        = new PhotoCommentNotify(operatorUser.UserID, targetID, photo.Name, tempCommentID, false, photo.UserID);
                            notify.UserID = photo.UserID;
                            NotifyBO.Instance.AddNotify(operatorUser, notify);
                        }

                        break;
                    }
                    return(true);
                }
            });

            if (success == false)
            {
                return(false);
            }

            newCommentId = tempCommentID;

            //更新热度
            if (targetUserID != 0)
            {
                HotType hotType = HotType.Comment;

                if (type == CommentType.Board)
                {
                    hotType = HotType.Messag;
                }

                FriendBO.Instance.UpdateFriendHot(operatorUser.UserID, hotType, targetUserID);
            }

            if (isApproved == false)
            {
                Context.ThrowError(new UnapprovedCommentError());
                return(false);
            }

            return(true);
        }
예제 #8
0
 public bool CheckCommentAddPermission(int operatorID)
 {
     return(Permission.Can(operatorID, UseAction));
 }