- Add doc comments for structs
- Small cleanup of the js file
This commit is contained in:
@@ -3,33 +3,69 @@
|
|||||||
namespace ChatTwo.Http.MessageProtocol;
|
namespace ChatTwo.Http.MessageProtocol;
|
||||||
|
|
||||||
#region Outgoing SSE
|
#region Outgoing SSE
|
||||||
|
/// <summary>
|
||||||
|
/// Contains the current channel name
|
||||||
|
/// </summary>
|
||||||
public struct SwitchChannel(MessageTemplate[] channelName)
|
public struct SwitchChannel(MessageTemplate[] channelName)
|
||||||
{
|
{
|
||||||
[JsonProperty("channelName")] public MessageTemplate[] ChannelName = channelName;
|
[JsonProperty("channelName")] public MessageTemplate[] ChannelName = channelName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Contains one or multiple channels that are valid for the user to pick from
|
||||||
|
/// </summary>
|
||||||
public struct ChannelList(Dictionary<string, uint> channels)
|
public struct ChannelList(Dictionary<string, uint> channels)
|
||||||
{
|
{
|
||||||
[JsonProperty("channels")] public Dictionary<string, uint> Channels = channels;
|
[JsonProperty("channels")] public Dictionary<string, uint> Channels = channels;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Contains one or multiple messages
|
||||||
|
/// </summary>
|
||||||
public struct Messages(MessageResponse[] set)
|
public struct Messages(MessageResponse[] set)
|
||||||
{
|
{
|
||||||
[JsonProperty("messages")] public MessageResponse[] Set = set;
|
[JsonProperty("messages")] public MessageResponse[] Set = set;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Contains a single message with all its templates and a timestamp
|
||||||
|
/// </summary>
|
||||||
public struct MessageResponse()
|
public struct MessageResponse()
|
||||||
{
|
{
|
||||||
[JsonProperty("timestamp")] public string Timestamp = "";
|
[JsonProperty("timestamp")] public string Timestamp = "";
|
||||||
[JsonProperty("templates")] public MessageTemplate[] Templates;
|
[JsonProperty("templates")] public MessageTemplate[] Templates;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Template that is used for the channel name or any message posted to the chatlog
|
||||||
|
/// </summary>
|
||||||
public struct MessageTemplate()
|
public struct MessageTemplate()
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Template type
|
||||||
|
///
|
||||||
|
/// icon = a game icon
|
||||||
|
/// emote = BetterTTV emote
|
||||||
|
/// url = Simple url that should be clickable
|
||||||
|
/// text = Simple text content of the message
|
||||||
|
///
|
||||||
|
/// empty = Ignore
|
||||||
|
/// </summary>
|
||||||
[JsonProperty("payload")] public required string Payload;
|
[JsonProperty("payload")] public required string Payload;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Used for text and emote.
|
||||||
|
/// </summary>
|
||||||
[JsonProperty("content")] public string Content = "";
|
[JsonProperty("content")] public string Content = "";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Used for icon.
|
||||||
|
/// </summary>
|
||||||
[JsonProperty("id")] public uint Id;
|
[JsonProperty("id")] public uint Id;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Used for text and url
|
||||||
|
/// </summary>
|
||||||
[JsonProperty("color")] public uint Color;
|
[JsonProperty("color")] public uint Color;
|
||||||
|
|
||||||
public static MessageTemplate Empty => new() {Payload = "empty"};
|
public static MessageTemplate Empty => new() {Payload = "empty"};
|
||||||
@@ -49,11 +85,19 @@ public struct ErrorResponse(string reason)
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Incoming POST
|
#region Incoming POST
|
||||||
|
/// <summary>
|
||||||
|
/// Message must fulfill the posting requirement
|
||||||
|
/// Greater than or equal 2 characters
|
||||||
|
/// Less than or equal 500 characters
|
||||||
|
/// </summary>
|
||||||
public struct IncomingMessage()
|
public struct IncomingMessage()
|
||||||
{
|
{
|
||||||
[JsonProperty("message")] public string Message = string.Empty;
|
[JsonProperty("message")] public string Message = string.Empty;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Channel must be a valid uint number
|
||||||
|
/// </summary>
|
||||||
public struct IncomingChannel()
|
public struct IncomingChannel()
|
||||||
{
|
{
|
||||||
[JsonProperty("channel")] public uint Channel = uint.MaxValue;
|
[JsonProperty("channel")] public uint Channel = uint.MaxValue;
|
||||||
|
|||||||
@@ -74,10 +74,7 @@
|
|||||||
|
|
||||||
updateChannelHint(templates) {
|
updateChannelHint(templates) {
|
||||||
this.elements.channelHint.innerHTML = '';
|
this.elements.channelHint.innerHTML = '';
|
||||||
|
this.elements.channelHint.appendChild(this.processTemplate(templates));
|
||||||
for(const template of templates) {
|
|
||||||
this.elements.channelHint.appendChild(this.processTemplate(template));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
updateChannels(channels) {
|
updateChannels(channels) {
|
||||||
@@ -131,14 +128,11 @@
|
|||||||
const liMessage = document.createElement('li');
|
const liMessage = document.createElement('li');
|
||||||
const spanTimestamp = document.createElement('span');
|
const spanTimestamp = document.createElement('span');
|
||||||
spanTimestamp.classList.add('timestamp');
|
spanTimestamp.classList.add('timestamp');
|
||||||
const spanMessage = document.createElement('span');
|
|
||||||
spanMessage.classList.add('message');
|
|
||||||
|
|
||||||
spanTimestamp.innerText = messageData.timestamp;
|
spanTimestamp.innerText = messageData.timestamp;
|
||||||
|
|
||||||
for(const template of messageData.templates) {
|
const spanMessage = document.createElement('span');
|
||||||
spanMessage.appendChild(this.processTemplate(template));
|
spanMessage.classList.add('message');
|
||||||
}
|
spanMessage.appendChild(this.processTemplate(messageData.templates))
|
||||||
|
|
||||||
liMessage.appendChild(spanTimestamp);
|
liMessage.appendChild(spanTimestamp);
|
||||||
liMessage.appendChild(spanMessage);
|
liMessage.appendChild(spanMessage);
|
||||||
@@ -149,27 +143,32 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
processTemplate(template) {
|
processTemplate(templates) {
|
||||||
const spanElement = document.createElement('span');
|
const frag = document.createDocumentFragment();
|
||||||
switch (template.payload) {
|
|
||||||
case 'text':
|
for( const template of templates ) {
|
||||||
this.processTextTemplate(template, spanElement);
|
const spanElement = frag.createElement('span');
|
||||||
break;
|
switch (template.payload) {
|
||||||
case 'url':
|
case 'text':
|
||||||
this.processUrlTemplate(template, spanElement);
|
this.processTextTemplate(template, spanElement);
|
||||||
break;
|
break;
|
||||||
case 'emote':
|
case 'url':
|
||||||
this.processEmote(template, spanElement);
|
this.processUrlTemplate(template, spanElement);
|
||||||
break;
|
break;
|
||||||
case 'icon':
|
case 'emote':
|
||||||
this.processIcon(template, spanElement);
|
this.processEmote(template, spanElement);
|
||||||
break;
|
break;
|
||||||
case 'empty':
|
case 'icon':
|
||||||
// Do nothing
|
this.processIcon(template, spanElement);
|
||||||
break;
|
break;
|
||||||
|
case 'empty':
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
frag.appendChild(this.processTemplate(template));
|
||||||
}
|
}
|
||||||
|
|
||||||
return spanElement;
|
return frag;
|
||||||
}
|
}
|
||||||
|
|
||||||
processTextTemplate(template, spanContent) {
|
processTextTemplate(template, spanContent) {
|
||||||
@@ -178,27 +177,26 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
processUrlTemplate(template, spanContent) {
|
processUrlTemplate(template, spanContent) {
|
||||||
// TODO Sanitize href?
|
const urlElement = document.createElement('a');
|
||||||
let urlElement = document.createElement('a');
|
|
||||||
urlElement.innerText = template.content;
|
urlElement.innerText = template.content;
|
||||||
urlElement.href = template.content;
|
urlElement.href = encodeURI(template.content);
|
||||||
urlElement.target = '_blank'
|
urlElement.target = '_blank'
|
||||||
|
|
||||||
this.processColor(template, spanContent);
|
this.processColor(template, spanContent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// converts a RGBA uint number to components
|
||||||
processColor(template, spanContent) {
|
processColor(template, spanContent) {
|
||||||
let r = (template.color & 0xFF000000) >>> 24;
|
const r = (template.color & 0xFF000000) >>> 24;
|
||||||
let g = (template.color & 0xFF0000) >>> 16;
|
const g = (template.color & 0xFF0000) >>> 16;
|
||||||
let b = (template.color & 0xFF00) >>> 8;
|
const b = (template.color & 0xFF00) >>> 8;
|
||||||
let a = (template.color & 0xFF) / 255.0;
|
const a = (template.color & 0xFF) / 255.0;
|
||||||
|
|
||||||
spanContent.style.color = `rgba(${r}, ${g}, ${b}, ${a})`;
|
spanContent.style.color = `rgba(${r}, ${g}, ${b}, ${a})`;
|
||||||
}
|
}
|
||||||
|
|
||||||
processEmote(template, spanContent) {
|
processEmote(template, spanContent) {
|
||||||
// TODO Sanitize url?
|
const imgElement = document.createElement('img');
|
||||||
let imgElement = document.createElement('img');
|
|
||||||
imgElement.src = `/emote/${template.content}`;
|
imgElement.src = `/emote/${template.content}`;
|
||||||
|
|
||||||
spanContent.classList.add('emote-icon');
|
spanContent.classList.add('emote-icon');
|
||||||
|
|||||||
Reference in New Issue
Block a user