OSDN Git Service

Twitter.GetTimelineApiメソッドをGetHomeTimelineApi, GetMentionsTimelineApiに分割
[opentween/open-tween.git] / OpenTween / Models / HomeTabModel.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2007-2011 kiri_feather (@kiri_feather) <kiri.feather@gmail.com>
3 //           (c) 2008-2011 Moz (@syo68k)
4 //           (c) 2008-2011 takeshik (@takeshik) <http://www.takeshik.org/>
5 //           (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
6 //           (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
7 //           (c) 2011      Egtra (@egtra) <http://dev.activebasic.com/egtra/>
8 //           (c) 2012      kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
9 // All rights reserved.
10 //
11 // This file is part of OpenTween.
12 //
13 // This program is free software; you can redistribute it and/or modify it
14 // under the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 3 of the License, or (at your option)
16 // any later version.
17 //
18 // This program is distributed in the hope that it will be useful, but
19 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 // for more details.
22 //
23 // You should have received a copy of the GNU General Public License along
24 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
25 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
26 // Boston, MA 02110-1301, USA.
27
28 using System;
29 using System.Collections.Concurrent;
30 using System.Collections.Generic;
31 using System.Linq;
32 using System.Text;
33 using System.Threading;
34 using System.Threading.Tasks;
35
36 namespace OpenTween.Models
37 {
38     public class HomeTabModel : TabModel
39     {
40         public override MyCommon.TabUsageType TabType
41             => MyCommon.TabUsageType.Home;
42
43         public int TweetsPerHour => this.tweetsPerHour;
44
45         // 流速計測用
46         private int tweetsPerHour = 0;
47         private ConcurrentDictionary<DateTime, int> tweetsTimestamps = new ConcurrentDictionary<DateTime, int>();
48
49         public HomeTabModel() : this(MyCommon.DEFAULTTAB.RECENT)
50         {
51         }
52
53         public HomeTabModel(string tabName) : base(tabName)
54         {
55         }
56
57         public override void AddPostQueue(PostClass post)
58         {
59             base.AddPostQueue(post);
60             this.UpdateTimelineSpeed(post.CreatedAt);
61         }
62
63         public override async Task RefreshAsync(Twitter tw, bool backward, bool startup, IProgress<string> progress)
64         {
65             bool read;
66             if (!SettingCommon.Instance.UnreadManage)
67                 read = true;
68             else
69                 read = startup && SettingCommon.Instance.Read;
70
71             progress.Report(string.Format(Properties.Resources.GetTimelineWorker_RunWorkerCompletedText5, backward ? -1 : 1));
72
73             await tw.GetHomeTimelineApi(read, this, backward, startup)
74                 .ConfigureAwait(false);
75
76             // 新着時未読クリア
77             if (SettingCommon.Instance.ReadOldPosts)
78                 TabInformations.GetInstance().SetReadHomeTab();
79
80             TabInformations.GetInstance().DistributePosts();
81
82             progress.Report(Properties.Resources.GetTimelineWorker_RunWorkerCompletedText1);
83         }
84
85         /// <summary>
86         /// タイムラインに追加された発言件数を反映し、タイムラインの流速を更新します
87         /// </summary>
88         private void UpdateTimelineSpeed(DateTime postCreatedAt)
89         {
90             var now = DateTime.Now;
91
92             // 1 時間以上前の時刻は追加しない
93             var oneHour = TimeSpan.FromHours(1);
94             if (now - postCreatedAt > oneHour)
95                 return;
96
97             this.tweetsTimestamps.AddOrUpdate(postCreatedAt, 1, (k, v) => v + 1);
98
99             var removeKeys = new List<DateTime>();
100             var tweetsInWindow = 0;
101             foreach (var pair in this.tweetsTimestamps)
102             {
103                 if (now - pair.Key > oneHour)
104                     removeKeys.Add(pair.Key);
105                 else
106                     tweetsInWindow += pair.Value;
107             }
108             Interlocked.Exchange(ref this.tweetsPerHour, tweetsInWindow);
109
110             int _;
111             foreach (var key in removeKeys)
112                 this.tweetsTimestamps.TryRemove(key, out _);
113         }
114     }
115 }