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