From 8649f435747dda47b4980f56dbb4b53c0d46efaf Mon Sep 17 00:00:00 2001 From: Kimura Youichi Date: Sun, 9 Jun 2024 21:52:48 +0900 Subject: [PATCH] =?utf8?q?Misskey=E3=81=AE=E3=83=8E=E3=83=BC=E3=83=88?= =?utf8?q?=E6=9C=AC=E6=96=87=E3=81=ABTwitter=E3=81=AEURL=E3=81=8C=E5=90=AB?= =?utf8?q?=E3=81=BE=E3=82=8C=E3=81=A6=E3=81=84=E3=81=9F=E5=A0=B4=E5=90=88?= =?utf8?q?=E3=81=AFQuoteStatusIds=E3=81=AB=E8=BF=BD=E5=8A=A0=E3=81=99?= =?utf8?q?=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../Misskey/MisskeyPostFactoryTest.cs | 24 ++++++++++++++++++++++ .../SocialProtocol/Misskey/MisskeyPostFactory.cs | 15 ++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/OpenTween.Tests/SocialProtocol/Misskey/MisskeyPostFactoryTest.cs b/OpenTween.Tests/SocialProtocol/Misskey/MisskeyPostFactoryTest.cs index b3529b2c..5d371ea6 100644 --- a/OpenTween.Tests/SocialProtocol/Misskey/MisskeyPostFactoryTest.cs +++ b/OpenTween.Tests/SocialProtocol/Misskey/MisskeyPostFactoryTest.cs @@ -20,6 +20,7 @@ // Boston, MA 02110-1301, USA. using OpenTween.Api.Misskey; +using OpenTween.Models; using Xunit; namespace OpenTween.SocialProtocol.Misskey @@ -223,5 +224,28 @@ namespace OpenTween.SocialProtocol.Misskey Assert.Null(post.RetweetedByUserId); Assert.Equal(new[] { new MisskeyNoteId("aaaaa") }, post.QuoteStatusIds); } + + [Fact] + public void CreateFromNote_TextContainsTwitterUrlTest() + { + var note = new MisskeyNote + { + Id = "aaaaa", + CreatedAt = "2023-12-31T00:00:00.000Z", + Text = "hoge https://twitter.com/hoge/status/12345", + User = new() + { + Id = "abcdef", + Username = "foo", + }, + Visibility = "public", + }; + var factory = new MisskeyPostFactory(new()); + var accountState = new MisskeyAccountState(new("https://example.com/"), new("aaaa"), "hoge"); + + var post = factory.CreateFromNote(note, accountState, firstLoad: false); + Assert.Equal(new MisskeyNoteId("aaaaa"), post.StatusId); + Assert.Equal(new[] { new TwitterStatusId("12345") }, post.QuoteStatusIds); + } } } diff --git a/OpenTween/SocialProtocol/Misskey/MisskeyPostFactory.cs b/OpenTween/SocialProtocol/Misskey/MisskeyPostFactory.cs index 038084d0..743341a9 100644 --- a/OpenTween/SocialProtocol/Misskey/MisskeyPostFactory.cs +++ b/OpenTween/SocialProtocol/Misskey/MisskeyPostFactory.cs @@ -24,6 +24,7 @@ using System; using System.Collections.Generic; using System.Linq; +using OpenTween.Api.DataModel; using OpenTween.Api.Misskey; using OpenTween.Models; using OpenTween.Setting; @@ -81,6 +82,10 @@ namespace OpenTween.SocialProtocol.Misskey var urlEntities = TweetExtractor.ExtractUrlEntities(originalText); var textHtml = TweetFormatter.AutoLinkHtml(originalText, urlEntities); + var quotedNoteIdsInText = this.ExtractPostIdFromUrls(urlEntities).ToArray(); + if (quotedNoteIdsInText.Length > 0) + quotedNoteIds = Enumerable.Concat(quotedNoteIdsInText, quotedNoteIds).ToArray(); + var textSuffix = ""; if (originalNote.Files.Length > 0) textSuffix += " [画像]"; @@ -134,6 +139,16 @@ namespace OpenTween.SocialProtocol.Misskey }; } + private IEnumerable ExtractPostIdFromUrls(IEnumerable urls) + { + foreach (var url in urls) + { + var match = OpenTween.Twitter.StatusUrlRegex.Match(url.ExpandedUrl); + if (match.Success) + yield return new TwitterStatusId(match.Groups["StatusId"].Value); + } + } + public static Uri CreateLocalPostUri(Uri serverUri, MisskeyNoteId noteId) => new(serverUri, $"/notes/{noteId.Id}"); -- 2.11.0