BirdsiteLive/src/BirdsiteLive.Domain/StatusService.cs

114 lines
4.0 KiB
C#
Raw Normal View History

2020-07-23 02:19:40 +02:00
using System;
using System.Collections.Generic;
2020-07-23 01:27:25 +02:00
using System.IO;
using System.Linq;
2020-08-01 04:13:52 +02:00
using System.Text.RegularExpressions;
2020-07-23 01:27:25 +02:00
using BirdsiteLive.ActivityPub;
2021-01-10 04:26:17 +01:00
using BirdsiteLive.ActivityPub.Converters;
2020-08-01 04:13:52 +02:00
using BirdsiteLive.ActivityPub.Models;
2020-07-23 01:27:25 +02:00
using BirdsiteLive.Common.Settings;
2021-02-28 04:12:50 +01:00
using BirdsiteLive.Domain.Repository;
2021-01-14 06:38:26 +01:00
using BirdsiteLive.Domain.Statistics;
2020-08-01 04:49:00 +02:00
using BirdsiteLive.Domain.Tools;
2020-07-23 02:19:40 +02:00
using BirdsiteLive.Twitter.Models;
2020-07-23 01:27:25 +02:00
using Tweetinvi.Models;
using Tweetinvi.Models.Entities;
namespace BirdsiteLive.Domain
{
public interface IStatusService
{
2020-07-23 02:19:40 +02:00
Note GetStatus(string username, ExtractedTweet tweet);
2020-07-23 01:27:25 +02:00
}
public class StatusService : IStatusService
{
private readonly InstanceSettings _instanceSettings;
2020-08-01 04:49:00 +02:00
private readonly IStatusExtractor _statusExtractor;
2021-01-14 06:38:26 +01:00
private readonly IExtractionStatisticsHandler _statisticsHandler;
2021-02-28 04:12:50 +01:00
private readonly IPublicationRepository _publicationRepository;
2020-07-23 01:27:25 +02:00
#region Ctor
2021-02-28 04:12:50 +01:00
public StatusService(InstanceSettings instanceSettings, IStatusExtractor statusExtractor, IExtractionStatisticsHandler statisticsHandler, IPublicationRepository publicationRepository)
2020-07-23 01:27:25 +02:00
{
_instanceSettings = instanceSettings;
2020-08-01 04:49:00 +02:00
_statusExtractor = statusExtractor;
2021-01-14 06:38:26 +01:00
_statisticsHandler = statisticsHandler;
2021-02-28 04:12:50 +01:00
_publicationRepository = publicationRepository;
2020-07-23 01:27:25 +02:00
}
#endregion
2020-07-23 02:19:40 +02:00
public Note GetStatus(string username, ExtractedTweet tweet)
2020-07-23 01:27:25 +02:00
{
2021-01-10 04:26:17 +01:00
var actorUrl = UrlFactory.GetActorUrl(_instanceSettings.Domain, username);
2021-01-10 04:52:52 +01:00
var noteUrl = UrlFactory.GetNoteUrl(_instanceSettings.Domain, username, tweet.Id.ToString());
2020-07-23 01:27:25 +02:00
var to = $"{actorUrl}/followers";
2021-02-28 04:12:50 +01:00
var isUnlisted = _publicationRepository.IsUnlisted(username);
var cc = new string[0];
if (isUnlisted)
cc = new[] {"https://www.w3.org/ns/activitystreams#Public"};
string summary = null;
var sensitive = _publicationRepository.IsSensitive(username);
if (sensitive)
summary = "Potential Content Warning";
var extractedTags = _statusExtractor.Extract(tweet.MessageContent);
2021-01-14 06:38:26 +01:00
_statisticsHandler.ExtractedStatus(extractedTags.tags.Count(x => x.type == "Mention"));
2020-08-01 06:32:04 +02:00
2021-01-29 07:15:10 +01:00
// Replace RT by a link
var content = extractedTags.content;
if (content.Contains("{RT}") && tweet.IsRetweet)
{
if (!string.IsNullOrWhiteSpace(tweet.RetweetUrl))
content = content.Replace("{RT}",
$@"<a href=""{tweet.RetweetUrl}"" rel=""nofollow noopener noreferrer"" target=""_blank"">RT</a>");
else
content = content.Replace("{RT}", "RT");
}
2020-08-01 06:32:04 +02:00
string inReplyTo = null;
if (tweet.InReplyToStatusId != default)
2021-01-10 04:26:17 +01:00
inReplyTo = $"https://{_instanceSettings.Domain}/users/{tweet.InReplyToAccount.ToLowerInvariant()}/statuses/{tweet.InReplyToStatusId}";
2020-08-01 06:32:04 +02:00
2020-07-23 01:27:25 +02:00
var note = new Note
{
2021-01-10 04:52:52 +01:00
id = noteUrl,
2020-07-23 01:27:25 +02:00
published = tweet.CreatedAt.ToString("s") + "Z",
url = noteUrl,
attributedTo = actorUrl,
2020-08-01 06:32:04 +02:00
inReplyTo = inReplyTo,
2020-07-23 01:27:25 +02:00
to = new[] { to },
2021-02-28 04:12:50 +01:00
cc = cc,
2020-07-23 01:27:25 +02:00
sensitive = sensitive,
summary = summary,
2021-01-29 07:15:10 +01:00
content = $"<p>{content}</p>",
2020-07-23 02:19:40 +02:00
attachment = Convert(tweet.Media),
2020-08-01 04:13:52 +02:00
tag = extractedTags.tags
2020-07-23 01:27:25 +02:00
};
return note;
}
2020-07-23 02:19:40 +02:00
private Attachment[] Convert(ExtractedMedia[] media)
2020-07-23 01:27:25 +02:00
{
2020-08-01 04:13:52 +02:00
if(media == null) return new Attachment[0];
2020-07-23 02:19:40 +02:00
return media.Select(x =>
2020-07-23 01:27:25 +02:00
{
2020-07-23 02:19:40 +02:00
return new Attachment
2020-07-23 01:27:25 +02:00
{
type = "Document",
2020-07-23 02:19:40 +02:00
url = x.Url,
mediaType = x.MediaType
2020-07-23 01:27:25 +02:00
};
2020-07-23 02:19:40 +02:00
}).ToArray();
2020-07-23 01:27:25 +02:00
}
}
}