-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathChatMessage.cs
More file actions
104 lines (90 loc) · 3.97 KB
/
Copy pathChatMessage.cs
File metadata and controls
104 lines (90 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using DCL.FeatureFlags;
using System;
using System.Globalization;
namespace DCL.Chat.History
{
public readonly struct ChatMessage : IEquatable<ChatMessage>
{
private const string DCL_SYSTEM_SENDER = "DCL System";
public readonly string Message;
public readonly string MessageId;
public readonly string SenderValidatedName;
public readonly string SenderWalletId;
public readonly string SenderWalletAddress;
public readonly bool IsSenderOfficial;
public readonly bool IsSentByOwnUser;
public readonly bool IsSystemMessage;
public readonly bool IsMention;
/// <summary>
/// The instant when the message was sent (UTC), in OLE Automation Date format. Zero means null or unassigned.
/// </summary>
public readonly double SentTimestampRaw;
public readonly DateTime? SentTimestamp;
public ChatMessage(
string message,
string senderValidatedName,
string senderWalletAddress,
bool isSentByOwnUser,
string senderWalletId,
double sentTimestamp,
bool isMention = false,
bool isSystemMessage = false)
{
// NOTE: pseudo-unique ID generation for non-system messages
MessageId = isSystemMessage
? Guid.NewGuid().ToString()
: ChatUtils.GetId(senderWalletAddress, sentTimestamp);
Message = message;
SenderValidatedName = senderValidatedName;
SenderWalletAddress = senderWalletAddress;
IsSentByOwnUser = isSentByOwnUser;
SenderWalletId = senderWalletId;
IsMention = isMention;
IsSenderOfficial = OfficialWalletsHelper.Instance.IsOfficialWallet(senderWalletAddress);
IsSystemMessage = isSystemMessage;
SentTimestampRaw = sentTimestamp;
SentTimestamp = sentTimestamp != 0.0 ? DateTime.FromOADate(sentTimestamp) : null;
}
public static ChatMessage CopyWithNewMessage(string newMessage, ChatMessage chatMessage) =>
new ( newMessage,
chatMessage.SenderValidatedName,
chatMessage.SenderWalletAddress,
chatMessage.IsSentByOwnUser,
chatMessage.SenderWalletId,
chatMessage.SentTimestampRaw,
chatMessage.IsMention,
false);
public static ChatMessage NewFromSystem(string message) =>
new (message, DCL_SYSTEM_SENDER, string.Empty, true,
string.Empty, DateTime.UtcNow.ToOADate(), false, true);
public bool Equals(ChatMessage other) => MessageId == other.MessageId;
public override bool Equals(object? obj) => obj is ChatMessage other && Equals(other);
public override int GetHashCode() => (MessageId != null ? MessageId.GetHashCode() : 0);
public override string ToString() =>
IsSystemMessage ? $"[System] {Message}" :
$"[{SenderValidatedName}] {Message}";
}
public static class ChatUtils
{
/// <summary>
/// Creates a composite, pseudo-unique identifier for a chat message.
/// </summary>
public static string GetId(string walletId, double timestampRaw) =>
$"{walletId}:{timestampRaw.ToString(CultureInfo.InvariantCulture)}";
/// <summary>
/// Recovers the sender wallet address from an id produced by <see cref="GetId" />.
/// Returns false for ids that don't follow the composite format (e.g. system-message GUIDs).
/// </summary>
public static bool TryGetSenderWalletAddress(string messageId, out string walletAddress)
{
int separator = messageId.IndexOf(':');
if (separator <= 0)
{
walletAddress = string.Empty;
return false;
}
walletAddress = messageId.Substring(0, separator);
return true;
}
}
}