Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
PopMessages.cs
1using System;
3using System.Text;
4using System.Threading.Tasks;
5using System.Xml;
6using Waher.Content;
11using Waher.Script;
14
16{
21 {
25 public PopMessages()
26 : base("Xmpp/PopMessages",
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(PopMessages).Namespace + ".JSON.PopMessages.req");
33 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(PopMessages).Namespace + ".XML.PopMessages.req");
34
39 public override bool Synchronous => false;
40
49 public override Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
50 {
52
53 int MaxCount = int.MaxValue;
54
55 if (Parameters.TryGetValue("PMaxCount", out IElement E) && !(E is null) &&
56 E.AssociatedObjectValue is double d2)
57 {
58 MaxCount = (int)d2;
59 }
60
61 Task _ = Task.Run(async () =>
62 {
63 try
64 {
65 PersistenceLayer PersistenceLayer = XmppServerModule.PersistenceLayer ?? new PersistenceLayer();
67 List<object> Messages2 = new List<object>();
68 IEnumerable<IOfflineMessage> Messages;
69 bool Found = false;
70
71 Messages = await PersistenceLayer.GetOfflineMessages(User.UserName, MaxCount);
72
73 if (!(Messages is null))
74 {
75 foreach (IOfflineMessage Message in Messages)
76 {
77 StringBuilder Xml = new StringBuilder();
78 Xml.Append("<message xmlns='");
80 Xml.Append("'>");
81 Xml.Append(Message.ContentXml);
82 Xml.Append("</message>");
83
84 XmlDocument Doc = XML.ParseXml(Xml.ToString(), true);
85
87 {
88 { "id", Message.Id },
89 { "from", Message.From.Value },
90 { "to", Message.To.Value },
91 { "lang", Message.Language },
92 { "type", Message.Type },
93 { "timestamp", Message.Timestamp },
94 { "Content", Doc.DocumentElement.ChildNodes }
95 });
96
97 Found = true;
98 MaxCount--;
99 if (MaxCount <= 0)
100 break;
101 }
102 }
103
104 Result["Messages"] = Messages2.ToArray();
105
106 await Response.Return(Result);
107
108 if (Found)
110 }
111 catch (XmlException ex)
112 {
113 await Response.SendResponse(XML.AnnotateException(ex));
114 }
115 catch (Exception ex)
116 {
117 await Response.SendResponse(ex);
118 }
119 });
120
121 return Task.CompletedTask;
122 }
123 }
124}
A Named dictionary is a dictionary, with a local name and a namespace. Use it to return content that ...
Helps with common XML-related tasks.
Definition: XML.cs:21
static XmlException AnnotateException(XmlException ex)
Creates a new XML Exception object, with reference to the source XML file, for information.
Definition: XML.cs:1762
static XmlDocument ParseXml(string Xml)
Parses an XML Document from its string representation.
Definition: XML.cs:713
Represents an HTTP request.
Definition: HttpRequest.cs:22
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:23
async Task SendResponse()
Sends the response back to the client. If the resource is synchronous, there's no need to call this m...
Task Return(Exception ex)
Returns an error to the client.
Abstract base class for XMPP client connections
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
Class managing a script expression.
Definition: Expression.cs:41
async Task DeleteOfflineMessages(IEnumerable< IOfflineMessage > Messages)
Deletes offline messages.
async Task< IEnumerable< IOfflineMessage > > GetOfflineMessages(CaseInsensitiveString ToUserName, int Max)
Gets the oldest offline messages stored for a given bare JID, up to a maximum count.
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.
Pops offline messages available for the account.
Definition: PopMessages.cs:21
PopMessages()
Pops offline messages available for the account.
Definition: PopMessages.cs:25
override bool Synchronous
If the resource is synchronous (i.e. returns a response in the method handler), or if it is asynchron...
Definition: PopMessages.cs:39
override Task POST(HttpRequest Request, HttpResponse Response, Dictionary< string, IElement > Parameters)
Executes the POST method on the resource.
Definition: PopMessages.cs:49
Basic interface for all types of elements.
Definition: IElement.cs:21
Definition: ImplTypes.g.cs:58