Skip to content

Commit

Permalink
[All] Fix #759
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkRRb committed Feb 6, 2025
1 parent c098da4 commit a59ffb3
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 8 deletions.
12 changes: 10 additions & 2 deletions Lagrange.Core/Event/EventArg/GroupInvitationEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@ namespace Lagrange.Core.Event.EventArg;
public class GroupInvitationEvent : EventBase
{
public uint GroupUin { get; }

public uint InvitorUin { get; }


public ulong? Sequence { get; }

internal GroupInvitationEvent(uint groupUin, uint invitorUin)
{
GroupUin = groupUin;
InvitorUin = invitorUin;
Sequence = null;
EventMessage = $"[{nameof(GroupInvitationEvent)}]: {GroupUin} from {InvitorUin}";
}

internal GroupInvitationEvent(uint groupUin, uint invitorUin, ulong? sequence) : this(groupUin, invitorUin)
{
Sequence = sequence;
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Text.Json;
using System.Web;
using Lagrange.Core.Event;
using Lagrange.Core.Event.EventArg;
using Lagrange.Core.Internal.Context.Attributes;
Expand Down Expand Up @@ -62,8 +64,26 @@ public override async Task Incoming(ProtocolEvent e)
MessageFilter.Filter(push.Chain);

var chain = push.Chain;
Collection.Log.LogVerbose(Tag, chain.ToPreviewString());

// Intercept group invitation
if (chain.Count == 1 && chain[0] is LightAppEntity { AppName: "com.tencent.qun.invite" } app)
{
using var document = JsonDocument.Parse(app.Payload);
var root = document.RootElement;

string url = root.GetProperty("meta").GetProperty("news").GetProperty("jumpUrl").GetString()
?? throw new Exception("sb tx! Is this 'com.tencent.qun.invite'?");
var query = HttpUtility.ParseQueryString(new Uri(url).Query);
uint groupUin = uint.Parse(query["groupcode"]
?? throw new Exception("sb tx! Is this '/group/invite_join'?"));
ulong sequence = ulong.Parse(query["msgseq"]
?? throw new Exception("sb tx! Is this '/group/invite_join'?"));

Collection.Invoker.PostEvent(new GroupInvitationEvent(groupUin, chain.FriendUin, sequence));
break;
}

Collection.Log.LogVerbose(Tag, chain.ToPreviewString());
EventBase args = push.Chain.Type switch
{
MessageChain.MessageType.Friend => new FriendMessageEvent(chain),
Expand Down Expand Up @@ -434,7 +454,7 @@ private async Task ResolveOutgoingChain(MessageChain chain)
break;

string name = (await cache.GetCachedFaceEntity(bounceFace.FaceId))?.QDes ?? string.Empty;

// Because the name is used as a preview text, it should not start with '/'
// But the QDes of the face may start with '/', so remove it
if (name.StartsWith('/'))
Expand Down
32 changes: 28 additions & 4 deletions Lagrange.OneBot/Core/Notify/NotifyService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,36 @@ public void RegisterEvents()
{
logger.LogInformation(@event.ToString());

var requests = await bot.FetchGroupRequests();
if (requests?.FirstOrDefault(x => @event.GroupUin == x.GroupUin && @event.InvitorUin == x.InvitorMemberUin) is { } request)
ulong? sequence = @event.Sequence;
string comment = string.Empty;
uint type = 2;
if (sequence == null) // received by msg
{
string flag = $"{request.Sequence}-{request.GroupUin}-{(uint)request.EventType}";
await service.SendJsonAsync(new OneBotGroupRequest(bot.BotUin, @event.InvitorUin, @event.GroupUin, "invite", request.Comment, flag));
var requests = await bot.FetchGroupRequests();
if (requests == null)
{
logger.LogWarning("OnGroupInvitationReceived but no group requests");
return;
}

var request = requests.FirstOrDefault(r =>
{
return r.EventType == BotGroupRequest.Type.SelfInvitation
&& r.GroupUin == @event.GroupUin
&& r.InvitorMemberUin == @event.InvitorUin;
});
if (request == null)
{
logger.LogWarning("OnGroupInvitationReceived but matching no group requests");
return;
}

sequence = request.Sequence;
if (request.Comment != null) comment = request.Comment;
}

string flag = $"{sequence}-{@event.GroupUin}-{type}";
await service.SendJsonAsync(new OneBotGroupRequest(bot.BotUin, @event.InvitorUin, @event.GroupUin, "invite", comment, flag));
};

bot.Invoker.OnGroupJoinRequestEvent += async (_, @event) =>
Expand Down

0 comments on commit a59ffb3

Please sign in to comment.