/// <inheritdoc/> public override SerializedMessageData Serialize(IWolfMessage message) { SerializedMessageData result = base.Serialize(message); JObject body = result.Payload["body"] as JObject; if (body == null) { return(result); } // metadata props JObject metadata = new JObject(); SerializationHelper.MovePropertyIfExists(ref body, ref metadata, "isDeleted"); SerializationHelper.MovePropertyIfExists(ref body, ref metadata, "isTipped"); if (metadata.HasValues) { body.Add(new JProperty("metadata", metadata)); } // palringo inconsistency strikes again!!! // when receiving, recipient is in object as "recipient". But when sending, it's "recipientId". So here, for sending we rename it // new protocol feature, same damn bad design if (message is ChatUpdateMessage updateMessage && updateMessage.SenderID == null) { JToken recipient = body["recipient"]; body.Remove("recipient"); body.Add("recipientId", recipient); } return(result); }
/// <inheritdoc/> public override SerializedMessageData Serialize(IWolfMessage message) { SerializedMessageData result = base.Serialize(message); JObject body = result.Payload["body"] as JObject; if (body == null) { return(result); } // extended props JObject extended = new JObject(); SerializationHelper.MovePropertyIfExists(ref body, ref extended, "name"); SerializationHelper.MovePropertyIfExists(ref body, ref extended, "about"); SerializationHelper.MovePropertyIfExists(ref body, ref extended, "lookingFor"); SerializationHelper.MovePropertyIfExists(ref body, ref extended, "gender"); SerializationHelper.MovePropertyIfExists(ref body, ref extended, "relationship"); SerializationHelper.MovePropertyIfExists(ref body, ref extended, "language"); SerializationHelper.MovePropertyIfExists(ref body, ref extended, "urls"); if (extended.HasValues) { body.Add(new JProperty("extended", extended)); } return(result); }
/// <inheritdoc/> public virtual IWolfMessage Deserialize(string command, SerializedMessageData messageData) { IWolfMessage result = messageData.Payload.ToObject <T>(SerializationHelper.DefaultSerializer); messageData.Payload.FlattenCommonProperties(result, SerializationHelper.DefaultSerializer); return(result); }
/// <inheritdoc/> /// <remarks>This callback will only invoke if <paramref name="message"/> is of type <typeparamref name="T"/>, /// and <paramref name="message"/>'s <see cref="IWolfMessage.Command"/> is the same as the one provided in class constructor.</remarks> public override bool TryInvoke(IWolfMessage message) { if (!string.Equals(message.Command, this.Command, StringComparison.OrdinalIgnoreCase)) { return(false); } return(base.TryInvoke(message)); }
/// <inheritdoc/> /// <remarks>Callback will only invoke if <paramref name="message"/> is of type <typeparamref name="T"/>.</remarks> public virtual bool TryInvoke(IWolfMessage message) { if (message is T msg) { _callback.Invoke(msg); return(true); } return(false); }
/// <summary>Attempts to invoke all callbacks.</summary> /// <param name="message">Message to pass to callbacks.</param> public void Invoke(IWolfMessage message) { lock (_callbacks) { for (int i = 0; i < _callbacks.Count; i++) { _callbacks[i].TryInvoke(message); } } }
/// <inheritdoc/> public override SerializedMessageData Serialize(IWolfMessage message) { ThrowIfInvalidMessageType(message.GetType()); SerializedMessageData result = base.Serialize(message); // when sending, UserID will be null, so can use that to determine if ((message is GroupJoinMessage joinMessage && joinMessage.UserID == null) || (message is GroupLeaveMessage leaveMessage && leaveMessage.UserID == null)) { JObject body = (JObject)result.Payload["body"]; JToken value = body["groupId"]; body.Remove("groupId"); body.Add("id", value); } return(result); }
/// <inheritdoc/> public override SerializedMessageData Serialize(IWolfMessage message) { SerializedMessageData result = base.Serialize(message); JObject body = result.Payload["body"] as JObject; if (body == null) { return(result); } // metadata props JObject context = new JObject(); SerializationHelper.MovePropertyIfExists(ref body, ref context, "id"); SerializationHelper.MovePropertyIfExists(ref body, ref context, "type"); if (context.HasValues) { body.Add(new JProperty("context", context)); } return(result); }
public override SerializedMessageData Serialize(IWolfMessage message) { SerializedMessageData result = base.Serialize(message); JObject body = result.Payload["body"] as JObject; if (body == null) { return(result); } // extended props JObject extended = new JObject(); SerializationHelper.MovePropertyIfExists(ref body, ref extended, "advancedAdmin"); SerializationHelper.MovePropertyIfExists(ref body, ref extended, "discoverable"); SerializationHelper.MovePropertyIfExists(ref body, ref extended, "entryLevel"); SerializationHelper.MovePropertyIfExists(ref body, ref extended, "language"); SerializationHelper.MovePropertyIfExists(ref body, ref extended, "longDescription"); if (extended.HasValues) { body.Add(new JProperty("extended", extended)); } return(result); }
/// <inheritdoc/> public SerializedMessageData Serialize(IWolfMessage message) { JObject payload = message.SerializeJsonPayload(); JObject body = payload["body"] as JObject; IChatMessage msg = (IChatMessage)message; // metadata props JObject metadata = new JObject(); SerializationHelper.MovePropertyIfExists(ref body, ref metadata, "isDeleted"); SerializationHelper.MovePropertyIfExists(ref body, ref metadata, "isTipped"); if (metadata.HasValues) { body.Add(new JProperty("metadata", metadata)); } // raw data if (msg.RawData?.Any() == true) { body["data"] = new JObject(new JProperty("_placeholder", true), new JProperty("num", 0)); return(new SerializedMessageData(payload, msg.RawData.ToArray())); } return(new SerializedMessageData(payload)); }
/// <summary>Creates a new instance of the exception.</summary> /// <param name="sentMessage">Message sent to the server.</param> /// <param name="response">Server's response.</param> /// <param name="message">Exception message.</param> public MessageSendingException(IWolfMessage sentMessage, IWolfResponse response, string message) : this(sentMessage, response, message, null) { }
/// <summary>Creates a new instance of event arguments.</summary> /// <param name="message">Wolf message sent.</param> /// <param name="response">Server's response.</param> public WolfMessageSentEventArgs(IWolfMessage message, IWolfResponse response) : base(message) { this.Response = response; }
/// <summary>Creates a new instance of event arguments.</summary> /// <param name="message">Wolf message sent or received.</param> public WolfMessageEventArgs(IWolfMessage message) : base() { this.Message = message; }
/// <summary>Gets response type for the message.</summary> /// <typeparam name="TFallbackType">Type of response to use as fallback.</typeparam> /// <param name="resolver">Resolver to get type from.</param> /// <param name="message">Sent message.</param> /// <returns>Type of the response.</returns> public static Type GetMessageResponseType <TFallbackType>(this IResponseTypeResolver resolver, IWolfMessage message) where TFallbackType : IWolfResponse => resolver.GetMessageResponseType(message.GetType(), typeof(TFallbackType));
/// <summary>Sends message, and waits for response from the server.</summary> /// <remarks><para>If client uses <see cref="DefaultResponseTypeResolver"/>, the type of response provided with /// <see cref="ResponseTypeAttribute"/> on <paramref name="message"/> will be used for deserialization.<br/> /// Otherwise, response will be deserialized as<see cref="WolfResponse"/>.</para></remarks> /// <param name="message">Message to send.</param> /// <returns>Sending response.</returns> /// <exception cref="MessageSendingException">Server responded with error.</exception> public static Task <WolfResponse> SendAsync(this IWolfClient client, IWolfMessage message, CancellationToken cancellationToken = default) => client.SendAsync <WolfResponse>(message, cancellationToken);
/// <inheritdoc/> public virtual SerializedMessageData Serialize(IWolfMessage message) => new SerializedMessageData(message.SerializeJsonPayload(SerializationHelper.DefaultSerializer));
/// <summary>Creates a new instance of the exception.</summary> /// <param name="sentMessage">Message sent to the server.</param> /// <param name="response">Server's response.</param> /// <param name="message">Exception message.</param> /// <param name="innerException">Inner exception.</param> public MessageSendingException(IWolfMessage sentMessage, IWolfResponse response, string message, Exception innerException) : base(message, innerException) { this.SentMessage = sentMessage; this.Response = response; }
/// <summary>Creates a new instance of the exception.</summary> /// <param name="sentMessage">Message sent to the server.</param> /// <param name="response">Server's response.</param> public MessageSendingException(IWolfMessage sentMessage, IWolfResponse response) : this(sentMessage, response, BuildDefaultMessage(sentMessage.EventName, response), null) { }
/// <summary>Creates a new instance of the exception.</summary> /// <param name="sentMessage">Message sent to the server.</param> /// <param name="response">Server's response.</param> /// <param name="innerException">Inner exception.</param> public MessageSendingException(IWolfMessage sentMessage, IWolfResponse response, Exception innerException) : this(sentMessage, response, BuildDefaultMessage(sentMessage.Command, response), innerException) { }