1
1
using Newtonsoft . Json ;
2
2
using OurUmbraco . Forum . Models ;
3
3
using OurUmbraco . Forum . Services ;
4
+ using System . Net . Http ;
5
+ using System ;
4
6
using System . Web . Http ;
5
7
using Umbraco . Core ;
6
8
using Umbraco . Web . WebApi ;
9
+ using System . Collections . Generic ;
10
+ using System . Linq ;
7
11
8
12
namespace OurUmbraco . Forum . Controllers
9
13
{
@@ -14,26 +18,119 @@ public IHttpActionResult Post(TopicModel topicModel)
14
18
{
15
19
var topicService = new TopicService ( ApplicationContext . DatabaseContext ) ;
16
20
var topic = topicService . GetById ( topicModel . TopicId ) ;
17
- var body = topic . Body . Replace ( ": /media/upload/" , ": https://our.umbraco.com/media/upload/" ) ;
18
- body = body + "\n <hr>\n <small>This is a companion discussion topic for the original entry at <a href=\" https://cultiv.nl/blog/released-search-engine-sitemap-package\" >https://cultiv.nl/blog/released-search-engine-sitemap-package</a></small>" ;
21
+ var forumUrl = System . Configuration . ConfigurationManager . AppSettings [ "OurUmbracoUrl" ] ;
22
+
23
+ var body = topic . Body . Replace ( "/media/upload/" , $ "{ forumUrl } /media/upload/") ;
24
+ var topicUrl = $ "{ forumUrl } /forum/{ topic . Id } -{ topic . Title . ToUrlSegment ( ) } ";
25
+ body = body + $ "\n <hr>\n <small>This is a companion discussion topic for the original entry at <a href=\" { topicUrl } \" >{ topicUrl } </a></small>";
19
26
var discourseCreateTopic = new DiscourseCreateTopic
20
27
{
21
28
Title = topic . Title ,
22
29
Raw = body ,
23
30
Category = 5 ,
24
31
CreatedAt = topic . Created ,
25
- EmbedUrl = $ "https://our.umbraco.com/forum/using-umbraco/ { topic . Id } - { topic . Title . ToUrlSegment ( ) } " ,
32
+ EmbedUrl = topicUrl ,
26
33
ExternalId = topic . Id
27
34
} ;
35
+ var redirectUrl = CreateTopicOnDiscourse ( discourseCreateTopic ) ;
36
+ return Ok ( redirectUrl ) ;
37
+ }
38
+
39
+ private string CreateTopicOnDiscourse ( DiscourseCreateTopic createTopic )
40
+ {
41
+ var forumBaseUrl = System . Configuration . ConfigurationManager . AppSettings [ "DiscourseApiBaseUrl" ] ;
42
+ using ( var client = new HttpClient ( ) )
43
+ {
44
+ client . BaseAddress = new Uri ( forumBaseUrl ) ;
45
+ client . DefaultRequestHeaders . Add ( "Api-Key" , $ "{ System . Configuration . ConfigurationManager . AppSettings [ "DiscourseApiKey" ] } ") ;
46
+ client . DefaultRequestHeaders . Add ( "Api-Username" , $ "{ System . Configuration . ConfigurationManager . AppSettings [ "DiscourseApiUsername" ] } ") ;
47
+
48
+ var result = client . PostAsJsonAsync ( "posts.json" , createTopic ) . Result ;
49
+ if ( result . IsSuccessStatusCode == false )
50
+ {
51
+ var resultContent = result . Content . ReadAsStringAsync ( ) . Result ;
52
+ var errorModel = JsonConvert . DeserializeObject < ErrorModel > ( resultContent ) ;
53
+ var firstError = errorModel . Errors . FirstOrDefault ( ) ;
54
+ if ( firstError != null && firstError == "External has already been taken" )
55
+ {
56
+ // the topic exists, see if we can find the URL for it
57
+ var topicUrl = GetTopicByExternalId ( createTopic . ExternalId ) ;
58
+ return topicUrl ;
59
+ }
60
+ return null ;
61
+ }
62
+ else
63
+ {
64
+ var resultContent = result . Content . ReadAsStringAsync ( ) . Result ;
65
+ var discourseTopic = JsonConvert . DeserializeObject < DiscourseTopic > ( resultContent ) ;
66
+ return forumBaseUrl + discourseTopic . PostUrl ;
67
+ }
68
+ }
69
+ }
70
+
71
+ private string GetTopicByExternalId ( int id )
72
+ {
73
+ var forumBaseUrl = System . Configuration . ConfigurationManager . AppSettings [ "DiscourseApiBaseUrl" ] ;
74
+ using ( var client = new HttpClient ( ) )
75
+ {
76
+ client . BaseAddress = new Uri ( forumBaseUrl ) ;
77
+ client . DefaultRequestHeaders . Add ( "Api-Key" , $ "{ System . Configuration . ConfigurationManager . AppSettings [ "DiscourseApiKey" ] } ") ;
78
+ client . DefaultRequestHeaders . Add ( "Api-Username" , $ "{ System . Configuration . ConfigurationManager . AppSettings [ "DiscourseApiUsername" ] } ") ;
28
79
29
- var x = discourseCreateTopic ;
30
- return Ok ( x ) ;
80
+ var result = client . GetAsync ( $ "t/external_id/{ id } .json") . Result ;
81
+ if ( result . IsSuccessStatusCode == false )
82
+ {
83
+ return null ;
84
+ }
85
+ else
86
+ {
87
+ var resultContent = result . Content . ReadAsStringAsync ( ) . Result ;
88
+ var discourseTopic = JsonConvert . DeserializeObject < DiscoursePostStream > ( resultContent ) ;
89
+ var firstPost = discourseTopic . PostStream . Posts . FirstOrDefault ( ) ;
90
+ if ( firstPost == null )
91
+ {
92
+ return null ;
93
+ }
94
+ return forumBaseUrl + firstPost . PostUrl ;
95
+ }
96
+ }
31
97
}
32
98
33
99
public class TopicModel
34
100
{
35
101
public int TopicId { get ; set ; }
36
102
}
37
103
104
+ internal class DiscourseTopic
105
+ {
106
+ [ JsonProperty ( "post_url" ) ]
107
+ public string PostUrl { get ; set ; }
108
+ }
109
+
110
+ internal class ErrorModel
111
+ {
112
+ [ JsonProperty ( "action" ) ]
113
+ public string Action { get ; set ; }
114
+
115
+ [ JsonProperty ( "errors" ) ]
116
+ public List < string > Errors { get ; set ; }
117
+ }
118
+
119
+ internal class DiscoursePostStream
120
+ {
121
+ [ JsonProperty ( "post_stream" ) ]
122
+ public PostStream PostStream { get ; set ; }
123
+ }
124
+
125
+ internal class PostStream
126
+ {
127
+ [ JsonProperty ( "posts" ) ]
128
+ public List < DiscoursePost > Posts { get ; set ; }
129
+ }
130
+ internal class DiscoursePost
131
+ {
132
+ [ JsonProperty ( "post_url" ) ]
133
+ public string PostUrl { get ; set ; }
134
+ }
38
135
}
39
136
}
0 commit comments