@@ -18,6 +18,14 @@ export function makeAsyncActionSet(actionName) {
1818 } ;
1919}
2020
21+ enum Methods {
22+ GET = 'GET' ,
23+ POST = 'POST' ,
24+ PUT = 'PUT' ,
25+ PATCH = 'PATCH' ,
26+ DELETE = 'DELETE' ,
27+ }
28+
2129// Authentication
2230
2331export const LOGIN = makeAsyncActionSet ( 'LOGIN' ) ;
@@ -27,7 +35,6 @@ export function loginUser(authOptions, code) {
2735
2836 return ( dispatch ) => {
2937 const url = `https://${ hostname } /login/oauth/access_token` ;
30- const method = 'POST' ;
3138 const data = {
3239 client_id : authOptions . clientId ,
3340 client_secret : authOptions . clientSecret ,
@@ -36,7 +43,7 @@ export function loginUser(authOptions, code) {
3643
3744 dispatch ( { type : LOGIN . REQUEST } ) ;
3845
39- return apiRequest ( url , method , data )
46+ return apiRequest ( url , Methods . POST , data )
4047 . then ( function ( response ) {
4148 dispatch ( {
4249 type : LOGIN . SUCCESS ,
@@ -60,7 +67,6 @@ export function logout(): LogoutAction {
6067export const NOTIFICATIONS = makeAsyncActionSet ( 'NOTIFICATIONS' ) ;
6168export function fetchNotifications ( ) {
6269 return ( dispatch , getState : ( ) => AppState ) => {
63- const method = 'GET' ;
6470 const { settings } : { settings : SettingsState } = getState ( ) ;
6571 const isGitHubLoggedIn = getState ( ) . auth . token !== null ;
6672 const endpointSuffix = `notifications?participating=${ settings . participating } ` ;
@@ -72,7 +78,7 @@ export function fetchNotifications() {
7278
7379 const url = `https://api.${ Constants . DEFAULT_AUTH_OPTIONS . hostname } /${ endpointSuffix } ` ;
7480 const token = getState ( ) . auth . token ;
75- return apiRequestAuth ( url , method , token ) ;
81+ return apiRequestAuth ( url , Methods . GET , token ) ;
7682 }
7783
7884 function getEnterpriseNotifications ( ) {
@@ -81,7 +87,7 @@ export function fetchNotifications() {
8187 const hostname = account . hostname ;
8288 const token = account . token ;
8389 const url = `https://${ hostname } /api/v3/${ endpointSuffix } ` ;
84- return apiRequestAuth ( url , method , token ) ;
90+ return apiRequestAuth ( url , Methods . GET , token ) ;
8591 } ) ;
8692 }
8793
@@ -130,7 +136,6 @@ export const MARK_NOTIFICATION = makeAsyncActionSet('MARK_NOTIFICATION');
130136export function markNotification ( id , hostname ) {
131137 return ( dispatch , getState : ( ) => AppState ) => {
132138 const url = `${ generateGitHubAPIUrl ( hostname ) } notifications/threads/${ id } ` ;
133- const method = 'PATCH' ;
134139
135140 const isEnterprise = hostname !== Constants . DEFAULT_AUTH_OPTIONS . hostname ;
136141 const entAccounts = getState ( ) . auth . enterpriseAccounts ;
@@ -140,7 +145,7 @@ export function markNotification(id, hostname) {
140145
141146 dispatch ( { type : MARK_NOTIFICATION . REQUEST } ) ;
142147
143- return apiRequestAuth ( url , method , token , { } )
148+ return apiRequestAuth ( url , Methods . PATCH , token , { } )
144149 . then ( function ( response ) {
145150 dispatch ( {
146151 type : MARK_NOTIFICATION . SUCCESS ,
@@ -156,6 +161,48 @@ export function markNotification(id, hostname) {
156161 } ;
157162}
158163
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+
159206// Repo's Notification
160207
161208export const MARK_REPO_NOTIFICATION = makeAsyncActionSet (
@@ -166,7 +213,6 @@ export function markRepoNotifications(repoSlug, hostname) {
166213 const url = `${ generateGitHubAPIUrl (
167214 hostname
168215 ) } repos/${ repoSlug } /notifications`;
169- const method = 'PUT' ;
170216
171217 const isEnterprise = hostname !== Constants . DEFAULT_AUTH_OPTIONS . hostname ;
172218 const entAccounts = getState ( ) . auth . enterpriseAccounts ;
@@ -176,7 +222,7 @@ export function markRepoNotifications(repoSlug, hostname) {
176222
177223 dispatch ( { type : MARK_REPO_NOTIFICATION . REQUEST } ) ;
178224
179- return apiRequestAuth ( url , method , token , { } )
225+ return apiRequestAuth ( url , Methods . PUT , token , { } )
180226 . then ( function ( response ) {
181227 dispatch ( {
182228 type : MARK_REPO_NOTIFICATION . SUCCESS ,
0 commit comments