Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SendPresenceSubscription.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using System.Xml;
5using Waher.Content;
12using Waher.Script;
15
17{
22 {
27 : base("Xmpp/SendPresenceSubscription",
28 new KeyValuePair<Type, Expression>(typeof(Dictionary<string, object>), new Expression(jsonPattern)),
29 new KeyValuePair<Type, Expression>(typeof(XmlDocument), new Expression(xmlPattern)))
30 {
31 }
32
33 private static readonly string jsonPattern = Resources.LoadResourceAsText(typeof(SendPresenceSubscription).Namespace + ".JSON.SendPresenceSubscription.req");
34 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(SendPresenceSubscription).Namespace + ".XML.SendPresenceSubscription.req");
35
44 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
45 {
47
48 string To = (string)Parameters["PTo"].AssociatedObjectValue;
49
50 if (string.IsNullOrEmpty(To))
51 throw new BadRequestException("Missing recipient JID.");
52
53 if (!IsValidBareJID(To))
54 throw new BadRequestException("Recpient must be a Bare JID.");
55
56 string CustomXml = (string)Parameters["PCustomXml"]?.AssociatedObjectValue;
57 string Language = (string)Parameters["PLanguage"]?.AssociatedObjectValue;
58 string Id = (string)Parameters["PId"]?.AssociatedObjectValue;
59
60 if (string.IsNullOrEmpty(Id))
61 Id = Guid.NewGuid().ToString();
62
63 if (string.IsNullOrEmpty(CustomXml))
64 CustomXml = await GetApprovedLegalIDXml(User.UserName);
65 else
66 {
67 if (!XML.IsValidXml(CustomXml))
68 throw new BadRequestException("Not valid XML.");
69
70 CustomXml += await GetApprovedLegalIDXml(User.UserName);
71 }
72
73 Dictionary<string, object> NewItem;
74 bool Result;
75
76 if (XmppServerModule.Server is null)
77 {
79
80 if (Client is null)
81 {
82 if (Types.TryGetModuleParameter("XMPP", out object Obj) && Obj is XmppClient Client2)
83 Client = Client2;
84 }
85
86 if (Client is null)
87 throw new ServiceUnavailableException("XMPP Client not available.");
88
89 await Client.RequestPresenceSubscription(To, CustomXml);
90 Result = true;
91
92 Networking.XMPP.RosterItem Item = Client[To];
93
94 NewItem = new Dictionary<string, object>()
95 {
96 { "bareJid", Item?.BareJid ?? To },
97 { "pendingSubscription", (Item?.PendingSubscription ?? PendingSubscription.Subscribe) == PendingSubscription.Subscribe },
98 { "status", Item?.State ?? SubscriptionState.None },
99 { "name", Item?.Name ?? string.Empty },
100 { "Groups", Item?.Groups ?? new string[0] }
101 };
102 }
103 else
104 {
105 PersistenceLayer PersistenceLayer = XmppServerModule.PersistenceLayer ?? new PersistenceLayer();
106 IRosterItem Item = await PersistenceLayer.GetRosterItem(User.UserName, To);
107
109 Item?.Name ?? string.Empty, null, null, null);
110
111 Result = await XmppServerModule.Server.Presence("subscribe", Id,
112 new XmppAddress(To),
113 new XmppAddress(User.UserName + "@" + XmppServerModule.Server.Domain),
114 Language, CustomXml, XmppServerModule.Server);
115
116 Item = await PersistenceLayer.GetRosterItem(User.UserName, To);
117 NewItem = new Dictionary<string, object>()
118 {
119 { "bareJid", Item.BareJid.Value },
120 { "pendingSubscription", Item.PendingSubscription },
121 { "status", Item.Subscription },
122 { "name", Item.Name },
123 { "Groups", Item.Groups }
124 };
125 }
126
127 await Response.Return(new NamedDictionary<string, object>("PresenceSubscriptionResult", AgentNamespace)
128 {
129 { "sent", Result },
130 { "id", Id },
131 { "RosterItem", NewItem }
132 });
133 }
134 }
135}
A Named dictionary is a dictionary, with a local name and a namespace. Use it to return content that ...
Static class managing loading of resources stored as embedded resources or in content files.
Definition: Resources.cs:15
static string LoadResourceAsText(string ResourceName)
Loads a text resource from an embedded resource.
Definition: Resources.cs:96
Helps with common XML-related tasks.
Definition: XML.cs:19
static bool IsValidXml(string Xml)
Checks if a string is valid XML
Definition: XML.cs:1223
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:126
static XmppClient XmppClient
XMPP Client connection of gateway.
Definition: Gateway.cs:3187
The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repe...
Represents an HTTP request.
Definition: HttpRequest.cs:18
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:21
async Task Return(object Object)
Returns an object to the client. This method can only be called once per response,...
The server is currently unable to handle the request due to a temporary overloading or maintenance of...
Maintains information about an item in the roster.
Definition: RosterItem.cs:75
SubscriptionState State
roup Current subscription state.
Definition: RosterItem.cs:268
string[] Groups
Any groups the roster item belongs to.
Definition: RosterItem.cs:186
string BareJid
Bare JID of the roster item.
Definition: RosterItem.cs:276
PendingSubscription PendingSubscription
If there's a pending unanswered presence subscription or unsubscription request made to the contact.
Definition: RosterItem.cs:291
string Name
Name of the roster item.
Definition: RosterItem.cs:282
Contains information about one XMPP address.
Definition: XmppAddress.cs:9
CaseInsensitiveString Domain
Domain name.
Definition: XmppServer.cs:882
async Task< bool > Presence(string Type, string Id, XmppAddress From, string Language, Stanza Stanza, ISender Sender)
Presence stanza.
Definition: XmppServer.cs:2647
Manages an XMPP client connection. Implements XMPP, as defined in https://tools.ietf....
Definition: XmppClient.cs:59
Task RequestPresenceSubscription(string BareJid)
Requests subscription of presence information from a contact.
Definition: XmppClient.cs:4919
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:14
static bool TryGetModuleParameter(string Name, out object Value)
Tries to get a module parameter value.
Definition: Types.cs:583
Class managing a script expression.
Definition: Expression.cs:39
async Task< IRosterItem > GetRosterItem(CaseInsensitiveString UserName, CaseInsensitiveString Jid)
Gets a roster item for an account.
async Task< IRosterItem > SetRosterItem(CaseInsensitiveString UserName, CaseInsensitiveString Jid, string Name, SubscriptionStatus? Subscription, bool? PendingSubscription, string[] Groups)
Sets a roster item in a users roster.
static AccountUser AssertUserAuthenticated(HttpRequest Request)
Makes sure the request is made by an authenticated API user.
const string AgentNamespace
https://waher.se/Schema/BrokerAgent.xsd
Abstract base class for XMPP resources.
static bool IsValidBareJID(string JID)
Checks if a string is a valid Bare JID.
static async Task< string > GetApprovedLegalIDXml(string Account)
Gets XML containing the latest approvied Legal ID for an account, for inclusion in requests to peers.
Sends a presence subscription request to an XMPP account.
override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary< string, IElement > Parameters)
Executes the POST method on the resource.
SendPresenceSubscription()
Sends a presence subscription request to an XMPP account.
Service Module hosting the XMPP broker and its components.
Interface for roster items.
Definition: IRosterItem.cs:43
string Name
Name of roster item.
Definition: IRosterItem.cs:56
PendingSubscription
Pending subscription states.
Definition: RosterItem.cs:54