Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Twitter.GetHomeTimelineApiメソッドをISocialProtocolQueryの各実装クラスに移動 #327

Merged
merged 4 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@

namespace OpenTween.Api.GraphQL
{
public class TimelineResponseTest
public class TimelineGraphqlResponseTest
{
[Fact]
public async Task ToTwitterStatuses_Test()
{
using var apiResponse = await TestUtils.CreateApiResponse("Resources/Responses/SearchTimeline_SimpleTweet.json");
var tweets = TimelineTweet.ExtractTimelineTweets(await apiResponse.ReadAsJsonXml());
var timelineResponse = new TimelineResponse(tweets, new(CursorType.Top, new("")), new(CursorType.Bottom, new("")));
var timelineResponse = new TimelineGraphqlResponse(tweets, new(CursorType.Top, new("")), new(CursorType.Bottom, new("")));

var statuses = timelineResponse.ToTwitterStatuses();
Assert.Single(statuses);
Expand Down
41 changes: 41 additions & 0 deletions OpenTween.Tests/SocialProtocol/Twitter/TwitterAccountTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,46 @@ public void Initialize_ReconfigureTest()
account.Initialize(accountSettings2, settingCommon2);
Assert.Equal(22222L, account.UserId);
}

[Fact]
public void Query_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<TwitterV1Query>(account.Query);
}

[Fact]
public void Query_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<TwitterGraphqlQuery>(account.Query);
}
}
}
2 changes: 1 addition & 1 deletion OpenTween/Api/GraphQL/HomeLatestTimelineRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public Dictionary<string, string> CreateParameters()
};
}

public async Task<TimelineResponse> Send(IApiConnection apiConnection)
public async Task<TimelineGraphqlResponse> Send(IApiConnection apiConnection)
{
var request = new GetRequest
{
Expand Down
2 changes: 1 addition & 1 deletion OpenTween/Api/GraphQL/LikesRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public Dictionary<string, string> CreateParameters()
};
}

public async Task<TimelineResponse> Send(IApiConnection apiConnection)
public async Task<TimelineGraphqlResponse> Send(IApiConnection apiConnection)
{
var request = new GetRequest
{
Expand Down
2 changes: 1 addition & 1 deletion OpenTween/Api/GraphQL/ListLatestTweetsTimelineRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public Dictionary<string, string> CreateParameters()
};
}

public async Task<TimelineResponse> Send(IApiConnection apiConnection)
public async Task<TimelineGraphqlResponse> Send(IApiConnection apiConnection)
{
var request = new GetRequest
{
Expand Down
2 changes: 1 addition & 1 deletion OpenTween/Api/GraphQL/SearchTimelineRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public Dictionary<string, string> CreateParameters()
};
}

