Skip to content

Commit

Permalink
[Core] Implemented SetAvatar and GroupSetAvatar
Browse files Browse the repository at this point in the history
  • Loading branch information
Linwenxuan04 committed Oct 3, 2024
1 parent 345dffa commit 64796b5
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Lagrange.Core/Common/Interface/Api/GroupExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ public static Task<bool> GroupSetMessageReaction(this BotContext bot, uint group

public static Task<bool> GroupSetMessageReaction(this BotContext bot, uint groupUin, uint sequence, string code, bool isSet)
=> bot.ContextCollection.Business.OperationLogic.SetMessageReaction(groupUin, sequence, code, isSet);

public static Task<bool> GroupSetAvatar(this BotContext bot, uint groupUin, ImageEntity imageEntity)
=> bot.ContextCollection.Business.OperationLogic.GroupSetAvatar(groupUin, imageEntity);

#region Group File System

Expand Down
8 changes: 8 additions & 0 deletions Lagrange.Core/Common/Interface/Api/OperationExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,12 @@ public static Task<MessageResult> FriendShake(this BotContext bot, uint friendUi

public static Task<List<string>?> FetchMarketFaceKey(this BotContext bot, List<string> faceIds)
=> bot.ContextCollection.Business.OperationLogic.FetchMarketFaceKey(faceIds);

/// <summary>
/// Set the avatar of the bot itself
/// </summary>
/// <param name="bot">target <see cref="BotContext"/></param>
/// <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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
using Lagrange.Core.Internal.Event.Action;
using Lagrange.Core.Internal.Event.Message;
using Lagrange.Core.Internal.Event.System;
using Lagrange.Core.Internal.Packets.Service.Highway;
using Lagrange.Core.Message;
using Lagrange.Core.Message.Entity;
using Lagrange.Core.Utility.Extension;

namespace Lagrange.Core.Internal.Context.Logic.Implementation;

Expand Down Expand Up @@ -580,4 +582,36 @@ public Task<MessageResult> FriendShake(uint friendUin, PokeFaceType type, uint s
.Build();
return SendMessage(chain);
}

public async Task<bool> SetAvatar(ImageEntity avatar)
{
if (avatar.ImageStream == null) return false;

var highwayUrlEvent = HighwayUrlEvent.Create();
var highwayUrlResults = await Collection.Business.SendEvent(highwayUrlEvent);
if (highwayUrlResults.Count == 0) return false;

var ticket = ((HighwayUrlEvent)highwayUrlResults[0]).SigSession;
return await Collection.Highway.UploadSrcByStreamAsync(90, avatar.ImageStream.Value, ticket, avatar.ImageMd5, Array.Empty<byte>());
}

public async Task<bool> GroupSetAvatar(uint groupUin, ImageEntity avatar)
{
if (avatar.ImageStream == null) return false;

var highwayUrlEvent = HighwayUrlEvent.Create();
var highwayUrlResults = await Collection.Business.SendEvent(highwayUrlEvent);
if (highwayUrlResults.Count == 0) return false;

var ticket = ((HighwayUrlEvent)highwayUrlResults[0]).SigSession;
var extra = new GroupAvatarExtra
{
Type = 101,
GroupUin = groupUin,
Field3 = new GroupAvatarExtraField3 { Field1 = 1 },
Field5 = 3,
Field6 = 1
}.Serialize().ToArray();
return await Collection.Highway.UploadSrcByStreamAsync(90, avatar.ImageStream.Value, ticket, avatar.ImageMd5, extra);
}
}
24 changes: 24 additions & 0 deletions Lagrange.Core/Internal/Packets/Service/Highway/GroupAvatarExtra.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using ProtoBuf;

namespace Lagrange.Core.Internal.Packets.Service.Highway;
#pragma warning disable CS8618

[ProtoContract]
internal class GroupAvatarExtra
{
[ProtoMember(1)] public uint Type { get; set; } // 101

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

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

[ProtoMember(5)] public uint Field5 { get; set; } // 3

[ProtoMember(6)] public uint Field6 { get; set; } // 1
}

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

0 comments on commit 64796b5

Please sign in to comment.