Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
sisi0318 committed Oct 28, 2024
2 parents 0262416 + 7815fc8 commit 3e430cd
Show file tree
Hide file tree
Showing 53 changed files with 1,290 additions and 125 deletions.
24 changes: 24 additions & 0 deletions Lagrange.Core/Common/Entity/BotGetGroupTodoResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace Lagrange.Core.Common.Entity;

[Serializable]
public class BotGetGroupTodoResult
{
public int Retcode { get; }

public string? ResultMessage { get; }

public uint GroupUin { get; }

public uint Sequence { get; }

public string Preview { get; }

public BotGetGroupTodoResult(int retcode, string? resultMessage, uint groupUin, uint sequence, string preview)
{
Retcode = retcode;
ResultMessage = resultMessage;
GroupUin = groupUin;
Sequence = sequence;
Preview = preview;
}
}
20 changes: 20 additions & 0 deletions Lagrange.Core/Common/Entity/BotGroupReaction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Lagrange.Core.Common.Entity;

public class BotGroupReaction
{
public string FaceId { get; set; }

public uint Type { get; set; }

public uint Count { get; set; }

public bool IsAdded { get; set; }

public BotGroupReaction(string faceId, uint type, uint count, bool isAdded)
{
FaceId = faceId;
Type = type;
Count = count;
IsAdded = isAdded;
}
}
67 changes: 67 additions & 0 deletions Lagrange.Core/Common/Entity/SysFaceEntry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
namespace Lagrange.Core.Common.Entity;

[Serializable]
public class SysFaceEntry
{
public string QSid { get; set; }

public string? QDes { get; set; }

public string? EMCode { get; set; }

public int? QCid { get; set; }

public int? AniStickerType { get; set; }

public int? AniStickerPackId { get; set; }

public int? AniStickerId { get; set; }

public string? Url { get; set; }

public string[]? EmojiNameAlias { get; set; }

public int? AniStickerWidth { get; set; }

public int? AniStickerHeight { get; set; }

public SysFaceEntry(string qSid, string? qDes, string? emCode, int? qCid, int? aniStickerType,
int? aniStickerPackId, int? aniStickerId, string? url, string[]? emojiNameAlias, int? aniStickerWidth,
int? aniStickerHeight)
{
QSid = qSid;
QDes = qDes;
EMCode = emCode;
QCid = qCid;
AniStickerType = aniStickerType;
AniStickerPackId = aniStickerPackId;
AniStickerId = aniStickerId;
Url = url;
EmojiNameAlias = emojiNameAlias;
AniStickerWidth = aniStickerWidth;
AniStickerHeight = aniStickerHeight;
}
}

