BirdsiteLive/src/BirdsiteLive.Domain/StatusService.cs

88 lines
2.7 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;
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;
2020-07-23 01:27:25 +02:00
#region Ctor
2020-08-01 04:49:00 +02:00
public StatusService(InstanceSettings instanceSettings, IStatusExtractor statusExtractor)
2020-07-23 01:27:25 +02:00
{
_instanceSettings = instanceSettings;
2020-08-01 04:49:00 +02:00
_statusExtractor = statusExtractor;
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";
var apPublic = "https://www.w3.org/ns/activitystreams#Public";
2020-08-01 04:49:00 +02:00
var extractedTags = _statusExtractor.ExtractTags(tweet.MessageContent);
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},
//cc = new [] { apPublic },
to = new[] { to },
2020-08-02 01:26:33 +02:00
//cc = new[] { apPublic },
cc = new string[0],
2020-07-23 01:27:25 +02:00
sensitive = false,
2020-08-01 04:13:52 +02:00
content = $"<p>{extractedTags.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
}
}
}