OSDN Git Service

followers/ids, no_retweets/ids, blocks/ids, help/configuration の取得処理を書き直し
authorKimura Youichi <kim.upsilon@bucyou.net>
Sat, 24 Aug 2013 21:22:58 +0000 (06:22 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Wed, 28 Aug 2013 14:14:45 +0000 (23:14 +0900)
OpenTween/Connection/HttpTwitter.cs
OpenTween/MyCommon.cs
OpenTween/OpenTween.csproj
OpenTween/Resources/ChangeLog.txt
OpenTween/Tween.cs
OpenTween/Twitter.cs
OpenTween/WebApiException.cs [new file with mode: 0644]

index 5867882..294ac79 100644 (file)
@@ -652,14 +652,11 @@ namespace OpenTween
                 HttpTwitter.API11Enabled ? CreateApi11Calllback("/followers/ids") : GetApiCallback);
         }
 
-        public HttpStatusCode NoRetweetIds(long cursor, ref string content)
+        public HttpStatusCode NoRetweetIds(ref string content)
         {
-            Dictionary<string, string> param = new Dictionary<string, string>();
-            param.Add("cursor", cursor.ToString());
-
             return httpCon.GetContent(GetMethod,
                 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/friendships/no_retweets/ids.json" : "/1/friendships/no_retweet_ids.json"),
-                param,
+                null,
                 ref content,
                 this.CreateRetelimitHeadersDict(),
                 HttpTwitter.API11Enabled ? CreateApi11Calllback("/friendships/no_retweets/ids") : GetApiCallback);
@@ -957,11 +954,16 @@ namespace OpenTween
                 GetApiCallback);
         }
 
-        public HttpStatusCode GetBlockUserIds(ref string content)
+        public HttpStatusCode GetBlockUserIds(ref string content, long? cursor)
         {
+            var param = new Dictionary<string, string>();
+
+            if (cursor != null)
+                param.Add("cursor", cursor.ToString());
+
             return httpCon.GetContent(GetMethod,
                 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/blocks/ids.json" : "/1/blocks/blocking/ids.json"),
-                null,
+                param,
                 ref content,
                 this.CreateRetelimitHeadersDict(),
                 HttpTwitter.API11Enabled ? CreateApi11Calllback("/blocks/ids") : GetApiCallback);
index 662c60b..9d919f4 100644 (file)
@@ -228,6 +228,16 @@ namespace OpenTween
             return Path.Combine(Path.GetDirectoryName(MyCommon.EntryAssembly.Location), "ErrorLogs");
         }
 
+        public static void TraceOut(WebApiException ex)
+        {
+            var message = ExceptionOutMessage(ex);
+
+            if (ex.ResponseText != null)
+                message += Environment.NewLine + "------- Response Data -------" + Environment.NewLine + ex.ResponseText;
+
+            TraceOut(TraceFlag, message);
+        }
+
         public static void TraceOut(Exception ex, string Message)
         {
             var buf = ExceptionOutMessage(ex);
index d90dc58..e550110 100644 (file)
     </Compile>
     <Compile Include="Twitter.cs" />
     <Compile Include="UserInfo.cs" />
+    <Compile Include="WebApiException.cs" />
     <Compile Include="WebBrowserController.cs" />
     <Compile Include="Win32Api.cs" />
   </ItemGroup>
index 7684be3..1ca4f9c 100644 (file)
@@ -4,6 +4,8 @@
  * CHG: Twitter API のエラー処理を改善
  * FIX: 「片思いユーザーリストを取得する」を無効にするとRT非表示設定が反映されない問題の修正 (thx @ui_nyan!)
  * FIX: ブロックされているユーザーのツイートをふぁぼった時にUserStreamsが切断されてしまう問題の修正
+ * FIX: 片思いユーザーリストの更新に失敗すると希に片思い表示がされなくなる現象を修正
+ * FIX: ブロック中のユーザーリストが正しく取得できていなかった問題の修正
 
 ==== Ver 1.1.2(2013/07/06)
  * NEW: 画像アップロード対応サービスについっぷるフォトを追加
index 56ee4c2..79b2dd7 100644 (file)
@@ -2596,13 +2596,25 @@ namespace OpenTween
                     break;
                 case MyCommon.WORKERTYPE.Follower:
                     bw.ReportProgress(50, Properties.Resources.UpdateFollowersMenuItem1_ClickText1);
-                    ret = tw.GetFollowersApi();
+                    try
+                    {
+                        tw.RefreshFollowerIds();
+                    }
+                    catch (WebApiException ex) { ret = ex.Message; }
                     break;
                 case MyCommon.WORKERTYPE.NoRetweetIds:
-                    ret = tw.GetNoRetweetIdsApi();
+                    try
+                    {
+                        tw.RefreshNoRetweetIds();
+                    }
+                    catch (WebApiException ex) { ret = ex.Message; }
                     break;
                 case MyCommon.WORKERTYPE.Configuration:
-                    ret = tw.ConfigurationApi();
+                    try
+                    {
+                        this.SettingDialog.TwitterConfiguration = tw.ConfigurationApi();
+                    }
+                    catch (WebApiException ex) { ret = ex.Message; }
                     break;
                 case MyCommon.WORKERTYPE.Favorites:
                     bw.ReportProgress(50, MakeStatusMessage(args, false));
@@ -2688,11 +2700,11 @@ namespace OpenTween
                     break;
                 case MyCommon.WORKERTYPE.BlockIds:
                     bw.ReportProgress(50, Properties.Resources.UpdateBlockUserText1);
-                    ret = tw.GetBlockUserIds();
-                    if (TabInformations.GetInstance().BlockIds.Count == 0)
+                    try
                     {
-                        tw.GetBlockUserIds();
+                        tw.RefreshBlockIds();
                     }
+                    catch (WebApiException ex) { ret = ex.Message; }
                     break;
             }
             //キャンセル要求
