diff --git a/src/components/ChannelItem.vue b/src/components/ChannelItem.vue
index 8b7f708e..e3eebb99 100644
--- a/src/components/ChannelItem.vue
+++ b/src/components/ChannelItem.vue
@@ -1,37 +1,72 @@
-
+
-
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
-
-
+
+
+
+
+
+
-
diff --git a/src/components/ChannelPage.vue b/src/components/ChannelPage.vue
index 1bc71548..88f73e86 100644
--- a/src/components/ChannelPage.vue
+++ b/src/components/ChannelPage.vue
@@ -127,24 +127,8 @@ export default {
methods: {
async fetchSubscribedStatus() {
if (!this.channel.id) return;
- if (!this.authenticated) {
- this.subscribed = this.isSubscribedLocally(this.channel.id);
- return;
- }
- this.fetchJson(
- this.authApiUrl() + "/subscribed",
- {
- channelId: this.channel.id,
- },
- {
- headers: {
- Authorization: this.getAuthToken(),
- },
- },
- ).then(json => {
- this.subscribed = json.subscribed;
- });
+ this.subscribed = await this.fetchSubscriptionStatus(this.channel.id);
},
async fetchChannel() {
const url = this.$route.path.includes("@")
@@ -216,21 +200,9 @@ export default {
});
},
subscribeHandler() {
- if (this.authenticated) {
- this.fetchJson(this.authApiUrl() + (this.subscribed ? "/unsubscribe" : "/subscribe"), null, {
- method: "POST",
- body: JSON.stringify({
- channelId: this.channel.id,
- }),
- headers: {
- Authorization: this.getAuthToken(),
- "Content-Type": "application/json",
- },
- });
- } else {
- if (!this.handleLocalSubscriptions(this.channel.id)) return;
- }
- this.subscribed = !this.subscribed;
+ this.toggleSubscriptionState(this.channel.id, this.subscribed).then(success => {
+ if (success) this.subscribed = !this.subscribed;
+ });
},
getTranslatedTabName(tabName) {
let translatedTabName = tabName;
diff --git a/src/components/WatchVideo.vue b/src/components/WatchVideo.vue
index 3a07df0c..9204a46e 100644
--- a/src/components/WatchVideo.vue
+++ b/src/components/WatchVideo.vue
@@ -551,24 +551,8 @@ export default {
},
async fetchSubscribedStatus() {
if (!this.channelId) return;
- if (!this.authenticated) {
- this.subscribed = this.isSubscribedLocally(this.channelId);
- return;
- }
- this.fetchJson(
- this.authApiUrl() + "/subscribed",
- {
- channelId: this.channelId,
- },
- {
- headers: {
- Authorization: this.getAuthToken(),
- },
- },
- ).then(json => {
- this.subscribed = json.subscribed;
- });
+ this.subscribed = await this.fetchSubscriptionStatus(this.channelId);
},
rewriteComments(data) {
data.forEach(comment => {
@@ -588,21 +572,9 @@ export default {
});
},
subscribeHandler() {
- if (this.authenticated) {
- this.fetchJson(this.authApiUrl() + (this.subscribed ? "/unsubscribe" : "/subscribe"), null, {
- method: "POST",
- body: JSON.stringify({
- channelId: this.channelId,
- }),
- headers: {
- Authorization: this.getAuthToken(),
- "Content-Type": "application/json",
- },
- });
- } else {
- if (!this.handleLocalSubscriptions(this.channelId)) return;
- }
- this.subscribed = !this.subscribed;
+ this.toggleSubscriptionState(this.channelId, this.subscribed).then(success => {
+ if (success) this.subscribed = !this.subscribed;
+ });
},
handleClick(event) {
if (!event || !event.target) return;
diff --git a/src/main.js b/src/main.js
index 9d6f8382..b4947cca 100644
--- a/src/main.js
+++ b/src/main.js
@@ -550,6 +550,41 @@ const mixin = {
});
});
},
+ async fetchSubscriptionStatus(channelId) {
+ if (!this.authenticated) {
+ return this.isSubscribedLocally(channelId);
+ }
+
+ const response = await this.fetchJson(
+ this.authApiUrl() + "/subscribed",
+ {
+ channelId: channelId,
+ },
+ {
+ headers: {
+ Authorization: this.getAuthToken(),
+ },
+ },
+ );
+
+ return response?.subscribed;
+ },
+ async toggleSubscriptionState(channelId, subscribed) {
+ if (!this.authenticated) return this.handleLocalSubscriptions(channelId);
+
+ const resp = await this.fetchJson(this.authApiUrl() + (subscribed ? "/unsubscribe" : "/subscribe"), null, {
+ method: "POST",
+ body: JSON.stringify({
+ channelId: channelId,
+ }),
+ headers: {
+ Authorization: this.getAuthToken(),
+ "Content-Type": "application/json",
+ },
+ });
+
+ return !resp.error;
+ },
},
computed: {
authenticated(_this) {