Skip to content

Commit

Permalink
Merge pull request #212 from opentween/graphql
Browse files Browse the repository at this point in the history
graphqlエンドポイントを使用したリストのタイムライン取得に対応
  • Loading branch information
upsilon authored Jul 4, 2023
2 parents d42985a + 5232e94 commit 8a65a05
Show file tree
Hide file tree
Showing 12 changed files with 3,779 additions and 1 deletion.
66 changes: 66 additions & 0 deletions OpenTween.Tests/Api/GraphQL/ListLatestTweetsTimelineRequestTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// OpenTween - Client of Twitter
// Copyright (c) 2023 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 System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Moq;
using OpenTween.Api.TwitterV2;
using OpenTween.Connection;
using Xunit;

namespace OpenTween.Api.GraphQL
{
public class ListLatestTweetsTimelineRequestTest
{
[Fact]
public async Task Send_Test()
{
using var responseStream = File.OpenRead("Resources/Responses/ListLatestTweetsTimeline_SimpleTweet.json");

var mock = new Mock<IApiConnection>();
mock.Setup(x =>
x.GetStreamAsync(It.IsAny<Uri>(), It.IsAny<IDictionary<string, string>>())
)
.Callback<Uri, IDictionary<string, string>>((url, param) =>
{
Assert.Equal(new("https://twitter.com/i/api/graphql/6ClPnsuzQJ1p7-g32GQw9Q/ListLatestTweetsTimeline"), url);
Assert.Equal(2, param.Count);
Assert.Equal("""{"listId":"1675863884757110790","count":20}""", param["variables"]);
Assert.True(param.ContainsKey("features"));
})
.ReturnsAsync(responseStream);

var request = new ListLatestTweetsTimelineRequest(listId: "1675863884757110790")
{
Count = 20,
};

var tweets = await request.Send(mock.Object).ConfigureAwait(false);
Assert.Single(tweets);

mock.VerifyAll();
}
}
}
114 changes: 114 additions & 0 deletions OpenTween.Tests/Api/GraphQL/TimelineTweetTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// OpenTween - Client of Twitter
// Copyright (c) 2023 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 System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using OpenTween.Models;
using Xunit;

namespace OpenTween.Api.GraphQL
{
public class TimelineTweetTest
{
private XElement LoadResponseDocument(string filename)
{
using var stream = File.OpenRead($"Resources/Responses/{filename}");
using var jsonReader = JsonReaderWriterFactory.CreateJsonReader(stream, XmlDictionaryReaderQuotas.Max);
return XElement.Load(jsonReader);
}

private TabInformations CreateTabInfo()
{
var tabinfo = new TabInformations();
tabinfo.AddDefaultTabs();
return tabinfo;
}

[Fact]
public void ExtractTimelineTweets_Single_Test()
{
var rootElm = this.LoadResponseDocument("ListLatestTweetsTimeline_SimpleTweet.json");
var timelineTweets = TimelineTweet.ExtractTimelineTweets(rootElm);
Assert.Single(timelineTweets);
}

[Fact]
public void ExtractTimelineTweets_Conversation_Test()
{
var rootElm = this.LoadResponseDocument("ListLatestTweetsTimeline_Conversation.json");
var timelineTweets = TimelineTweet.ExtractTimelineTweets(rootElm);
Assert.Equal(3, timelineTweets.Length);
}

[Fact]
public void ToStatus_WithTwitterPostFactory_SimpleTweet_Test()
{
var rootElm = this.LoadResponseDocument("TimelineTweet_SimpleTweet.json");
var timelineTweet = new TimelineTweet(rootElm);
var status = timelineTweet.ToTwitterStatus();
var postFactory = new TwitterPostFactory(this.CreateTabInfo());
var post = postFactory.CreateFromStatus(status, selfUserId: 1L, new HashSet<long>());

Assert.Equal("1613784711020826626", post.StatusId.Id);
Assert.Equal(40480664L, post.UserId);
}

[Fact]
public void ToStatus_WithTwitterPostFactory_TweetWithMedia_Test()
{
var rootElm = this.LoadResponseDocument("TimelineTweet_TweetWithMedia.json");
var timelineTweet = new TimelineTweet(rootElm);
var status = timelineTweet.ToTwitterStatus();
var postFactory = new TwitterPostFactory(this.CreateTabInfo());
var post = postFactory.CreateFromStatus(status, selfUserId: 1L, new HashSet<long>());

Assert.Equal("1614587968567783424", post.StatusId.Id);
Assert.Equal(40480664L, post.UserId);
Assert.Equal(2, post.Media.Count);
Assert.Equal("https://pbs.twimg.com/media/FmgrJiEaAAEU42G.png", post.Media[0].Url);
Assert.Equal("OpenTweenで @opentween のツイート一覧を表示しているスクショ", post.Media[0].AltText);
Assert.Equal("https://pbs.twimg.com/media/FmgrJiXaMAEu873.jpg", post.Media[1].Url);
Assert.Equal("OpenTweenの新しい画像投稿画面を動かしている様子のスクショ", post.Media[1].AltText);
}

[Fact]
public void ToStatus_WithTwitterPostFactory_RetweetedTweet_Test()
{
var rootElm = this.LoadResponseDocument("TimelineTweet_RetweetedTweet.json");
var timelineTweet = new TimelineTweet(rootElm);
var status = timelineTweet.ToTwitterStatus();
var postFactory = new TwitterPostFactory(this.CreateTabInfo());
var post = postFactory.CreateFromStatus(status, selfUserId: 1L, new HashSet<long>());

Assert.Equal("1617128268548964354", post.StatusId.Id);
Assert.Equal(40480664L, post.RetweetedByUserId);
Assert.Equal("1617126084138659840", post.RetweetedId!.Id);
Assert.Equal(514241801L, post.UserId);
}
}
}
15 changes: 15 additions & 0 deletions OpenTween.Tests/OpenTween.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,20 @@
<None Update="Resources\re1.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\Responses\TimelineTweet_RetweetedTweet.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\Responses\TimelineTweet_TweetWithMedia.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\Responses\TimelineTweet_SimpleTweet.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\Responses\ListLatestTweetsTimeline_Conversation.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\Responses\ListLatestTweetsTimeline_SimpleTweet.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
Loading

0 comments on commit 8a65a05

Please sign in to comment.