Esempio n. 1
0
        internal static JArray AddPreviewComment(string comments, string userName, int page, double x, double y,
                                                 string text, out PreviewCommentData commentData)
        {
            if (string.IsNullOrEmpty(userName) || page < 0 || x < 0 || y < 0)
            {
                throw new InvalidOperationException("Incorrect comment parameter.");
            }

            if (string.IsNullOrEmpty(comments))
            {
                comments = "[]";
            }

            var commentsArray = (JArray)JsonConvert.DeserializeObject(comments);

            commentData = new PreviewCommentData
            {
                Id           = Guid.NewGuid().ToString(),
                CreatedBy    = userName,
                CreationDate = DateTime.UtcNow,
                Page         = page,
                X            = x,
                Y            = y,
                Text         = text?.Substring(0, Math.Min(text.Length, 500))
            };

            var index = 0;

            // Find the correct place to insert the comment to. The order is determined
            // by the page and the comment coordinates inside the page.
            foreach (var jToken in commentsArray)
            {
                var currentPage = jToken["page"]?.Value <int>() ?? 0;
                var currentX    = jToken["x"]?.Value <double>() ?? 0;
                var currentY    = jToken["y"]?.Value <double>() ?? 0;

                // current comment comes before the new one
                if (currentPage < page || currentPage == page && (currentY < y || Math.Abs(currentY - y) < 0.0001 && currentX <= x))
                {
                    index++;
                    continue;
                }

                break;
            }

            commentsArray.Insert(index, JToken.FromObject(commentData));

            return(commentsArray);
        }
        public PreviewComment(PreviewCommentData data)
        {
            Data = data;

            // Workaround: we only have a domain\username information here that cannot be used
            // to load a node head. We have to try to load the whole user node in elevated mode
            // and check for permissions after.
            var caller = AccessProvider.Current.GetOriginalUser();
            var user   = SystemAccount.Execute(() => string.IsNullOrEmpty(data.CreatedBy) ? null : User.Load(data.CreatedBy));

            if (user == null || !SecurityHandler.HasPermission(caller, user.Id, PermissionType.Open))
            {
                user = User.Somebody;
            }

            CreatedBy = new PreviewCommentUser
            {
                Id          = user.Id,
                Path        = user.Path,
                Username    = user.Username,
                DisplayName = user.DisplayName,
                AvatarUrl   = user.AvatarUrl
            };
        }