#215 applying the same rule to new comments

This commit is contained in:
Hamid Reza Mohammadi 2021-10-02 14:38:16 +03:30
parent 080c5a173b
commit aefe319016
2 changed files with 50 additions and 42 deletions

View File

@ -507,6 +507,51 @@ namespace RMuseum.Services.Implementation
return new RServiceResult<bool>(true); return new RServiceResult<bool>(true);
} }
private async Task<string> _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("</a>", closeIndex) != -1)
{
closeIndex += ">".Length;
string urlText = commentText.Substring(closeIndex, commentText.IndexOf("</a>", 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("</a>", closeIndex));
textFixed = true;
}
}
if (!textFixed)
commentText = commentText.Substring(0, closeIndex) + "پیوند به وبگاه بیرونی" + commentText.Substring(commentText.IndexOf("</a>", closeIndex));
}
}
}
index = commentText.IndexOf("href=\"", index);
}
return commentText;
}
/// <summary> /// <summary>
/// examine comments for long links /// examine comments for long links
/// </summary> /// </summary>
@ -539,48 +584,7 @@ namespace RMuseum.Services.Implementation
var comment = comments[i]; var comment = comments[i];
string commentText = comment.HtmlComment; string commentText = await _ProcessCommentHtmlLinks(comment.HtmlComment, 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("</a>", closeIndex) != -1)
{
closeIndex += ">".Length;
string urlText = commentText.Substring(closeIndex, commentText.IndexOf("</a>", 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("</a>", closeIndex));
textFixed = true;
}
}
if(!textFixed)
commentText = commentText.Substring(0, closeIndex) + "پیوند به وبگاه بیرونی" + commentText.Substring(commentText.IndexOf("</a>", closeIndex));
}
}
}
index = commentText.IndexOf("href=\"", index);
}
if(commentText != comment.HtmlComment) if(commentText != comment.HtmlComment)
{ {

View File

@ -738,6 +738,8 @@ namespace RMuseum.Services.Implementation
content = content.ApplyCorrectYeKe(); content = content.ApplyCorrectYeKe();
content = await _ProcessCommentHtmlLinks(content, _context);
GanjoorComment comment = new GanjoorComment() GanjoorComment comment = new GanjoorComment()
{ {
UserId = userId, UserId = userId,
@ -835,6 +837,8 @@ namespace RMuseum.Services.Implementation
htmlComment = htmlComment.ApplyCorrectYeKe(); htmlComment = htmlComment.ApplyCorrectYeKe();
htmlComment = await _ProcessCommentHtmlLinks(htmlComment, _context);
comment.HtmlComment = htmlComment; comment.HtmlComment = htmlComment;
_context.GanjoorComments.Update(comment); _context.GanjoorComments.Update(comment);