-
-
Notifications
You must be signed in to change notification settings - Fork 203
/
SentryUser.cs
191 lines (172 loc) · 4.61 KB
/
SentryUser.cs
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using Sentry.Extensibility;
using Sentry.Internal.Extensions;
namespace Sentry;
/// <summary>
/// An interface which describes the authenticated User for a request.
/// </summary>
/// <see href="https://develop.sentry.dev/sdk/event-payloads/user/"/>
public sealed class SentryUser : IJsonSerializable
{
internal Action<SentryUser>? PropertyChanged { get; set; }
private string? _id;
private string? _username;
private string? _email;
private string? _ipAddress;
private string? _segment;
private IDictionary<string, string>? _other;
/// <summary>
/// The unique ID of the user.
/// </summary>
public string? Id
{
get => _id;
set
{
if (_id != value)
{
_id = value;
PropertyChanged?.Invoke(this);
}
}
}
/// <summary>
/// The username of the user.
/// </summary>
public string? Username
{
get => _username;
set
{
if (_username != value)
{
_username = value;
PropertyChanged?.Invoke(this);
}
}
}
/// <summary>
/// The email address of the user.
/// </summary>
public string? Email
{
get => _email;
set
{
if (_email != value)
{
_email = value;
PropertyChanged?.Invoke(this);
}
}
}
/// <summary>
/// The IP address of the user.
/// </summary>
public string? IpAddress
{
get => _ipAddress;
set
{
if (_ipAddress != value)
{
_ipAddress = value;
PropertyChanged?.Invoke(this);
}
}
}
/// <summary>
/// The segment the user belongs to.
/// </summary>
public string? Segment
{
get => _segment;
set
{
if (_segment != value)
{
_segment = value;
PropertyChanged?.Invoke(this);
}
}
}
/// <summary>
/// Additional information about the user.
/// </summary>
public IDictionary<string, string> Other
{
get => _other ??= new Dictionary<string, string>();
set
{
if (_other != value)
{
_other = value;
PropertyChanged?.Invoke(this);
}
}
}
/// <summary>
/// Clones the current <see cref="SentryUser"/> instance.
/// </summary>
/// <returns>The cloned user.</returns>
public SentryUser Clone()
{
var user = new SentryUser();
CopyTo(user);
return user;
}
internal void CopyTo(SentryUser? user)
{
if (user == null)
{
return;
}
user.Id ??= Id;
user.Username ??= Username;
user.Email ??= Email;
user.IpAddress ??= IpAddress;
user.Segment ??= Segment;
user._other ??= _other?.ToDictionary(
entry => entry.Key,
entry => entry.Value);
}
internal bool HasAnyData() =>
Id is not null ||
Username is not null ||
Email is not null ||
IpAddress is not null ||
Segment is not null ||
_other?.Count > 0;
/// <inheritdoc />
public void WriteTo(Utf8JsonWriter writer, IDiagnosticLogger? _)
{
writer.WriteStartObject();
writer.WriteStringIfNotWhiteSpace("id", Id);
writer.WriteStringIfNotWhiteSpace("username", Username);
writer.WriteStringIfNotWhiteSpace("email", Email);
writer.WriteStringIfNotWhiteSpace("ip_address", IpAddress);
writer.WriteStringIfNotWhiteSpace("segment", Segment);
writer.WriteStringDictionaryIfNotEmpty("other", _other!);
writer.WriteEndObject();
}
/// <summary>
/// Parses from JSON.
/// </summary>
public static SentryUser FromJson(JsonElement json)
{
var id = json.GetPropertyOrNull("id")?.GetString();
var username = json.GetPropertyOrNull("username")?.GetString();
var email = json.GetPropertyOrNull("email")?.GetString();
var ip = json.GetPropertyOrNull("ip_address")?.GetString();
var segment = json.GetPropertyOrNull("segment")?.GetString();
var other = json.GetPropertyOrNull("other")?.GetStringDictionaryOrNull();
return new SentryUser
{
Id = id,
Username = username,
Email = email,
IpAddress = ip,
Segment = segment,
_other = other?.WhereNotNullValue().ToDict()
};
}
}