2020-07-31 22:49:00 -04:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using BirdsiteLive.ActivityPub.Models;
|
|
|
|
|
using BirdsiteLive.Common.Settings;
|
|
|
|
|
|
|
|
|
|
namespace BirdsiteLive.Domain.Tools
|
2020-07-31 22:13:52 -04:00
|
|
|
|
{
|
2020-07-31 22:49:00 -04:00
|
|
|
|
public interface IStatusExtractor
|
2020-07-31 22:13:52 -04:00
|
|
|
|
{
|
2020-07-31 22:49:00 -04:00
|
|
|
|
(string content, Tag[] tags) ExtractTags(string messageContent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class StatusExtractor : IStatusExtractor
|
|
|
|
|
{
|
2020-07-31 23:11:41 -04:00
|
|
|
|
private readonly Regex _hastagRegex = new Regex(@"\W(\#[a-zA-Z0-9_]+\b)(?!;)");
|
|
|
|
|
private readonly Regex _mentionRegex = new Regex(@"\W(\@[a-zA-Z0-9_]+\b)(?!;)");
|
2020-07-31 22:49:00 -04:00
|
|
|
|
private readonly InstanceSettings _instanceSettings;
|
|
|
|
|
|
|
|
|
|
#region Ctor
|
|
|
|
|
public StatusExtractor(InstanceSettings instanceSettings)
|
|
|
|
|
{
|
|
|
|
|
_instanceSettings = instanceSettings;
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
public (string content, Tag[] tags) ExtractTags(string messageContent)
|
|
|
|
|
{
|
|
|
|
|
var tags = new List<Tag>();
|
2020-07-31 23:03:20 -04:00
|
|
|
|
|
|
|
|
|
var hashtagMatch = _hastagRegex.Matches(messageContent);
|
|
|
|
|
foreach (var m in hashtagMatch)
|
2020-07-31 22:49:00 -04:00
|
|
|
|
{
|
|
|
|
|
var tag = m.ToString().Replace("#", string.Empty).Replace("\n", string.Empty).Trim();
|
|
|
|
|
var url = $"https://{_instanceSettings.Domain}/tags/{tag}";
|
|
|
|
|
|
|
|
|
|
tags.Add(new Tag
|
|
|
|
|
{
|
|
|
|
|
name = $"#{tag}",
|
|
|
|
|
href = url,
|
|
|
|
|
type = "Hashtag"
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
messageContent = Regex.Replace(messageContent, m.ToString(),
|
|
|
|
|
$@"<a href=""{url}"" class=""mention hashtag"" rel=""tag"">#<span>{tag}</span></a>");
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-31 23:03:20 -04:00
|
|
|
|
var mentionMatch = _mentionRegex.Matches(messageContent);
|
|
|
|
|
foreach (var m in mentionMatch)
|
|
|
|
|
{
|
|
|
|
|
var mention = m.ToString().Replace("@", string.Empty).Replace("\n", string.Empty).Trim();
|
|
|
|
|
var url = $"https://{_instanceSettings.Domain}/users/{mention}";
|
|
|
|
|
var name = $"@{mention}@{_instanceSettings.Domain}";
|
|
|
|
|
|
|
|
|
|
tags.Add(new Tag
|
|
|
|
|
{
|
|
|
|
|
name = name,
|
|
|
|
|
href = url,
|
|
|
|
|
type = "Mention"
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
messageContent = Regex.Replace(messageContent, m.ToString(),
|
|
|
|
|
$@"<span class=""h-card""><a href=""https://{_instanceSettings.Domain}/@{mention}"" class=""u-url mention"">@<span>{mention}</span></a></span>");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (messageContent, tags.ToArray());
|
2020-07-31 22:49:00 -04:00
|
|
|
|
}
|
2020-07-31 22:13:52 -04:00
|
|
|
|
}
|
|
|
|
|
}
|