Skip to content

Commit

Permalink
[Core] supported pin crd
Browse files Browse the repository at this point in the history
Co-authored-by: Decrabbityyy <[email protected]>
  • Loading branch information
DarkRRb and Decrabbityyy committed Nov 16, 2024
1 parent 47b819d commit 843492e
Show file tree
Hide file tree
Showing 18 changed files with 538 additions and 6 deletions.
9 changes: 9 additions & 0 deletions Lagrange.Core/Common/Interface/Api/OperationExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,4 +281,13 @@ public static Task<string> UploadImage(this BotContext bot, ImageEntity entity)

public static Task<(int Code, string ErrMsg, List<AiCharacterList>? Result)> GetAiCharacters(this BotContext bot, uint chatType, uint groupUin = 42)
=> bot.ContextCollection.Business.OperationLogic.GetAiCharacters(chatType, groupUin);

public static Task<(int Retcode, string Message, List<uint> FriendUins, List<uint> GroupUins)> GetPins(this BotContext bot)
=> bot.ContextCollection.Business.OperationLogic.GetPins();

public static Task<(int Retcode, string Message)> SetPinFriend(this BotContext bot, uint uin, bool isPin)
=> bot.ContextCollection.Business.OperationLogic.SetPinFriend(uin, isPin);

public static Task<(int Retcode, string Message)> SetPinGroup(this BotContext bot, uint uin, bool isPin)
=> bot.ContextCollection.Business.OperationLogic.SetPinGroup(uin, isPin);
}
26 changes: 26 additions & 0 deletions Lagrange.Core/Event/EventArg/PinChangedEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace Lagrange.Core.Event.EventArg;

public class PinChangedEvent : EventBase
{
public ChatType Type { get; }

public uint Uin { get; }

public bool IsPin { get; }

public PinChangedEvent(ChatType type, uint uin, bool isPin)
{
Type = type;
Uin = uin;
IsPin = isPin;

EventMessage = $"{nameof(PinChangedEvent)} {{ChatType: {Type} | Uin: {Uin} | IsPin: {IsPin}}}";
}

public enum ChatType
{
Friend,
Group,
Service
}
}
2 changes: 2 additions & 0 deletions Lagrange.Core/Event/EventInvoker.Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,6 @@ public partial class EventInvoker
public event LagrangeEvent<GroupTodoEvent>? OnGroupTodoEvent;

public event LagrangeEvent<GroupMemberEnterEvent>? OnGroupMemberEnterEvent;

public event LagrangeEvent<PinChangedEvent>? OnPinChangedEvent;
}
2 changes: 1 addition & 1 deletion Lagrange.Core/Event/EventInvoker.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Runtime.CompilerServices;
using Lagrange.Core.Event.EventArg;
using Lagrange.Core.Internal.Event.Notify;

namespace Lagrange.Core.Event;

Expand Down Expand Up @@ -43,6 +42,7 @@ internal EventInvoker(BotContext context)
RegisterEvent((GroupNameChangeEvent e) => OnGroupNameChangeEvent?.Invoke(context, e));
RegisterEvent((GroupTodoEvent e) => OnGroupTodoEvent?.Invoke(context, e));
RegisterEvent((GroupMemberEnterEvent e) => OnGroupMemberEnterEvent?.Invoke(context, e));
RegisterEvent((PinChangedEvent e) => OnPinChangedEvent?.Invoke(context, e));
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using Lagrange.Core.Message;
using Lagrange.Core.Message.Entity;
using Lagrange.Core.Message.Filter;
using Lagrange.Core.Utility.Extension;
using FriendPokeEvent = Lagrange.Core.Event.EventArg.FriendPokeEvent;
using GroupPokeEvent = Lagrange.Core.Event.EventArg.GroupPokeEvent;

