Beispiel #1
0
        public static async Task <FollowManager> Create(HohoemaApp hohoemaApp, uint userId)
        {
            var followManager = new FollowManager(hohoemaApp, userId);

            await followManager.Initialize();

            return(followManager);
        }
Beispiel #2
0
        public IAsyncOperation <NiconicoSignInStatus> SignIn(string mailOrTelephone, string password)
        {
            return(AsyncInfo.Run <NiconicoSignInStatus>(async(cancelToken) =>
            {
                if (!Util.InternetConnection.IsInternet())
                {
                    NiconicoContext?.Dispose();
                    NiconicoContext = new NiconicoContext();
                    return NiconicoSignInStatus.Failed;
                }

                if (NiconicoContext != null &&
                    NiconicoContext.AuthenticationToken?.MailOrTelephone == mailOrTelephone &&
                    NiconicoContext.AuthenticationToken?.Password == password)
                {
                    return NiconicoSignInStatus.Success;
                }

                await SignOut();

                try
                {
                    await _SigninLock.WaitAsync();



                    var context = new NiconicoContext(new NiconicoAuthenticationToken(mailOrTelephone, password));

                    context.AdditionalUserAgent = HohoemaUserAgent;

                    LoginErrorText = "";

                    Debug.WriteLine("try login");

                    NiconicoSignInStatus result = NiconicoSignInStatus.Failed;
                    try
                    {
                        result = await context.SignInAsync();
                    }
                    catch
                    {
                        LoginErrorText = "サインインに失敗、再起動をお試しください";
                    }

                    UpdateServiceStatus(result);

                    NiconicoContext = context;

                    if (result == NiconicoSignInStatus.Success)
                    {
                        Debug.WriteLine("login success");


                        using (var loginActivityLogger = LoggingChannel.StartActivity("login process"))
                        {
                            loginActivityLogger.LogEvent("begin login process.");

                            var fields = new LoggingFields();

                            await Task.Delay(500);
                            try
                            {
                                loginActivityLogger.LogEvent("getting UserInfo.");
                                var userInfo = await NiconicoContext.User.GetInfoAsync();

                                LoginUserId = userInfo.Id;
                                IsPremiumUser = userInfo.IsPremium;

                                {
                                    try
                                    {
                                        var user = await NiconicoContext.User.GetUserDetail(LoginUserId.ToString());
                                        LoginUserName = user.Nickname;
                                        UserIconUrl = user.ThumbnailUri;

                                        OnPropertyChanged(nameof(LoginUserName));
                                        OnPropertyChanged(nameof(UserIconUrl));
                                    }
                                    catch (Exception ex)
                                    {
                                        throw new Exception("ユーザー名取得のフォールバック処理に失敗 + " + LoginUserId, ex);
                                    }
                                }

                                fields.AddString("user id", LoginUserId.ToString());
                                fields.AddString("user name", LoginUserName);
                                fields.AddBoolean("is premium", IsPremiumUser);

                                loginActivityLogger.LogEvent("[Success]:get UserInfo.", fields, LoggingLevel.Information);
                            }
                            catch (Exception ex)
                            {
                                LoginErrorText = $"ユーザー情報の取得に失敗しました。再起動をお試しください。({ex.Message})";

                                fields.AddString("mail", mailOrTelephone);
                                loginActivityLogger.LogEvent(LoginErrorText, fields, LoggingLevel.Warning);

                                NiconicoContext.Dispose();
                                NiconicoContext = new NiconicoContext();

                                return NiconicoSignInStatus.Failed;
                            }

                            fields.Clear();



                            Debug.WriteLine("user id is : " + LoginUserId);


                            // 0.4.0以前のバージョンからのログインユーザー情報の移行処理
                            try
                            {
                                await MigrateLegacyUserSettings(LoginUserId.ToString());
                            }
                            catch
                            {
                                LoginErrorText = "ユーザー設定の過去バージョンとの統合処理に失敗しました。";

                                return NiconicoSignInStatus.Failed;
                            }


                            try
                            {
                                Debug.WriteLine("initilize: fav");
                                loginActivityLogger.LogEvent("initialize user favorite");
                                FollowManager = await FollowManager.Create(this, LoginUserId);
                            }
                            catch
                            {
                                LoginErrorText = "お気に入り情報の取得に失敗しました。再起動をお試しください。";
                                Debug.WriteLine(LoginErrorText);
                                loginActivityLogger.LogEvent(LoginErrorText, fields, LoggingLevel.Error);
                                NiconicoContext.Dispose();
                                NiconicoContext = new NiconicoContext();
                                return NiconicoSignInStatus.Failed;
                            }

                            FollowManagerUpdater = BackgroundUpdater.RegistrationBackgroundUpdateScheduleHandler(
                                FollowManager,
                                "FollowManager",
                                label: "フォロー"
                                );


                            Debug.WriteLine("Login done.");
                            loginActivityLogger.LogEvent("[Success]: Login done");
                        }

                        // BG更新をスケジュール
                        UpdateAllComponent();

                        // 動画のキャッシュフォルダの選択状態をチェック
                        await(App.Current as App).CheckVideoCacheFolderState();


                        // サインイン完了
                        OnSignin?.Invoke();

                        // TODO: 途中だった動画のダウンロードを再開
                        // await MediaManager.StartBackgroundDownload();



                        // ニコニコサービスの裏で取得させたいので強制的に待ちを挟む
                        await Task.Delay(1000);
                    }
                    else
                    {
                        Debug.WriteLine("login failed");
                        NiconicoContext?.Dispose();
                        NiconicoContext = null;
                    }

                    return result;
                }
                finally
                {
                    _SigninLock.Release();
                }
            }));
        }
Beispiel #3
0
        public static Task <FollowManager> Create(HohoemaApp hohoemaApp, uint userId)
        {
            var followManager = new FollowManager(hohoemaApp, userId);

            return(Task.FromResult(followManager));
        }