OSDN Git Service

OAuth2Sessionを使用したAPIアクセスに対応
[opentween/open-tween.git] / OpenTween / Setting / SettingCommon.cs
index 895dce8..ed6ae8e 100644 (file)
@@ -30,10 +30,12 @@ using System;
 using System.Collections.Generic;
 using System.Windows.Forms;
 using System.Xml.Serialization;
+using OpenTween.Connection;
 using OpenTween.Thumbnail;
 
 namespace OpenTween
 {
+    [XmlSerializerAssembly(null, null)] // OpenTween アセンブリ内の XmlSerializerContract を使用させる
     public class SettingCommon : SettingBase<SettingCommon>
     {
         #region "Settingクラス基本"
@@ -45,6 +47,13 @@ namespace OpenTween
         #endregion
 
         public List<UserAccount> UserAccounts = new();
+
+        public Guid? SelectedAccountKey { get; set; } = null;
+
+        [XmlIgnore]
+        public UserAccount? SelectedAccount
+            => this.UserAccounts.Find(x => x.UniqueKey == this.SelectedAccountKey);
+
         public string UserName = "";
 
         [XmlIgnore]
@@ -156,6 +165,12 @@ namespace OpenTween
         public bool TinyUrlResolve = true;
         public bool StartupVersion = true;
         public bool StartupFollowers = true;
+
+        /// <summary>
+        /// Twitter API v2 の使用を有効にする
+        /// </summary>
+        public bool EnableTwitterV2Api { get; set; } = true;
+
         public bool RestrictFavCheck = false;
         public bool AlwaysTop = false;
         public string CultureCode = "";
@@ -261,28 +276,25 @@ namespace OpenTween
         [XmlElement(ElementName = nameof(SkipUpdateVersion))]
         public string SkipUpdateVersionStr { get; set; } = "";
 
-        public void Validate(bool noLimit = false)
+        public void Validate()
         {
-            if (!noLimit)
-            {
-                if (this.TimelinePeriod < 15 && this.TimelinePeriod > 0)
-                    this.TimelinePeriod = 15;
+            if (this.TimelinePeriod < 0)
+                this.TimelinePeriod = 15;
 
-                if (this.ReplyPeriod < 15 && this.ReplyPeriod > 0)
-                    this.ReplyPeriod = 15;
+            if (this.ReplyPeriod < 0)
+                this.ReplyPeriod = 15;
 
-                if (this.DMPeriod < 15 && this.DMPeriod > 0)
-                    this.DMPeriod = 15;
+            if (this.DMPeriod < 0)
+                this.DMPeriod = 15;
 
-                if (this.PubSearchPeriod < 30 && this.PubSearchPeriod > 0)
-                    this.PubSearchPeriod = 30;
+            if (this.PubSearchPeriod < 0)
+                this.PubSearchPeriod = 30;
 
-                if (this.UserTimelinePeriod < 15 && this.UserTimelinePeriod > 0)
-                    this.UserTimelinePeriod = 15;
+            if (this.UserTimelinePeriod < 0)
+                this.UserTimelinePeriod = 15;
 
-                if (this.ListsPeriod < 15 && this.ListsPeriod > 0)
-                    this.ListsPeriod = 15;
-            }
+            if (this.ListsPeriod < 0)
+                this.ListsPeriod = 15;
 
             if (!Twitter.VerifyApiResultCount(MyCommon.WORKERTYPE.Timeline, this.CountApi))
                 this.CountApi = 60;
@@ -312,9 +324,20 @@ namespace OpenTween
             if (this.AutoShortUrlFirst < 0)
                 this.AutoShortUrlFirst = MyCommon.UrlConverter.Uxnu;
 
-            var selectedAccount = this.UserAccounts.Find(
-                x => string.Equals(x.Username, this.UserName, StringComparison.InvariantCultureIgnoreCase)
-            );
+            UserAccount? selectedAccount;
+            if (this.SelectedAccountKey != null)
+            {
+                selectedAccount = this.SelectedAccount;
+            }
+            else
+            {
+                selectedAccount = this.UserAccounts.Find(
+                    x => string.Equals(x.Username, this.UserName, StringComparison.InvariantCultureIgnoreCase)
+                );
+            }
+
+            this.SelectedAccountKey = selectedAccount?.UniqueKey;
+
             if (selectedAccount?.UserId == 0)
                 selectedAccount.UserId = this.UserId;
 
@@ -325,9 +348,35 @@ namespace OpenTween
 
     public class UserAccount
     {
+        public Guid UniqueKey { get; set; } = Guid.NewGuid();
+
         public string Username = "";
         public long UserId = 0;
+
+        public APIAuthType TwitterAuthType { get; set; }
+
+        public string TwitterOAuth1ConsumerKey { get; set; } = "";
+
+        [XmlIgnore]
+        public string TwitterOAuth1ConsumerSecret { get; set; } = "";
+
+        public string TwitterOAuth1ConsumerSecretEncrypted
+        {
+            get => this.Encrypt(this.TwitterOAuth1ConsumerSecret);
+            set => this.TwitterOAuth1ConsumerSecret = this.Decrypt(value);
+        }
+
+        [XmlIgnore]
+        public string TwitterComCookie { get; set; } = "";
+
+        public string TwitterComCookieEncrypted
+        {
+            get => this.Encrypt(this.TwitterComCookie);
+            set => this.TwitterComCookie = this.Decrypt(value);
+        }
+
         public string Token = "";
+
         [XmlIgnore]
         public string TokenSecret = "";
 
@@ -337,6 +386,17 @@ namespace OpenTween
             set => this.TokenSecret = this.Decrypt(value);
         }
 
+        public TwitterAppToken GetTwitterAppToken()
+        {
+            return new()
+            {
+                AuthType = this.TwitterAuthType,
+                OAuth1ConsumerKey = ApiKey.Create(this.TwitterOAuth1ConsumerKey),
+                OAuth1ConsumerSecret = ApiKey.Create(this.TwitterOAuth1ConsumerSecret),
+                TwitterComCookie = this.TwitterComCookie,
+            };
+        }
+
         private string Encrypt(string password)
         {
             if (MyCommon.IsNullOrEmpty(password)) password = "";
@@ -377,4 +437,10 @@ namespace OpenTween
         public override string ToString()
             => this.Username;
     }
+
+    public enum APIAuthType
+    {
+        OAuth1,
+        TwitterComCookie,
+    }
 }
\ No newline at end of file