index 5bc1e41..084b034 100644 (file)
@@ -138,7 +138,7 @@ namespace OpenTween
         private readonly object LockObj = new object();
         private List<long> followerId = new List<long>();
         private bool _GetFollowerResult = false;
-        private List<long> noRTId = new List<long>();
+        private long[] noRTId = new long[0];
         private bool _GetNoRetweetResult = false;
 
         private int _followersCount = 0;
@@ -2678,29 +2678,27 @@ namespace OpenTween
             return text;
         }
 
-        public string GetFollowersApi()
+        /// <summary>
+        /// フォロワーIDを更新します
+        /// </summary>
+        /// <exception cref="WebApiException"/>
+        public void RefreshFollowerIds()
         {
-            if (MyCommon._endingFlag) return "";
-            long cursor = -1;
-            var tmpFollower = new List<long>(followerId);
+            if (MyCommon._endingFlag) return;
 
-            followerId.Clear();
+            var cursor = -1L;
+            var newFollowerIds = new List<long>();
             do
             {
-                var ret = FollowerApi(ref cursor);
-                if (!string.IsNullOrEmpty(ret))
-                {
-                    followerId.Clear();
-                    followerId.AddRange(tmpFollower);
-                    _GetFollowerResult = false;
-                    return ret;
-                }
-            } while (cursor > 0);
+                var ret = this.GetFollowerIdsApi(ref cursor);
+                newFollowerIds.AddRange(ret.Id);
+                cursor = ret.NextCursor;
+            } while (cursor != 0);
 
-            TabInformations.GetInstance().RefreshOwl(followerId);
+            this.followerId = newFollowerIds;
+            TabInformations.GetInstance().RefreshOwl(this.followerId);
 
-            _GetFollowerResult = true;
-            return "";
+            this._GetFollowerResult = true;
         }
 
         public bool GetFollowersSuccess
@@ -2711,9 +2709,10 @@ namespace OpenTween
             }
         }
 
