Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
PublicTokenHistoryView.cs
1using System;
3using System.Text;
4using System.Threading.Tasks;
10
12{
17 {
18 private readonly Dictionary<string, TaskCompletionSource<string>> renderings = new Dictionary<string, TaskCompletionSource<string>>();
19
24 : base("/NFH")
25 {
26 }
27
28 public override bool HandlesSubPaths => true;
29 public override bool UserSessions => true;
30 public bool AllowsGET => true;
31
33
34 public async Task GET(HttpRequest Request, HttpResponse Response)
35 {
36 if (string.IsNullOrEmpty(Request.SubPath))
37 {
38 await Response.SendResponse(new BadRequestException("Missing Token ID."));
39 return;
40 }
41
42 string TokenId = Request.SubPath[1..];
43 int i = TokenId.IndexOf('@');
44 if (i < 0 || !Guid.TryParse(TokenId[..i], out _))
45 {
46 await Response.SendResponse(new BadRequestException("Invalid Token ID."));
47 return;
48 }
49
50 Token Token = await NeuroFeaturesProcessor.GetToken(TokenId, true);
51 if (Token is null)
52 {
53 await Response.SendResponse(new NotFoundException("Token '" + TokenId + "' not found on this neuron."));
54 return;
55 }
56
57 if (!Token.IsPublic)
58 {
59 await Response.SendResponse(new ForbiddenException("Token not publicly accissible via the web."));
60 return;
61 }
62
64 {
65 await Response.SendResponse(new BadRequestException("Token does not have an associated state-machine."));
66 return;
67 }
68
69 TaskCompletionSource<string> Pending;
70 bool WaitPending;
71 string Result;
72
73 lock (this.renderings)
74 {
75 if (this.renderings.TryGetValue(TokenId, out Pending))
76 WaitPending = true;
77 else
78 {
79 WaitPending = false;
80 Pending = new TaskCompletionSource<string>();
81 this.renderings[TokenId] = Pending;
82 }
83 }
84
85 try
86 {
87 if (WaitPending)
88 Result = await Pending.Task;
89 else
90 {
91 StringBuilder Html = new StringBuilder();
92 string s;
93
95 string Description = await Doc.GeneratePlainText();
96
97 Html.AppendLine("<!DOCTYPE html>");
98 Html.AppendLine("<html itemscope itemtype=\"http://schema.org/WebPage\">");
99 Html.AppendLine("<head>");
100 Html.Append("<title>");
102 Html.AppendLine("</title>");
103 Html.Append("<meta name=\"twitter:title\" content=\"");
104 Html.Append(s = XML.HtmlAttributeEncode(Token.FriendlyName));
105 Html.AppendLine("\"/>");
106 Html.Append("<meta name=\"og:title\" content=\"");
107 Html.Append(s);
108 Html.AppendLine("\"/>");
109 Html.Append("<meta itemprop=\"description\" content=\"");
110 Html.Append(s = XML.HtmlAttributeEncode(Description));
111 Html.AppendLine("\"/>");
112 Html.Append("<meta name=\"twitter:description\" content=\"");
113 Html.Append(s);
114 Html.AppendLine("\"/>");
115 Html.Append("<meta name=\"og:description\" content=\"");
116 Html.Append(s);
117 Html.AppendLine("\"/>");
118 Html.Append("<meta name=\"description\" content=\"");
119 Html.Append(s);
120 Html.Append(" Author: ");
121 Html.Append(s = MarkdownDocument.Encode(Token.Creator.Value));
122 Html.Append(", Date: ");
123 Html.Append(XML.Encode(Token.Created, true));
124 Html.AppendLine("\"/>");
125 Html.AppendLine("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"/>");
126 Html.Append("<meta name=\"author\" content=\"");
127 Html.Append(s);
128 Html.AppendLine("\"/>");
129 Html.AppendLine("<link rel=\"shortcut icon\" href=\"/favicon.ico\"/>");
130 Html.Append("<link rel=\"stylesheet\" href=\"");
131 Html.Append(XML.HtmlAttributeEncode(Theme.CurrentTheme.CSSX));
132 Html.AppendLine("\"/>");
133 Html.AppendLine("<script type=\"application/javascript\" src=\"/Events.js\"></script>");
134 Html.AppendLine("<script type=\"text/javascript\">");
135 Html.AppendLine("function UpdatePresent(Data) { document.getElementById('Present').innerHTML=Data; }");
136 Html.AppendLine("</script>");
137 Html.AppendLine("</head>");
138 Html.AppendLine("<body>");
139 Html.AppendLine("<main>");
140 Html.AppendLine("<section id='Present'>");
141
142 Html.AppendLine(await Token.GeneratePresentReport(ReportFormat.Html));
143
144 Html.AppendLine("</section>");
145 Html.AppendLine("<section id='History'>");
146
147 Html.AppendLine(await Token.GenerateHistoryReport(ReportFormat.Html));
148
149 Html.AppendLine("</section>");
150 Html.AppendLine("</main>");
151 Html.AppendLine("</body>");
152 Html.AppendLine("</html>");
153
154 Result = Html.ToString();
155 Pending.TrySetResult(Result);
156 }
157 }
158 catch (Exception ex)
159 {
160 if (WaitPending)
161 Pending.TrySetException(ex);
162
163 await Response.SendResponse(ex);
164 return;
165 }
166 finally
167 {
168 if (!WaitPending)
169 {
170 lock (this.renderings)
171 {
172 this.renderings.Remove(TokenId);
173 }
174 }
175 }
176
177 Response.ContentType = HtmlCodec.DefaultContentType;
178 Response.SetHeader("Cache-Control", "max-age=0, no-cache, no-store");
179
180 await Response.Write(Result);
181 await Response.SendResponse();
182 }
183 }
184}
HTML encoder/decoder.
Definition: HtmlCodec.cs:15
const string DefaultContentType
Default Content-Type for HTML: text/html
Definition: HtmlCodec.cs:26
Contains a markdown document. This markdown document class supports original markdown,...
static string Encode(string s)
Encodes all special characters in a string so that it can be included in a markdown document without ...
async Task< string > GeneratePlainText()
Generates Plain Text from the markdown text.
static Task< MarkdownDocument > CreateAsync(string MarkdownText, params Type[] TransparentExceptionTypes)
Contains a markdown document. This markdown document class supports original markdown,...
Helps with common XML-related tasks.
Definition: XML.cs:21
static string HtmlValueEncode(string s)
Differs from Encode(String), in that it does not encode the aposotrophe or the quote.
Definition: XML.cs:211
static string Encode(string s)
Encodes a string for use in XML.
Definition: XML.cs:29
static string HtmlAttributeEncode(string s)
Differs from Encode(String), in that it does not encode the aposotrophe.
Definition: XML.cs:121
static ThemeDefinition CurrentTheme
Current theme.
Definition: Theme.cs:90
The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repe...
The server understood the request, but is refusing to fulfill it. Authorization will not help and the...
Base class for all HTTP authentication schemes, as defined in RFC-7235: https://datatracker....
Represents an HTTP request.
Definition: HttpRequest.cs:22
string SubPath
Sub-path. If a resource is found handling the request, this property contains the trailing sub-path o...
Definition: HttpRequest.cs:194
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 Write(byte[] Data)
Returns binary data in the response.
void SetHeader(string FieldName, string Value)
Sets a custom header field value.
Base class for all synchronous HTTP resources. A synchronous resource responds within the method hand...
The server has not found anything matching the Request-URI. No indication is given of whether the con...
Marketplace processor, brokering sales of items via tenders and offers defined in smart contracts.
Task< string > GeneratePresentReport()
Generates a present report for the token, if the token is associated with a state-machine,...
Definition: Token.cs:1463
bool HasStateMachine
If the token has an associated state-machine.
Definition: Token.cs:1345
bool IsPublic
If the token is public.
Definition: Token.cs:181
string FriendlyName
A friendly name for the token.
Definition: Token.cs:512
Task< string > GenerateHistoryReport()
Generates a history report for the token, if the token is associated with a state-machine,...
Definition: Token.cs:1486
string Description
Description Markdown for the token.
Definition: Token.cs:531
DateTime Created
When token was created.
Definition: Token.cs:384
CaseInsensitiveString Creator
Creator of token
Definition: Token.cs:189
override HttpAuthenticationScheme[] GetAuthenticationSchemes(HttpRequest Request)
Any authentication schemes used to authenticate users before access is granted to the corresponding r...
async Task GET(HttpRequest Request, HttpResponse Response)
Executes the GET method on the resource.
GET Interface for HTTP resources.
Definition: ImplTypes.g.cs:58
ReportFormat
Desired report format
Definition: ReportFormat.cs:7