Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SetRosterItem.cs
1using System;
3using System.Threading.Tasks;
4using System.Xml;
5using Waher.Content;
12using Waher.Script;
15
17{
22 {
27 : base("Xmpp/SetRosterItem",
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(SetRosterItem).Namespace + ".JSON.SetRosterItem.req");
34 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(SetRosterItem).Namespace + ".XML.SetRosterItem.req");
35
44 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
45 {
47 string BareJid = (string)Parameters["PBareJid"].AssociatedObjectValue;
48 string Name = (string)Parameters["PName"].AssociatedObjectValue;
49 string[] Groups;
50
51 if (Parameters.TryGetValue("PGroups", out IElement E) && E?.AssociatedObjectValue is object[] GroupObjs)
52 {
53 int i, c = GroupObjs.Length;
54
55 Groups = new string[c];
56
57 for (i = 0; i < c; i++)
58 Groups[i] = GroupObjs[i].ToString();
59 }
60 else
61 Groups = Array.Empty<string>();
62
63 if (string.IsNullOrEmpty(BareJid))
64 throw new BadRequestException("Empty Bare JID.");
65
66 if (!IsValidBareJID(BareJid))
67 throw new BadRequestException("Invalid Bare JID.");
68
69 if (XmppServerModule.Server is null)
70 {
72
73 if (Client is null)
74 {
75 if (Types.TryGetModuleParameter("XMPP", out XmppClient Client2))
76 Client = Client2;
77 }
78
79 if (Client is null)
80 throw new ServiceUnavailableException("XMPP Client not available.");
81
82 Networking.XMPP.RosterItem Item = Client[BareJid];
83 if (Item is null)
84 {
85 Item = new Networking.XMPP.RosterItem(BareJid, Name, Groups);
86 await Client.AddRosterItem(Item);
87 }
88 else
89 {
90 await Client.UpdateRosterItem(BareJid, Name, Groups);
91 Item = Client[BareJid];
92 }
93
94 await Response.Return(new NamedDictionary<string, object>("RosterItem", AgentNamespace)
95 {
96 { "bareJid", Item.BareJid },
97 { "pendingSubscription", Item.PendingSubscription == PendingSubscription.Subscribe },
98 { "status", Item.State },
99 { "name", Item.Name },
100 { "Groups", Item.Groups }
101 });
102 }
103 else
104 {
105 PersistenceLayer PersistenceLayer = XmppServerModule.PersistenceLayer ?? new PersistenceLayer();
106 IRosterItem Item = await PersistenceLayer.SetRosterItem(User.UserName, BareJid, Name, null, null, Groups)
107 ?? throw new ServiceUnavailableException("XMPP Server not available.");
108
109 await Response.Return(new NamedDictionary<string, object>("RosterItem", AgentNamespace)
110 {
111 { "bareJid", Item.BareJid.Value },
112 { "pendingSubscription", Item.PendingSubscription },
113 { "status", Item.Subscription },
114 { "name", Item.Name },
115 { "Groups", Item.Groups }
116 });
117 }
118 }
119 }
120}
A Named dictionary is a dictionary, with a local name and a namespace. Use it to return content that ...
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:147
static XmppClient XmppClient
XMPP Client connection of gateway.
Definition: Gateway.cs:4038
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:22
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:23
Task Return(Exception ex)
Returns an error to the client.
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
RosterItem(string BareJID, string Name, params string[] Groups)
Maintains information about an item in the roster.
Definition: RosterItem.cs:90
Manages an XMPP client connection. Implements XMPP, as defined in https://tools.ietf....
Definition: XmppClient.cs:58
Task UpdateRosterItem(string BareJID, string Name, params string[] Groups)
Updates an item in the roster.
Definition: XmppClient.cs:4635
Task AddRosterItem(RosterItem Item)
Adds an item to the roster. If an item with the same Bare JID is found in the roster,...
Definition: XmppClient.cs:4591
Static class managing loading of resources stored as embedded resources or in content files.
Definition: Resources.cs:13
static string LoadResourceAsText(string ResourceName)
Loads a text resource from an embedded resource.
Definition: Resources.cs:55
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:15
static bool TryGetModuleParameter(string Name, out object Value)
Tries to get a module parameter value.
Definition: Types.cs:607
Class managing a script expression.
Definition: Expression.cs:41
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.
Sets information about a specific item in the account's roster.
override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary< string, IElement > Parameters)
Executes the POST method on the resource.
SetRosterItem()
Sets information about a specific item in the account's roster.
Service Module hosting the XMPP broker and its components.
Interface for roster items.
Definition: IRosterItem.cs:43
Basic interface for all types of elements.
Definition: IElement.cs:21
delegate string ToString(IElement Element)
Delegate for callback methods that convert an element value to a string.