Skip to content

Commit

Permalink
Remove login form and function. #57
Browse files Browse the repository at this point in the history
  • Loading branch information
noriokun4649 committed May 31, 2022
1 parent 78ec000 commit 020687e
Showing 1 changed file with 10 additions and 58 deletions.
68 changes: 10 additions & 58 deletions TVTComment/Model/ChatService/NiconicoChatService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TVTComment.Model.NiconicoUtils;

namespace TVTComment.Model.ChatService
{
class NiconicoChatServiceSettings
{
public string UserId { get; set; } = "";
public string Password { get; set; } = "";
public string OAuthToken { get; set; } = string.Empty;
}

class NiconicoChatService : IChatService
Expand All @@ -26,30 +26,26 @@ class NiconicoChatService : IChatService
private readonly NiconicoUtils.JkIdResolver jkIdResolver;
private readonly NiconicoChatServiceSettings settings;

public string UserId
public string OAuthToken
{
get { return settings.UserId; }
get { return settings.OAuthToken; }
set { settings.OAuthToken = value; }
}
public string UserPassword
{
get { return settings.Password; }
}
public bool IsLoggedin { get; private set; }
public bool IsAuthorization { get; private set; }

public NiconicoChatService(
NiconicoChatServiceSettings settings, ChannelDatabase channelDatabase,
string jikkyouIdTableFilePath, string liveIdTableFilePath
)
{
this.settings = settings; this.settings = settings;
this.settings = settings;
jkIdResolver = new NiconicoUtils.JkIdResolver(channelDatabase, new NiconicoUtils.JkIdTable(jikkyouIdTableFilePath));
liveIdResolver = new NiconicoUtils.LiveIdResolver(channelDatabase, new NiconicoUtils.LiveIdTable(liveIdTableFilePath));

try
{
if (!string.IsNullOrWhiteSpace(UserId) && !string.IsNullOrWhiteSpace(UserPassword))
SetUser(UserId, UserPassword).Wait();
if (!string.IsNullOrWhiteSpace(OAuthToken))
SetToken(OAuthToken).ConfigureAwait(false);
}
catch (AggregateException e)
when (e.InnerExceptions.Count == 1 && e.InnerExceptions[0] is NiconicoUtils.NiconicoLoginSessionException)
Expand All @@ -63,38 +59,6 @@ public NiconicoChatService(
ChatTrendServiceEntries = new IChatTrendServiceEntry[] { new NewNiconicoChatTrendServiceEntry(liveIdResolver, loginSession) };
}

/// <summary>
/// ニコニコのユーザーを指定しログインする
/// 失敗した場合オブジェクトの状態は変化しない
/// </summary>
/// <param name="userId">ニコニコのユーザーID</param>
/// <param name="userPassword">ニコニコのパスワード</param>
/// <exception cref="ArgumentException"><paramref name="userId"/>または<paramref name="userPassword"/>がnull若しくはホワイトスペースだった時</exception>
/// <exception cref="NiconicoUtils.NiconicoLoginSessionException">ログインに失敗した時</exception>
public async Task SetUser(string userId, string userPassword)
{
if (string.IsNullOrWhiteSpace(userId))
throw new ArgumentException($"{nameof(userId)} must not be null nor white space", nameof(userId));
if (string.IsNullOrWhiteSpace(userPassword))
throw new ArgumentException($"{nameof(userPassword)} must not be null nor white space", nameof(userPassword));

//ログインしてみる
var tmpSession = new NiconicoUtils.NiconicoLoginSession(userId, userPassword);
await tmpSession.Login().ConfigureAwait(false);

//成功したら設定、セッションを置き換える
IsLoggedin = true;
settings.UserId = userId;
settings.Password = userPassword;
try
{
await (loginSession.Value?.Logout() ?? Task.CompletedTask);
}
catch (NiconicoUtils.NiconicoLoginSessionException)
{ }
loginSession.Value = tmpSession;
}

/// <summary>
/// ニコニコの認可トークンを指定し検証する
/// 失敗した場合オブジェクトの状態は変化しない
Expand All @@ -106,29 +70,17 @@ public async Task SetToken(string token)
{
if (string.IsNullOrWhiteSpace(token))
throw new ArgumentException($"{nameof(token)} must not be null nor white space", nameof(token));

loginSession.Value = new NiconicoLoginSession();
//検証してみる
await loginSession.Value.Authorization(token).ConfigureAwait(false);

//成功したら設定
IsAuthorization = true;
IsLoggedin = true;
OAuthToken = token;
}

public void Dispose()
{
if(this.loginSession.Value?.IsLoggedin ?? false)
{
try
{
this.loginSession.Value.Logout().Wait();
}
catch (AggregateException e) when (e.InnerExceptions.All(
x => x is NiconicoUtils.NiconicoLoginSessionException
))
{
}
}
}
}
}

0 comments on commit 020687e

Please sign in to comment.