@@ -18,6 +18,14 @@ export function makeAsyncActionSet(actionName) {
18
18
} ;
19
19
}
20
20
21
+ enum Methods {
22
+ GET = 'GET' ,
23
+ POST = 'POST' ,
24
+ PUT = 'PUT' ,
25
+ PATCH = 'PATCH' ,
26
+ DELETE = 'DELETE' ,
27
+ }
28
+
21
29
// Authentication
22
30
23
31
export const LOGIN = makeAsyncActionSet ( 'LOGIN' ) ;
@@ -27,7 +35,6 @@ export function loginUser(authOptions, code) {
27
35
28
36
return ( dispatch ) => {
29
37
const url = `https://${ hostname } /login/oauth/access_token` ;
30
- const method = 'POST' ;
31
38
const data = {
32
39
client_id : authOptions . clientId ,
33
40
client_secret : authOptions . clientSecret ,
@@ -36,7 +43,7 @@ export function loginUser(authOptions, code) {
36
43
37
44
dispatch ( { type : LOGIN . REQUEST } ) ;
38
45
39
- return apiRequest ( url , method , data )
46
+ return apiRequest ( url , Methods . POST , data )
40
47
. then ( function ( response ) {
41
48
dispatch ( {
42
49
type : LOGIN . SUCCESS ,
@@ -60,7 +67,6 @@ export function logout(): LogoutAction {
60
67
export const NOTIFICATIONS = makeAsyncActionSet ( 'NOTIFICATIONS' ) ;
61
68
export function fetchNotifications ( ) {
62
69
return ( dispatch , getState : ( ) => AppState ) => {
63
- const method = 'GET' ;
64
70
const { settings } : { settings : SettingsState } = getState ( ) ;
65
71
const isGitHubLoggedIn = getState ( ) . auth . token !== null ;
66
72
const endpointSuffix = `notifications?participating=${ settings . participating } ` ;
@@ -72,7 +78,7 @@ export function fetchNotifications() {
72
78
73
79
const url = `https://api.${ Constants . DEFAULT_AUTH_OPTIONS . hostname } /${ endpointSuffix } ` ;
74
80
const token = getState ( ) . auth . token ;
75
- return apiRequestAuth ( url , method , token ) ;
81
+ return apiRequestAuth ( url , Methods . GET , token ) ;
76
82
}
77
83
78
84
function getEnterpriseNotifications ( ) {
@@ -81,7 +87,7 @@ export function fetchNotifications() {
81
87
const hostname = account . hostname ;
82
88
const token = account . token ;
83
89
const url = `https://${ hostname } /api/v3/${ endpointSuffix } ` ;
84
- return apiRequestAuth ( url , method , token ) ;
90
+ return apiRequestAuth ( url , Methods . GET , token ) ;
85
91
} ) ;
86
92
}
87
93
@@ -130,7 +136,6 @@ export const MARK_NOTIFICATION = makeAsyncActionSet('MARK_NOTIFICATION');
130
136
export function markNotification ( id , hostname ) {
131
137
return ( dispatch , getState : ( ) => AppState ) => {
132
138
const url = `${ generateGitHubAPIUrl ( hostname ) } notifications/threads/${ id } ` ;
133
- const method = 'PATCH' ;
134
139
135
140
const isEnterprise = hostname !== Constants . DEFAULT_AUTH_OPTIONS . hostname ;
136
141
const entAccounts = getState ( ) . auth . enterpriseAccounts ;
@@ -140,7 +145,7 @@ export function markNotification(id, hostname) {
140
145
141
146
dispatch ( { type : MARK_NOTIFICATION . REQUEST } ) ;
142
147
143
- return apiRequestAuth ( url , method , token , { } )
148
+ return apiRequestAuth ( url , Methods . PATCH , token , { } )
144
149
. then ( function ( response ) {
145
150
dispatch ( {
146
151
type : MARK_NOTIFICATION . SUCCESS ,
@@ -156,6 +161,48 @@ export function markNotification(id, hostname) {
156
161
} ;
157
162
}
158
163
164
+ export const UNSUBSCRIBE_NOTIFICATION = makeAsyncActionSet (
165
+ 'UNSUBSCRIBE_NOTIFICATION'
166
+ ) ;
167
+ export function unsubscribeNotification ( id , hostname ) {
168
+ return ( dispatch , getState : ( ) => AppState ) => {
169
+ const markReadURL = `${ generateGitHubAPIUrl (
170
+ hostname
171
+ ) } notifications/threads/${ id } `;
172
+ const unsubscribeURL = `${ generateGitHubAPIUrl (
173
+ hostname
174
+ ) } notifications/threads/${ id } /subscription`;
175
+
176
+ const isEnterprise = hostname !== Constants . DEFAULT_AUTH_OPTIONS . hostname ;
177
+ const entAccounts = getState ( ) . auth . enterpriseAccounts ;
178
+ const token = isEnterprise
179
+ ? getEnterpriseAccountToken ( hostname , entAccounts )
180
+ : getState ( ) . auth . token ;
181
+
182
+ dispatch ( { type : UNSUBSCRIBE_NOTIFICATION . REQUEST } ) ;
183
+
184
+ return apiRequestAuth ( unsubscribeURL , Methods . DELETE , token , { } )
185
+ . then ( ( response ) => {
186
+ // The GitHub notifications API doesn't automatically mark things as read
187
+ // like it does in the UI, so after unsubscribing we also need to hit the
188
+ // endpoint to mark it as read.
189
+ return apiRequestAuth ( markReadURL , Methods . PATCH , token , { } ) ;
190
+ } )
191
+ . then ( ( response ) => {
192
+ dispatch ( {
193
+ type : UNSUBSCRIBE_NOTIFICATION . SUCCESS ,
194
+ meta : { id, hostname } ,
195
+ } ) ;
196
+ } )
197
+ . catch ( ( error ) => {
198
+ dispatch ( {
199
+ type : UNSUBSCRIBE_NOTIFICATION . FAILURE ,
200
+ payload : error . response . data ,
201
+ } ) ;
202
+ } ) ;
203
+ } ;
204
+ }
205
+
159
206
// Repo's Notification
160
207
161
208
export const MARK_REPO_NOTIFICATION = makeAsyncActionSet (
@@ -166,7 +213,6 @@ export function markRepoNotifications(repoSlug, hostname) {
166
213
const url = `${ generateGitHubAPIUrl (
167
214
hostname
168
215
) } repos/${ repoSlug } /notifications`;
169
- const method = 'PUT' ;
170
216
171
217
const isEnterprise = hostname !== Constants . DEFAULT_AUTH_OPTIONS . hostname ;
172
218
const entAccounts = getState ( ) . auth . enterpriseAccounts ;
@@ -176,7 +222,7 @@ export function markRepoNotifications(repoSlug, hostname) {
176
222
177
223
dispatch ( { type : MARK_REPO_NOTIFICATION . REQUEST } ) ;
178
224
179
- return apiRequestAuth ( url , method , token , { } )
225
+ return apiRequestAuth ( url , Methods . PUT , token , { } )
180
226
. then ( function ( response ) {
181
227
dispatch ( {
182
228
type : MARK_REPO_NOTIFICATION . SUCCESS ,
0 commit comments