Archived
1
0

Added subscriptions management

This commit is contained in:
Michael Gordeev
2020-05-11 02:43:13 +03:00
parent 454ea6c0d0
commit f89bf80018
2 changed files with 73 additions and 5 deletions
+67 -3
View File
@@ -1,11 +1,16 @@
using Google.Apis.Auth.OAuth2; using FoxTube.Utils;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Oauth2.v2; using Google.Apis.Oauth2.v2;
using Google.Apis.Oauth2.v2.Data; using Google.Apis.Oauth2.v2.Data;
using Google.Apis.Services; using Google.Apis.Services;
using Google.Apis.YouTube.v3; using Google.Apis.YouTube.v3;
using Google.Apis.YouTube.v3.Data; using Google.Apis.YouTube.v3.Data;
using Newtonsoft.Json;
using System;
using System.Linq;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Windows.Storage;
using YouTube; using YouTube;
using YoutubeExplode; using YoutubeExplode;
@@ -21,11 +26,69 @@ namespace FoxTube.Models
public ExtendedYouTubeService Service { get; private set; } public ExtendedYouTubeService Service { get; private set; }
public YoutubeClient Client { get; private set; } public YoutubeClient Client { get; private set; }
private User() { }
public async Task<bool> UpdateSubscriptionState(string channelId)
{
if (Subscriptions.Exists(x => x.Snippet.ResourceId.ChannelId == channelId))
{
Subscription s = Subscriptions.Find(x => x.Snippet.ResourceId.ChannelId == channelId);
try { await Service.Subscriptions.Delete(s.Id).ExecuteAsync(); }
catch (Exception e)
{
Metrics.SendReport(new Exception("Failed to unsubscribe", e));
return true;
}
UserManagement.SubscriptionsChangedInvoker(this, s);
Subscriptions.Remove(s);
SaveSubscriptions();
return false;
}
else
{
var request = Service.Subscriptions.Insert(new Subscription()
{
Snippet = new SubscriptionSnippet()
{
ResourceId = new ResourceId()
{
ChannelId = channelId,
Kind = "youtube#channel"
}
}
}, "snippet");
Subscription s = await request.ExecuteAsync();
if (s == null)
return false;
Subscriptions.Add(s);
UserManagement.SubscriptionsChangedInvoker(this, s);
SaveSubscriptions();
return true;
}
}
private void SaveSubscriptions()
{
Dictionary<string, string> subs = Subscriptions.Select(i =>
new KeyValuePair<string, string>(i.Snippet.ResourceId.ChannelId, i.Snippet.Thumbnails.Default__?.Url))
as Dictionary<string, string>;
ApplicationData.Current.RoamingSettings.Values[$"Subscriptions.{UserInfo.Id}"] = JsonConvert.SerializeObject(subs);
}
public static async Task<User> GetUser(UserCredential credential) public static async Task<User> GetUser(UserCredential credential)
{ {
User user = new User(); User user = new User
{
Credential = credential
};
user.Credential = credential;
BaseClientService.Initializer initializer = new BaseClientService.Initializer BaseClientService.Initializer initializer = new BaseClientService.Initializer
{ {
ApplicationName = "FoxTube", ApplicationName = "FoxTube",
@@ -53,6 +116,7 @@ namespace FoxTube.Models
} while (!string.IsNullOrWhiteSpace(subRequest.PageToken)); } while (!string.IsNullOrWhiteSpace(subRequest.PageToken));
var request = user.Service.Channels.List("snippet,contentDetails,brandingSettings"); var request = user.Service.Channels.List("snippet,contentDetails,brandingSettings");
request.Mine = true; request.Mine = true;
user.Channel = request.Execute().Items[0]; user.Channel = request.Execute().Items[0];
+6 -2
View File
@@ -27,13 +27,13 @@ namespace FoxTube
#region Private members #region Private members
private static readonly ApplicationDataContainer storage = ApplicationData.Current.LocalSettings; private static readonly ApplicationDataContainer storage = ApplicationData.Current.LocalSettings;
private static ExtendedYouTubeService _defaultService = new ExtendedYouTubeService(new Google.Apis.Services.BaseClientService.Initializer private static readonly ExtendedYouTubeService _defaultService = new ExtendedYouTubeService(new Google.Apis.Services.BaseClientService.Initializer
{ {
ApplicationName = "FoxTube", ApplicationName = "FoxTube",
ApiKey = "AIzaSyBgHrCnrlzlVmk0cJKL8RqP9Y8x6XSuk_0", ApiKey = "AIzaSyBgHrCnrlzlVmk0cJKL8RqP9Y8x6XSuk_0",
//ApiKey = "AIzaSyD7tpbuvmYDv9h4udo9L_g3r0sLPFAnN00" //ApiKey = "AIzaSyD7tpbuvmYDv9h4udo9L_g3r0sLPFAnN00"
}); });
private static YoutubeClient _defaultYteClient = new YoutubeClient(); private static readonly YoutubeClient _defaultYteClient = new YoutubeClient();
private static string[] Scopes { get; } = new string[] private static string[] Scopes { get; } = new string[]
{ {
@@ -131,6 +131,7 @@ namespace FoxTube
await CurrentUser.Credential.RevokeTokenAsync(CancellationToken.None); await CurrentUser.Credential.RevokeTokenAsync(CancellationToken.None);
storage.Values.Remove($"Subscriptions.{CurrentUser.UserInfo.Id}");
CurrentUser = null; CurrentUser = null;
Users[Users.ToList().FindIndex(i => i.Id == userId)] = null; Users[Users.ToList().FindIndex(i => i.Id == userId)] = null;
@@ -201,5 +202,8 @@ namespace FoxTube
else else
vaultCredential.Password = refreshToken; vaultCredential.Password = refreshToken;
} }
internal static void SubscriptionsChangedInvoker(User sender, Subscription subscription) =>
SubscriptionsChanged?.Invoke(sender, subscription);
} }
} }