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.
11 // This file is part of OpenTween.
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)
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
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.
29 using System.Collections.Concurrent;
30 using System.Collections.Generic;
33 using System.Threading;
34 using System.Threading.Tasks;
35 using OpenTween.Setting;
37 namespace OpenTween.Models
39 public class HomeTabModel : TabModel
41 public override MyCommon.TabUsageType TabType
42 => MyCommon.TabUsageType.Home;
44 public int TweetsPerHour => this.tweetsPerHour;
47 private int tweetsPerHour = 0;
48 private ConcurrentDictionary<DateTimeUtc, int> tweetsTimestamps = new ConcurrentDictionary<DateTimeUtc, int>();
50 public HomeTabModel() : this(MyCommon.DEFAULTTAB.RECENT)
54 public HomeTabModel(string tabName) : base(tabName)
58 public override void AddPostQueue(PostClass post)
60 base.AddPostQueue(post);
61 this.UpdateTimelineSpeed(post.CreatedAt);
64 public override async Task RefreshAsync(Twitter tw, bool backward, bool startup, IProgress<string> progress)
67 if (!SettingManager.Common.UnreadManage)
70 read = startup && SettingManager.Common.Read;
72 progress.Report(string.Format(Properties.Resources.GetTimelineWorker_RunWorkerCompletedText5, backward ? -1 : 1));
74 await tw.GetHomeTimelineApi(read, this, backward, startup)
75 .ConfigureAwait(false);
78 if (SettingManager.Common.ReadOldPosts)
79 TabInformations.GetInstance().SetReadHomeTab();
81 TabInformations.GetInstance().DistributePosts();
83 progress.Report(Properties.Resources.GetTimelineWorker_RunWorkerCompletedText1);
87 /// タイムラインに追加された発言件数を反映し、タイムラインの流速を更新します
89 private void UpdateTimelineSpeed(DateTimeUtc postCreatedAt)
91 var now = DateTimeUtc.Now;
94 var oneHour = TimeSpan.FromHours(1);
95 if (now - postCreatedAt > oneHour)
98 this.tweetsTimestamps.AddOrUpdate(postCreatedAt, 1, (k, v) => v + 1);
100 var removeKeys = new List<DateTimeUtc>();
101 var tweetsInWindow = 0;
102 foreach (var (timestamp, count) in this.tweetsTimestamps)
104 if (now - timestamp > oneHour)
105 removeKeys.Add(timestamp);
107 tweetsInWindow += count;
109 Interlocked.Exchange(ref this.tweetsPerHour, tweetsInWindow);
111 foreach (var key in removeKeys)
112 this.tweetsTimestamps.TryRemove(key, out var _);