-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from TehGM/dev
- Loading branch information
Showing
24 changed files
with
405 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
using System.Runtime.CompilerServices; | ||
|
||
[assembly: InternalsVisibleTo("Wolfringo.Commands")] | ||
[assembly: InternalsVisibleTo("Wolfringo.Utilities")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using Newtonsoft.Json; | ||
using System; | ||
|
||
namespace TehGM.Wolfringo.Messages.Embeds | ||
{ | ||
/// <summary>Represent a chat embed for image link.</summary> | ||
public class ImagePreviewChatEmbed : IChatEmbed | ||
{ | ||
/// <inheritdoc/> | ||
public string EmbedType => "imagePreview"; | ||
|
||
/// <summary>ID of the group to embed.</summary> | ||
[JsonProperty("url")] | ||
public string URL { get; } | ||
|
||
/// <summary>Creates a new link preview embed with an image.</summary> | ||
/// <param name="url">Link to preview.</param> | ||
public ImagePreviewChatEmbed(string url) | ||
{ | ||
if (string.IsNullOrWhiteSpace(url)) | ||
throw new ArgumentException("Image URL is required", nameof(url)); | ||
|
||
this.URL = url; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using Newtonsoft.Json; | ||
using System; | ||
|
||
namespace TehGM.Wolfringo.Messages.Embeds | ||
{ | ||
/// <summary>Represent a chat embed for website link.</summary> | ||
public class LinkPreviewChatEmbed : IChatEmbed | ||
{ | ||
/// <inheritdoc/> | ||
public string EmbedType => "linkPreview"; | ||
|
||
/// <summary>Title of the webpage.</summary> | ||
[JsonProperty("title")] | ||
public string Title { get; set; } | ||
/// <summary>URL of the webpage.</summary> | ||
[JsonProperty("url")] | ||
public string URL { get; } | ||
|
||
/// <summary>Creates a new link preview embed.</summary> | ||
/// <param name="title">Title of the webpage.</param> | ||
/// <param name="url">Link to preview.</param> | ||
public LinkPreviewChatEmbed(string title, string url) | ||
{ | ||
if (string.IsNullOrWhiteSpace(title)) | ||
throw new ArgumentException("Link title is required", nameof(title)); | ||
if (string.IsNullOrWhiteSpace(url)) | ||
throw new ArgumentException("Link URL is required", nameof(url)); | ||
|
||
this.Title = title; | ||
this.URL = url; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
Wolfringo.Core/Messages/Serialization/IChatEmbedDeserializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
using Newtonsoft.Json.Linq; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using TehGM.Wolfringo.Messages.Embeds; | ||
using TehGM.Wolfringo.Messages.Serialization.Internal; | ||
|
||
namespace TehGM.Wolfringo.Messages.Serialization | ||
{ | ||
/// <summary>Maps values present in 'type' property of chat embeds and provides means to deserialize them into a <see cref="ChatMessage"/>.</summary> | ||
public interface IChatEmbedDeserializer | ||
{ | ||
/// <summary>Attempts to retrieve the embed type.</summary> | ||
/// <param name="type">Value of 'type' property.</param> | ||
/// <param name="result">Resulting type if mapped.</param> | ||
/// <returns>Whether a type for given embed type was found.</returns> | ||
bool TryGetChatEmbedType(string type, out Type result); | ||
/// <summary>Maps chat embed type to an implementation type.</summary> | ||
/// <typeparam name="T">Implementation type of the chat embed.</typeparam> | ||
/// <param name="type">Type of the chat embed.</param> | ||
void MapChatEmbedType<T>(string type) where T : IChatEmbed; | ||
/// <summary>deserializes all embeds from message body.</summary> | ||
/// <remarks>Embeds are deserialized if body has 'embeds' array. Otherwise an empty enumerable is returned.</remarks> | ||
/// <param name="messageBody">Body of the message.</param> | ||
/// <returns>Enumerable of chat embeds.</returns> | ||
IEnumerable<IChatEmbed> DeserializeEmbeds(JObject messageBody); | ||
/// <summary>Populates chat message's embeds.</summary> | ||
/// <param name="message">Chat message.</param> | ||
/// <param name="embeds">Deserialized embeds.</param> | ||
void PopulateMessageEmbeds(ref ChatMessage message, IEnumerable<IChatEmbed> embeds); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public class ChatEmbedDeserializer : IChatEmbedDeserializer | ||
{ | ||
internal static ChatEmbedDeserializer Instance { get; } = new ChatEmbedDeserializer(); | ||
|
||
private readonly Dictionary<string, Type> _registeredEmbedTypes = new Dictionary<string, Type>() | ||
{ | ||
["linkPreview"] = typeof(LinkPreviewChatEmbed), | ||
["imagePreview"] = typeof(ImagePreviewChatEmbed), | ||
["groupPreview"] = typeof(GroupPreviewChatEmbed) | ||
}; | ||
|
||
/// <inheritdoc/> | ||
public bool TryGetChatEmbedType(string type, out Type result) | ||
{ | ||
return this._registeredEmbedTypes.TryGetValue(type, out result); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public void MapChatEmbedType<T>(string type) where T : IChatEmbed | ||
{ | ||
this._registeredEmbedTypes[type] = typeof(T); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public IEnumerable<IChatEmbed> DeserializeEmbeds(JObject messageBody) | ||
{ | ||
if (messageBody == null || !messageBody.ContainsKey("embeds") || !(messageBody["embeds"] is JArray embeds)) | ||
yield break; | ||
|
||
foreach (JToken embed in embeds) | ||
{ | ||
if (!(embed is JObject embedObject) || !embedObject.ContainsKey("type")) | ||
continue; | ||
|
||
string embedType = embedObject["type"].ToObject<string>(); | ||
if (string.IsNullOrWhiteSpace(embedType)) | ||
continue; | ||
|
||
if (this.TryGetChatEmbedType(embedType, out Type type)) | ||
{ | ||
yield return (IChatEmbed)embedObject.ToObject(type, SerializationHelper.DefaultSerializer); | ||
} | ||
} | ||
} | ||
|
||
/// <inheritdoc/> | ||
public void PopulateMessageEmbeds(ref ChatMessage message, IEnumerable<IChatEmbed> embeds) | ||
{ | ||
if (message == null) | ||
throw new ArgumentNullException(nameof(message)); | ||
if (embeds?.Any() != true) | ||
return; | ||
|
||
if (message.Embeds == null || !(message.Embeds is ICollection<IChatEmbed> embedCollection) || embedCollection.IsReadOnly) | ||
throw new InvalidOperationException($"Cannot populate embeds in {message.GetType().Name} as the collection is read only or null"); | ||
embedCollection.Clear(); | ||
|
||
// if it's a list, we can do it in a more performant way | ||
if (message.Embeds is List<IChatEmbed> embedList) | ||
embedList.AddRange(embeds); | ||
// otherwise do it one by one | ||
else | ||
{ | ||
foreach (IChatEmbed e in embeds) | ||
embedCollection.Add(e); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.