Skip to content

Commit

Permalink
Merge branch 'LagrangeDev:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
sisi0318 authored Nov 29, 2024
2 parents d4df713 + 22d6b79 commit 29b7e1f
Show file tree
Hide file tree
Showing 16 changed files with 177 additions and 9 deletions.
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 @@ -109,7 +109,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 @@ -138,6 +138,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 @@ -442,6 +442,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; }
}
3 changes: 2 additions & 1 deletion Lagrange.OneBot/Core/Network/Service/HttpService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private async Task HandleRequestAsync(HttpListenerContext context, CancellationT
if (!MediaTypeHeaderValue.TryParse(request.ContentType, out var mediaType))
{
Log.LogCannotParseMediaType(_logger, request.ContentType ?? string.Empty);
response.StatusCode = (int)HttpStatusCode.UnsupportedMediaType;
response.StatusCode = (int)HttpStatusCode.NotAcceptable;
response.Close();
return;
}
Expand All @@ -150,6 +150,7 @@ private async Task HandleRequestAsync(HttpListenerContext context, CancellationT
var body = await reader.ReadToEndAsync(token);
Log.LogReceived(_logger, identifier, body);
var @params = body.Split('&')
.Where(pair => !string.IsNullOrEmpty(pair))
.Select(pair => pair.Split('=', 2))
.ToDictionary(pair => pair[0], pair => Uri.UnescapeDataString(pair[1]));
payload = JsonSerializer.Serialize(new { action, @params });
Expand Down
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ public async Task<OneBotResult> HandleOperation(BotContext context, JsonNode? pa
);
if (code != 0 || recordEntity == null) return new OneBotResult(null, code, "failed");


var chain = MessageBuilder.Group(message.GroupId).Add(recordEntity).Build();
var result = await context.SendMessage(chain);

if (result.Result != 0) return new OneBotResult(null, (int)result.Result, "failed");
if (result.Sequence == null || result.Sequence == 0) return new OneBotResult(null, 9000, "failed");

int hash = MessageRecord.CalcMessageHash(chain.MessageId, result.Sequence ?? 0);

return new OneBotResult(new OneBotMessageResponse(hash), (int)result.Result, "ok");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public async Task<OneBotResult> HandleOperation(BotContext context, JsonNode? pa
var multi = new MultiMsgEntity(chains);
var chain = MessageBuilder.Group(forward.GroupId).Add(multi).Build();
var ret = await context.SendMessage(chain);

if (ret.Result != 0) return new OneBotResult(null, (int)ret.Result, "failed");
if (ret.Sequence == null || ret.Sequence == 0) return new OneBotResult(null, 9000, "failed");

int hash = MessageRecord.CalcMessageHash(chain.MessageId, ret.Sequence ?? 0);

return new OneBotResult(new OneBotForwardResponse(hash, multi.ResId ?? ""), (int)ret.Result, "ok");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ public async Task<OneBotResult> HandleOperation(BotContext context, JsonNode? pa
OneBotGroupMessageText messageText => common.ParseChain(messageText).Build(),
_ => throw new Exception()
};

var result = await context.SendMessage(chain);

if (result.Result != 0) return new OneBotResult(null, (int)result.Result, "failed");
if (result.Sequence == null || result.Sequence == 0) return new OneBotResult(null, 9000, "failed");

int hash = MessageRecord.CalcMessageHash(chain.MessageId, result.Sequence ?? 0);

return new OneBotResult(new OneBotMessageResponse(hash), (int)result.Result, "ok");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public async Task<OneBotResult> HandleOperation(BotContext context, JsonNode? pa
var result = await context.SendMessage(chain);

if (result.Result != 0) return new OneBotResult(null, (int)result.Result, "failed");
if (result.Sequence == null || result.Sequence == 0) return new OneBotResult(null, 9000, "failed");

int hash = MessageRecord.CalcMessageHash(result.MessageId, result.Sequence ?? 0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public async Task<OneBotResult> HandleOperation(BotContext context, JsonNode? pa
var result = await context.SendMessage(chain);

if (result.Result != 0) return new OneBotResult(null, (int)result.Result, "failed");
if (result.Sequence == null || result.Sequence == 0) return new OneBotResult(null, 9000, "failed");

int hash = MessageRecord.CalcMessageHash(result.MessageId, result.Sequence ?? 0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public async Task<OneBotResult> HandleOperation(BotContext context, JsonNode? pa
var result = await context.SendMessage(chain);

if (result.Result != 0) return new OneBotResult(null, (int)result.Result, "failed");
if (result.Sequence == null || result.Sequence == 0) return new OneBotResult(null, 9000, "failed");

int hash = MessageRecord.CalcMessageHash(result.MessageId, result.Sequence ?? 0);

Expand Down
2 changes: 1 addition & 1 deletion Lagrange.OneBot/LagrangeAppBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public LagrangeAppBuilder ConfigureBots()
{
appInfo = JsonSerializer.Deserialize<BotAppInfo>(File.ReadAllText(appInfoPath)) ?? BotAppInfo.ProtocolToAppInfo[config.Protocol];
}

Services.AddSingleton(BotFactory.Create(config, deviceInfo, keystore, appInfo));

return this;
Expand Down
9 changes: 6 additions & 3 deletions Lagrange.OneBot/Utility/QrCodeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ internal static void Output(string text, bool compatibilityMode)
if (foregroundBlack && !backgroundBlack)
{
Console.Write(bottomHalfBlock);
} else if (!foregroundBlack && backgroundBlack)
}
else if (!foregroundBlack && backgroundBlack)
{
Console.Write(topHalfBlock);
} else if (foregroundBlack && backgroundBlack)
}
else if (foregroundBlack && backgroundBlack)
{
Console.Write(emptyBlock);

} else if (!foregroundBlack && !backgroundBlack)
}
else if (!foregroundBlack && !backgroundBlack)
{
Console.Write(fullBlock);
}
Expand Down

0 comments on commit 29b7e1f

Please sign in to comment.