@@ -126,39 +126,80 @@ async function getDiscussionUrl(
126
126
) : Promise < string > {
127
127
let url = `${ notification . repository . html_url } /discussions` ;
128
128
129
+ const discussion = await fetchDiscussion ( notification , token , true ) ;
130
+
131
+ if ( discussion ) {
132
+ url = discussion . url ;
133
+
134
+ let comments = discussion . comments . nodes ;
135
+
136
+ let latestCommentId : string | number ;
137
+
138
+ if ( comments ?. length ) {
139
+ latestCommentId = getLatestDiscussionCommentId ( comments ) ;
140
+ url += `#discussioncomment-${ latestCommentId } ` ;
141
+ }
142
+ }
143
+
144
+ return url ;
145
+ }
146
+
147
+ export async function fetchDiscussion (
148
+ notification : Notification ,
149
+ token : string ,
150
+ includeComments : boolean ,
151
+ ) : Promise < DiscussionSearchResultNode | null > {
129
152
const response : GraphQLSearch < DiscussionSearchResultNode > =
130
153
await apiRequestAuth ( `https://api.github.com/graphql` , 'POST' , token , {
131
- query : `{
132
- search(query:"${ formatSearchQueryString (
133
- notification . repository . full_name ,
134
- notification . subject . title ,
135
- notification . updated_at ,
136
- ) } ", type: DISCUSSION, first: 10) {
137
- nodes {
138
- ... on Discussion {
139
- viewerSubscription
140
- title
141
- url
142
- comments(last: 100) {
143
- nodes {
144
- databaseId
145
- createdAt
146
- replies(last: 1) {
147
- nodes {
148
- databaseId
149
- createdAt
154
+ query : `query fetchDiscussions(
155
+ $includeComments: Boolean!,
156
+ $queryStatement: String!,
157
+ $type: SearchType!,
158
+ $firstDiscussions: Int,
159
+ $lastComments: Int,
160
+ $firstReplies: Int
161
+ ) {
162
+ search(query:$queryStatement, type: $type, first: $firstDiscussions) {
163
+ nodes {
164
+ ... on Discussion {
165
+ viewerSubscription
166
+ title
167
+ stateReason
168
+ isAnswered
169
+ url
170
+ comments(last: $lastComments) @include(if: $includeComments){
171
+ nodes {
172
+ databaseId
173
+ createdAt
174
+ replies(last: $firstReplies) {
175
+ nodes {
176
+ databaseId
177
+ createdAt
178
+ }
150
179
}
151
180
}
152
181
}
153
182
}
154
183
}
155
184
}
156
185
}
157
- }` ,
186
+ ` ,
187
+ variables : {
188
+ queryStatement : formatSearchQueryString (
189
+ notification . repository . full_name ,
190
+ notification . subject . title ,
191
+ notification . updated_at ,
192
+ ) ,
193
+ type : 'DISCUSSION' ,
194
+ firstDiscussions : 10 ,
195
+ lastComments : 100 ,
196
+ firstReplies : 1 ,
197
+ includeComments : includeComments ,
198
+ } ,
158
199
} ) ;
159
200
160
201
let discussions =
161
- response ?. data ?. data ? .search ? .nodes ? .filter (
202
+ response ?. data ?. data . search . nodes . filter (
162
203
( discussion ) => discussion . title === notification . subject . title ,
163
204
) || [ ] ;
164
205
@@ -167,29 +208,17 @@ async function getDiscussionUrl(
167
208
( discussion ) => discussion . viewerSubscription === 'SUBSCRIBED' ,
168
209
) ;
169
210
170
- if ( discussions [ 0 ] ) {
171
- const discussion = discussions [ 0 ] ;
172
- url = discussion . url ;
173
-
174
- let comments = discussion . comments . nodes ;
175
-
176
- let latestCommentId : string | number ;
177
- if ( comments ?. length ) {
178
- latestCommentId = getLatestDiscussionCommentId ( comments ) ;
179
- url += `#discussioncomment-${ latestCommentId } ` ;
180
- }
181
- }
182
-
183
- return url ;
211
+ return discussions [ 0 ] ;
184
212
}
185
213
186
- export const getLatestDiscussionCommentId = (
214
+ export function getLatestDiscussionCommentId (
187
215
comments : DiscussionCommentNode [ ] ,
188
- ) =>
189
- comments
216
+ ) {
217
+ return comments
190
218
. flatMap ( ( comment ) => comment . replies . nodes )
191
219
. concat ( [ comments . at ( - 1 ) ] )
192
220
. reduce ( ( a , b ) => ( a . createdAt > b . createdAt ? a : b ) ) ?. databaseId ;
221
+ }
193
222
194
223
export async function generateGitHubWebUrl (
195
224
notification : Notification ,
0 commit comments