OSDN Git Service

ISocialAccountインタフェースを追加
authorKimura Youichi <kim.upsilon@bucyou.net>
Tue, 16 Jan 2024 17:24:29 +0000 (02:24 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Tue, 16 Jan 2024 19:40:28 +0000 (04:40 +0900)
OpenTween.Tests/SocialProtocol/AccountCollectionTest.cs
OpenTween.Tests/SocialProtocol/Twitter/TwitterAccountTest.cs [new file with mode: 0644]
OpenTween/ApplicationEvents.cs
OpenTween/SocialProtocol/AccountCollection.cs
OpenTween/SocialProtocol/ISocialAccount.cs [new file with mode: 0644]
OpenTween/SocialProtocol/Twitter/TwitterAccount.cs [new file with mode: 0644]
OpenTween/Tween.cs

index a0b9528..63edea7 100644 (file)
@@ -20,6 +20,7 @@
 // Boston, MA 02110-1301, USA.
 
 using System;
+using OpenTween.SocialProtocol.Twitter;
 using Xunit;
 
 namespace OpenTween.SocialProtocol
@@ -90,7 +91,7 @@ namespace OpenTween.SocialProtocol
 
             // 欠けている ID は削除される
             Assert.Empty(accounts.Items);
-            Assert.Equal(APIAuthType.None, accounts.Primary.Api.AuthType);
+            Assert.Equal(APIAuthType.None, ((TwitterAccount)accounts.Primary).AuthType);
             Assert.True(accountItem1.IsDisposed);
         }
 
diff --git a/OpenTween.Tests/SocialProtocol/Twitter/TwitterAccountTest.cs b/OpenTween.Tests/SocialProtocol/Twitter/TwitterAccountTest.cs
new file mode 100644 (file)
index 0000000..d820796
--- /dev/null
@@ -0,0 +1,85 @@
+// OpenTween - Client of Twitter
+// Copyright (c) 2024 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
+// All rights reserved.
+//
+// This file is part of OpenTween.
+//
+// This program is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3 of the License, or (at your option)
+// any later version.
+//
+// This program is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+// for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program. If not, see <http://www.gnu.org/licenses/>, or write to
+// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
+// Boston, MA 02110-1301, USA.
+
+using System;
+using Xunit;
+
+namespace OpenTween.SocialProtocol.Twitter
+{
+    public class TwitterAccountTest
+    {
+        [Fact]
+        public void Initialize_Test()
+        {
+            var accountKey = Guid.NewGuid();
+            using var account = new TwitterAccount(accountKey);
+
+            var accountSettings = new UserAccount
+            {
+                UniqueKey = accountKey,
+                TwitterAuthType = APIAuthType.OAuth1,
+                Token = "aaaaa",
+                TokenSecret = "aaaaa",
+                UserId = 11111L,
+                Username = "tetete",
+            };
+            var settingCommon = new SettingCommon();
+            account.Initialize(accountSettings, settingCommon);
+            Assert.Equal(11111L, account.UserId);
+            Assert.Equal("tetete", account.UserName);
+            Assert.Equal(APIAuthType.OAuth1, account.AuthType);
+            Assert.Same(account.Legacy.Api.Connection, account.Connection);
+        }
+
+        [Fact]
+        public void Initialize_ReconfigureTest()
+        {
+            var accountKey = Guid.NewGuid();
+            using var account = new TwitterAccount(accountKey);
+
+            var accountSettings1 = new UserAccount
+            {
+                UniqueKey = accountKey,
+                TwitterAuthType = APIAuthType.OAuth1,
+                Token = "aaaaa",
+                TokenSecret = "aaaaa",
+                UserId = 11111L,
+                Username = "tetete",
+            };
+            var settingCommon1 = new SettingCommon();
+            account.Initialize(accountSettings1, settingCommon1);
+            Assert.Equal(11111L, account.UserId);
+
+            var accountSettings2 = new UserAccount
+            {
+                UniqueKey = accountKey,
+                TwitterAuthType = APIAuthType.OAuth1,
+                Token = "bbbbb",
+                TokenSecret = "bbbbb",
+                UserId = 22222L,
+                Username = "hoge",
+            };
+            var settingCommon2 = new SettingCommon();
+            account.Initialize(accountSettings2, settingCommon2);
+            Assert.Equal(22222L, account.UserId);
+        }
+    }
+}
index 57789b9..9eb8b8b 100644 (file)
@@ -32,6 +32,7 @@ using System.Windows.Forms;
 using OpenTween.Connection;
 using OpenTween.Setting;
 using OpenTween.SocialProtocol;
