Пример #1
0
        /// <summary>
        /// 解析内容用于创建AtUser
        /// </summary>
        /// <param name="body">待解析的内容</param>
        /// <param name="userId">内容作者UserId</param>
        /// <param name="associateId">关联项Id</param>
        public void ResolveBodyForEdit(string body, long userId, long associateId)
        {
            if (string.IsNullOrEmpty(body) || !body.Contains("@"))
            {
                return;
            }

            List <long> userIds = new List <long>();

            PrivacyService privacyService = new PrivacyService();
            IUserService   userService    = DIContainer.Resolve <IUserService>();

            string userNameRegex = new UserSettings().NickNameRegex, tempNickName = string.Empty;

            userNameRegex = userNameRegex.TrimStart('^').TrimEnd('$');

            Regex           rg      = new Regex("(?<=(\\@))" + userNameRegex, RegexOptions.Multiline | RegexOptions.Singleline);
            MatchCollection matches = rg.Matches(body);

            if (matches != null)
            {
                foreach (Match m in matches)
                {
                    if (string.IsNullOrEmpty(m.Value) || tempNickName.Equals(m.Value, StringComparison.CurrentCultureIgnoreCase))
                    {
                        continue;
                    }

                    tempNickName = m.Value;
                    IUser user = userService.GetUserByNickName(tempNickName);

                    if (user == null ||
                        userIds.Contains(user.UserId) ||
                        !privacyService.Validate(user.UserId, userId, PrivacyItemKeys.Instance().AtUser()))
                    {
                        continue;
                    }

                    userIds.Add(user.UserId);
                }
            }

            if (userIds.Count > 0)
            {
                BatchCreateAtUser(userIds, associateId, userId);
            }
        }
Пример #2
0
        /// <summary>
        /// 解析内容中的AtUser用户展示展示
        /// </summary>
        /// <param name="body">待解析的内容</param>
        /// <param name="associateId">关联项Id</param>
        /// <param name="userId">关联项作者Id</param>
        /// <param name="TagGenerate">用户生成对应标签的方法</param>
        public string ResolveBodyForDetail(string body, long associateId, long userId, Func <string, string, string> TagGenerate)
        {
            if (string.IsNullOrEmpty(body) || !body.Contains("@") || userId <= 0)
            {
                return(body);
            }

            IList <long> userIds = GetAtUserIds(associateId);

            if (userIds != null)
            {
                PrivacyService privacyService = new PrivacyService();
                IUserService   userService    = DIContainer.Resolve <IUserService>();
                bool           endMatch       = false;
                foreach (var atUserId in userIds)
                {
                    if (atUserId == 0)
                    {
                        continue;
                    }

                    IUser user = userService.GetUser(atUserId);
                    if (user == null)
                    {
                        continue;
                    }

                    if (privacyService.Validate(user.UserId, userId, PrivacyItemKeys.Instance().AtUser()))
                    {
                        string nickName = user.NickName;

                        body = body.Replace("@" + nickName, TagGenerate(user.UserName, nickName));
                        body = body.Replace("@" + nickName + "</p>", TagGenerate(user.UserName, nickName) + "</p>");

                        if (!endMatch && body.EndsWith("@" + nickName))
                        {
                            endMatch = true;
                            body     = body.Remove(body.Length - (nickName.Length + 1), nickName.Length + 1) + TagGenerate(user.UserName, nickName);
                        }
                    }
                }
            }
            return(body);
        }