Expand Down Expand Up @@ -41,6 +40,9 @@ namespace Lagrange.Core.Internal.Context.Logic.Implementation;
[EventSubscribe(typeof(LoginNotifyEvent))]
[EventSubscribe(typeof(MultiMsgDownloadEvent))]
[EventSubscribe(typeof(GroupSysTodoEvent))]
[EventSubscribe(typeof(SysPinChangedEvent))]
[EventSubscribe(typeof(FetchPinsEvent))]
[EventSubscribe(typeof(SetPinFriendEvent))]
[BusinessLogic("MessagingLogic", "Manage the receiving and sending of messages and notifications")]
internal class MessagingLogic : LogicBase
{
Expand Down Expand Up @@ -251,6 +253,26 @@ public override async Task Incoming(ProtocolEvent e)
Collection.Invoker.PostEvent(new GroupTodoEvent(todo.GroupUin, uin));
break;
}
case SysPinChangedEvent pin:
{
uint uin = pin.GroupUin ?? await Collection.Business.CachingLogic.ResolveUin(null, pin.Uid) ?? 0;

Collection.Invoker.PostEvent(new PinChangedEvent(
pin.GroupUin == null ? PinChangedEvent.ChatType.Friend : PinChangedEvent.ChatType.Group,
uin,
pin.IsPin
));
break;
}
case FetchPinsEvent pins:
{
foreach (var friendUid in pins.FriendUids)
{
pins.FriendUins.Add(await Collection.Business.CachingLogic.ResolveUin(null, friendUid) ?? 0);
}

break;
}
}
}

Expand All @@ -275,6 +297,13 @@ public override async Task Outgoing(ProtocolEvent e)
await Collection.Highway.UploadResources(send.Chain);
break;
}
case SetPinFriendEvent pinFriend: // resolve Uin to Uid
{
pinFriend.Uid = await Collection.Business.CachingLogic.ResolveUid(null, pinFriend.Uin)
?? throw new Exception();

break;
}
}
}

Expand Down Expand Up @@ -396,7 +425,6 @@ private async Task ResolveOutgoingChain(MessageChain chain)
face.SysFaceEntry ??= await cache.GetCachedFaceEntity(face.FaceId);
break;
}

case ForwardEntity forward when forward.TargetUin != 0:
{
var cache = Collection.Business.CachingLogic;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -798,4 +798,46 @@ public async Task<string> UploadImage(ImageEntity image)
var imageUrl = await UploadImage(image);
return await ImageOcr(imageUrl);
}

public async Task<(int Retcode, string Message, List<uint> FriendUins, List<uint> GroupUins)> GetPins()
{
var @event = FetchPinsEvent.Create();

var results = await Collection.Business.SendEvent(@event);
if (results.Count == 0)
{
return (-1, "No Result", new(), new());
}

var result = (FetchPinsEvent)results[0];
return (result.ResultCode, result.Message, result.FriendUins, result.GroupUins);
}

public async Task<(int Retcode, string Message)> SetPinFriend(uint uin, bool isPin)
{
var @event = SetPinFriendEvent.Create(uin, isPin);

var results = await Collection.Business.SendEvent(@event);
if (results.Count == 0)
{
return (-1, "No Result");
}

var result = (SetPinFriendEvent)results[0];
return (result.ResultCode, result.Message);
}

public async Task<(int Retcode, string Message)> SetPinGroup(uint uin, bool isPin)
{
var @event = SetPinGroupEvent.Create(uin, isPin);

var results = await Collection.Business.SendEvent(@event);
if (results.Count == 0)
{
return (-1, "No Result");
}

var result = (SetPinGroupEvent)results[0];
return (result.ResultCode, result.Message);
}
}
42 changes: 42 additions & 0 deletions Lagrange.Core/Internal/Event/Action/FetchPinsEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
namespace Lagrange.Core.Internal.Event.Action;

internal class FetchPinsEvent : ProtocolEvent
{
internal List<string> FriendUids { get; set; }

public List<uint> FriendUins { get; set; }

public List<uint> GroupUins { get; set; }

public string Message { get; set; }

protected FetchPinsEvent() : base(true)
{
FriendUids = new();
FriendUins = new();
GroupUins = new();
Message = string.Empty;
}

protected FetchPinsEvent(List<string> friendUids, List<uint> groupUins) : base(0)
{
FriendUids = friendUids;
FriendUins = new();
GroupUins = groupUins;
Message = string.Empty;
}

protected FetchPinsEvent(int retcode, string message) : base(retcode)
{
FriendUids = new();
FriendUins = new();
GroupUins = new();
Message = string.Empty;
}

public static FetchPinsEvent Create() => new();

public static FetchPinsEvent Result(List<string> friendUids, List<uint> groupUins) => new(friendUids, groupUins);

public static FetchPinsEvent Result(int retcode, string message) => new(retcode, message);
}
31 changes: 31 additions & 0 deletions Lagrange.Core/Internal/Event/Action/SetPinFriendEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace Lagrange.Core.Internal.Event.Action;

