Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
GetRoster.cs
1using System;
3using System.Threading.Tasks;
4using System.Xml;
5using Waher.Content;
12using Waher.Script;
15
17{
22 {
26 public GetRoster()
27 : base("Xmpp/GetRoster",
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(GetRoster).Namespace + ".JSON.GetRoster.req");
34 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(GetRoster).Namespace + ".XML.GetRoster.req");
35
44 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
45 {
47
48 int Offset = 0;
49 int MaxCount = int.MaxValue;
50
51 if (Parameters.TryGetValue("POffset", out IElement E) && !(E is null) &&
52 E.AssociatedObjectValue is double d)
53 {
54 Offset = (int)d;
55 }
56
57 if (Parameters.TryGetValue("PMaxCount", out E) && !(E is null) &&
58 E.AssociatedObjectValue is double d2)
59 {
60 MaxCount = (int)d2;
61 }
62
63 List<Dictionary<string, object>> Roster = new List<Dictionary<string, object>>();
64
65 if (XmppServerModule.Server is null)
66 {
68
69 if (Client is null)
70 {
71 if (Types.TryGetModuleParameter("XMPP", out XmppClient Client2))
72 Client = Client2;
73 }
74
75 if (Client is null)
76 throw new ServiceUnavailableException("XMPP Client not available.");
77
78 foreach (Networking.XMPP.RosterItem Item in Client.Roster)
79 {
80 if (Offset > 0)
81 {
82 Offset--;
83 continue;
84 }
85
86 Roster.Add(new Dictionary<string, object>()
87 {
88 { "bareJid", Item.BareJid },
89 { "pendingSubscription", Item.PendingSubscription == PendingSubscription.Subscribe },
90 { "status", Item.State },
91 { "name", Item.Name },
92 { "Groups", Item.Groups }
93 });
94
95 MaxCount--;
96 if (MaxCount <= 0)
97 break;
98 }
99 }
100 else
101 {
102 PersistenceLayer PersistenceLayer = XmppServerModule.PersistenceLayer ?? new PersistenceLayer();
103 IEnumerable<IRosterItem> Items = await PersistenceLayer.GetRoster(User.UserName);
104
105 if (!(Items is null))
106 {
107 foreach (IRosterItem Item in Items)
108 {
109 if (Offset > 0)
110 {
111 Offset--;
112 continue;
113 }
114
115 Roster.Add(new Dictionary<string, object>()
116 {
117 { "bareJid", Item.BareJid.Value },
118 { "pendingSubscription", Item.PendingSubscription },
119 { "status", Item.Subscription },
120 { "name", Item.Name },
121 { "Groups", Item.Groups }
122 });
123
124 MaxCount--;
125 if (MaxCount <= 0)
126 break;
127 }
128 }
129 }
130
131 await Response.Return(new NamedDictionary<string, object>("Roster", AgentNamespace)
132 {
133 { "RosterItems", Roster.ToArray() }
134 });
135 }
136 }
137}
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
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...
Manages an XMPP client connection. Implements XMPP, as defined in https://tools.ietf....
Definition: XmppClient.cs:58
RosterItem[] Roster
Items in the roster.
Definition: XmppClient.cs:4720
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< IEnumerable< IRosterItem > > GetRoster(CaseInsensitiveString UserName)
Gets the roster of an account.
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.
Gets the roster of the authenticated account.
Definition: GetRoster.cs:22
override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary< string, IElement > Parameters)
Executes the POST method on the resource.
Definition: GetRoster.cs:44
GetRoster()
Gets the roster of the authenticated account.
Definition: GetRoster.cs:26
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