|
20 | 20 | using System.Net.Mail;
|
21 | 21 | using System.Net;
|
22 | 22 | using System.IO;
|
| 23 | +using CloudNative.CloudEvents; |
| 24 | +using System.Xml.Linq; |
| 25 | +using CloudNative.CloudEvents.Http; |
| 26 | +using CloudNative.CloudEvents.SystemTextJson; |
| 27 | +using System.Net.Http; |
23 | 28 |
|
24 | 29 | namespace DasBlog.Managers
|
25 | 30 | {
|
@@ -187,22 +192,134 @@ public EntrySaveState CreateEntry(Entry entry)
|
187 | 192 | {
|
188 | 193 | var rtn = InternalSaveEntry(entry, null, null);
|
189 | 194 | LogEvent(EventCodes.EntryAdded, entry);
|
| 195 | + RaisePostCreatedCloudEvent(entry); |
190 | 196 | return rtn;
|
191 | 197 | }
|
192 | 198 |
|
| 199 | + |
| 200 | + private void RaisePostCreatedCloudEvent(Entry entry) |
| 201 | + { |
| 202 | + var ext = CloudEventAttribute.CreateExtension("tags", CloudEventAttributeType.String); |
| 203 | + var cloudEvent = new CloudEvent(CloudEventsSpecVersion.V1_0, new[] { ext }) |
| 204 | + { |
| 205 | + Type = "dasblog.post.created", |
| 206 | + Source = new Uri(dasBlogSettings.GetBaseUrl()), |
| 207 | + Subject = entry.Link, |
| 208 | + Data = MapEntryToCloudEventData(entry), |
| 209 | + Id = Guid.NewGuid().ToString(), |
| 210 | + Time = DateTime.UtcNow, |
| 211 | + }; |
| 212 | + cloudEvent.SetAttributeFromString("tags", entry.Categories); |
| 213 | + RaiseCloudEvent(cloudEvent); |
| 214 | + |
| 215 | + } |
| 216 | + |
| 217 | + private void RaiseCloudEvent(CloudEvent cloudEvent) |
| 218 | + { |
| 219 | + if ( dasBlogSettings.SiteConfiguration.EnableCloudEvents && |
| 220 | + dasBlogSettings.SiteConfiguration.CloudEventsTargets != null) |
| 221 | + { |
| 222 | + foreach (var target in dasBlogSettings.SiteConfiguration.CloudEventsTargets) |
| 223 | + { |
| 224 | + if (!string.IsNullOrEmpty(target.Uri)) |
| 225 | + { |
| 226 | + try |
| 227 | + { |
| 228 | + var content = cloudEvent.ToHttpContent(ContentMode.Structured, new JsonEventFormatter()); |
| 229 | + var uriBuilder = new UriBuilder(target.Uri); |
| 230 | + if (target.Headers != null) |
| 231 | + { |
| 232 | + foreach (var header in target.Headers) |
| 233 | + { |
| 234 | + if (!string.IsNullOrEmpty(header.Name)) |
| 235 | + { |
| 236 | + content.Headers.Add(header.Name, header.Value); |
| 237 | + } |
| 238 | + } |
| 239 | + } |
| 240 | + if (target.QueryArgs!= null) |
| 241 | + { |
| 242 | + foreach (var queryArgs in target.QueryArgs) |
| 243 | + { |
| 244 | + uriBuilder.Query = (string.IsNullOrEmpty(uriBuilder.Query) ? string.Empty : uriBuilder.Query + "&") + queryArgs.Name + "=" + queryArgs.Value; |
| 245 | + } |
| 246 | + } |
| 247 | + var httpClient = new HttpClient(); |
| 248 | + var result = httpClient.PostAsync(uriBuilder.Uri, content).GetAwaiter().GetResult(); |
| 249 | + } |
| 250 | + catch(Exception ex) |
| 251 | + { |
| 252 | + logger.LogError(ex, "Failed to post CloudEvent"); |
| 253 | + } |
| 254 | + } |
| 255 | + } |
| 256 | + } |
| 257 | + } |
| 258 | + |
| 259 | + private EntryCloudEventData MapEntryToCloudEventData(Entry entry) |
| 260 | + { |
| 261 | + var data = new EntryCloudEventData(); |
| 262 | + data.Id = entry.EntryId; |
| 263 | + data.Title = entry.Title; |
| 264 | + data.CreatedUtc = entry.CreatedUtc; |
| 265 | + data.ModifiedUtc = entry.ModifiedUtc; |
| 266 | + data.Tags = entry.Categories; |
| 267 | + data.Description = entry.Description; |
| 268 | + data.PermaLink = entry.Link; |
| 269 | + data.DetailsLink = dasBlogSettings.GetRssEntryUrl(entry.EntryId); |
| 270 | + data.IsPublic = entry.IsPublic; |
| 271 | + data.Author = entry.Author; |
| 272 | + data.Longitude = entry.Longitude; |
| 273 | + data.Latitude = entry.Latitude; |
| 274 | + return data; |
| 275 | + } |
| 276 | + |
193 | 277 | public EntrySaveState UpdateEntry(Entry entry)
|
194 | 278 | {
|
195 | 279 | var rtn = InternalSaveEntry(entry, null, null);
|
196 | 280 | LogEvent(EventCodes.EntryChanged, entry);
|
| 281 | + RaisePostUpdatedCloudEvent(entry); |
197 | 282 | return rtn;
|
198 | 283 | }
|
199 | 284 |
|
| 285 | + private void RaisePostUpdatedCloudEvent(Entry entry) |
| 286 | + { |
| 287 | + var ext = CloudEventAttribute.CreateExtension("tags", CloudEventAttributeType.String); |
| 288 | + var cloudEvent = new CloudEvent(CloudEventsSpecVersion.V1_0, new[] { ext }) |
| 289 | + { |
| 290 | + Type = "dasblog.post.updated", |
| 291 | + Source = new Uri(dasBlogSettings.GetBaseUrl()), |
| 292 | + Subject = entry.Link, |
| 293 | + Data = MapEntryToCloudEventData(entry), |
| 294 | + Id = Guid.NewGuid().ToString(), |
| 295 | + Time = DateTime.UtcNow, |
| 296 | + }; |
| 297 | + cloudEvent.SetAttributeFromString("tags", entry.Categories); |
| 298 | + RaiseCloudEvent(cloudEvent); |
| 299 | + } |
| 300 | + |
200 | 301 | public void DeleteEntry(string postid)
|
201 | 302 | {
|
202 | 303 | var entry = GetEntryForEdit(postid);
|
203 | 304 | dataService.DeleteEntry(postid, null);
|
204 |
| - |
205 | 305 | LogEvent(EventCodes.EntryDeleted, entry);
|
| 306 | + RaisePostDeletedCloudEvent(entry); |
| 307 | + } |
| 308 | + |
| 309 | + private void RaisePostDeletedCloudEvent(Entry entry) |
| 310 | + { |
| 311 | + var ext = CloudEventAttribute.CreateExtension("tags", CloudEventAttributeType.String); |
| 312 | + var cloudEvent = new CloudEvent(CloudEventsSpecVersion.V1_0, new[] { ext }) |
| 313 | + { |
| 314 | + Type = "dasblog.post.deleted", |
| 315 | + Source = new Uri(dasBlogSettings.GetBaseUrl()), |
| 316 | + Subject = entry.Link, |
| 317 | + Id = Guid.NewGuid().ToString(), |
| 318 | + Time = DateTime.UtcNow, |
| 319 | + Data = MapEntryToCloudEventData(entry) |
| 320 | + }; |
| 321 | + cloudEvent.SetAttributeFromString("tags", entry.Categories); |
| 322 | + RaiseCloudEvent(cloudEvent); |
206 | 323 | }
|
207 | 324 |
|
208 | 325 | private static StringCollection GetSearchWords(string searchString)
|
|
0 commit comments