From 7819342f96f1e27afc9add4e7ecd1266ae1a39bb Mon Sep 17 00:00:00 2001 From: Kimura Youichi Date: Tue, 28 May 2024 08:01:08 +0900 Subject: [PATCH] =?utf8?q?MisskeyClient.UnretweetPost=E3=83=A1=E3=82=BD?= =?utf8?q?=E3=83=83=E3=83=89=E3=82=92=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../Api/Misskey/NoteUnrenoteRequestTest.cs | 58 ++++++++++++++++++++ OpenTween/Api/Misskey/NoteUnrenoteRequest.cs | 63 ++++++++++++++++++++++ OpenTween/SocialProtocol/Misskey/MisskeyClient.cs | 12 ++++- 3 files changed, 131 insertions(+), 2 deletions(-) create mode 100644 OpenTween.Tests/Api/Misskey/NoteUnrenoteRequestTest.cs create mode 100644 OpenTween/Api/Misskey/NoteUnrenoteRequest.cs diff --git a/OpenTween.Tests/Api/Misskey/NoteUnrenoteRequestTest.cs b/OpenTween.Tests/Api/Misskey/NoteUnrenoteRequestTest.cs new file mode 100644 index 00000000..055e4503 --- /dev/null +++ b/OpenTween.Tests/Api/Misskey/NoteUnrenoteRequestTest.cs @@ -0,0 +1,58 @@ +// OpenTween - Client of Twitter +// Copyright (c) 2024 kim_upsilon (@kim_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 , or write to +// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, +// Boston, MA 02110-1301, USA. + +using System; +using System.Threading.Tasks; +using Moq; +using OpenTween.Connection; +using Xunit; + +namespace OpenTween.Api.Misskey +{ + public class NoteUnrenoteRequestTest + { + [Fact] + public async Task Send_Test() + { + var mock = new Mock(); + mock.Setup(x => + x.SendAsync(It.IsAny()) + ) + .Callback(x => + { + var request = Assert.IsType(x); + Assert.Equal(new("notes/unrenote", UriKind.Relative), request.RequestUri); + Assert.Equal( + """{"noteId":"aaaaa"}""", + request.JsonString + ); + }); + + var request = new NoteUnrenoteRequest + { + NoteId = new("aaaaa"), + }; + await request.Send(mock.Object); + + mock.VerifyAll(); + } + } +} diff --git a/OpenTween/Api/Misskey/NoteUnrenoteRequest.cs b/OpenTween/Api/Misskey/NoteUnrenoteRequest.cs new file mode 100644 index 00000000..4502800c --- /dev/null +++ b/OpenTween/Api/Misskey/NoteUnrenoteRequest.cs @@ -0,0 +1,63 @@ +// OpenTween - Client of Twitter +// Copyright (c) 2024 kim_upsilon (@kim_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 , or write to +// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, +// Boston, MA 02110-1301, USA. + +#nullable enable + +using System; +using System.Runtime.Serialization; +using System.Threading.Tasks; +using OpenTween.Connection; +using OpenTween.SocialProtocol.Misskey; + +namespace OpenTween.Api.Misskey +{ + public class NoteUnrenoteRequest + { + public required MisskeyNoteId NoteId { get; set; } + + public async Task Send(IApiConnection apiConnection) + { + var request = new PostJsonRequest + { + RequestUri = new("notes/unrenote", UriKind.Relative), + JsonString = this.CreateRequestJson(), + }; + + using var response = await apiConnection.SendAsync(request) + .ConfigureAwait(false); + } + + [DataContract] + private record RequestBody( + [property: DataMember(Name = "noteId")] + string NoteId + ); + + private string CreateRequestJson() + { + var body = new RequestBody( + NoteId: this.NoteId.Id + ); + + return JsonUtils.SerializeJsonByDataContract(body); + } + } +} diff --git a/OpenTween/SocialProtocol/Misskey/MisskeyClient.cs b/OpenTween/SocialProtocol/Misskey/MisskeyClient.cs index 923fb24f..a0be942b 100644 --- a/OpenTween/SocialProtocol/Misskey/MisskeyClient.cs +++ b/OpenTween/SocialProtocol/Misskey/MisskeyClient.cs @@ -154,8 +154,16 @@ namespace OpenTween.SocialProtocol.Misskey return post; } - public Task UnretweetPost(PostId postId) - => throw this.CreateException(); + public async Task UnretweetPost(PostId postId) + { + var request = new NoteUnrenoteRequest + { + NoteId = this.AssertMisskeyNoteId(postId), + }; + + await request.Send(this.account.Connection) + .ConfigureAwait(false); + } public Task RefreshConfiguration() => Task.CompletedTask; -- 2.11.0