-        private string FollowerApi(ref long cursor)
+        private TwitterDataModel.Ids GetFollowerIdsApi(ref long cursor)
         {
-            if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
+            if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
+                throw new WebApiException("AccountState invalid");
 
             HttpStatusCode res;
             var content = "";
@@ -2721,89 +2720,81 @@ namespace OpenTween
             {
                 res = twCon.FollowerIds(cursor, ref content);
             }
-            catch(Exception ex)
+            catch(Exception e)
             {
-                return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
+                throw new WebApiException("Err:" + e.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", e);
             }
 
             var err = this.CheckStatusCode(res, content);
-            if (err != null) return err;
+            if (err != null)
+                throw new WebApiException(err, content);
 
             try
             {
-                var followers = MyCommon.CreateDataFromJson<TwitterDataModel.Ids>(content);
-                followerId.AddRange(followers.Id);
-                cursor = followers.NextCursor;
-                return "";
+                return MyCommon.CreateDataFromJson<TwitterDataModel.Ids>(content);
             }
-            catch(SerializationException ex)
+            catch(SerializationException e)
             {
-                MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
-                return "Err:Json Parse Error(DataContractJsonSerializer)";
+                var ex = new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, e);
+                MyCommon.TraceOut(ex);
+                throw ex;
             }
-            catch(Exception ex)
+            catch(Exception e)
             {
-                MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
-                return "Err:Invalid Json!";
+                var ex = new WebApiException("Err:Invalid Json!", content, e);
+                MyCommon.TraceOut(ex);
+                throw ex;
             }
         }
 
-        public string GetNoRetweetIdsApi()
+        /// <summary>
+        /// RT 非表示ユーザーを更新します
+        /// </summary>
+        /// <exception cref="WebApiException"/>
+        public void RefreshNoRetweetIds()
         {
-            if (MyCommon._endingFlag) return "";
-            long cursor = -1;
-            var tmpIds = new List<long>(noRTId);
+            if (MyCommon._endingFlag) return;
 
-            noRTId.Clear();
-            do
-            {
-                var ret = NoRetweetApi(ref cursor);
-                if (!string.IsNullOrEmpty(ret))
-                {
-                    noRTId.Clear();
-                    noRTId.AddRange(tmpIds);
-                    return ret;
-                }
-            } while (cursor > 0);
+            this.noRTId = this.NoRetweetIdsApi();
 
-            _GetNoRetweetResult = true;
-            return "";
+            this._GetNoRetweetResult = true;
         }
 
-        private string NoRetweetApi(ref long cursor)
+        private long[] NoRetweetIdsApi()
         {
-            if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
+            if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
+                throw new WebApiException("AccountState invalid");
 
             HttpStatusCode res;
             var content = "";
             try
             {
-                res = twCon.NoRetweetIds(cursor, ref content);
+                res = twCon.NoRetweetIds(ref content);
             }
-            catch(Exception ex)
+            catch(Exception e)
             {
-                return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
+                throw new WebApiException("Err:" + e.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", e);
             }
 
             var err = this.CheckStatusCode(res, content);
-            if (err != null) return err;
+            if (err != null)
+                throw new WebApiException(err, content);
 
             try
             {
-                var ids = MyCommon.CreateDataFromJson<long[]>(content);
-                noRTId.AddRange(ids);
-                cursor = 0;  //0より小さければ何でも良い。
-                return "";
+                return MyCommon.CreateDataFromJson<long[]>(content);
             }
-            catch(SerializationException ex)
+            catch(SerializationException e)
             {
-                MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
-                return "Err:Json Parse Error(DataContractJsonSerializer)";
+                var ex = new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, e);
+                MyCommon.TraceOut(ex);
+                throw ex;
             }
-            catch(Exception ex)
+            catch(Exception e)
             {
-                MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
-                return "Err:Invalid Json!";
+                var ex = new WebApiException("Err:Invalid Json!", content, e);
+                MyCommon.TraceOut(ex);
+                throw ex;
             }
         }
 
@@ -2815,7 +2806,11 @@ namespace OpenTween
             }
         }
 
-        public string ConfigurationApi()
+        /// <summary>
+        /// t.co の文字列長などの設定情報を取得します
+        /// </summary>
+        /// <exception cref="WebApiException"/>
+        public TwitterDataModel.Configuration ConfigurationApi()
         {
             HttpStatusCode res;
             var content = "";
@@ -2823,28 +2818,30 @@ namespace OpenTween
             {
                 res = twCon.GetConfiguration(ref content);
             }
-            catch(Exception ex)
+            catch(Exception e)
             {
-                return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
+                throw new WebApiException("Err:" + e.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", e);
             }
 
             var err = this.CheckStatusCode(res, content);
-            if (err != null) return err;
+            if (err != null)
+                throw new WebApiException(err, content);
 
             try
             {
-                AppendSettingDialog.Instance.TwitterConfiguration = MyCommon.CreateDataFromJson<TwitterDataModel.Configuration>(content);
-                return "";
+                return MyCommon.CreateDataFromJson<TwitterDataModel.Configuration>(content);
             }
-            catch(SerializationException ex)
+            catch(SerializationException e)
             {
-                MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
-                return "Err:Json Parse Error(DataContractJsonSerializer)";
+                var ex = new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, e);
+                MyCommon.TraceOut(ex);
+                throw ex;
             }
-            catch(Exception ex)
+            catch(Exception e)
             {
-                MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
-                return "Err:Invalid Json!";
+                var ex = new WebApiException("Err:Invalid Json!", content, e);
+                MyCommon.TraceOut(ex);
+                throw ex;
             }
         }
 
