From 3130905852bd285bf746ebce2fa0970cec1a753d Mon Sep 17 00:00:00 2001 From: Jon Ziebell Date: Mon, 19 Sep 2022 17:33:24 -0400 Subject: [PATCH] Fixed #367 - Cannot unlink Patreon account after linking --- api/patreon_token.php | 24 +++++++++++++------- api/user.php | 26 ++++++++++++++++++++- css/dashboard.css | 1 + js/component/modal/patreon_status.js | 34 ++++++++++++++++++++++++++-- 4 files changed, 74 insertions(+), 11 deletions(-) diff --git a/api/patreon_token.php b/api/patreon_token.php index 7e6fa5b..a96efb3 100644 --- a/api/patreon_token.php +++ b/api/patreon_token.php @@ -42,17 +42,25 @@ class patreon_token extends cora\crud { $new_patreon_token = [ 'access_token' => $response['access_token'], - 'refresh_token' => $response['refresh_token'] + 'refresh_token' => $response['refresh_token'], + 'deleted' => false ]; - $existing_patreon_tokens = $this->read(); - if(count($existing_patreon_tokens) > 0) { - $new_patreon_token['patreon_token_id'] = $existing_patreon_tokens[0]['patreon_token_id']; - $this->update( + /** + * Look for existing tokens (in case access was revoked and then + * re-granted). Include deleted tokens and revive that row since each user + * is limited to one token row. + */ + $existing_patreon_token = $this->read([ + 'deleted' => [0, 1] + ]); + + if (count($existing_patreon_token) > 0) { + $this->update(array_merge( + ['patreon_token_id' => $existing_patreon_token[0]['patreon_token_id']], $new_patreon_token - ); - } - else { + )); + } else { $this->create($new_patreon_token); } diff --git a/api/user.php b/api/user.php index d937ef9..f2060af 100644 --- a/api/user.php +++ b/api/user.php @@ -13,7 +13,8 @@ class user extends cora\crud { 'read_id', 'update_setting', 'log_out', - 'sync_patreon_status' + 'sync_patreon_status', + 'unlink_patreon_account', ], 'public' => [] ]; @@ -345,4 +346,27 @@ class user extends cora\crud { $this->database->release_lock($lock_name); } + /** + * Unlink the Patreon account for the current user. + */ + public function unlink_patreon_account() { + $patreon_tokens = $this->api('patreon_token', 'read_id'); + foreach($patreon_tokens as $patreon_token) { + $this->api( + 'patreon_token', + 'delete', + [ + 'id' => $patreon_token['patreon_token_id'] + ] + ); + } + + $this->update( + [ + 'user_id' => $this->session->get_user_id(), + 'patreon_status' => null + ] + ); + } + } diff --git a/css/dashboard.css b/css/dashboard.css index 8c54cc7..3d771a9 100644 --- a/css/dashboard.css +++ b/css/dashboard.css @@ -480,6 +480,7 @@ input[type=range]::-moz-range-thumb { .icon.label:before { content: "\F0315"; } .icon.layers:before { content: "\F0328"; } .icon.layers_plus:before { content: "\F0E4D"; } +.icon.link_off:before { content: "\F0338"; } .icon.magnify_close:before { content: "\F0980"; } .icon.magnify_minus_outline:before { content: "\F06EC"; } .icon.magnify_plus_outline:before { content: "\F06ED"; } diff --git a/js/component/modal/patreon_status.js b/js/component/modal/patreon_status.js index f23028b..d766473 100644 --- a/js/component/modal/patreon_status.js +++ b/js/component/modal/patreon_status.js @@ -199,7 +199,34 @@ beestat.component.modal.patreon_status.prototype.get_title_ = function() { */ beestat.component.modal.patreon_status.prototype.get_buttons_ = function() { if (beestat.user.patreon_is_connected() === true) { - var refresh = new beestat.component.tile() + const unlink = new beestat.component.tile() + .set_text('Unlink') + .set_icon('link_off') + .set_shadow(false) + .set_background_color('#fff') + .set_text_color(beestat.style.color.gray.base) + .set_text_hover_color(beestat.style.color.red.base) + .addEventListener('click', function() { + this + .removeEventListener('click'); + + new beestat.api() + .add_call( + 'user', + 'unlink_patreon_account', + {}, + 'unlink_patreon_account' + ) + .add_call('user', 'read_id', {}, 'user') + .set_callback(function(response) { + // Update the cache. + self.dispose(); + beestat.cache.set('user', response.user); + }) + .send(); + }); + + const refresh = new beestat.component.tile() .set_text('Refresh Status') .set_icon('refresh') .set_background_color(beestat.style.color.green.base) @@ -226,7 +253,10 @@ beestat.component.modal.patreon_status.prototype.get_buttons_ = function() { .send(); }); - return [refresh]; + return [ + unlink, + refresh + ]; } return [];