OSDN Git Service

twitter.com のURLを抽出する正規表現パターンを x.com に対応させる
authorKimura Youichi <kim.upsilon@bucyou.net>
Wed, 15 May 2024 17:23:26 +0000 (02:23 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Wed, 15 May 2024 17:23:26 +0000 (02:23 +0900)
CHANGELOG.txt
OpenTween.Tests/TwitterTest.cs
OpenTween/TweetDetailsView.cs
OpenTween/Twitter.cs

index a637258..1b76178 100644 (file)
@@ -5,6 +5,7 @@
    - Unicode 15.1 で追加された絵文字が表示されるようになります
  * CHG: 設定画面でのアカウント一覧の表示形式を変更
  * CHG: 新規タブの初回に読み込まれた発言を既読状態にする(起動時の初回の読み込みと同じ動作となる)
+ * CHG: ドメインに x.com が使われている引用ツイートの展開・投稿に対応
  * FIX: 発言の削除中にタブを切り替えるとエラーが発生する不具合を修正 (thx @Tan90909090!)
 
 ==== Ver 3.13.0(2024/01/27)
index 2ac7bee..b3a655f 100644 (file)
@@ -45,6 +45,8 @@ namespace OpenTween
             new[] { "21984934471" })]
         [InlineData("https://twitter.com/imgazyobuzi/status/293333871171354624/photo/1",
             new[] { "293333871171354624" })]
+        [InlineData("https://x.com/twitterapi/status/22634515958",
+            new[] { "22634515958" })]
         public void StatusUrlRegexTest(string url, string[] expected)
         {
             var results = Twitter.StatusUrlRegex.Matches(url).Cast<Match>()
@@ -64,6 +66,9 @@ namespace OpenTween
         [InlineData("https://twitter.com/messages/compose?recipient_id=514241801", true)]
         [InlineData("http://twitter.com/messages/compose?recipient_id=514241801", true)]
         [InlineData("https://twitter.com/messages/compose?recipient_id=514241801&text=%E3%81%BB%E3%81%92", true)]
+        [InlineData("https://x.com/twitterapi/status/22634515958", true)]
+        [InlineData("https://mobile.x.com/twitterapi/status/22634515958", true)]
+        [InlineData("https://x.com/messages/compose?recipient_id=514241801", false)] // DM は twitter.com のみ通る
         public void AttachmentUrlRegexTest(string url, bool isMatch)
             => Assert.Equal(isMatch, Twitter.AttachmentUrlRegex.IsMatch(url));
 
index ba661dc..4e59fa5 100644 (file)
@@ -459,7 +459,7 @@ namespace OpenTween
 
         private string? GetUserId()
         {
-            var m = Regex.Match(this.postBrowserStatusText, @"^https?://twitter.com/(#!/)?(?<ScreenName>[a-zA-Z0-9_]+)(/status(es)?/[0-9]+)?$");
+            var m = Twitter.StatusUrlRegex.Match(this.postBrowserStatusText);
             if (m.Success && this.Owner.IsTwitterId(m.Result("${ScreenName}")))
                 return m.Result("${ScreenName}");
             else
@@ -821,7 +821,7 @@ namespace OpenTween
                     this.SearchAtPostsDetailToolStripMenuItem.Enabled = false;
                 }
 
-                if (Regex.IsMatch(this.postBrowserStatusText, @"^https?://twitter.com/search\?q=%23"))
+                if (Regex.IsMatch(this.postBrowserStatusText, @"^https?://(twitter|x).com/search\?q=%23"))
                     this.UseHashtagMenuItem.Enabled = true;
                 else
                     this.UseHashtagMenuItem.Enabled = false;
@@ -855,7 +855,7 @@ namespace OpenTween
                 this.SelectionTranslationToolStripMenuItem.Enabled = true;
             }
             // 発言内に自分以外のユーザーが含まれてればフォロー状態全表示を有効に
-            var ma = Regex.Matches(this.PostBrowser.DocumentText, @"href=""https?://twitter.com/(#!/)?(?<ScreenName>[a-zA-Z0-9_]+)(/status(es)?/[0-9]+)?""");
+            var ma = Regex.Matches(this.PostBrowser.DocumentText, @"href=""https?://(twitter|x).com/(#!/)?(?<ScreenName>[a-zA-Z0-9_]+)(/status(es)?/[0-9]+)?""");
             var fAllFlag = false;
             foreach (Match mu in ma)
             {
@@ -974,7 +974,7 @@ namespace OpenTween
 
         private async void FriendshipAllMenuItem_Click(object sender, EventArgs e)
         {
-            var ma = Regex.Matches(this.PostBrowser.DocumentText, @"href=""https?://twitter.com/(#!/)?(?<ScreenName>[a-zA-Z0-9_]+)(/status(es)?/[0-9]+)?""");
+            var ma = Regex.Matches(this.PostBrowser.DocumentText, @"href=""https?://(twitter|x).com/(#!/)?(?<ScreenName>[a-zA-Z0-9_]+)(/status(es)?/[0-9]+)?""");
             var ids = new List<string>();
             foreach (Match mu in ma)
             {
@@ -1038,7 +1038,7 @@ namespace OpenTween
 
         private void UseHashtagMenuItem_Click(object sender, EventArgs e)
         {
-            var m = Regex.Match(this.postBrowserStatusText, @"^https?://twitter.com/search\?q=%23(?<hash>.+)$");
+            var m = Regex.Match(this.postBrowserStatusText, @"^https?://(twitter|x).com/search\?q=%23(?<hash>.+)$");
             if (m.Success)
                 this.Owner.SetPermanentHashtag(Uri.UnescapeDataString(m.Groups["hash"].Value));
         }
index 4a9036d..f48ff06 100644 (file)
@@ -128,15 +128,15 @@ namespace OpenTween
         /// <summary>
         /// ツイートへのパーマリンクURLを判定する正規表現
         /// </summary>
-        public static readonly Regex StatusUrlRegex = new(@"https?://([^.]+\.)?twitter\.com/(#!/)?(?<ScreenName>[a-zA-Z0-9_]+)/status(es)?/(?<StatusId>[0-9]+)(/photo)?", RegexOptions.IgnoreCase);
+        public static readonly Regex StatusUrlRegex = new(@"https?://([^.]+\.)?(twitter|x)\.com/(#!/)?(?<ScreenName>[a-zA-Z0-9_]+)/status(es)?/(?<StatusId>[0-9]+)(/photo)?", RegexOptions.IgnoreCase);
 
         /// <summary>
         /// attachment_url に指定可能な URL を判定する正規表現
         /// </summary>
         public static readonly Regex AttachmentUrlRegex = new(
             @"https?://(
-   twitter\.com/[0-9A-Za-z_]+/status/[0-9]+
- | mobile\.twitter\.com/[0-9A-Za-z_]+/status/[0-9]+
+   (twitter|x)\.com/[0-9A-Za-z_]+/status/[0-9]+
+ | mobile\.(twitter|x)\.com/[0-9A-Za-z_]+/status/[0-9]+
  | twitter\.com/messages/compose\?recipient_id=[0-9]+(&.+)?
 )$",
             RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);