Skip to content

Commit

Permalink
Merge pull request #338 from opentween/account-setup-dialog
Browse files Browse the repository at this point in the history
アカウント追加時にTwitter{Cookie,OAuth}SetupDialogを使用する
  • Loading branch information
upsilon authored May 19, 2024
2 parents e0730fe + cdda913 commit 4fdfb37
Show file tree
Hide file tree
Showing 27 changed files with 1,231 additions and 829 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* NEW: Twemoji 15.1.0 に対応しました
- Unicode 15.1 で追加された絵文字が表示されるようになります
* CHG: 設定画面でのアカウント一覧の表示形式を変更
* CHG: 新規アカウント追加時のダイアログの構成を変更
* CHG: 新規タブの初回に読み込まれた発言を既読状態にする(起動時の初回の読み込みと同じ動作となる)
* CHG: ドメインに x.com が使われている引用ツイートの展開・投稿に対応
* FIX: 発言の削除中にタブを切り替えるとエラーが発生する不具合を修正 (thx @Tan90909090!)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// OpenTween - Client of Twitter
// Copyright (c) 2023 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
// Copyright (c) 2024 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
// All rights reserved.
//
// This file is part of OpenTween.
Expand All @@ -19,21 +19,16 @@
// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
// Boston, MA 02110-1301, USA.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace OpenTween
namespace OpenTween.SocialProtocol.Twitter
{
public class AuthDialogTest
public class TwitterCookieSetupDialogTest
{
[WinFormsFact]
public void Initialize_Test()
{
using var dialog = new AuthDialog();
using var dialog = new TwitterCookieSetupDialog();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// OpenTween - Client of Twitter
// Copyright (c) 2023 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
// Copyright (c) 2024 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
// All rights reserved.
//
// This file is part of OpenTween.
Expand All @@ -19,21 +19,16 @@
// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
// Boston, MA 02110-1301, USA.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace OpenTween
namespace OpenTween.SocialProtocol.Twitter
{
public class AuthTypeSelectDialogTest
public class TwitterOAuthSetupDialogTest
{
[WinFormsFact]
public void Initialize_Test()
{
using var dialog = new AuthTypeSelectDialog();
using var dialog = new TwitterOAuthSetupDialog();
}
}
}
92 changes: 4 additions & 88 deletions OpenTween/AppendSettingDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenTween.Api;
using OpenTween.Connection;
using OpenTween.Models;
using OpenTween.Setting.Panel;
Expand All @@ -47,7 +46,8 @@ public AppendSettingDialog()
{
this.InitializeComponent();

this.BasedPanel.AddAccountButton.Click += this.AddAccountButton_Click;
this.BasedPanel.ApplyNetworkSettings = this.ApplyNetworkSettings;
this.BasedPanel.OpenInBrowser = this.OpenInBrowser;
this.GetPeriodPanel.CheckPostAndGet.CheckedChanged += this.CheckPostAndGet_CheckedChanged;
this.ActionPanel.UReadMng.CheckedChanged += this.UReadMng_CheckedChanged;

Expand Down Expand Up @@ -166,60 +166,6 @@ private void UReadMng_CheckedChanged(object sender, EventArgs e)
}
}

private async void AddAccountButton_Click(object sender, EventArgs e)
{
using (ControlTransaction.Disabled(this.BasedPanel.AddAccountButton))
{
try
{
this.ApplyNetworkSettings();

var appToken = this.SelectAuthType();
if (appToken == null)
return;

UserAccount newAccount;
if (appToken.AuthType == APIAuthType.TwitterComCookie)
{
newAccount = new()
{
TwitterAuthType = appToken.AuthType,
TwitterComCookie = appToken.TwitterComCookie,
};

using var twitterApi = new TwitterApi();
using var apiConnection = new TwitterApiConnection(new TwitterCredentialCookie(appToken), new());
twitterApi.Initialize(apiConnection);
var twitterUser = await twitterApi.AccountVerifyCredentials();
newAccount.UserId = twitterUser.IdStr;
newAccount.Username = twitterUser.ScreenName;
}
else
{
var account = await this.PinAuth(appToken);
if (account == null)
return;
newAccount = account;
}

this.BasedPanel.AddAccount(newAccount);

MessageBox.Show(
this,
Properties.Resources.AuthorizeButton_Click1,
"Authenticate",
MessageBoxButtons.OK);
}
catch (TwitterApiException ex)
{
var message = Properties.Resources.AuthorizeButton_Click2 + Environment.NewLine +
string.Join(Environment.NewLine, ex.LongMessages);

MessageBox.Show(this, message, "Authenticate", MessageBoxButtons.OK);
}
}
}

/// <summary>
/// 現在設定画面に入力されているネットワーク関係の設定を適用します
/// </summary>
Expand Down Expand Up @@ -250,40 +196,10 @@ public void ApplyNetworkSettings()
TwitterApiConnection.RestApiHost = this.ConnectionPanel.TwitterAPIText.Text.Trim();
}

private TwitterAppToken? SelectAuthType()
{
using var dialog = new AuthTypeSelectDialog();

var ret = dialog.ShowDialog(this);
if (ret != DialogResult.OK)
return null;

return dialog.Result;
}

private async Task<UserAccount?> PinAuth(TwitterAppToken appToken)
private async Task OpenInBrowser(IWin32Window? owner, Uri uri)
{
var requestToken = await TwitterApiConnection.GetRequestTokenAsync(appToken);

var pinPageUrl = TwitterApiConnection.GetAuthorizeUri(requestToken);

var browserPath = this.ActionPanel.BrowserPathText.Text;
var pin = AuthDialog.DoAuth(this, pinPageUrl, browserPath);
if (MyCommon.IsNullOrEmpty(pin))
return null; // キャンセルされた場合

var accessTokenResponse = await TwitterApiConnection.GetAccessTokenAsync(requestToken, pin);

return new UserAccount
{
TwitterAuthType = appToken.AuthType,
TwitterOAuth1ConsumerKey = appToken.OAuth1CustomConsumerKey?.Value ?? "",
TwitterOAuth1ConsumerSecret = appToken.OAuth1CustomConsumerSecret?.Value ?? "",
Username = accessTokenResponse["screen_name"],
UserId = accessTokenResponse["user_id"],
Token = accessTokenResponse["oauth_token"],
TokenSecret = accessTokenResponse["oauth_token_secret"],
};
await MyCommon.OpenInBrowserAsync(owner, browserPath, uri);
}

private void CheckPostAndGet_CheckedChanged(object sender, EventArgs e)
Expand Down
140 changes: 0 additions & 140 deletions OpenTween/AuthDialog.Designer.cs

This file was deleted.

Loading

0 comments on commit 4fdfb37

Please sign in to comment.