+using OpenTween.SocialProtocol.Twitter;
 
 namespace OpenTween
 {
@@ -148,7 +149,7 @@ namespace OpenTween
             // ここが Twitter API への最初のアクセスになるようにすること
             try
             {
-                accounts.Primary.VerifyCredentials();
+                ((TwitterAccount)accounts.Primary).Legacy.VerifyCredentials();
             }
             catch (WebApiException ex)
             {
index 03b15ad..6693bf2 100644 (file)
 using System;
 using System.Collections.Generic;
 using System.Linq;
+using OpenTween.SocialProtocol.Twitter;
 
 namespace OpenTween.SocialProtocol
 {
     public sealed class AccountCollection : IDisposable
     {
-        private Dictionary<Guid, Twitter> accounts = new();
+        private Dictionary<Guid, ISocialAccount> accounts = new();
         private Guid? primaryId;
-        private readonly Twitter emptyAccount = new(new());
+        private readonly ISocialAccount emptyAccount = new TwitterAccount(Guid.Empty);
 
         public bool IsDisposed { get; private set; }
 
-        public Twitter Primary
+        public ISocialAccount Primary
             => this.primaryId != null ? this.accounts[this.primaryId.Value] : this.emptyAccount;
 
-        public Twitter[] Items
+        public ISocialAccount[] Items
             => this.accounts.Values.ToArray();
 
         public void LoadFromSettings(SettingCommon settingCommon)
         {
             var oldAccounts = this.accounts;
-            var newAccounts = new Dictionary<Guid, Twitter>();
+            var newAccounts = new Dictionary<Guid, ISocialAccount>();
 
             foreach (var accountSettings in settingCommon.UserAccounts)
             {
                 var accountKey = accountSettings.UniqueKey;
                 if (!oldAccounts.TryGetValue(accountKey, out var account))
-                    account = new(new());
-
-                var credential = accountSettings.GetTwitterCredential();
-                account.Initialize(credential, accountSettings.Username, accountSettings.UserId);
-
-                account.RestrictFavCheck = settingCommon.RestrictFavCheck;
-                account.ReadOwnPost = settingCommon.ReadOwnPost;
+                    account = new TwitterAccount(accountKey);
 
+                account.Initialize(accountSettings, settingCommon);
                 newAccounts[accountKey] = account;
             }
 
@@ -82,7 +78,7 @@ namespace OpenTween.SocialProtocol
             this.IsDisposed = true;
         }
 
-        private void DisposeAccounts(IEnumerable<Twitter> accounts)
+        private void DisposeAccounts(IEnumerable<ISocialAccount> accounts)
         {
             foreach (var account in accounts)
                 account.Dispose();
diff --git a/OpenTween/SocialProtocol/ISocialAccount.cs b/OpenTween/SocialProtocol/ISocialAccount.cs
new file mode 100644 (file)
index 0000000..645a5c0
--- /dev/null
@@ -0,0 +1,41 @@
+// OpenTween - Client of Twitter
+// Copyright (c) 2024 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
+// All rights reserved.
+//
+// This file is part of OpenTween.
+//
+// This program is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3 of the License, or (at your option)
+// any later version.
+//
+// This program is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+// for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program. If not, see <http://www.gnu.org/licenses/>, or write to
+// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
+// Boston, MA 02110-1301, USA.
+
+#nullable enable
+
+using System;
+using OpenTween.Connection;
+
+namespace OpenTween.SocialProtocol
+{
+    public interface ISocialAccount : IDisposable
+    {
+        public long UserId { get; }
+
+        public string UserName { get; }
+
+        public IApiConnection Connection { get; }
+
+        public bool IsDisposed { get; }
+
+        public void Initialize(UserAccount accountSettings, SettingCommon settingCommon);
+    }
+}
diff --git a/OpenTween/SocialProtocol/Twitter/TwitterAccount.cs b/OpenTween/SocialProtocol/Twitter/TwitterAccount.cs
new file mode 100644 (file)
index 0000000..b2bade4
--- /dev/null
@@ -0,0 +1,75 @@
+// OpenTween - Client of Twitter
+// Copyright (c) 2024 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
+// All rights reserved.
+//
+// This file is part of OpenTween.
+//
+// This program is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3 of the License, or (at your option)
+// any later version.
+//
+// This program is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+// for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program. If not, see <http://www.gnu.org/licenses/>, or write to
+// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
+// Boston, MA 02110-1301, USA.
+
+#nullable enable
+
+using System;
+using System.Diagnostics;
+using OpenTween.Connection;
+
+namespace OpenTween.SocialProtocol.Twitter
+{
+    public class TwitterAccount : ISocialAccount
+    {
+        private readonly OpenTween.Twitter twLegacy = new(new());
+
+        public Guid UniqueKey { get; }
+
+        public bool IsDisposed { get; private set; }
+
+        public OpenTween.Twitter Legacy
+            => this.twLegacy;
+
+        public long UserId
+            => this.Legacy.UserId;
+
+        public string UserName
+            => this.Legacy.Username;
+
+        public APIAuthType AuthType
+            => this.Legacy.Api.AuthType;
+
+        public IApiConnection Connection
+            => this.Legacy.Api.Connection;
+
+        public TwitterAccount(Guid uniqueKey)
+            => this.UniqueKey = uniqueKey;
+
+        public void Initialize(UserAccount accountSettings, SettingCommon settingCommon)
+        {
+            Debug.Assert(accountSettings.UniqueKey == this.UniqueKey, "UniqueKey must be same as current value.");
+
+            var credential = accountSettings.GetTwitterCredential();
+            this.twLegacy.Initialize(credential, accountSettings.Username, accountSettings.UserId);
+            this.twLegacy.RestrictFavCheck = settingCommon.RestrictFavCheck;
+            this.twLegacy.ReadOwnPost = settingCommon.ReadOwnPost;
+        }
+
+        public void Dispose()
+        {
+            if (this.IsDisposed)
+                return;
+
+            this.twLegacy.Dispose();
+            this.IsDisposed = true;
+        }
+    }
+}
index f5dfca7..93c423c 100644 (file)
@@ -60,6 +60,7 @@ using OpenTween.Models;
 using OpenTween.OpenTweenCustomControl;
 using OpenTween.Setting;
 using OpenTween.SocialProtocol;
+using OpenTween.SocialProtocol.Twitter;
 using OpenTween.Thumbnail;
 
 namespace OpenTween
@@ -113,7 +114,7 @@ namespace OpenTween
         private readonly AccountCollection accounts;
 
 #pragma warning disable SA1300
-        private Twitter tw => this.accounts.Primary; // AccountCollection への移行用
+        private Twitter tw => ((TwitterAccount)this.accounts.Primary).Legacy; // AccountCollection への移行用
 #pragma warning restore SA1300
 
         // Growl呼び出し部