BirdsiteLive/src/BirdsiteLive.Domain/StatusService.cs

76 lines
2.1 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;
using BirdsiteLive.ActivityPub;
using BirdsiteLive.Common.Settings;
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;
#region Ctor
public StatusService(InstanceSettings instanceSettings)
{
_instanceSettings = instanceSettings;
}
#endregion
2020-07-23 02:19:40 +02:00
public Note GetStatus(string username, ExtractedTweet tweet)
2020-07-23 01:27:25 +02:00
{
var actorUrl = $"https://{_instanceSettings.Domain}/users/{username}";
var noteId = $"https://{_instanceSettings.Domain}/users/{username}/statuses/{tweet.Id}";
var noteUrl = $"https://{_instanceSettings.Domain}/@{username}/{tweet.Id}";
var to = $"{actorUrl}/followers";
var apPublic = "https://www.w3.org/ns/activitystreams#Public";
var note = new Note
{
id = $"{noteId}/activity",
published = tweet.CreatedAt.ToString("s") + "Z",
url = noteUrl,
attributedTo = actorUrl,
//to = new [] {to},
//cc = new [] { apPublic },
to = new[] { to },
cc = new[] { apPublic },
//cc = new string[0],
sensitive = false,
2020-07-23 02:19:40 +02:00
content = $"<p>{tweet.MessageContent}</p>",
attachment = Convert(tweet.Media),
2020-07-23 01:27:25 +02:00
tag = new string[0]
};
return note;
}
2020-07-23 02:19:40 +02:00
private Attachment[] Convert(ExtractedMedia[] media)
2020-07-23 01:27:25 +02:00
{
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
}
}
}