예제 #1
0
        /// <inheritdoc/>
        public override IWolfResponse Deserialize(Type responseType, SerializedMessageData responseData)
        {
            // if body contains an object that contains yet another body, means it's nested, so treat it as normal
            // otherwise, it's just one group, and we need to nest it deeper so this serializer works
            // yes. This protocol is damn stupid. "What is consistency? We don't know, unless it's consistently bad!"
            SerializedMessageData data = responseData;
            IEnumerable <JToken>  nestedGroupBodies = GetResponseJson(responseData).SelectTokens("body.*.body");

            if (nestedGroupBodies?.Any() != true)
            {
                JToken  newJson = responseData.Payload.DeepClone();
                JObject newBody = GetResponseJson(newJson).SelectToken("body") as JObject;
                JEnumerable <JToken> children = newBody.Children();
                JObject groupBody             = new JObject();
                foreach (JToken obj in children)
                {
                    groupBody.Add(obj);
                }
                newBody.RemoveAll();
                newBody.Add(new JProperty("0", new JObject(new JProperty("body", groupBody))));
                data = new SerializedMessageData(newJson, responseData.BinaryMessages);
            }

            GroupProfileResponse result = (GroupProfileResponse)base.Deserialize(responseType, data);

            return(result);
        }
        /// <summary>Gets profile of the recipient of the message.</summary>
        /// <typeparam name="T">Type of recipient, for example <see cref="WolfUser"/> or <see cref="WolfGroup"/>.</typeparam>
        /// <param name="context">Command context.</param>
        /// <param name="cancellationToken">Token to cancel server request with.</param>
        /// <returns>Profile of the message's recipient; if <typeparamref name="T"/> does not match the message type, null will be returned.</returns>
        /// <seealso cref="GetSenderAsync(ICommandContext, CancellationToken)"/>
        /// <seealso cref="GetBotProfileAsync(ICommandContext, CancellationToken)"/>
        public static async Task <T> GetRecipientAsync <T>(this ICommandContext context, CancellationToken cancellationToken = default) where T : class, IWolfEntity
        {
            if (context.Message.IsGroupMessage)
            {
                WolfGroup result;
                if (context.Client is IWolfClientCacheAccessor cache)
                {
                    result = cache.GetCachedGroup(context.Message.RecipientID);
                    if (result != null)
                    {
                        await context.Client.RevalidateGroupMembersAsync(result, cancellationToken).ConfigureAwait(false);

                        return(result as T);
                    }
                }

                GroupProfileResponse response = await context.Client.SendAsync <GroupProfileResponse>(
                    new GroupProfileMessage(new uint[] { context.Message.RecipientID }, true), cancellationToken).ConfigureAwait(false);

                result = response?.GroupProfiles?.FirstOrDefault(g => g.ID == context.Message.RecipientID);
                if (result != null)
                {
                    await context.Client.RevalidateGroupMembersAsync(result, cancellationToken).ConfigureAwait(false);
                }
                return(result as T);
            }
            else
            {
                return(await GetUserAsync(context, context.Message.RecipientID, cancellationToken) as T);
            }
        }