internal class SetPinFriendEvent : ProtocolEvent
{
internal string Uid { get; set; }

public uint Uin { get; set; }

public bool IsPin { get; set; }

public string Message { get; set; }

protected SetPinFriendEvent(uint uin, bool isPin) : base(true)
{
Uid = string.Empty;
Uin = uin;
Message = string.Empty;
IsPin = isPin;
}

protected SetPinFriendEvent(int retcode, string message) : base(retcode)
{
Uid = string.Empty;
Uin = 0;
Message = message;
}

public static SetPinFriendEvent Create(uint uin, bool isPin) => new(uin, isPin);

public static SetPinFriendEvent Result(int retcode, string message) => new(retcode, message);
}
27 changes: 27 additions & 0 deletions Lagrange.Core/Internal/Event/Action/SetPinGroupEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace Lagrange.Core.Internal.Event.Action;

internal class SetPinGroupEvent : ProtocolEvent
{
public uint Uin { get; set; }

public bool IsPin { get; set; }

public string Message { get; set; }

protected SetPinGroupEvent(uint uin, bool isPin) : base(true)
{
Uin = uin;
Message = string.Empty;
IsPin = isPin;
}

protected SetPinGroupEvent(int retcode, string message) : base(retcode)
{
Uin = 0;
Message = message;
}

public static SetPinGroupEvent Create(uint uin, bool isPin) => new(uin, isPin);

public static SetPinGroupEvent Result(int retcode, string message) => new(retcode, message);
}
19 changes: 19 additions & 0 deletions Lagrange.Core/Internal/Event/Notify/SysPinChangedEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace Lagrange.Core.Internal.Event.Notify;

internal class SysPinChangedEvent : ProtocolEvent
{
public string Uid { get; }

public uint? GroupUin { get; }

public bool IsPin { get; }

private SysPinChangedEvent(string uid, uint? groupUin, bool isPin) : base(0)
{
Uid = uid;
GroupUin = groupUin;
IsPin = isPin;
}

public static SysPinChangedEvent Result(string uid, uint? groupUin, bool isPin) => new(uid, groupUin, isPin);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using ProtoBuf;

namespace Lagrange.Core.Internal.Packets.Message.Notify;

#pragma warning disable CS8618

// Stupid TX
// TODO: Currently only supports PinChanged
[ProtoContract]
internal class FriendDeleteOrPinChanged
{
[ProtoMember(1)] public FriendDeleteOrPinChangedBody Body { get; set; }
}

[ProtoContract]
internal class FriendDeleteOrPinChangedBody
{
// Maybe is type, need check
// 7 Pin changed
// 5 Friend delete
[ProtoMember(2)] public uint Type { get; set; }

[ProtoMember(20)] public PinChanged? PinChanged { get; set; }
}

[ProtoContract]
internal class PinChanged
{
[ProtoMember(1)] public PinChangedBody Body { get; set; }
}

[ProtoContract]
internal class PinChangedBody
{
[ProtoMember(1)] public string Uid { get; set; }

[ProtoMember(2)] public uint? GroupUin { get; set; }

[ProtoMember(400)] public PinChangedInfo Info { get; set; }
}

[ProtoContract]
internal class PinChangedInfo
{
// if (Timestamp.Length != 0) pin
// if (Timestamp.Length == 0) unpin
[ProtoMember(2)] public byte[] Timestamp { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using ProtoBuf;

#pragma warning disable CS8618

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

[ProtoContract]
[OidbSvcTrpcTcp(0x5d6, 1)]
internal class OidbSvcTrpcTcp0x5D6_1
{
[ProtoMember(1)] public uint Field1 { get; set; }

[ProtoMember(2)] public OidbSvcTrpcTcp0x5D6_1Info Info { get; set; }

[ProtoMember(3)] public uint Field3 { get; set; }
}

[ProtoContract]
internal class OidbSvcTrpcTcp0x5D6_1Info
{
[ProtoMember(2)] public uint GroupUin { get; set; }

[ProtoMember(400)] public OidbSvcTrpcTcp0x5D6_1Field4_2_400 Field400 { get; set; }
}

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

[ProtoMember(2)] public byte[] Timestamp { get; set; }
}
Loading

0 comments on commit 843492e

Please sign in to comment.