Skip to content

Commit

Permalink
[All] feat: implemented delete friend (#697)
Browse files Browse the repository at this point in the history
* [Core] feat: implemented delete friend

* [OneBot] feat: implemented delete friend

* [Core] fix possible null reference
  • Loading branch information
Redmomn authored Nov 28, 2024
1 parent c79f814 commit 22d6b79
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Lagrange.Core/Common/Interface/Api/OperationExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public static Task<bool> RecallFriendMessage(this BotContext bot, MessageChain c
=> bot.ContextCollection.Business.OperationLogic.FetchGroupRequests();

/// <summary>
///
///
/// </summary>
/// <param name="bot"></param>
/// <returns></returns>
Expand Down Expand Up @@ -137,6 +137,9 @@ public static Task<bool> SetCustomStatus(this BotContext bot, uint faceId, strin
public static Task<bool> GroupTransfer(this BotContext bot, uint groupUin, uint targetUin)
=> bot.ContextCollection.Business.OperationLogic.GroupTransfer(groupUin, targetUin);

public static Task<bool> DeleteFriend (this BotContext bot, uint friendUin, bool block)
=>bot.ContextCollection.Business.OperationLogic.DeleteFriend(friendUin, block);

public static Task<bool> RequestFriend(this BotContext bot, uint targetUin, string question = "", string message = "")
=> bot.ContextCollection.Business.OperationLogic.RequestFriend(targetUin, question, message);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,14 @@ public async Task<bool> SetCustomStatus(uint faceId, string text)
return results.Count != 0 && results[0].ResultCode == 0;
}

public async Task<bool> DeleteFriend(uint targetUin, bool block)
{
var uid = await Collection.Business.CachingLogic.ResolveUid(null, targetUin);
var deleteFriendEvent = DeleteFriendEvent.Create(uid, block);
var results = await Collection.Business.SendEvent(deleteFriendEvent);
return results.Count != 0 && results[0].ResultCode == 0;
}

public async Task<bool> RequestFriend(uint targetUin, string question, string message)
{
var requestFriendSearchEvent = RequestFriendSearchEvent.Create(targetUin);
Expand Down
20 changes: 20 additions & 0 deletions Lagrange.Core/Internal/Event/Action/DeleteFriendEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Lagrange.Core.Internal.Event.Action;

internal class DeleteFriendEvent : ProtocolEvent
{
public string? TargetUid { get; set; }

public bool Block { get; set; }

private DeleteFriendEvent(string? targetUid, bool block) : base(true)
{
TargetUid = targetUid;
Block = block;
}

private DeleteFriendEvent(int resultCode) : base(resultCode) { }

public static DeleteFriendEvent Create(string? targetUid, bool block) => new (targetUid, block);

public static DeleteFriendEvent Result(int resultCode) => new (resultCode);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using ProtoBuf;

namespace Lagrange.Core.Internal.Packets.Service.Oidb.Request;

// ReSharper disable InconsistentNaming
#pragma warning disable CS8618

[ProtoContract]
[OidbSvcTrpcTcp(0x126B, 0)]
internal class OidbSvcTrpcTcp0x126B_0
{
[ProtoMember(1)] public OidbSvcTrpcTcp0x126B_0_Field1 Field1 { get; set; } = new();
}

[ProtoContract]
internal class OidbSvcTrpcTcp0x126B_0_Field1
{
[ProtoMember(1)] public string? TargetUid { get; set; }

[ProtoMember(2)] public OidbSvcTrpcTcp0x126B_0_Field1_2 Field2 { get; set; } = new();

[ProtoMember(3)] public bool Block { get; set; }

[ProtoMember(4)] public bool Field4 { get; set; }
}

[ProtoContract]
internal class OidbSvcTrpcTcp0x126B_0_Field1_2
{
[ProtoMember(1)] public uint Field1 { get; set; } = 130;

[ProtoMember(2)] public uint Field2 { get; set; } = 109;

[ProtoMember(3)] public OidbSvcTrpcTcp0x126B_0_Field1_2_3 Field3 { get; set; } = new();
}

[ProtoContract]
internal class OidbSvcTrpcTcp0x126B_0_Field1_2_3
{
[ProtoMember(1)] public uint Field1 { get; set; } = 8;

[ProtoMember(2)] public uint Field2 { get; set; } = 8;

[ProtoMember(3)] public uint Field3 { get; set; } = 50;
}
39 changes: 39 additions & 0 deletions Lagrange.Core/Internal/Service/Action/DeleteFriendService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Lagrange.Core.Common;
using Lagrange.Core.Internal.Event;
using Lagrange.Core.Internal.Event.Action;
using Lagrange.Core.Internal.Packets.Service.Oidb;
using Lagrange.Core.Internal.Packets.Service.Oidb.Request;
using Lagrange.Core.Utility.Extension;
using ProtoBuf;

namespace Lagrange.Core.Internal.Service.Action;

[EventSubscribe(typeof(DeleteFriendEvent))]
[Service("OidbSvcTrpcTcp.0x126b_0")]
internal class DeleteFriendService : BaseService<DeleteFriendEvent>
{
protected override bool Build(DeleteFriendEvent input, BotKeystore keystore, BotAppInfo appInfo,
BotDeviceInfo device, out Span<byte> output,
out List<Memory<byte>>? extraPackets)
{
var packet = new OidbSvcTrpcTcpBase<OidbSvcTrpcTcp0x126B_0>(new OidbSvcTrpcTcp0x126B_0
{
Field1 = new OidbSvcTrpcTcp0x126B_0_Field1 { TargetUid = input.TargetUid, Block = input.Block }
},0x126b, 0, false, false);

output = packet.Serialize();
extraPackets = null;
return true;
}

protected override bool Parse(Span<byte> input, BotKeystore keystore, BotAppInfo appInfo, BotDeviceInfo device,
out DeleteFriendEvent output,
out List<ProtocolEvent>? extraEvents)
{
var payload = Serializer.Deserialize<OidbSvcTrpcTcpBase<byte[]>>(input);

output = DeleteFriendEvent.Result((int)payload.ErrorCode);
extraEvents = null;
return true;
}
}
11 changes: 11 additions & 0 deletions Lagrange.OneBot/Core/Entity/Action/OneBotDeleteFriend.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Text.Json.Serialization;

namespace Lagrange.OneBot.Core.Entity.Action;

[Serializable]
public class OneBotDeleteFriend
{
[JsonPropertyName("user_id")] public uint UserId { get; set; }

[JsonPropertyName("block")] public bool Block { get; set; }
}
24 changes: 24 additions & 0 deletions Lagrange.OneBot/Core/Operation/Generic/DeleteFriendOperation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Text.Json;
using System.Text.Json.Nodes;
using Lagrange.Core;
using Lagrange.Core.Common.Interface.Api;
using Lagrange.OneBot.Core.Entity.Action;
using Lagrange.OneBot.Core.Operation.Converters;

namespace Lagrange.OneBot.Core.Operation.Generic;

[Operation("delete_friend")]
public class DeleteFriendOperation : IOperation
{
public async Task<OneBotResult> HandleOperation(BotContext context, JsonNode? payload)
{
if (payload.Deserialize<OneBotDeleteFriend>(SerializerOptions.DefaultOptions) is {} delete)
{
return await context.DeleteFriend(delete.UserId, delete.Block)
? new OneBotResult(null, 0, "ok")
: new OneBotResult(null, 0, "failed");
}

throw new Exception();
}
}

1 comment on commit 22d6b79

@RTAkland
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome

Please sign in to comment.