public async Task<TimelineResponse> Send(IApiConnection apiConnection)
public async Task<TimelineGraphqlResponse> Send(IApiConnection apiConnection)
{
var request = new GetRequest
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

namespace OpenTween.Api.GraphQL
{
public record TimelineResponse(
public record TimelineGraphqlResponse(
TimelineTweet[] Tweets,
QueryCursor<TwitterGraphqlCursor>? CursorTop,
QueryCursor<TwitterGraphqlCursor>? CursorBottom
Expand Down
2 changes: 1 addition & 1 deletion OpenTween/Api/GraphQL/TimelineResponseParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace OpenTween.Api.GraphQL
{
public class TimelineResponseParser
{
public static TimelineResponse Parse(XElement rootElm)
public static TimelineGraphqlResponse Parse(XElement rootElm)
{
ErrorResponse.ThrowIfError(rootElm);

Expand Down
2 changes: 1 addition & 1 deletion OpenTween/Api/GraphQL/UserTweetsAndRepliesRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public Dictionary<string, string> CreateParameters()
};
}

public async Task<TimelineResponse> Send(IApiConnection apiConnection)
public async Task<TimelineGraphqlResponse> Send(IApiConnection apiConnection)
{
var request = new GetRequest
{
Expand Down
16 changes: 12 additions & 4 deletions OpenTween/Models/HomeTabModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,30 @@ public override void AddPostQueue(PostClass post)

public override async Task RefreshAsync(ISocialAccount account, bool backward, IProgress<string> progress)
{
if (account is not TwitterAccount twAccount)
throw new ArgumentException($"Invalid account type: {account.GetType()}", nameof(account));

progress.Report(string.Format(Properties.Resources.GetTimelineWorker_RunWorkerCompletedText5, backward ? -1 : 1));

var firstLoad = !this.IsFirstLoadCompleted;
var count = Twitter.GetApiResultCount(MyCommon.WORKERTYPE.Timeline, backward, firstLoad);
var cursor = backward ? this.CursorBottom : this.CursorTop;

await twAccount.Legacy.GetHomeTimelineApi(this, backward, firstLoad)
var response = await account.Query.GetHomeTimeline(count, cursor, firstLoad)
.ConfigureAwait(false);

foreach (var post in response.Posts)
TabInformations.GetInstance().AddPost(post);

// 新着時未読クリア
if (SettingManager.Instance.Common.ReadOldPosts)
TabInformations.GetInstance().SetReadHomeTab();

TabInformations.GetInstance().DistributePosts();

if (response.CursorTop != null && !backward)
this.CursorTop = response.CursorTop;

if (response.CursorBottom != null)
this.CursorBottom = response.CursorBottom;

if (firstLoad)
this.IsFirstLoadCompleted = true;

Expand Down
31 changes: 31 additions & 0 deletions OpenTween/Models/TimelineResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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

namespace OpenTween.Models
{
public record TimelineResponse(
PostClass[] Posts,
IQueryCursor? CursorTop,
IQueryCursor? CursorBottom
);
}
2 changes: 2 additions & 0 deletions OpenTween/SocialProtocol/ISocialAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public interface ISocialAccount : IDisposable

public IApiConnection Connection { get; }

public ISocialProtocolQuery Query { get; }

public bool IsDisposed { get; }

public void Initialize(UserAccount accountSettings, SettingCommon settingCommon);
Expand Down
33 changes: 33 additions & 0 deletions OpenTween/SocialProtocol/ISocialProtocolQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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 ISocialProtocolQuery
{
public Task<TimelineResponse> GetHomeTimeline(int count, IQueryCursor? cursor, bool firstLoad);
}
}
18 changes: 17 additions & 1 deletion OpenTween/SocialProtocol/Twitter/TwitterAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class TwitterAccount : ISocialAccount

public Guid UniqueKey { get; }

public ISocialProtocolQuery Query { get; private set; }

public bool IsDisposed { get; private set; }

public TwitterAccountState AccountState { get; private set; } = new();
Expand All @@ -53,14 +55,19 @@ public IApiConnection Connection
=> this.Legacy.Api.Connection;

public TwitterAccount(Guid uniqueKey)
=> this.UniqueKey = uniqueKey;
{
this.UniqueKey = uniqueKey;
this.Query = this.CreateQueryInstance(APIAuthType.None);
}

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.AccountState = new TwitterAccountState(accountSettings.UserId, accountSettings.Username);
this.Query = this.CreateQueryInstance(credential.AuthType);

this.twLegacy.Initialize(credential, this.AccountState);
this.twLegacy.RestrictFavCheck = settingCommon.RestrictFavCheck;
}
Expand All @@ -73,5 +80,14 @@ public void Dispose()
this.twLegacy.Dispose();
this.IsDisposed = true;
}

private ISocialProtocolQuery CreateQueryInstance(APIAuthType authType)
{
return authType switch
{
APIAuthType.TwitterComCookie => new TwitterGraphqlQuery(this),
_ => new TwitterV1Query(this),
};
}
}
}
68 changes: 68 additions & 0 deletions OpenTween/SocialProtocol/Twitter/TwitterGraphqlQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// OpenTween - Client of Twitter
// Copyright (c) 2007-2011 kiri_feather (@kiri_feather) <[email protected]>
// (c) 2008-2011 Moz (@syo68k)
// (c) 2008-2011 takeshik (@takeshik) <http://www.takeshik.org/>
// (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
// (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
// (c) 2011 Egtra (@egtra) <http://dev.activebasic.com/egtra/>
// (c) 2013 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 TwitterGraphqlQuery : ISocialProtocolQuery
{
private readonly TwitterAccount account;

public TwitterGraphqlQuery(TwitterAccount account)
{
this.account = account;
}

public async Task<TimelineResponse> GetHomeTimeline(int count, IQueryCursor? cursor, bool firstLoad)
{
this.account.Legacy.CheckAccountState();

var request = new HomeLatestTimelineRequest
{
Count = count,
Cursor = cursor?.As<TwitterGraphqlCursor>(),
};

var response = await request.Send(this.account.Connection)
.ConfigureAwait(false);

var statuses = response.ToTwitterStatuses();
var cursorTop = response.CursorTop;
var cursorBottom = response.CursorBottom;

var posts = this.account.Legacy.CreatePostsFromJson(statuses, firstLoad);
posts = this.account.Legacy.FilterNoRetweetUserPosts(posts);

return new(posts, cursorTop, cursorBottom);
}
}
}
Loading
Loading