-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathPepClient.cs
More file actions
703 lines (589 loc) · 21.8 KB
/
PepClient.cs
File metadata and controls
703 lines (589 loc) · 21.8 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using System.Xml;
using Waher.Events;
using Waher.Networking.XMPP.PEP.Events;
using Waher.Networking.XMPP.PubSub;
using Waher.Networking.XMPP.PubSub.Events;
using Waher.Runtime.Inventory;
namespace Waher.Networking.XMPP.PEP
{
/// <summary>
/// Client managing the Personal Eventing Protocol (XEP-0163).
/// https://xmpp.org/extensions/xep-0163.html
/// </summary>
public class PepClient : XmppExtension
{
private static Dictionary<string, IPersonalEvent> personalEventTypes = GetPersonalEventTypes();
private PubSubClient pubSubClient;
private readonly string pubSubComponentAddress;
private readonly Dictionary<Type, EventHandlerAsync<PersonalEventNotificationEventArgs>[]> handlers = new Dictionary<Type, EventHandlerAsync<PersonalEventNotificationEventArgs>[]>();
private readonly bool hasPubSubComponent;
/// <summary>
/// Client managing the Personal Eventing Protocol (XEP-0163).
/// https://xmpp.org/extensions/xep-0163.html
/// </summary>
/// <param name="Client">XMPP Client to use.</param>
public PepClient(XmppClient Client)
: this(Client, string.Empty)
{
}
/// <summary>
/// Client managing the Personal Eventing Protocol (XEP-0163).
/// https://xmpp.org/extensions/xep-0163.html
/// </summary>
/// <param name="Client">XMPP Client to use.</param>
/// <param name="PubSubComponentAddress">Default Publish/Subscribe component address.</param>
public PepClient(XmppClient Client, string PubSubComponentAddress)
: base(Client)
{
this.pubSubComponentAddress = PubSubComponentAddress;
this.pubSubClient = new PubSubClient(Client, this.pubSubComponentAddress);
this.hasPubSubComponent = !string.IsNullOrEmpty(this.pubSubComponentAddress);
this.pubSubClient.ItemNotification += this.PubSubClient_ItemNotification;
this.PubSubClient.ItemRetracted += this.PubSubClient_ItemRetracted;
}
/// <inheritdoc/>
public override void Dispose()
{
if (!(this.pubSubClient is null))
{
this.pubSubClient.ItemNotification -= this.PubSubClient_ItemNotification;
this.PubSubClient.ItemRetracted -= this.PubSubClient_ItemRetracted;
this.pubSubClient.Dispose();
this.pubSubClient = null;
}
LinkedList<IPersonalEvent> ToUnregister = new LinkedList<IPersonalEvent>();
lock (this.handlers)
{
foreach (Type T in this.handlers.Keys)
{
try
{
ToUnregister.AddLast((IPersonalEvent)Types.Instantiate(T));
}
catch (Exception ex)
{
Log.Exception(ex);
}
}
this.handlers.Clear();
}
foreach (IPersonalEvent PersonalEvent in ToUnregister)
this.client.UnregisterFeature(PersonalEvent.Namespace + "+notify");
base.Dispose();
}
/// <summary>
/// <see cref="PubSubClient"/> used for the Personal Eventing Protocol. Use this client to perform administrative tasks
/// of the PEP service.
/// </summary>
public PubSubClient PubSubClient => this.pubSubClient;
/// <summary>
/// Implemented extensions.
/// </summary>
public override string[] Extensions => new string[] { "XEP-0163" };
#region Personal Eventing Protocol (XEP-0163)
/// <summary>
/// Publishes an item on a node.
/// </summary>
/// <param name="Node">Node name.</param>
/// <param name="Callback">Method to call when operation completes.</param>
/// <param name="State">State object to pass on to callback method.</param>
public Task Publish(string Node, EventHandlerAsync<ItemResultEventArgs> Callback, object State)
{
return this.pubSubClient.Publish(string.Empty, Node, string.Empty, Callback, State);
}
/// <summary>
/// Publishes an item on a node.
/// </summary>
/// <param name="Node">Node name.</param>
/// <param name="PayloadXml">Payload XML.</param>
/// <param name="Callback">Method to call when operation completes.</param>
/// <param name="State">State object to pass on to callback method.</param>
public Task Publish(string Node, string PayloadXml, EventHandlerAsync<ItemResultEventArgs> Callback, object State)
{
return this.pubSubClient.Publish(string.Empty, Node, string.Empty, PayloadXml, Callback, State);
}
/// <summary>
/// Publishes an item on a node.
/// </summary>
/// <param name="PersonalEvent">Personal event.</param>
/// <param name="Callback">Method to call when operation completes.</param>
/// <param name="State">State object to pass on to callback method.</param>
public Task Publish(IPersonalEvent PersonalEvent, EventHandlerAsync<ItemResultEventArgs> Callback, object State)
{
string ItemId = PersonalEvent.ItemId;
if (ItemId is null)
return this.pubSubClient.Publish(string.Empty, PersonalEvent.Node, string.Empty, PersonalEvent.PayloadXml, Callback, State);
else
return this.pubSubClient.Publish(string.Empty, PersonalEvent.Node, ItemId, PersonalEvent.PayloadXml, Callback, State);
}
/// <summary>
/// Publishes an item on a node.
/// </summary>
/// <param name="Node">Node name.</param>
/// <param name="ItemId">Item identity, if available. If used, and an existing item
/// is available with that identity, it will be updated with the new content.</param>
/// <param name="PayloadXml">Payload XML.</param>
/// <param name="Callback">Method to call when operation completes.</param>
/// <param name="State">State object to pass on to callback method.</param>
public Task Publish(string Node, string ItemId, string PayloadXml, EventHandlerAsync<ItemResultEventArgs> Callback, object State)
{
return this.pubSubClient.Publish(string.Empty, Node, ItemId, PayloadXml, Callback, State);
}
/// <summary>
/// Publishes an item on a node.
/// </summary>
/// <param name="Node">Node name.</param>
/// <returns>ID of published item.</returns>
public async Task<string> PublishAsync(string Node)
{
TaskCompletionSource<string> Result = new TaskCompletionSource<string>();
await this.pubSubClient.Publish(string.Empty, Node, string.Empty, this.AsyncCallback, Result);
return await Result.Task;
}
/// <summary>
/// Publishes an item on a node.
/// </summary>
/// <param name="Node">Node name.</param>
/// <param name="PayloadXml">Payload XML.</param>
/// <returns>ID of published item.</returns>
public async Task<string> PublishAsync(string Node, string PayloadXml)
{
TaskCompletionSource<string> Result = new TaskCompletionSource<string>();
await this.pubSubClient.Publish(string.Empty, Node, string.Empty, PayloadXml, this.AsyncCallback, Result);
return await Result.Task;
}
/// <summary>
/// Publishes an item on a node.
/// </summary>
/// <param name="PersonalEvent">Personal event.</param>
/// <returns>ID of published item.</returns>
public async Task<string> PublishAsync(IPersonalEvent PersonalEvent)
{
TaskCompletionSource<string> Result = new TaskCompletionSource<string>();
string ItemId = PersonalEvent.ItemId;
if (ItemId is null)
await this.pubSubClient.Publish(string.Empty, PersonalEvent.Node, string.Empty, PersonalEvent.PayloadXml, this.AsyncCallback, Result);
else
await this.pubSubClient.Publish(string.Empty, PersonalEvent.Node, ItemId, PersonalEvent.PayloadXml, this.AsyncCallback, Result);
return await Result.Task;
}
/// <summary>
/// Publishes an item on a node.
/// </summary>
/// <param name="Node">Node name.</param>
/// <param name="ItemId">Item identity, if available. If used, and an existing item
/// is available with that identity, it will be updated with the new content.</param>
/// <param name="PayloadXml">Payload XML.</param>
/// <returns>ID of published item.</returns>
public async Task<string> PublishAsync(string Node, string ItemId, string PayloadXml)
{
TaskCompletionSource<string> Result = new TaskCompletionSource<string>();
await this.pubSubClient.Publish(string.Empty, Node, ItemId, PayloadXml, this.AsyncCallback, Result);
return await Result.Task;
}
private Task AsyncCallback(object Sender, ItemResultEventArgs e)
{
TaskCompletionSource<string> Result = (TaskCompletionSource<string>)e.State;
if (e.Ok)
Result.TrySetResult(e.ItemId);
else
Result.TrySetException(e.StanzaError ?? new Exception("Unable to publish event."));
return Task.CompletedTask;
}
private async Task PubSubClient_ItemNotification(object Sender, ItemNotificationEventArgs e)
{
if (this.hasPubSubComponent && e.From.IndexOf('@') < 0)
await this.NonPepItemNotification.Raise(this, e);
else
{
if (string.Compare(e.FromBareJID, this.client.BareJID, true) != 0)
{
RosterItem Item = this.client[e.FromBareJID];
if (Item is null || (Item.State != SubscriptionState.Both && Item.State != SubscriptionState.To))
return;
}
IPersonalEvent PersonalEvent = null;
foreach (XmlNode N in e.Item.ChildNodes)
{
if (N is XmlElement E && personalEventTypes.TryGetValue(E.LocalName + " " + E.NamespaceURI, out IPersonalEvent PersonalEvent2))
{
PersonalEvent = PersonalEvent2.Parse(E);
break;
}
}
if (!(PersonalEvent is null))
{
EventHandlerAsync<PersonalEventNotificationEventArgs>[] Handlers;
lock (this.handlers)
{
if (!this.handlers.TryGetValue(PersonalEvent.GetType(), out Handlers))
return;
}
PersonalEventNotificationEventArgs e2 = new PersonalEventNotificationEventArgs(PersonalEvent, this, e);
foreach (EventHandlerAsync<PersonalEventNotificationEventArgs> Handler in Handlers)
await Handler.Raise(this, e2);
}
}
}
/// <summary>
/// Tries to parse a personal event from its XML representation.
/// </summary>
/// <param name="Xml">XML representation of event.</param>
/// <param name="Event">Parsed event, if recognized.</param>
/// <returns>If the XML could be parsed as a personal event.</returns>
public static bool TryParse(XmlElement Xml, out IPersonalEvent Event)
{
if (personalEventTypes.TryGetValue(Xml.LocalName + " " + Xml.NamespaceURI, out IPersonalEvent PersonalEvent))
{
Event = PersonalEvent.Parse(Xml);
return true;
}
else
{
Event = null;
return false;
}
}
private async Task PubSubClient_ItemRetracted(object Sender, ItemNotificationEventArgs e)
{
if (this.hasPubSubComponent && e.From.IndexOf('@') < 0)
await this.NonPepItemRetraction.Raise(this, e);
}
/// <summary>
/// Event raised when an item notification from the publish/subscribe component that is not related to PEP has been received.
/// </summary>
public event EventHandlerAsync<ItemNotificationEventArgs> NonPepItemNotification = null;
/// <summary>
/// Event raised when an item retraction from the publish/subscribe component that is not related to PEP has been received.
/// </summary>
public event EventHandlerAsync<ItemNotificationEventArgs> NonPepItemRetraction = null;
private static Dictionary<string, IPersonalEvent> GetPersonalEventTypes()
{
Types.OnInvalidated += Types_OnInvalidated;
return GetPersonalEventTypes2();
}
private static Dictionary<string, IPersonalEvent> GetPersonalEventTypes2()
{
Dictionary<string, IPersonalEvent> Result = new Dictionary<string, IPersonalEvent>();
foreach (Type T in Types.GetTypesImplementingInterface(typeof(IPersonalEvent)))
{
ConstructorInfo CI = Types.GetDefaultConstructor(T);
if (CI is null)
continue;
try
{
IPersonalEvent PersonalEvent = (IPersonalEvent)CI.Invoke(Types.NoParameters);
Result[PersonalEvent.LocalName + " " + PersonalEvent.Namespace] = PersonalEvent;
}
catch (Exception ex)
{
Log.Exception(ex);
}
}
return Result;
}
private static void Types_OnInvalidated(object Sender, EventArgs e)
{
personalEventTypes = GetPersonalEventTypes2();
}
/// <summary>
/// Registers an event handler of a specific type of personal events.
/// </summary>
/// <param name="PersonalEventType">Type of personal event.</param>
/// <param name="Handler">Event handler.</param>
public void RegisterHandler(Type PersonalEventType, EventHandlerAsync<PersonalEventNotificationEventArgs> Handler)
{
if (!typeof(IPersonalEvent).IsAssignableFrom(PersonalEventType.GetTypeInfo()))
throw new ArgumentException("Not a personal event type.", nameof(PersonalEventType));
IPersonalEvent PersonalEvent = (IPersonalEvent)Types.Instantiate(PersonalEventType);
lock (this.handlers)
{
if (!this.handlers.TryGetValue(PersonalEventType, out EventHandlerAsync<PersonalEventNotificationEventArgs>[] Handlers))
Handlers = null;
if (Handlers is null)
Handlers = new EventHandlerAsync<PersonalEventNotificationEventArgs>[] { Handler };
else
{
int c = Handlers.Length;
EventHandlerAsync<PersonalEventNotificationEventArgs>[] Handlers2 = new EventHandlerAsync<PersonalEventNotificationEventArgs>[c + 1];
Array.Copy(Handlers, 0, Handlers2, 0, c);
Handlers2[c] = Handler;
Handlers = Handlers2;
}
this.handlers[PersonalEventType] = Handlers;
}
this.client.RegisterFeature(PersonalEvent.Namespace + "+notify");
}
/// <summary>
/// Unregisters an event handler of a specific type of personal events.
/// </summary>
/// <param name="PersonalEventType">Type of personal event.</param>
/// <param name="Handler">Event handler.</param>
/// <returns>If the event handler was found and removed.</returns>
public bool UnregisterHandler(Type PersonalEventType, EventHandlerAsync<PersonalEventNotificationEventArgs> Handler)
{
lock (this.handlers)
{
if (!this.handlers.TryGetValue(PersonalEventType, out EventHandlerAsync<PersonalEventNotificationEventArgs>[] Handlers))
return false;
List<EventHandlerAsync<PersonalEventNotificationEventArgs>> List = new List<EventHandlerAsync<PersonalEventNotificationEventArgs>>();
List.AddRange(Handlers);
if (!List.Remove(Handler))
return false;
if (List.Count == 0)
this.handlers.Remove(PersonalEventType);
else
{
Handlers = List.ToArray();
this.handlers[PersonalEventType] = Handlers;
}
this.client.UnregisterFeature(PersonalEventType.Namespace + "+notify");
return true;
}
}
#endregion
#region XEP-0080: User Location
/// <summary>
/// Publishes a personal user location.
/// </summary>
/// <param name="Location">User location</param>
public Task Publish(UserLocation Location)
{
return this.Publish(Location, null, null);
}
/// <summary>
/// Event raised when a user location personal event has been received.
/// </summary>
public event EventHandlerAsync<UserLocationEventArgs> OnUserLocation
{
add
{
if (this.onUserLocation is null)
this.RegisterHandler(typeof(UserLocation), this.UserLocationEventHandler);
this.onUserLocation += value;
}
remove
{
this.onUserLocation -= value;
if (this.onUserLocation is null)
this.UnregisterHandler(typeof(UserLocation), this.UserLocationEventHandler);
}
}
private EventHandlerAsync<UserLocationEventArgs> onUserLocation = null;
private async Task UserLocationEventHandler(object Sender, PersonalEventNotificationEventArgs e)
{
if (e.PersonalEvent is UserLocation UserLocation)
await this.onUserLocation.Raise(this, new UserLocationEventArgs(UserLocation, e));
}
#endregion
#region XEP-0084: User Avatar
/// <summary>
/// Publishes a personal user avatar.
/// </summary>
/// <param name="Images">Images of different types, representing the same avatar.</param>
public Task Publish(params UserAvatarImage[] Images)
{
List<UserAvatarReference> References = new List<UserAvatarReference>();
foreach (UserAvatarImage Image in Images)
{
UserAvatarData Data = new UserAvatarData()
{
Data = Image.Data
};
this.Publish(Data, null, null);
References.Add(new UserAvatarReference()
{
Bytes = Image.Data.Length,
Height = Image.Height,
Id = Data.ItemId,
Type = Image.ContentType,
URL = Image.URL,
Width = Image.Width
});
}
return this.Publish(new UserAvatarMetaData(References.ToArray()), null, null);
}
/// <summary>
/// Event raised when a user location personal event has been received.
/// </summary>
public event EventHandlerAsync<UserAvatarMetaDataEventArgs> OnUserAvatarMetaData
{
add
{
if (this.onUserAvatarMetaData is null)
this.RegisterHandler(typeof(UserAvatarMetaData), this.UserAvatarMetaDataEventHandler);
this.onUserAvatarMetaData += value;
}
remove
{
this.onUserAvatarMetaData -= value;
if (this.onUserAvatarMetaData is null)
this.UnregisterHandler(typeof(UserAvatarMetaData), this.UserAvatarMetaDataEventHandler);
}
}
private EventHandlerAsync<UserAvatarMetaDataEventArgs> onUserAvatarMetaData = null;
private async Task UserAvatarMetaDataEventHandler(object Sender, PersonalEventNotificationEventArgs e)
{
if (e.PersonalEvent is UserAvatarMetaData UserAvatarMetaData)
await this.onUserAvatarMetaData.Raise(this, new UserAvatarMetaDataEventArgs(UserAvatarMetaData, e));
}
/// <summary>
/// Gets an avatar published by a user using the Personal Eventing Protocol
/// </summary>
/// <param name="UserBareJid">Bare JID of user publishing the avatar.</param>
/// <param name="Reference">Avatar reference, selected from an <see cref="UserAvatarMetaData"/> event.</param>
/// <param name="Callback">Method to call when avatar has been retrieved.</param>
/// <param name="State">State object to pass on to callback method.</param>
public Task GetUserAvatarData(string UserBareJid, UserAvatarReference Reference, EventHandlerAsync<UserAvatarImageEventArgs> Callback, object State)
{
return this.pubSubClient.GetItems(UserBareJid, UserAvatarData.AvatarDataNamespace, new string[] { Reference.Id }, async (Sender, e) =>
{
UserAvatarImage Image = null;
if (e.Ok)
{
foreach (PubSubItem Item in e.Items)
{
if (Item.ItemId == Reference.Id)
{
foreach (XmlNode N in Item.Item)
{
if (N is XmlElement E && E.LocalName == "data" && E.NamespaceURI == UserAvatarData.AvatarDataNamespace)
{
Image = new UserAvatarImage()
{
Data = Convert.FromBase64String(E.InnerText),
ContentType = Reference.Type,
Height = Reference.Height,
URL = Reference.URL,
Width = Reference.Width
};
break;
}
}
}
}
}
await Callback.Raise(this, new UserAvatarImageEventArgs(Image, e));
}, State);
}
#endregion
#region XEP-0107: User Mood
/// <summary>
/// Publishes a personal user mood.
/// </summary>
/// <param name="Mood">Mood</param>
/// <param name="Text">Custom</param>
public Task Publish(UserMoods Mood, string Text)
{
return this.Publish(new UserMood()
{
Mood = Mood,
Text = Text
}, null, null);
}
/// <summary>
/// Event raised when a user mood personal event has been received.
/// </summary>
public event EventHandlerAsync<UserMoodEventArgs> OnUserMood
{
add
{
if (this.onUserMood is null)
this.RegisterHandler(typeof(UserMood), this.UserMoodEventHandler);
this.onUserMood += value;
}
remove
{
this.onUserMood -= value;
if (this.onUserMood is null)
this.UnregisterHandler(typeof(UserMood), this.UserMoodEventHandler);
}
}
private EventHandlerAsync<UserMoodEventArgs> onUserMood = null;
private async Task UserMoodEventHandler(object Sender, PersonalEventNotificationEventArgs e)
{
if (e.PersonalEvent is UserMood UserMood)
await this.onUserMood.Raise(this, new UserMoodEventArgs(UserMood, e));
}
#endregion
#region XEP-0108: User Activity
/// <summary>
/// Publishes a personal user activity.
/// </summary>
/// <param name="GeneralActivity">General activity</param>
/// <param name="SpecificActivity">Specific activity</param>
/// <param name="Text">Custom</param>
public Task Publish(UserGeneralActivities GeneralActivity, UserSpecificActivities SpecificActivity, string Text)
{
return this.Publish(new UserActivity()
{
GeneralActivity = GeneralActivity,
SpecificActivity = SpecificActivity,
Text = Text
}, null, null);
}
/// <summary>
/// Event raised when a user activity personal event has been received.
/// </summary>
public event EventHandlerAsync<UserActivityEventArgs> OnUserActivity
{
add
{
if (this.onUserActivity is null)
this.RegisterHandler(typeof(UserActivity), this.UserActivityEventHandler);
this.onUserActivity += value;
}
remove
{
this.onUserActivity -= value;
if (this.onUserActivity is null)
this.UnregisterHandler(typeof(UserActivity), this.UserActivityEventHandler);
}
}
private EventHandlerAsync<UserActivityEventArgs> onUserActivity = null;
private async Task UserActivityEventHandler(object Sender, PersonalEventNotificationEventArgs e)
{
if (e.PersonalEvent is UserActivity UserActivity)
await this.onUserActivity.Raise(this, new UserActivityEventArgs(UserActivity, e));
}
#endregion
#region XEP-0118: User Tune
/// <summary>
/// Publishes a personal user activity.
/// </summary>
/// <param name="Tune">User tune</param>
public Task Publish(UserTune Tune)
{
return this.Publish(Tune, null, null);
}
/// <summary>
/// Event raised when a user tune personal event has been received.
/// </summary>
public event EventHandlerAsync<UserTuneEventArgs> OnUserTune
{
add
{
if (this.onUserTune is null)
this.RegisterHandler(typeof(UserTune), this.UserTuneEventHandler);
this.onUserTune += value;
}
remove
{
this.onUserTune -= value;
if (this.onUserTune is null)
this.UnregisterHandler(typeof(UserTune), this.UserTuneEventHandler);
}
}
private EventHandlerAsync<UserTuneEventArgs> onUserTune = null;
private async Task UserTuneEventHandler(object Sender, PersonalEventNotificationEventArgs e)
{
if (e.PersonalEvent is UserTune UserTune)
await this.onUserTune.Raise(this, new UserTuneEventArgs(UserTune, e));
}
#endregion
}
}