cloutier--bird.makeup/src/BirdsiteLive.Domain/StatusService.cs

115 lines
4 KiB
C#
Raw Normal View History

2020-07-22 20:19:40 -04:00
using System;
using System.Collections.Generic;
2020-07-22 19:27:25 -04:00
using System.IO;
using System.Linq;
2020-07-31 22:13:52 -04:00
using System.Text.RegularExpressions;
2020-07-22 19:27:25 -04:00
using BirdsiteLive.ActivityPub;
2021-01-09 22:26:17 -05:00
using BirdsiteLive.ActivityPub.Converters;
2020-07-31 22:13:52 -04:00
using BirdsiteLive.ActivityPub.Models;
2020-07-22 19:27:25 -04:00
using BirdsiteLive.Common.Settings;
2021-02-27 22:12:50 -05:00
using BirdsiteLive.Domain.Repository;
2021-01-14 00:38:26 -05:00
using BirdsiteLive.Domain.Statistics;
2020-07-31 22:49:00 -04:00
using BirdsiteLive.Domain.Tools;
2020-07-22 20:19:40 -04:00
using BirdsiteLive.Twitter.Models;
2020-07-22 19:27:25 -04:00
using Tweetinvi.Models;
using Tweetinvi.Models.Entities;
namespace BirdsiteLive.Domain
{
public interface IStatusService
{
2020-07-22 20:19:40 -04:00
Note GetStatus(string username, ExtractedTweet tweet);
2020-07-22 19:27:25 -04:00
}
public class StatusService : IStatusService
{
private readonly InstanceSettings _instanceSettings;
2020-07-31 22:49:00 -04:00
private readonly IStatusExtractor _statusExtractor;
2021-01-14 00:38:26 -05:00
private readonly IExtractionStatisticsHandler _statisticsHandler;
2021-02-27 22:12:50 -05:00
private readonly IPublicationRepository _publicationRepository;
2020-07-22 19:27:25 -04:00
#region Ctor
2021-02-27 22:12:50 -05:00
public StatusService(InstanceSettings instanceSettings, IStatusExtractor statusExtractor, IExtractionStatisticsHandler statisticsHandler, IPublicationRepository publicationRepository)
2020-07-22 19:27:25 -04:00
{
_instanceSettings = instanceSettings;
2020-07-31 22:49:00 -04:00
_statusExtractor = statusExtractor;
2021-01-14 00:38:26 -05:00
_statisticsHandler = statisticsHandler;
2021-02-27 22:12:50 -05:00
_publicationRepository = publicationRepository;
2020-07-22 19:27:25 -04:00
}
#endregion
2020-07-22 20:19:40 -04:00
public Note GetStatus(string username, ExtractedTweet tweet)
2020-07-22 19:27:25 -04:00
{
2021-01-09 22:26:17 -05:00
var actorUrl = UrlFactory.GetActorUrl(_instanceSettings.Domain, username);
2021-01-09 22:52:52 -05:00
var noteUrl = UrlFactory.GetNoteUrl(_instanceSettings.Domain, username, tweet.Id.ToString());
2022-05-09 20:31:18 -04:00
if (tweet.IsRetweet)
{
2022-05-11 18:26:38 -04:00
actorUrl = UrlFactory.GetActorUrl(_instanceSettings.Domain, tweet.OriginalAuthor.Acct);
noteUrl = UrlFactory.GetNoteUrl(_instanceSettings.Domain, tweet.OriginalAuthor.Acct, tweet.Id.ToString());
2022-05-09 20:31:18 -04:00
}
2020-07-22 19:27:25 -04:00
var to = $"{actorUrl}/followers";
2021-02-27 22:12:50 -05: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 00:38:26 -05:00
_statisticsHandler.ExtractedStatus(extractedTags.tags.Count(x => x.type == "Mention"));
2020-08-01 00:32:04 -04:00
2021-01-29 01:15:10 -05:00
// Replace RT by a link
var content = extractedTags.content;
2022-05-08 19:10:08 -04:00
if (tweet.IsRetweet)
2021-01-29 01:15:10 -05:00
{
2022-05-08 19:10:08 -04:00
content = "RT: " + content;
2021-01-29 01:15:10 -05:00
}
2020-08-01 00:32:04 -04:00
string inReplyTo = null;
2022-05-10 19:52:23 -04:00
// if (tweet.InReplyToStatusId != default)
// inReplyTo = $"https://{_instanceSettings.Domain}/users/{tweet.InReplyToAccount.ToLowerInvariant()}/statuses/{tweet.InReplyToStatusId}";
2020-08-01 00:32:04 -04:00
2020-07-22 19:27:25 -04:00
var note = new Note
{
2021-01-09 22:52:52 -05:00
id = noteUrl,
2020-07-22 19:27:25 -04:00
published = tweet.CreatedAt.ToString("s") + "Z",
url = noteUrl,
attributedTo = actorUrl,
2020-08-01 00:32:04 -04:00
inReplyTo = inReplyTo,
2020-07-22 19:27:25 -04:00
to = new[] { to },
2021-02-27 22:12:50 -05:00
cc = cc,
2020-07-22 19:27:25 -04:00
sensitive = sensitive,
summary = summary,
2021-01-29 01:15:10 -05:00
content = $"<p>{content}</p>",
2020-07-22 20:19:40 -04:00
attachment = Convert(tweet.Media),
2020-07-31 22:13:52 -04:00
tag = extractedTags.tags
2020-07-22 19:27:25 -04:00
};
return note;
}
2020-07-22 20:19:40 -04:00
private Attachment[] Convert(ExtractedMedia[] media)
2020-07-22 19:27:25 -04:00
{
2020-07-31 22:13:52 -04:00
if(media == null) return new Attachment[0];
2020-07-22 20:19:40 -04:00
return media.Select(x =>
2020-07-22 19:27:25 -04:00
{
2020-07-22 20:19:40 -04:00
return new Attachment
2020-07-22 19:27:25 -04:00
{
type = "Document",
2020-07-22 20:19:40 -04:00
url = x.Url,
mediaType = x.MediaType
2020-07-22 19:27:25 -04:00
};
2020-07-22 20:19:40 -04:00
}).ToArray();
2020-07-22 19:27:25 -04:00
}
}
}