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;
2using System.Collections.Generic;
3using System.Text;
4using System.Threading.Tasks;
5using System.Xml;
6using Waher.Content;
10using Waher.Script;
13
15{
20 {
24 public PopMessages()
25 : base("Xmpp/PopMessages",
26 new KeyValuePair<Type, Expression>(typeof(Dictionary<string, object>), new Expression(jsonPattern)),
27 new KeyValuePair<Type, Expression>(typeof(XmlDocument), new Expression(xmlPattern)))
28 {
29 }
30
31 private static readonly string jsonPattern = Resources.LoadResourceAsText(typeof(PopMessages).Namespace + ".JSON.PopMessages.req");
32 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(PopMessages).Namespace + ".XML.PopMessages.req");
33
38 public override bool Synchronous => false;
39
48 public override Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
49 {
51
52 int MaxCount = int.MaxValue;
53
54 if (Parameters.TryGetValue("PMaxCount", out IElement E) && !(E is null) &&
55 E.AssociatedObjectValue is double d2)
56 {
57 MaxCount = (int)d2;
58 }
59
60 Task _ = Task.Run(async () =>
61 {
62 try
63 {
64 PersistenceLayer PersistenceLayer = XmppServerModule.PersistenceLayer ?? new PersistenceLayer();
66 List<object> Messages2 = new List<object>();
67 IEnumerable<IOfflineMessage> Messages;
68 bool Found = false;
69
70 Messages = await PersistenceLayer.GetOfflineMessages(User.UserName, MaxCount);
71
72 if (!(Messages is null))
73 {
74 foreach (IOfflineMessage Message in Messages)
75 {
76 StringBuilder Xml = new StringBuilder();
77 Xml.Append("<message xmlns='");
79 Xml.Append("'>");
80 Xml.Append(Message.ContentXml);
81 Xml.Append("</message>");
82
83 XmlDocument Doc = new XmlDocument()
84 {
85 PreserveWhitespace = true
86 };
87 Doc.LoadXml(Xml.ToString());
88
89 Messages2.Add(new Dictionary<string, object>()
90 {
91 { "id", Message.Id },
92 { "from", Message.From.Value },
93 { "to", Message.To.Value },
94 { "lang", Message.Language },
95 { "type", Message.Type },
96 { "timestamp", Message.Timestamp },
97 { "Content", Doc.DocumentElement.ChildNodes }
98 });
99
100 Found = true;
101 MaxCount--;
102 if (MaxCount <= 0)
103 break;
104 }
105 }
106
107 Result["Messages"] = Messages2.ToArray();
108
109 await Response.Return(Result);
110
111 if (Found)
113 }
114 catch (XmlException ex)
115 {
116 await Response.SendResponse(XML.AnnotateException(ex));
117 }
118 catch (Exception ex)
119 {
120 await Response.SendResponse(ex);
121 }
122 });
123
124 return Task.CompletedTask;
125 }
126 }
127}
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
Helps with common XML-related tasks.
Definition: XML.cs:19
static XmlException AnnotateException(XmlException ex)
Creates a new XML Exception object, with reference to the source XML file, for information.
Definition: XML.cs:1588
Represents an HTTP request.
Definition: HttpRequest.cs:18
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:21
async Task SendResponse()
Sends the response back to the client. If the resource is synchronous, there's no need to call this m...
async Task Return(object Object)
Returns an object to the client. This method can only be called once per response,...
Abstract base class for XMPP client connections
Class managing a script expression.
Definition: Expression.cs:39
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:20
PopMessages()
Pops offline messages available for the account.
Definition: PopMessages.cs:24
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:38
override Task POST(HttpRequest Request, HttpResponse Response, Dictionary< string, IElement > Parameters)
Executes the POST method on the resource.
Definition: PopMessages.cs:48
Basic interface for all types of elements.
Definition: IElement.cs:20