[Serializable]
public class SysFacePackEntry
{
public string EmojiPackName { get; set; }

public SysFaceEntry[] Emojis { get; set; }

public SysFacePackEntry(string emojiPackName, SysFaceEntry[] emojis)
{
EmojiPackName = emojiPackName;
Emojis = emojis;
}

public uint[] GetUniqueSuperQSids((int AniStickerType, int AniStickerPackId)[] excludeAniStickerTypesAndPackIds)
=> Emojis
.Where(e => e.AniStickerType is not null
&& e.AniStickerPackId is not null
&& !excludeAniStickerTypesAndPackIds.Contains((e.AniStickerType.Value, e.AniStickerPackId.Value)))
.Select(e => uint.Parse(e.QSid))
.Distinct()
.ToArray();
}
13 changes: 13 additions & 0 deletions Lagrange.Core/Common/Interface/Api/GroupExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ public static Task<bool> KickGroupMember(this BotContext bot, uint groupUin, uin
public static Task<bool> SetGroupAdmin(this BotContext bot, uint groupUin, uint targetUin, bool isAdmin)
=> bot.ContextCollection.Business.OperationLogic.SetGroupAdmin(groupUin, targetUin, isAdmin);

// 300204 Check group manager:Not an administrator
public static Task<(int, string?)> SetGroupTodo(this BotContext bot, uint groupUin, uint sequence)
=> bot.ContextCollection.Business.OperationLogic.SetGroupTodo(groupUin, sequence);

public static Task<(int, string?)> RemoveGroupTodo(this BotContext bot, uint groupUin)
=> bot.ContextCollection.Business.OperationLogic.RemoveGroupTodo(groupUin);

public static Task<(int, string?)> FinishGroupTodo(this BotContext bot, uint groupUin)
=> bot.ContextCollection.Business.OperationLogic.FinishGroupTodo(groupUin);

public static Task<BotGetGroupTodoResult> GetGroupTodo(this BotContext bot, uint groupUin)
=> bot.ContextCollection.Business.OperationLogic.GetGroupTodo(groupUin);

public static Task<bool> SetGroupBot(this BotContext bot, uint targetUin, uint On, uint groupUin)
=> bot.ContextCollection.Business.OperationLogic.SetGroupBot(targetUin, On, groupUin);

Expand Down
12 changes: 12 additions & 0 deletions Lagrange.Core/Common/Interface/Api/OperationExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,5 +257,17 @@ public static async Task<List<string>> FetchRkey(this BotContext bot)
/// <param name="avatar">The avatar object, <see cref="ImageEntity"/></param>
public static Task<bool> SetAvatar(this BotContext bot, ImageEntity avatar)
=> bot.ContextCollection.Business.OperationLogic.SetAvatar(avatar);

public static Task<bool> FetchSuperFaceId(this BotContext bot, uint id)
=> bot.ContextCollection.Business.OperationLogic.FetchSuperFaceId(id);

public static Task<SysFaceEntry?> FetchFaceEntity(this BotContext bot, uint id)
=> bot.ContextCollection.Business.OperationLogic.FetchFaceEntity(id);

public static Task<bool> GroupJoinEmojiChain(this BotContext bot, uint groupUin, uint emojiId, uint targetMessageSeq)
=> bot.ContextCollection.Business.OperationLogic.GroupJoinEmojiChain(groupUin, emojiId, targetMessageSeq);

public static Task<bool> FriendJoinEmojiChain(this BotContext bot, uint friendUin, uint emojiId, uint targetMessageSeq)
=> bot.ContextCollection.Business.OperationLogic.FriendJoinEmojiChain(friendUin, emojiId, targetMessageSeq);
}

16 changes: 16 additions & 0 deletions Lagrange.Core/Event/EventArg/GroupTodoEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Lagrange.Core.Event.EventArg;

public class GroupTodoEvent : EventBase
{
public uint GroupUin { get; }

public uint OperatorUin { get; }

public GroupTodoEvent(uint groupUin, uint operatorUin)
{
GroupUin = groupUin;
OperatorUin = operatorUin;

EventMessage = $"{nameof(GroupPokeEvent)}: GroupUin: {GroupUin} | OperatorUin: {OperatorUin}";
}
}
2 changes: 2 additions & 0 deletions Lagrange.Core/Event/EventInvoker.Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,6 @@ public partial class EventInvoker
public event LagrangeEvent<GroupReactionEvent>? OnGroupReactionEvent;

public event LagrangeEvent<GroupNameChangeEvent>? OnGroupNameChangeEvent;

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

namespace Lagrange.Core.Event;

Expand Down Expand Up @@ -41,6 +42,7 @@ internal EventInvoker(BotContext context)
RegisterEvent((GroupEssenceEvent e) => OnGroupEssenceEvent?.Invoke(context, e));
RegisterEvent((GroupReactionEvent e) => OnGroupReactionEvent?.Invoke(context, e));
RegisterEvent((GroupNameChangeEvent e) => OnGroupNameChangeEvent?.Invoke(context, e));
RegisterEvent((GroupTodoEvent e) => OnGroupTodoEvent?.Invoke(context, e));
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ internal class CachingLogic : LogicBase

private readonly ConcurrentDictionary<uint, BotUserInfo> _cacheUsers;

private readonly Dictionary<uint, SysFaceEntry> _cacheFaceEntities;
private readonly List<uint> _cacheSuperFaceId;

