Assert.IsType<TwitterGraphqlQuery>(account.Query);
}
+
+ [Fact]
+ public void Mutation_V1_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.IsType<TwitterV1Mutation>(account.Mutation);
+ }
+
+ [Fact]
+ public void Mutation_Graphql_Test()
+ {
+ var accountKey = Guid.NewGuid();
+ using var account = new TwitterAccount(accountKey);
+
+ var accountSettings = new UserAccount
+ {
+ UniqueKey = accountKey,
+ TwitterAuthType = APIAuthType.TwitterComCookie,
+ TwitterComCookie = "auth_token=foo; ct0=bar",
+ UserId = 11111L,
+ Username = "tetete",
+ };
+ var settingCommon = new SettingCommon();
+ account.Initialize(accountSettings, settingCommon);
+
+ Assert.IsType<TwitterGraphqlMutation>(account.Mutation);
+ }
}
}
public ISocialProtocolQuery Query { get; }
+ public ISocialProtocolMutation Mutation { 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.Threading.Tasks;
+using OpenTween.Models;
+
+namespace OpenTween.SocialProtocol
+{
+ public interface ISocialProtocolMutation
+ {
+ public Task FavoritePost(PostId postId);
+ }
+}
public ISocialProtocolQuery Query { get; private set; }
+ public ISocialProtocolMutation Mutation { get; private set; }
+
public bool IsDisposed { get; private set; }
public TwitterAccountState AccountState { get; private set; } = new();
{
this.UniqueKey = uniqueKey;
this.Query = this.CreateQueryInstance(APIAuthType.None);
+ this.Mutation = this.CreateMutationInstance(APIAuthType.None);
}
public void Initialize(UserAccount accountSettings, SettingCommon settingCommon)
var credential = accountSettings.GetTwitterCredential();
this.AccountState = new TwitterAccountState(accountSettings.UserId, accountSettings.Username);
this.Query = this.CreateQueryInstance(credential.AuthType);
+ this.Mutation = this.CreateMutationInstance(credential.AuthType);
this.twLegacy.Initialize(credential, this.AccountState);
this.twLegacy.RestrictFavCheck = settingCommon.RestrictFavCheck;
_ => new TwitterV1Query(this),
};
}
+
+ private ISocialProtocolMutation CreateMutationInstance(APIAuthType authType)
+ {
+ return authType switch
+ {
+ APIAuthType.TwitterComCookie => new TwitterGraphqlMutation(this),
+ _ => new TwitterV1Mutation(this),
+ };
+ }
}
}
--- /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.Threading.Tasks;
+using OpenTween.Api.GraphQL;
+using OpenTween.Models;
+
+namespace OpenTween.SocialProtocol.Twitter
+{
+ public class TwitterGraphqlMutation : ISocialProtocolMutation
+ {
+ private readonly TwitterAccount account;
+
+ public TwitterGraphqlMutation(TwitterAccount account)
+ {
+ this.account = account;
+ }
+
+ public async Task FavoritePost(PostId postId)
+ {
+ var statusId = this.AssertTwitterStatusId(postId);
+ var request = new FavoriteTweetRequest
+ {
+ TweetId = statusId,
+ };
+
+ await request.Send(this.account.Connection)
+ .ConfigureAwait(false);
+ }
+
+ private TwitterStatusId AssertTwitterStatusId(PostId postId)
+ {
+ return postId is TwitterStatusId statusId
+ ? statusId
+ : throw new WebApiException($"Not supported type: {postId.GetType()}");
+ }
+ }
+}
--- /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.Linq;
+using System.Threading.Tasks;
+using OpenTween.Api;
+using OpenTween.Api.DataModel;
+using OpenTween.Connection;
+using OpenTween.Models;
+
+namespace OpenTween.SocialProtocol.Twitter
+{
+ public class TwitterV1Mutation : ISocialProtocolMutation
+ {
+ private readonly TwitterAccount account;
+
+ public TwitterV1Mutation(TwitterAccount account)
+ {
+ this.account = account;
+ }
+
+ public async Task FavoritePost(PostId postId)
+ {
+ var statusId = this.AssertTwitterStatusId(postId);
+
+ try
+ {
+ await this.account.Legacy.Api.FavoritesCreate(statusId)
+ .IgnoreResponse()
+ .ConfigureAwait(false);
+ }
+ catch (TwitterApiException ex)
+ when (ex.Errors.All(x => x.Code == TwitterErrorCode.AlreadyFavorited))
+ {
+ // エラーコード 139 のみの場合は成功と見なす
+ }
+ }
+
+ private TwitterStatusId AssertTwitterStatusId(PostId postId)
+ {
+ return postId is TwitterStatusId statusId
+ ? statusId
+ : throw new WebApiException($"Not supported type: {postId.GetType()}");
+ }
+ }
+}
try
{
- var twitterStatusId = (post.RetweetedId ?? post.StatusId).ToTwitterStatusId();
+ var originalPostId = post.RetweetedId ?? post.StatusId;
- await this.tw.PostFavAdd(twitterStatusId)
+ await this.CurrentTabAccount.Mutation.FavoritePost(originalPostId)
.ConfigureAwait(false);
if (this.settings.Common.RestrictFavCheck)
{
+ var twitterStatusId = originalPostId.ToTwitterStatusId();
var status = await this.tw.Api.StatusesShow(twitterStatusId)
.ConfigureAwait(false);
}
}
- public async Task PostFavAdd(TwitterStatusId statusId)
- {
- if (this.Api.AuthType == APIAuthType.TwitterComCookie)
- {
- var request = new FavoriteTweetRequest
- {
- TweetId = statusId,
- };
-
- await request.Send(this.Api.Connection)
- .ConfigureAwait(false);
- }
- else
- {
- try
- {
- await this.Api.FavoritesCreate(statusId)
- .IgnoreResponse()
- .ConfigureAwait(false);
- }
- catch (TwitterApiException ex)
- when (ex.Errors.All(x => x.Code == TwitterErrorCode.AlreadyFavorited))
- {
- // エラーコード 139 のみの場合は成功と見なす
- }
- }
- }
-
public async Task PostFavRemove(TwitterStatusId statusId)
{
if (this.Api.AuthType == APIAuthType.TwitterComCookie)