2
2
3
3
using System ;
4
4
using System . Collections . Generic ;
5
+ using System . ComponentModel ;
5
6
using System . IO ;
6
7
using System . Linq ;
7
8
using System . Net ;
16
17
using Newtonsoft . Json . Linq ;
17
18
using ReverseMarkdown ;
18
19
using Matterhook . NET . MatterhookClient ;
20
+ using Microsoft . IdentityModel . Tokens ;
19
21
20
22
21
23
namespace Matterhook . NET . Controllers
@@ -54,10 +56,21 @@ public async Task<IActionResult> Receive()
54
56
Request . Headers . TryGetValue ( "X-Discourse-Event" , out StringValues eventName ) ;
55
57
Request . Headers . TryGetValue ( "X-Discourse-Event-Signature" , out StringValues signature ) ;
56
58
Request . Headers . TryGetValue ( "X-Discourse-Instance" , out StringValues discourseUrl ) ;
59
+ Request . Headers . TryGetValue ( "Content-type" , out var content ) ;
60
+
57
61
_discourseUrl = discourseUrl ;
58
62
59
63
stuffToLog . Add ( $ "Hook Id: { eventId } ") ;
60
64
65
+ if ( content != "application/json" )
66
+ {
67
+ const string error = "Invalid content type. Expected application/json" ;
68
+ stuffToLog . Add ( error ) ;
69
+ Util . LogList ( stuffToLog ) ;
70
+ return StatusCode ( 400 , error ) ;
71
+
72
+ }
73
+
61
74
using ( var reader = new StreamReader ( Request . Body , Encoding . UTF8 ) )
62
75
{
63
76
payloadText = await reader . ReadToEndAsync ( ) . ConfigureAwait ( false ) ;
@@ -68,13 +81,13 @@ public async Task<IActionResult> Receive()
68
81
69
82
if ( signature == calcSig )
70
83
{
71
- var discourseHook = new DiscourseHook ( eventId , eventType , eventName , signature , payloadText ) ;
84
+ var discourseHook = new DiscourseHook ( eventId , eventType , eventName , signature , payloadText ) ;
72
85
var matterHook = new MatterhookClient . MatterhookClient ( _config . MattermostConfig . WebhookUrl ) ;
73
86
HttpResponseMessage response = null ;
74
87
MattermostMessage message = null ;
75
88
if ( discourseHook . EventName == "post_created" )
76
89
{
77
- message = PostCreated ( ( PostPayload ) discourseHook . Payload ) ;
90
+ message = PostCreated ( ( PostPayload ) discourseHook . Payload ) ;
78
91
response = await matterHook . PostAsync ( message ) ;
79
92
}
80
93
@@ -84,30 +97,37 @@ public async Task<IActionResult> Receive()
84
97
? $ "Unable to post to Mattermost { response . StatusCode } "
85
98
: "Unable to post to Mattermost" ) ;
86
99
87
- return Content ( response != null ? $ "Problem posting to Mattermost: { response . StatusCode } " : "Problem Posting to Mattermost" ) ;
100
+ return StatusCode ( 500 , response != null
101
+ ? $ "Unable to post to Mattermost: { response . StatusCode } "
102
+ : "Unable to post to Mattermost" ) ;
88
103
}
89
- if ( message != null ) stuffToLog . Add ( message . Text ) ;
90
- stuffToLog . Add ( "Succesfully posted to Mattermost" ) ;
91
- Util . LogList ( stuffToLog ) ;
92
- return Ok ( ) ;
93
- }
94
- else
95
- {
96
- stuffToLog . Add ( "Invalid Signature!" ) ;
97
- stuffToLog . Add ( $ "Expected: { signature } ") ;
98
- stuffToLog . Add ( $ "Calculated: { calcSig } ") ;
99
- Util . LogList ( stuffToLog ) ;
100
- return Unauthorized ( ) ;
104
+
105
+ if ( ! _config . LogOnlyErrors )
106
+ {
107
+ if ( message != null ) stuffToLog . Add ( message . Text ) ;
108
+ stuffToLog . Add ( "Succesfully posted to Mattermost" ) ;
109
+ Util . LogList ( stuffToLog ) ;
110
+ }
111
+
112
+ return StatusCode ( 200 , "Succesfully posted to Mattermost" ) ;
101
113
}
102
-
114
+
115
+ stuffToLog . Add ( "Invalid Signature!" ) ;
116
+ stuffToLog . Add ( $ "Expected: { signature } ") ;
117
+ stuffToLog . Add ( $ "Calculated: { calcSig } ") ;
118
+ Util . LogList ( stuffToLog ) ;
119
+ return StatusCode ( 401 , "Invalid signature. Please check your secret values in the config and Discourse" ) ;
120
+
103
121
}
104
122
catch ( Exception e )
105
123
{
106
124
stuffToLog . Add ( e . Message ) ;
107
125
Util . LogList ( stuffToLog ) ;
108
- return Content ( e . Message ) ;
126
+ return StatusCode ( e is NotImplementedException ? 501 : e is WarningException ? 202 : 500 , e . Message ) ;
109
127
}
110
-
128
+
129
+
130
+
111
131
}
112
132
113
133
private static string ExpandDiscourseUrls ( string input , string discourseUrl )
@@ -123,7 +143,8 @@ private MattermostMessage PostCreated(PostPayload payload)
123
143
124
144
if ( _config . IgnoredTopicTitles . Contains ( p . topic_title ) )
125
145
{
126
- throw new Exception ( "Post title matches ignored titles" ) ;
146
+ throw new WarningException ( "Post title matches an ignored title" ) ;
147
+
127
148
}
128
149
129
150
if ( _config . IgnorePrivateMessages )
@@ -137,11 +158,15 @@ private MattermostMessage PostCreated(PostPayload payload)
137
158
}
138
159
catch
139
160
{
140
- throw new Exception ( "Unable to retrieve topic, possibly a PM so we should ignore this." ) ;
161
+ throw new WarningException ( "Unable to retrieve topic, possibly a PM so we should ignore this." ) ;
141
162
}
142
163
}
143
164
144
-
165
+ if ( p . cooked == "" )
166
+ {
167
+ throw new WarningException ( "Body empty, nothing to post." ) ;
168
+ }
169
+
145
170
146
171
var retVal = new MattermostMessage
147
172
{
0 commit comments