@@ -3582,43 +3579,64 @@ namespace OpenTween
             }
         }
 
-        public string GetBlockUserIds()
+        /// <summary>
+        /// ブロック中のユーザーを更新します
+        /// </summary>
+        /// <exception cref="WebApiException"/>
+        public void RefreshBlockIds()
         {
-            if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
+            if (MyCommon._endingFlag) return;
+
+            var cursor = -1L;
+            var newBlockIds = new List<long>();
+            do
+            {
+                var ret = this.GetBlockIdsApi(cursor);
+                newBlockIds.AddRange(ret.Id);
+                cursor = ret.NextCursor;
+            } while (cursor != 0);
+
+            newBlockIds.Remove(this.UserId); // 元のソースにあったので一応残しておく
+
+            TabInformations.GetInstance().BlockIds = newBlockIds;
+        }
+
+        public TwitterDataModel.Ids GetBlockIdsApi(long cursor)
+        {
+            if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
+                throw new WebApiException("AccountState invalid");
 
             HttpStatusCode res;
             var content = "";
-
             try
             {
-                res = twCon.GetBlockUserIds(ref content);
+                res = twCon.GetBlockUserIds(ref content, cursor);
             }
-            catch(Exception ex)
+            catch(Exception e)
             {
-                return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
+                throw new WebApiException("Err:" + e.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", e);
             }
 
             var err = this.CheckStatusCode(res, content);
-            if (err != null) return err;
+            if (err != null)
+                throw new WebApiException(err, content);
 
             try
             {
-                var Ids = MyCommon.CreateDataFromJson<List<long>>(content);
-                if (Ids.Contains(this.UserId)) Ids.Remove(this.UserId);
-                TabInformations.GetInstance().BlockIds.AddRange(Ids);
-                return ("");
+                return MyCommon.CreateDataFromJson<TwitterDataModel.Ids>(content);
             }
-            catch(SerializationException ex)
+            catch(SerializationException e)
             {
-                MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
-                return "Err:Json Parse Error(DataContractJsonSerializer)";
+                var ex = new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, e);
+                MyCommon.TraceOut(ex);
+                throw ex;
             }
-            catch(Exception ex)
+            catch(Exception e)
             {
-                MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
-                return "Err:Invalid Json!";
+                var ex = new WebApiException("Err:Invalid Json!", content, e);
+                MyCommon.TraceOut(ex);
+                throw ex;
             }
-
         }
 
         public string[] GetHashList()
diff --git a/OpenTween/WebApiException.cs b/OpenTween/WebApiException.cs
new file mode 100644 (file)
index 0000000..4874446
--- /dev/null
@@ -0,0 +1,52 @@
+// OpenTween - Client of Twitter
+// Copyright (c) 2013 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
+// All rights reserved.
+//
+// This file is part of OpenTween.
+//
+// This program is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3 of the License, or (at your option)
+// any later version.
+//
+// This program is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+// for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program. If not, see <http://www.gnu.org/licenses/>, or write to
+// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
+// Boston, MA 02110-1301, USA.
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace OpenTween
+{
+    /// <summary>
+    /// Twitter などの API 実行時に発生した例外を扱うクラス
+    /// </summary>
+    public class WebApiException : Exception
+    {
+        public readonly string ResponseText = null;
+
+        public WebApiException() : base() { }
+        public WebApiException(string message) : base(message) { }
+        public WebApiException(string message, Exception innerException) : base(message, innerException) { }
+
+        public WebApiException(string message, string responseText)
+            : this(message)
+        {
+            this.ResponseText = responseText;
+        }
+
+        public WebApiException(string message, string responseText, Exception innerException)
+            : this(message, innerException)
+        {
+            this.ResponseText = responseText;
+        }
+    }
+}