Skip to content

Commit

Permalink
Twitter.GetHomeTimelineApiメソッドをISocialProtocolQueryの各実装クラスに移動
Browse files Browse the repository at this point in the history
  • Loading branch information
upsilon committed May 7, 2024
1 parent 28a8ca6 commit 20f9672
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 59 deletions.
5 changes: 1 addition & 4 deletions OpenTween/Models/HomeTabModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,13 @@ 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;

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

foreach (var post in response.Posts)
Expand Down
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),
};
}
}
}
62 changes: 62 additions & 0 deletions OpenTween/SocialProtocol/Twitter/TwitterGraphqlQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// 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 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);
}
}
}
72 changes: 72 additions & 0 deletions OpenTween/SocialProtocol/Twitter/TwitterV1Query.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// 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.GraphQL;
using OpenTween.Models;

namespace OpenTween.SocialProtocol.Twitter
{
public class TwitterV1Query : ISocialProtocolQuery
{
private readonly TwitterAccount account;

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

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

TwitterStatusId? maxId = null, sinceId = null;

if (cursor is QueryCursor<TwitterStatusId> statusIdCursor)
{
if (statusIdCursor.Type == CursorType.Top)
sinceId = statusIdCursor.Value;
else if (statusIdCursor.Type == CursorType.Bottom)
maxId = statusIdCursor.Value;
}

var statuses = await this.account.Legacy.Api.StatusesHomeTimeline(count, maxId, sinceId)
.ConfigureAwait(false);

IQueryCursor? cursorTop = null, cursorBottom = null;

if (statuses.Length > 0)
{
var (min, max) = statuses.Select(x => new TwitterStatusId(x.IdStr)).MinMax();
cursorTop = new QueryCursor<TwitterStatusId>(CursorType.Top, max);
cursorBottom = new QueryCursor<TwitterStatusId>(CursorType.Bottom, min);
}

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

return new(posts, cursorTop, cursorBottom);
}
}
}
57 changes: 3 additions & 54 deletions OpenTween/Twitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -623,57 +623,6 @@ public static int GetApiResultCount(MyCommon.WORKERTYPE type, bool more, bool fi
return Math.Min(count, GetMaxApiResultCount(type));
}

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

TwitterStatus[] statuses;
IQueryCursor? cursorTop = null, cursorBottom = null;

if (this.Api.AuthType == APIAuthType.TwitterComCookie)
{
var request = new HomeLatestTimelineRequest
{
Count = count,
Cursor = cursor?.As<TwitterGraphqlCursor>(),
};
var response = await request.Send(this.Api.Connection)
.ConfigureAwait(false);

statuses = response.ToTwitterStatuses();

cursorTop = response.CursorTop;
cursorBottom = response.CursorBottom;
}
else
{
TwitterStatusId? maxId = null, sinceId = null;

if (cursor is QueryCursor<TwitterStatusId> statusIdCursor)
{
if (statusIdCursor.Type == CursorType.Top)
sinceId = statusIdCursor.Value;
else if (statusIdCursor.Type == CursorType.Bottom)
maxId = statusIdCursor.Value;
}

statuses = await this.Api.StatusesHomeTimeline(count, maxId, sinceId)
.ConfigureAwait(false);

if (statuses.Length > 0)
{
var (min, max) = statuses.Select(x => new TwitterStatusId(x.IdStr)).MinMax();
cursorTop = new QueryCursor<TwitterStatusId>(CursorType.Top, max);
cursorBottom = new QueryCursor<TwitterStatusId>(CursorType.Bottom, min);
}
}

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

return new(posts, cursorTop, cursorBottom);
}

public async Task GetMentionsTimelineApi(MentionsTabModel tab, bool more, bool firstLoad)
{
this.CheckAccountState();
Expand Down Expand Up @@ -815,7 +764,7 @@ private PostClass CreatePostsFromStatusData(TwitterStatus status, bool firstLoad
return post;
}

private PostClass[] CreatePostsFromJson(TwitterStatus[] statuses, bool firstLoad)
internal PostClass[] CreatePostsFromJson(TwitterStatus[] statuses, bool firstLoad)
{
var posts = statuses.Select(x => this.CreatePostsFromStatusData(x, firstLoad)).ToArray();

Expand All @@ -824,7 +773,7 @@ private PostClass[] CreatePostsFromJson(TwitterStatus[] statuses, bool firstLoad
return posts;
}

private PostClass[] FilterNoRetweetUserPosts(PostClass[] posts)
internal PostClass[] FilterNoRetweetUserPosts(PostClass[] posts)
=> posts.Where(x => x.RetweetedByUserId == null || !this.Api.AccountState.NoRetweetUserIds.Contains(x.RetweetedByUserId.Value)).ToArray();

public async Task GetListStatus(ListTimelineTabModel tab, bool more, bool firstLoad)
Expand Down Expand Up @@ -1445,7 +1394,7 @@ public async Task RefreshMuteUserIdsAsync()
public string[] GetHashList()
=> this.postFactory.GetReceivedHashtags();

private void CheckAccountState()
internal void CheckAccountState()
{
if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
throw new WebApiException("Auth error. Check your account");
Expand Down

0 comments on commit 20f9672

Please sign in to comment.