// Boston, MA 02110-1301, USA.
using System;
+using OpenTween.SocialProtocol.Twitter;
using Xunit;
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);
}
--- /dev/null
+// 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);
+ }
+ }
+}
using OpenTween.Connection;
using OpenTween.Setting;
using OpenTween.SocialProtocol;
+using OpenTween.SocialProtocol.Twitter;
namespace OpenTween
{
// ここが Twitter API への最初のアクセスになるようにすること
try
{
- accounts.Primary.VerifyCredentials();
+ ((TwitterAccount)accounts.Primary).Legacy.VerifyCredentials();
}
catch (WebApiException ex)
{
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;
}
this.IsDisposed = true;
}
- private void DisposeAccounts(IEnumerable<Twitter> accounts)
+ private void DisposeAccounts(IEnumerable<ISocialAccount> accounts)
{
foreach (var account in accounts)
account.Dispose();
--- /dev/null
+// 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);
+ }
+}
--- /dev/null
+// 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;
+ }
+ }
+}
using OpenTween.OpenTweenCustomControl;
using OpenTween.Setting;
using OpenTween.SocialProtocol;
+using OpenTween.SocialProtocol.Twitter;
using OpenTween.Thumbnail;
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呼び出し部