234 lines
6.9 KiB
C#
234 lines
6.9 KiB
C#
using System.Collections.Generic;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
using GsDChat.Core.Models;
|
|
|
|
namespace GsDChat.Core.Protocol;
|
|
|
|
public static class MessageTypes
|
|
{
|
|
public const string Auth = "AUTH";
|
|
public const string AuthResult = "AUTH_RESULT";
|
|
public const string UiSync = "UI_SYNC";
|
|
public const string SendMessage = "SEND_MESSAGE";
|
|
public const string MessageBroadcast = "MESSAGE_BROADCAST";
|
|
public const string HistorySync = "HISTORY_SYNC";
|
|
public const string JoinChannel = "JOIN_CHANNEL";
|
|
public const string ChannelListSync = "CHANNEL_LIST_SYNC";
|
|
public const string CreateChannel = "CREATE_CHANNEL";
|
|
public const string DeleteChannel = "DELETE_CHANNEL";
|
|
public const string StartDm = "START_DM";
|
|
public const string CreateGroup = "CREATE_GROUP";
|
|
public const string InviteToGroup = "INVITE_TO_GROUP";
|
|
public const string LeaveGroup = "LEAVE_GROUP";
|
|
public const string PublishPublicKey = "PUBLISH_PUBLIC_KEY";
|
|
public const string ToggleE2ee = "TOGGLE_E2EE";
|
|
public const string FetchHistory = "FETCH_HISTORY";
|
|
public const string UserListSync = "USER_LIST_SYNC";
|
|
public const string FileListSync = "FILE_LIST_SYNC";
|
|
public const string SystemNotice = "SYSTEM_NOTICE";
|
|
public const string Error = "ERROR";
|
|
public const string ExecuteCommand = "EXECUTE_COMMAND";
|
|
public const string CommandResult = "COMMAND_RESULT";
|
|
public const string Ping = "PING";
|
|
public const string Pong = "PONG";
|
|
}
|
|
|
|
public class NetworkEnvelope
|
|
{
|
|
private static readonly JsonSerializerOptions s_jsonOptions = new()
|
|
{
|
|
PropertyNameCaseInsensitive = true,
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
|
};
|
|
|
|
[JsonPropertyName("type")]
|
|
public string Type { get; set; } = string.Empty;
|
|
|
|
[JsonPropertyName("payload")]
|
|
public string Payload { get; set; } = "{}";
|
|
|
|
public static NetworkEnvelope Create<T>(string type, T payload)
|
|
{
|
|
return new NetworkEnvelope
|
|
{
|
|
Type = type,
|
|
Payload = JsonSerializer.Serialize(payload, s_jsonOptions)
|
|
};
|
|
}
|
|
|
|
public T? Unpack<T>()
|
|
{
|
|
return JsonSerializer.Deserialize<T>(Payload, s_jsonOptions);
|
|
}
|
|
}
|
|
|
|
public class AuthPacket
|
|
{
|
|
public string Token { get; set; } = string.Empty;
|
|
public string? ServerPassword { get; set; }
|
|
}
|
|
|
|
public class AuthResultPacket
|
|
{
|
|
public bool Success { get; set; }
|
|
public string UserId { get; set; } = string.Empty;
|
|
public string Username { get; set; } = string.Empty;
|
|
public string Role { get; set; } = UserRoles.Member;
|
|
public string? PublicKey { get; set; }
|
|
public string? EncryptedPrivateKey { get; set; }
|
|
public bool E2eeEnabled { get; set; } = true;
|
|
public string? ErrorMessage { get; set; }
|
|
public ServerUiSync? UiSync { get; set; }
|
|
public string? ActiveChannelId { get; set; }
|
|
}
|
|
|
|
public class SendMessagePacket
|
|
{
|
|
public string ChannelId { get; set; } = string.Empty;
|
|
public string Content { get; set; } = string.Empty;
|
|
public bool IsEncrypted { get; set; } = false;
|
|
public string? SenderPublicKey { get; set; }
|
|
public string? RecipientPublicKey { get; set; }
|
|
}
|
|
|
|
public class MessageBroadcastPacket
|
|
{
|
|
public ChatMessage Message { get; set; } = new();
|
|
}
|
|
|
|
public class HistorySyncPacket
|
|
{
|
|
public string ChannelId { get; set; } = string.Empty;
|
|
public List<ChatMessage> Messages { get; set; } = new();
|
|
public bool Prepend { get; set; } = false;
|
|
public bool HasMore { get; set; } = false;
|
|
public int Offset { get; set; } = 0;
|
|
}
|
|
|
|
public class FetchHistoryPacket
|
|
{
|
|
public string ChannelId { get; set; } = string.Empty;
|
|
public int Offset { get; set; } = 0;
|
|
public int Limit { get; set; } = 50;
|
|
}
|
|
|
|
public class JoinChannelPacket
|
|
{
|
|
public string ChannelId { get; set; } = string.Empty;
|
|
}
|
|
|
|
public class ChannelListSyncPacket
|
|
{
|
|
public List<Channel> Channels { get; set; } = new();
|
|
public List<Channel> DirectMessages { get; set; } = new();
|
|
public List<Channel> GroupChats { get; set; } = new();
|
|
public string ActiveChannelId { get; set; } = string.Empty;
|
|
}
|
|
|
|
public class CreateChannelPacket
|
|
{
|
|
public string Name { get; set; } = string.Empty;
|
|
public string Topic { get; set; } = string.Empty;
|
|
}
|
|
|
|
public class DeleteChannelPacket
|
|
{
|
|
public string ChannelId { get; set; } = string.Empty;
|
|
}
|
|
|
|
public class StartDmPacket
|
|
{
|
|
public string TargetUserId { get; set; } = string.Empty;
|
|
public string TargetUsername { get; set; } = string.Empty;
|
|
}
|
|
|
|
public class CreateGroupPacket
|
|
{
|
|
public string Name { get; set; } = string.Empty;
|
|
public string Topic { get; set; } = string.Empty;
|
|
public List<string> MemberUsernames { get; set; } = new();
|
|
public int? MaxUsers { get; set; }
|
|
}
|
|
|
|
public class InviteToGroupPacket
|
|
{
|
|
public string ChannelId { get; set; } = string.Empty;
|
|
public string TargetUsername { get; set; } = string.Empty;
|
|
}
|
|
|
|
public class LeaveGroupPacket
|
|
{
|
|
public string ChannelId { get; set; } = string.Empty;
|
|
}
|
|
|
|
public class PublishPublicKeyPacket
|
|
{
|
|
public string PublicKey { get; set; } = string.Empty;
|
|
public string? EncryptedPrivateKey { get; set; }
|
|
public bool ResetKeys { get; set; } = false;
|
|
}
|
|
|
|
public class ToggleE2eePacket
|
|
{
|
|
public string Scope { get; set; } = "dm"; // "dm" or "account"
|
|
public string? ChannelId { get; set; }
|
|
public bool Enabled { get; set; } = true;
|
|
}
|
|
|
|
public class UserSummary
|
|
{
|
|
public string Id { get; set; } = string.Empty;
|
|
public string Username { get; set; } = string.Empty;
|
|
public string Role { get; set; } = UserRoles.Member;
|
|
public bool IsOnline { get; set; }
|
|
public string? PublicKey { get; set; }
|
|
public bool E2eeEnabled { get; set; } = true;
|
|
}
|
|
|
|
public class UserListSyncPacket
|
|
{
|
|
public List<UserSummary> Users { get; set; } = new();
|
|
}
|
|
|
|
public class FileSummary
|
|
{
|
|
public string Id { get; set; } = string.Empty;
|
|
public string ChannelId { get; set; } = string.Empty;
|
|
public string OriginalFileName { get; set; } = string.Empty;
|
|
public string ContentType { get; set; } = string.Empty;
|
|
public long FileSizeBytes { get; set; }
|
|
public string UploaderUsername { get; set; } = string.Empty;
|
|
public System.DateTime CreatedAt { get; set; }
|
|
}
|
|
|
|
public class FileListSyncPacket
|
|
{
|
|
public List<FileSummary> Files { get; set; } = new();
|
|
}
|
|
|
|
public class SystemNoticePacket
|
|
{
|
|
public string Text { get; set; } = string.Empty;
|
|
public string Level { get; set; } = "Info"; // Info, Warning, Success
|
|
}
|
|
|
|
public class ErrorPacket
|
|
{
|
|
public string Message { get; set; } = string.Empty;
|
|
public string? Code { get; set; }
|
|
}
|
|
|
|
public class ExecuteCommandPacket
|
|
{
|
|
public string Command { get; set; } = string.Empty;
|
|
public string Args { get; set; } = string.Empty;
|
|
public string ChannelId { get; set; } = string.Empty;
|
|
}
|
|
|
|
public class CommandResultPacket
|
|
{
|
|
public bool Success { get; set; }
|
|
public string Output { get; set; } = string.Empty;
|
|
public string ChannelId { get; set; } = string.Empty;
|
|
}
|