// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System.ComponentModel.DataAnnotations;
using System.Text;
using Squidex.Domain.Apps.Core.Rules.Deprecated;
using Squidex.Extensions.Actions.Algolia;
using Squidex.Flows;
using Squidex.Flows.Steps.Utils;
using Squidex.Infrastructure.Reflection;
using Squidex.Infrastructure.Validation;
namespace Squidex.Extensions.Actions.Discourse;
[FlowStep(
Title = "Discourse",
IconImage = "",
IconColor = "#eB6121",
Display = "Post to discourse",
Description = "Create a post or topic at discourse.",
ReadMore = "https://www.discourse.org/")]
#pragma warning disable CS0618 // Type or member is obsolete
public sealed record DiscourseFlowStep : FlowStep, IConvertibleToAction
#pragma warning restore CS0618 // Type or member is obsolete
{
[AbsoluteUrl]
[LocalizedRequired]
[Display(Name = "Server Url", Description = "The url to the discourse server.")]
[Editor(FlowStepEditor.Url)]
public Uri Url { get; set; }
[LocalizedRequired]
[Display(Name = "Api Key", Description = "The api key to authenticate to your discourse server.")]
[Editor(FlowStepEditor.Text)]
public string ApiKey { get; set; }
[LocalizedRequired]
[Display(Name = "Api User", Description = "The api username to authenticate to your discourse server.")]
[Editor(FlowStepEditor.Text)]
public string ApiUsername { get; set; }
[LocalizedRequired]
[Display(Name = "Text", Description = "The text as markdown.")]
[Editor(FlowStepEditor.TextArea)]
[Expression]
public string Text { get; set; }
[Display(Name = "Title", Description = "The optional title when creating new topics.")]
[Editor(FlowStepEditor.Text)]
[Expression]
public string? Title { get; set; }
[Display(Name = "Topic", Description = "The optional topic id.")]
[Editor(FlowStepEditor.Text)]
public int? Topic { get; set; }
[Display(Name = "Category", Description = "The optional category id.")]
[Editor(FlowStepEditor.Text)]
public int? Category { get; set; }
public override async ValueTask ExecuteAsync(FlowExecutionContext executionContext,
CancellationToken ct)
{
var url = $"{Url.ToString().TrimEnd('/')}/posts.json?api_key={ApiKey}&api_username={ApiUsername}";
var body = new Dictionary
{
["title"] = Title,
};
if (Topic != null)
{
body.Add("topic_id", Topic.Value);
}
if (Category != null)
{
body.Add("category", Category.Value);
}
body["raw"] = Text;
var jsonRequest = executionContext.SerializeJson(body);
var request = new HttpRequestMessage(HttpMethod.Post, url)
{
Content = new StringContent(jsonRequest, Encoding.UTF8, "application/json"),
};
request.Headers.TryAddWithoutValidation("Api-Key", ApiKey);
request.Headers.TryAddWithoutValidation("Api-Username", ApiUsername);
if (executionContext.IsSimulation)
{
executionContext.LogSkipSimulation(
HttpDumpFormatter.BuildDump(request, null, null));
return Next();
}
var httpClient =
executionContext.Resolve()
.CreateClient("DiscourseAction");
var (_, dump) = await httpClient.SendAsync(executionContext, request, jsonRequest, ct);
if (Topic != null)
{
executionContext.Log("Created post in topic {Topic}", dump);
}
else
{
executionContext.Log("Created Topic", dump);
}
return Next();
}
#pragma warning disable CS0618 // Type or member is obsolete
public RuleAction ToAction()
{
return SimpleMapper.Map(this, new AlgoliaAction());
}
#pragma warning restore CS0618 // Type or member is obsolete
}