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;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using System.Xml;
5using Waher.Content;
11using Waher.Script;
14
16{
21 {
26 : base("Xmpp/SetRosterItem",
27 new KeyValuePair<Type, Expression>(typeof(Dictionary<string, object>), new Expression(jsonPattern)),
28 new KeyValuePair<Type, Expression>(typeof(XmlDocument), new Expression(xmlPattern)))
29 {
30 }
31
32 private static readonly string jsonPattern = Resources.LoadResourceAsText(typeof(SetRosterItem).Namespace + ".JSON.SetRosterItem.req");
33 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(SetRosterItem).Namespace + ".XML.SetRosterItem.req");
34
43 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
44 {
46 string BareJid = (string)Parameters["PBareJid"].AssociatedObjectValue;
47 string Name = (string)Parameters["PName"].AssociatedObjectValue;
48 string[] Groups;
49
50 if (Parameters.TryGetValue("PGroups", out IElement E) && E?.AssociatedObjectValue is object[] GroupObjs)
51 {
52 int i, c = GroupObjs.Length;
53
54 Groups = new string[c];
55
56 for (i = 0; i < c; i++)
57 Groups[i] = GroupObjs[i].ToString();
58 }
59 else
60 Groups = new string[0];
61
62 if (string.IsNullOrEmpty(BareJid))
63 throw new BadRequestException("Empty Bare JID.");
64
65 if (!IsValidBareJID(BareJid))
66 throw new BadRequestException("Invalid Bare JID.");
67
68 if (XmppServerModule.Server is null)
69 {
71
72 if (Client is null)
73 {
74 if (Types.TryGetModuleParameter("XMPP", out object Obj) && Obj is XmppClient Client2)
75 Client = Client2;
76 }
77
78 if (Client is null)
79 throw new ServiceUnavailableException("XMPP Client not available.");
80
81 Networking.XMPP.RosterItem Item = Client[BareJid];
82 if (Item is null)
83 {
84 Item = new Networking.XMPP.RosterItem(BareJid, Name, Groups);
85 await Client.AddRosterItem(Item);
86 }
87 else
88 {
89 await Client.UpdateRosterItem(BareJid, Name, Groups);
90 Item = Client[BareJid];
91 }
92
93 await Response.Return(new NamedDictionary<string, object>("RosterItem", AgentNamespace)
94 {
95 { "bareJid", Item.BareJid },
96 { "pendingSubscription", Item.PendingSubscription == PendingSubscription.Subscribe },
97 { "status", Item.State },
98 { "name", Item.Name },
99 { "Groups", Item.Groups }
100 });
101 }
102 else
103 {
104 PersistenceLayer PersistenceLayer = XmppServerModule.PersistenceLayer ?? new PersistenceLayer();
105 IRosterItem Item = await PersistenceLayer.SetRosterItem(User.UserName, BareJid, Name, null, null, Groups)
106 ?? throw new ServiceUnavailableException("XMPP Server not available.");
107
108 await Response.Return(new NamedDictionary<string, object>("RosterItem", AgentNamespace)
109 {
110 { "bareJid", Item.BareJid.Value },
111 { "pendingSubscription", Item.PendingSubscription },
112 { "status", Item.Subscription },
113 { "name", Item.Name },
114 { "Groups", Item.Groups }
115 });
116 }
117 }
118 }
119}
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
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
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:59
Task UpdateRosterItem(string BareJID, string Name, params string[] Groups)
Updates an item in the roster.
Definition: XmppClient.cs:4586
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:4542
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 > 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:20
delegate string ToString(IElement Element)
Delegate for callback methods that convert an element value to a string.