OSDN Git Service

readonly structを使用する
[opentween/open-tween.git] / OpenTween / Models / PostClass.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.Generic;
30 using System.Linq;
31 using System.Net;
32 using System.Text;
33 using System.Threading;
34 using System.Threading.Tasks;
35
36 namespace OpenTween.Models
37 {
38     public class PostClass : ICloneable
39     {
40         public readonly struct StatusGeo : IEquatable<StatusGeo>
41         {
42             public double Longitude { get; }
43             public double Latitude { get; }
44
45             public StatusGeo(double longitude, double latitude)
46             {
47                 this.Longitude = longitude;
48                 this.Latitude = latitude;
49             }
50
51             public override int GetHashCode()
52                 => this.Longitude.GetHashCode() ^ this.Latitude.GetHashCode();
53
54             public override bool Equals(object obj)
55                 => obj is StatusGeo && this.Equals((StatusGeo)obj);
56
57             public bool Equals(StatusGeo other)
58                 => this.Longitude == other.Longitude && this.Latitude == other.Longitude;
59
60             public static bool operator ==(StatusGeo left, StatusGeo right)
61                 => left.Equals(right);
62
63             public static bool operator !=(StatusGeo left, StatusGeo right)
64                 => !left.Equals(right);
65         }
66         public string Nickname { get; set; }
67         public string TextFromApi { get; set; }
68
69         /// <summary>スクリーンリーダーでの読み上げを考慮したテキスト</summary>
70         public string AccessibleText { get; set; }
71
72         public string ImageUrl { get; set; }
73         public string ScreenName { get; set; }
74         public DateTimeUtc CreatedAt { get; set; }
75         public long StatusId { get; set; }
76         private bool _IsFav;
77
78         public string Text
79         {
80             get
81             {
82                 if (this.expandComplatedAll)
83                     return this._text;
84
85                 var expandedHtml = this.ReplaceToExpandedUrl(this._text, out this.expandComplatedAll);
86                 if (this.expandComplatedAll)
87                     this._text = expandedHtml;
88
89                 return expandedHtml;
90             }
91             set => this._text = value;
92         }
93         private string _text;
94
95         public bool IsRead { get; set; }
96         public bool IsReply { get; set; }
97         public bool IsExcludeReply { get; set; }
98         private bool _IsProtect;
99         public bool IsOwl { get; set; }
100         private bool _IsMark;
101         public string InReplyToUser { get; set; }
102         private long? _InReplyToStatusId;
103         public string Source { get; set; }
104         public Uri SourceUri { get; set; }
105         public List<(long UserId, string ScreenName)> ReplyToList { get; set; }
106         public bool IsMe { get; set; }
107         public bool IsDm { get; set; }
108         public long UserId { get; set; }
109         public bool FilterHit { get; set; }
110         public string RetweetedBy { get; set; }
111         public long? RetweetedId { get; set; }
112         private bool _IsDeleted = false;
113         private StatusGeo? _postGeo = null;
114         public int RetweetedCount { get; set; }
115         public long? RetweetedByUserId { get; set; }
116         public long? InReplyToUserId { get; set; }
117         public List<MediaInfo> Media { get; set; }
118         public long[] QuoteStatusIds { get; set; }
119         public ExpandedUrlInfo[] ExpandedUrls { get; set; }
120
121         /// <summary>
122         /// <see cref="PostClass"/> に含まれる t.co の展開後の URL を保持するクラス
123         /// </summary>
124         public class ExpandedUrlInfo : ICloneable
125         {
126             /// <summary>展開前の t.co ドメインの URL</summary>
127             public string Url { get; }
128
129             /// <summary>展開後の URL</summary>
130             /// <remarks>
131             /// <see cref="ShortUrl"/> による展開が完了するまでは Entity に含まれる expanded_url の値を返します
132             /// </remarks>
133             public string ExpandedUrl => this._expandedUrl;
134
135             /// <summary><see cref="ShortUrl"/> による展開を行うタスク</summary>
136             public Task ExpandTask { get; private set; }
137
138             /// <summary><see cref="DeepExpandAsync"/> による展開が完了したか否か</summary>
139             public bool ExpandedCompleted => this.ExpandTask.IsCompleted;
140
141             protected string _expandedUrl;
142
143             public ExpandedUrlInfo(string url, string expandedUrl)
144                 : this(url, expandedUrl, deepExpand: true)
145             {
146             }
147
148             public ExpandedUrlInfo(string url, string expandedUrl, bool deepExpand)
149             {
150                 this.Url = url;
151                 this._expandedUrl = expandedUrl;
152
153                 if (deepExpand)
154                     this.ExpandTask = this.DeepExpandAsync();
155                 else
156                     this.ExpandTask = Task.CompletedTask;
157             }
158
159             protected virtual async Task DeepExpandAsync()
160             {
161                 var origUrl = this._expandedUrl;
162                 var newUrl = await ShortUrl.Instance.ExpandUrlAsync(origUrl)
163                     .ConfigureAwait(false);
164
165                 Interlocked.CompareExchange(ref this._expandedUrl, newUrl, origUrl);
166             }
167
168             public ExpandedUrlInfo Clone()
169                 => new ExpandedUrlInfo(this.Url, this.ExpandedUrl, deepExpand: false);
170
171             object ICloneable.Clone()
172                 => this.Clone();
173         }
174
175         public int FavoritedCount { get; set; }
176
177         private States _states = States.None;
178         private bool expandComplatedAll = false;
179
180         [Flags]
181         private enum States
182         {
183             None = 0,
184             Protect = 1,
185             Mark = 2,
186             Reply = 4,
187             Geo = 8,
188         }
189
190         public PostClass()
191         {
192             RetweetedBy = "";
193             Media = new List<MediaInfo>();
194             ReplyToList = new List<(long, string)>();
195             QuoteStatusIds = Array.Empty<long>();
196             ExpandedUrls = Array.Empty<ExpandedUrlInfo>();
197         }
198
199         public string TextSingleLine
200             => this.TextFromApi?.Replace("\n", " ");
201
202         public bool IsFav
203         {
204             get
205             {
206                 if (this.RetweetedId != null)
207                 {
208                     var post = this.RetweetSource;
209                     if (post != null)
210                     {
211                         return post.IsFav;
212                     }
213                 }
214
215                 return _IsFav;
216             }
217             set
218             {
219                 _IsFav = value;
220                 if (this.RetweetedId != null)
221                 {
222                     var post = this.RetweetSource;
223                     if (post != null)
224                     {
225                         post.IsFav = value;
226                     }
227                 }
228             }
229         }
230
231         public bool IsProtect
232         {
233             get => this._IsProtect;
234             set
235             {
236                 if (value)
237                 {
238                     _states = _states | States.Protect;
239                 }
240                 else
241                 {
242                     _states = _states & ~States.Protect;
243                 }
244                 _IsProtect = value;
245             }
246         }
247         public bool IsMark
248         {
249             get => this._IsMark;
250             set
251             {
252                 if (value)
253                 {
254                     _states = _states | States.Mark;
255                 }
256                 else
257                 {
258                     _states = _states & ~States.Mark;
259                 }
260                 _IsMark = value;
261             }
262         }
263         public long? InReplyToStatusId
264         {
265             get => this._InReplyToStatusId;
266             set
267             {
268                 if (value != null)
269                 {
270                     _states = _states | States.Reply;
271                 }
272                 else
273                 {
274                     _states = _states & ~States.Reply;
275                 }
276                 _InReplyToStatusId = value;
277             }
278         }
279
280         public bool IsDeleted
281         {
282             get => this._IsDeleted;
283             set
284             {
285                 if (value)
286                 {
287                     this.InReplyToStatusId = null;
288                     this.InReplyToUser = "";
289                     this.InReplyToUserId = null;
290                     this.IsReply = false;
291                     this.ReplyToList = new List<(long, string)>();
292                     this._states = States.None;
293                 }
294                 _IsDeleted = value;
295             }
296         }
297
298         protected virtual PostClass RetweetSource
299             => TabInformations.GetInstance().RetweetSource(this.RetweetedId.Value);
300
301         public StatusGeo? PostGeo
302         {
303             get => this._postGeo;
304             set
305             {
306                 if (value != null)
307                 {
308                     _states |= States.Geo;
309                 }
310                 else
311                 {
312                     _states &= ~States.Geo;
313                 }
314                 _postGeo = value;
315             }
316         }
317
318         public int StateIndex
319             => (int)_states - 1;
320
321         // 互換性のために用意
322         public string SourceHtml
323         {
324             get
325             {
326                 if (this.SourceUri == null)
327                     return WebUtility.HtmlEncode(this.Source);
328
329                 return string.Format("<a href=\"{0}\" rel=\"nofollow\">{1}</a>",
330                     WebUtility.HtmlEncode(this.SourceUri.AbsoluteUri), WebUtility.HtmlEncode(this.Source));
331             }
332         }
333
334         /// <summary>
335         /// このツイートが指定したユーザーによって削除可能であるかを判定します
336         /// </summary>
337         /// <param name="selfUserId">ツイートを削除しようとするユーザーのID</param>
338         /// <returns>削除可能であれば true、そうでなければ false</returns>
339         public bool CanDeleteBy(long selfUserId)
340         {
341             // 自分が送った DM と自分に届いた DM のどちらも削除可能
342             if (this.IsDm)
343                 return true;
344
345             // 自分のツイート or 他人に RT された自分のツイート
346             if (this.UserId == selfUserId)
347                 return true;
348
349             // 自分が RT したツイート
350             if (this.RetweetedByUserId == selfUserId)
351                 return true;
352
353             return false;
354         }
355
356         /// <summary>
357         /// このツイートが指定したユーザーによってリツイート可能であるかを判定します
358         /// </summary>
359         /// <param name="selfUserId">リツイートしようとするユーザーのID</param>
360         /// <returns>リツイート可能であれば true、そうでなければ false</returns>
361         public bool CanRetweetBy(long selfUserId)
362         {
363             // DM は常にリツイート不可
364             if (this.IsDm)
365                 return false;
366
367             // 自分のツイートであれば鍵垢であるかに関わらずリツイート可
368             if (this.UserId == selfUserId)
369                 return true;
370
371             return !this.IsProtect;
372         }
373
374         public PostClass ConvertToOriginalPost()
375         {
376             if (this.RetweetedId == null)
377                 throw new InvalidOperationException();
378
379             var originalPost = this.Clone();
380
381             originalPost.StatusId = this.RetweetedId.Value;
382             originalPost.RetweetedId = null;
383             originalPost.RetweetedBy = "";
384             originalPost.RetweetedByUserId = null;
385             originalPost.RetweetedCount = 1;
386
387             return originalPost;
388         }
389
390         public string GetExpandedUrl(string urlStr)
391         {
392             var urlInfo = this.ExpandedUrls.FirstOrDefault(x => x.Url == urlStr);
393             if (urlInfo == null)
394                 return urlStr;
395
396             return urlInfo.ExpandedUrl;
397         }
398
399         public string[] GetExpandedUrls()
400             => this.ExpandedUrls.Select(x => x.ExpandedUrl).ToArray();
401
402         /// <summary>
403         /// <paramref name="html"/> に含まれる短縮 URL を展開済みの URL に置換します
404         /// </summary>
405         /// <param name="html">置換する対象の HTML 文字列</param>
406         /// <param name="completedAll">全ての URL の展開が完了していれば true、未完了の URL があれば false</param>
407         private string ReplaceToExpandedUrl(string html, out bool completedAll)
408         {
409             if (this.ExpandedUrls.Length == 0)
410             {
411                 completedAll = true;
412                 return html;
413             }
414
415             completedAll = true;
416
417             foreach (var urlInfo in this.ExpandedUrls)
418             {
419                 if (!urlInfo.ExpandedCompleted)
420                     completedAll = false;
421
422                 var tcoUrl = urlInfo.Url;
423                 var expandedUrl = MyCommon.ConvertToReadableUrl(urlInfo.ExpandedUrl);
424                 html = html.Replace($"title=\"{WebUtility.HtmlEncode(tcoUrl)}\"",
425                     $"title=\"{WebUtility.HtmlEncode(expandedUrl)}\"");
426             }
427
428             return html;
429         }
430
431         public PostClass Clone()
432         {
433             var clone = (PostClass)this.MemberwiseClone();
434             clone.ReplyToList = new List<(long, string)>(this.ReplyToList);
435             clone.Media = new List<MediaInfo>(this.Media);
436             clone.QuoteStatusIds = this.QuoteStatusIds.ToArray();
437             clone.ExpandedUrls = this.ExpandedUrls.Select(x => x.Clone()).ToArray();
438
439             return clone;
440         }
441
442         object ICloneable.Clone()
443             => this.Clone();
444
445         public override bool Equals(object obj)
446         {
447             if (obj == null || this.GetType() != obj.GetType()) return false;
448             return this.Equals((PostClass)obj);
449         }
450
451         public bool Equals(PostClass other)
452         {
453             if (other == null) return false;
454             return (this.Nickname == other.Nickname) &&
455                     (this.TextFromApi == other.TextFromApi) &&
456                     (this.ImageUrl == other.ImageUrl) &&
457                     (this.ScreenName == other.ScreenName) &&
458                     (this.CreatedAt == other.CreatedAt) &&
459                     (this.StatusId == other.StatusId) &&
460                     (this.IsFav == other.IsFav) &&
461                     (this.Text == other.Text) &&
462                     (this.IsRead == other.IsRead) &&
463                     (this.IsReply == other.IsReply) &&
464                     (this.IsExcludeReply == other.IsExcludeReply) &&
465                     (this.IsProtect == other.IsProtect) &&
466                     (this.IsOwl == other.IsOwl) &&
467                     (this.IsMark == other.IsMark) &&
468                     (this.InReplyToUser == other.InReplyToUser) &&
469                     (this.InReplyToStatusId == other.InReplyToStatusId) &&
470                     (this.Source == other.Source) &&
471                     (this.SourceUri == other.SourceUri) &&
472                     (this.ReplyToList.SequenceEqual(other.ReplyToList)) &&
473                     (this.IsMe == other.IsMe) &&
474                     (this.IsDm == other.IsDm) &&
475                     (this.UserId == other.UserId) &&
476                     (this.FilterHit == other.FilterHit) &&
477                     (this.RetweetedBy == other.RetweetedBy) &&
478                     (this.RetweetedId == other.RetweetedId) &&
479                     (this.IsDeleted == other.IsDeleted) &&
480                     (this.InReplyToUserId == other.InReplyToUserId);
481
482         }
483
484         public override int GetHashCode()
485             => this.StatusId.GetHashCode();
486     }
487 }