diff --git a/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-Maintenance.cs b/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-Maintenance.cs index 015d3562..9abde78e 100644 --- a/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-Maintenance.cs +++ b/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-Maintenance.cs @@ -507,6 +507,51 @@ namespace RMuseum.Services.Implementation return new RServiceResult(true); } + private async Task _ProcessCommentHtmlLinks(string commentText, RMuseumDbContext context) + { + int index = commentText.IndexOf("href="); + while (index != -1) + { + index += "href=\"".Length; + commentText = commentText.Replace("'", "\""); + if (commentText.IndexOf("\"", index) != -1) + { + int closeIndex = commentText.IndexOf("\"", index); + if (closeIndex == -1) + { + continue; + } + string url = commentText.Substring(index, closeIndex - index); + closeIndex = commentText.IndexOf(">", index); + if (closeIndex != -1 && commentText.IndexOf("", closeIndex) != -1) + { + closeIndex += ">".Length; + string urlText = commentText.Substring(closeIndex, commentText.IndexOf("", closeIndex) - closeIndex); + if (urlText == url) + { + bool textFixed = false; + if (urlText.IndexOf("http://ganjoor.net") == 0 || urlText.IndexOf("https://ganjoor.net") == 0) + { + urlText = urlText.Replace("http://ganjoor.net", "").Replace("https://ganjoor.net", ""); + if (urlText.Length > 0 && urlText[urlText.Length - 1] == '/') + urlText = urlText.Substring(0, urlText.Length - 1); + var page = await context.GanjoorPages.AsNoTracking().Where(p => p.FullUrl == urlText).FirstOrDefaultAsync(); + if (page != null) + { + commentText = commentText.Substring(0, closeIndex) + page.FullTitle + commentText.Substring(commentText.IndexOf("", closeIndex)); + textFixed = true; + } + } + if (!textFixed) + commentText = commentText.Substring(0, closeIndex) + "پیوند به وبگاه بیرونی" + commentText.Substring(commentText.IndexOf("", closeIndex)); + } + } + } + index = commentText.IndexOf("href=\"", index); + } + return commentText; + } + /// /// examine comments for long links /// @@ -539,48 +584,7 @@ namespace RMuseum.Services.Implementation var comment = comments[i]; - string commentText = comment.HtmlComment; - - int index = commentText.IndexOf("href="); - while(index != -1) - { - index += "href=\"".Length; - commentText = commentText.Replace("'", "\""); - if(commentText.IndexOf("\"", index) != -1) - { - int closeIndex = commentText.IndexOf("\"", index); - if(closeIndex == -1) - { - continue; - } - string url = commentText.Substring(index, closeIndex - index); - closeIndex = commentText.IndexOf(">", index); - if (closeIndex != -1 && commentText.IndexOf("", closeIndex) != -1) - { - closeIndex += ">".Length; - string urlText = commentText.Substring(closeIndex, commentText.IndexOf("", closeIndex) - closeIndex); - if(urlText == url) - { - bool textFixed = false; - if(urlText.IndexOf("http://ganjoor.net") == 0 || urlText.IndexOf("https://ganjoor.net") == 0) - { - urlText = urlText.Replace("http://ganjoor.net", "").Replace("https://ganjoor.net", ""); - if (urlText.Length > 0 && urlText[urlText.Length - 1] == '/') - urlText = urlText.Substring(0, urlText.Length - 1); - var page = await context.GanjoorPages.AsNoTracking().Where(p => p.FullUrl == urlText).FirstOrDefaultAsync(); - if(page != null) - { - commentText = commentText.Substring(0, closeIndex) + page.FullTitle + commentText.Substring(commentText.IndexOf("", closeIndex)); - textFixed = true; - } - } - if(!textFixed) - commentText = commentText.Substring(0, closeIndex) + "پیوند به وبگاه بیرونی" + commentText.Substring(commentText.IndexOf("", closeIndex)); - } - } - } - index = commentText.IndexOf("href=\"", index); - } + string commentText = await _ProcessCommentHtmlLinks(comment.HtmlComment, context); if(commentText != comment.HtmlComment) { diff --git a/RMuseum/Services/Implementation/GanjoorService.cs b/RMuseum/Services/Implementation/GanjoorService.cs index 244df299..8ad8d61f 100644 --- a/RMuseum/Services/Implementation/GanjoorService.cs +++ b/RMuseum/Services/Implementation/GanjoorService.cs @@ -738,6 +738,8 @@ namespace RMuseum.Services.Implementation content = content.ApplyCorrectYeKe(); + content = await _ProcessCommentHtmlLinks(content, _context); + GanjoorComment comment = new GanjoorComment() { UserId = userId, @@ -835,6 +837,8 @@ namespace RMuseum.Services.Implementation htmlComment = htmlComment.ApplyCorrectYeKe(); + htmlComment = await _ProcessCommentHtmlLinks(htmlComment, _context); + comment.HtmlComment = htmlComment; _context.GanjoorComments.Update(comment);