internal CachingLogic(ContextCollection collection) : base(collection)
{
_uinToUid = new Dictionary<uint, string>();
Expand All @@ -35,6 +38,9 @@ internal CachingLogic(ContextCollection collection) : base(collection)
_cachedGroupMembers = new Dictionary<uint, List<BotGroupMember>>();

_cacheUsers = new ConcurrentDictionary<uint, BotUserInfo>();

_cacheFaceEntities = new Dictionary<uint, SysFaceEntry>();
_cacheSuperFaceId = new List<uint>();
}

public override Task Incoming(ProtocolEvent e)
Expand Down Expand Up @@ -92,6 +98,7 @@ public async Task<List<BotGroupMember>> GetCachedMembers(uint groupUin, bool ref
await ResolveMembersUid(groupUin);
return _cachedGroupMembers.TryGetValue(groupUin, out members) ? members : new List<BotGroupMember>();
}

return members;
}

Expand Down Expand Up @@ -142,6 +149,7 @@ private async Task ResolveFriendsUidAndFriendGroups()
{
friend.Group = new(friend.Group.GroupId, friendGroups[friend.Group.GroupId]);
}

friends.AddRange(result.Friends);

next = result.NextUin;
Expand Down Expand Up @@ -190,4 +198,32 @@ private async Task ResolveUser(uint uin)
_cacheUsers.AddOrUpdate(uin, @event.UserInfo, (_key, _value) => @event.UserInfo);
}
}

private async Task ResolveEmojiCache()
{
var fetchSysEmojisEvent = FetchFullSysFacesEvent.Create();
var events = await Collection.Business.SendEvent(fetchSysEmojisEvent);
var emojiPacks = ((FetchFullSysFacesEvent)events[0]).FacePacks;

emojiPacks
.SelectMany(pack => pack.Emojis)
.Where(emoji => uint.TryParse(emoji.QSid, out _))
.ToList()
.ForEach(emoji => _cacheFaceEntities[uint.Parse(emoji.QSid)] = emoji);

_cacheSuperFaceId.AddRange(emojiPacks
.SelectMany(emojiPack => emojiPack.GetUniqueSuperQSids(new[] { (1, 1) })));
}

public async Task<bool> GetCachedIsSuperFaceId(uint id)
{
if (!_cacheSuperFaceId.Any()) await ResolveEmojiCache();
return _cacheSuperFaceId.Contains(id);
}

public async Task<SysFaceEntry?> GetCachedFaceEntity(uint faceId)
{
if (!_cacheFaceEntities.ContainsKey(faceId)) await ResolveEmojiCache();
return _cacheFaceEntities.GetValueOrDefault(faceId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
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 @@ -39,6 +40,7 @@ namespace Lagrange.Core.Internal.Context.Logic.Implementation;
[EventSubscribe(typeof(FriendSysPokeEvent))]
[EventSubscribe(typeof(LoginNotifyEvent))]
[EventSubscribe(typeof(MultiMsgDownloadEvent))]
[EventSubscribe(typeof(GroupSysTodoEvent))]
[BusinessLogic("MessagingLogic", "Manage the receiving and sending of messages and notifications")]
internal class MessagingLogic : LogicBase
{
Expand Down Expand Up @@ -237,6 +239,13 @@ public override async Task Incoming(ProtocolEvent e)
}
break;
}
case GroupSysTodoEvent todo:
{
uint uin = await Collection.Business.CachingLogic.ResolveUin(todo.GroupUin, todo.OperatorUid) ?? 0;

Collection.Invoker.PostEvent(new GroupTodoEvent(todo.GroupUin, uin));
break;
}
}
}

Expand Down Expand Up @@ -376,6 +385,13 @@ private async Task ResolveOutgoingChain(MessageChain chain)
{
switch (entity)
{
case FaceEntity face:
{
var cache = Collection.Business.CachingLogic;
face.SysFaceEntry ??= await cache.GetCachedFaceEntity(face.FaceId);
break;
}

case ForwardEntity forward when forward.TargetUin != 0:
{
var cache = Collection.Business.CachingLogic;
Expand Down
Loading

0 comments on commit 3e430cd

Please sign in to comment.