OSDN Git Service

9d1f6fa0ad73437967ffaa59c474edc9d0fa71ba
[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 #nullable enable
29
30 using System;
31 using System.Collections.Concurrent;
32 using System.Collections.Generic;
33 using System.Linq;
34 using System.Text;
35 using System.Threading;
36 using System.Threading.Tasks;
37 using OpenTween.Setting;
38
39 namespace OpenTween.Models
40 {
41     public class HomeTabModel : TabModel
42     {
43         public override MyCommon.TabUsageType TabType
44             => MyCommon.TabUsageType.Home;
45
46         public PostId? OldestId { get; set; }
47
48         public int TweetsPerHour => this.tweetsPerHour;
49
50         // 流速計測用
51         private int tweetsPerHour = 0;
52         private readonly ConcurrentDictionary<DateTimeUtc, int> tweetsTimestamps = new();
53
54         public HomeTabModel()
55             : this(MyCommon.DEFAULTTAB.RECENT)
56         {
57         }
58
59         public HomeTabModel(string tabName)
60             : base(tabName)
61         {
62         }
63
64         public override void AddPostQueue(PostClass post)
65         {
66             base.AddPostQueue(post);
67             this.UpdateTimelineSpeed(post.CreatedAt);
68         }
69
70         public override async Task RefreshAsync(Twitter tw, bool backward, bool startup, IProgress<string> progress)
71         {
72             bool read;
73             if (!SettingManager.Instance.Common.UnreadManage)
74                 read = true;
75             else
76                 read = startup && SettingManager.Instance.Common.Read;
77
78             progress.Report(string.Format(Properties.Resources.GetTimelineWorker_RunWorkerCompletedText5, backward ? -1 : 1));
79
80             await tw.GetHomeTimelineApi(read, this, backward, startup)
81                 .ConfigureAwait(false);
82
83             // 新着時未読クリア
84             if (SettingManager.Instance.Common.ReadOldPosts)
85                 TabInformations.GetInstance().SetReadHomeTab();
86
87             TabInformations.GetInstance().DistributePosts();
88
89             progress.Report(Properties.Resources.GetTimelineWorker_RunWorkerCompletedText1);
90         }
91
92         /// <summary>
93         /// タイムラインに追加された発言件数を反映し、タイムラインの流速を更新します
94         /// </summary>
95         private void UpdateTimelineSpeed(DateTimeUtc postCreatedAt)
96         {
97             var now = DateTimeUtc.Now;
98
99             // 1 時間以上前の時刻は追加しない
100             var oneHour = TimeSpan.FromHours(1);
101             if (now - postCreatedAt > oneHour)
102                 return;
103
104             this.tweetsTimestamps.AddOrUpdate(postCreatedAt, 1, (k, v) => v + 1);
105
106             var removeKeys = new List<DateTimeUtc>();
107             var tweetsInWindow = 0;
108             foreach (var (timestamp, count) in this.tweetsTimestamps)
109             {
110                 if (now - timestamp > oneHour)
111                     removeKeys.Add(timestamp);
112                 else
113                     tweetsInWindow += count;
114             }
115             Interlocked.Exchange(ref this.tweetsPerHour, tweetsInWindow);
116
117             foreach (var key in removeKeys)
118                 this.tweetsTimestamps.TryRemove(key, out _);
119         }
120     }
121 }