OSDN Git Service

null値の比較を簡略化する (IDE0031, IDE0041)
[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 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<Tuple<long, string>> 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.FromResult(0);
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<Tuple<long, string>>();
195             QuoteStatusIds = new long[0];
196             ExpandedUrls = new ExpandedUrlInfo[0];
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
234             {
235                 return _IsProtect;
236             }
237             set
238             {
239                 if (value)
240                 {
241                     _states = _states | States.Protect;
242                 }
243                 else
244                 {
245                     _states = _states & ~States.Protect;
246                 }
247                 _IsProtect = value;
248             }
249         }
250         public bool IsMark
251         {
252             get
253             {
254                 return _IsMark;
255             }
256             set
257             {
258                 if (value)
259                 {
260                     _states = _states | States.Mark;
261                 }
262                 else
263                 {
264                     _states = _states & ~States.Mark;
265                 }
266                 _IsMark = value;
267             }
268         }
269         public long? InReplyToStatusId
270         {
271             get
272             {
273                 return _InReplyToStatusId;
274             }
275             set
276             {
277                 if (value != null)
278                 {
279                     _states = _states | States.Reply;
280                 }
281                 else
282                 {
283                     _states = _states & ~States.Reply;
284                 }
285                 _InReplyToStatusId = value;
286             }
287         }
288
289         public bool IsDeleted
290         {
291             get
292             {
293                 return _IsDeleted;
294             }
295             set
296             {
297                 if (value)
298                 {
299                     this.InReplyToStatusId = null;
300                     this.InReplyToUser = "";
301                     this.InReplyToUserId = null;
302                     this.IsReply = false;
303                     this.ReplyToList = new List<Tuple<long, string>>();
304                     this._states = States.None;
305                 }
306                 _IsDeleted = value;
307             }
308         }
309
310         protected virtual PostClass RetweetSource
311         {
312             get
313             {
314                 return TabInformations.GetInstance().RetweetSource(this.RetweetedId.Value);
315             }
316         }
317
318         public StatusGeo? PostGeo
319         {
320             get
321             {
322                 return _postGeo;
323             }
324             set
325             {
326                 if (value != null)
327                 {
328                     _states |= States.Geo;
329                 }
330                 else
331                 {
332                     _states &= ~States.Geo;
333                 }
334                 _postGeo = value;
335             }
336         }
337
338         public int StateIndex
339         {
340             get
341             {
342                 return (int)_states - 1;
343             }
344         }
345
346         // 互換性のために用意
347         public string SourceHtml
348         {
349             get
350             {
351                 if (this.SourceUri == null)
352                     return WebUtility.HtmlEncode(this.Source);
353
354                 return string.Format("<a href=\"{0}\" rel=\"nofollow\">{1}</a>",
355                     WebUtility.HtmlEncode(this.SourceUri.AbsoluteUri), WebUtility.HtmlEncode(this.Source));
356             }
357         }
358
359         /// <summary>
360         /// このツイートが指定したユーザーによって削除可能であるかを判定します
361         /// </summary>
362         /// <param name="selfUserId">ツイートを削除しようとするユーザーのID</param>
363         /// <returns>削除可能であれば true、そうでなければ false</returns>
364         public bool CanDeleteBy(long selfUserId)
365         {
366             // 自分が送った DM と自分に届いた DM のどちらも削除可能
367             if (this.IsDm)
368                 return true;
369
370             // 自分のツイート or 他人に RT された自分のツイート
371             if (this.UserId == selfUserId)
372                 return true;
373
374             // 自分が RT したツイート
375             if (this.RetweetedByUserId == selfUserId)
376                 return true;
377
378             return false;
379         }
380
381         /// <summary>
382         /// このツイートが指定したユーザーによってリツイート可能であるかを判定します
383         /// </summary>
384         /// <param name="selfUserId">リツイートしようとするユーザーのID</param>
385         /// <returns>リツイート可能であれば true、そうでなければ false</returns>
386         public bool CanRetweetBy(long selfUserId)
387         {
388             // DM は常にリツイート不可
389             if (this.IsDm)
390                 return false;
391
392             // 自分のツイートであれば鍵垢であるかに関わらずリツイート可
393             if (this.UserId == selfUserId)
394                 return true;
395
396             return !this.IsProtect;
397         }
398
399         public PostClass ConvertToOriginalPost()
400         {
401             if (this.RetweetedId == null)
402                 throw new InvalidOperationException();
403
404             var originalPost = this.Clone();
405
406             originalPost.StatusId = this.RetweetedId.Value;
407             originalPost.RetweetedId = null;
408             originalPost.RetweetedBy = "";
409             originalPost.RetweetedByUserId = null;
410             originalPost.RetweetedCount = 1;
411
412             return originalPost;
413         }
414
415         public string GetExpandedUrl(string urlStr)
416         {
417             var urlInfo = this.ExpandedUrls.FirstOrDefault(x => x.Url == urlStr);
418             if (urlInfo == null)
419                 return urlStr;
420
421             return urlInfo.ExpandedUrl;
422         }
423
424         public string[] GetExpandedUrls()
425             => this.ExpandedUrls.Select(x => x.ExpandedUrl).ToArray();
426
427         /// <summary>
428         /// <paramref name="html"/> に含まれる短縮 URL を展開済みの URL に置換します
429         /// </summary>
430         /// <param name="html">置換する対象の HTML 文字列</param>
431         /// <param name="completedAll">全ての URL の展開が完了していれば true、未完了の URL があれば false</param>
432         private string ReplaceToExpandedUrl(string html, out bool completedAll)
433         {
434             if (this.ExpandedUrls.Length == 0)
435             {
436                 completedAll = true;
437                 return html;
438             }
439
440             completedAll = true;
441
442             foreach (var urlInfo in this.ExpandedUrls)
443             {
444                 if (!urlInfo.ExpandedCompleted)
445                     completedAll = false;
446
447                 var tcoUrl = urlInfo.Url;
448                 var expandedUrl = MyCommon.ConvertToReadableUrl(urlInfo.ExpandedUrl);
449                 html = html.Replace($"title=\"{WebUtility.HtmlEncode(tcoUrl)}\"",
450                     $"title=\"{WebUtility.HtmlEncode(expandedUrl)}\"");
451             }
452
453             return html;
454         }
455
456         public PostClass Clone()
457         {
458             var clone = (PostClass)this.MemberwiseClone();
459             clone.ReplyToList = new List<Tuple<long, string>>(this.ReplyToList);
460             clone.Media = new List<MediaInfo>(this.Media);
461             clone.QuoteStatusIds = this.QuoteStatusIds.ToArray();
462             clone.ExpandedUrls = this.ExpandedUrls.Select(x => x.Clone()).ToArray();
463
464             return clone;
465         }
466
467         object ICloneable.Clone()
468             => this.Clone();
469
470         public override bool Equals(object obj)
471         {
472             if (obj == null || this.GetType() != obj.GetType()) return false;
473             return this.Equals((PostClass)obj);
474         }
475
476         public bool Equals(PostClass other)
477         {
478             if (other == null) return false;
479             return (this.Nickname == other.Nickname) &&
480                     (this.TextFromApi == other.TextFromApi) &&
481                     (this.ImageUrl == other.ImageUrl) &&
482                     (this.ScreenName == other.ScreenName) &&
483                     (this.CreatedAt == other.CreatedAt) &&
484                     (this.StatusId == other.StatusId) &&
485                     (this.IsFav == other.IsFav) &&
486                     (this.Text == other.Text) &&
487                     (this.IsRead == other.IsRead) &&
488                     (this.IsReply == other.IsReply) &&
489                     (this.IsExcludeReply == other.IsExcludeReply) &&
490                     (this.IsProtect == other.IsProtect) &&
491                     (this.IsOwl == other.IsOwl) &&
492                     (this.IsMark == other.IsMark) &&
493                     (this.InReplyToUser == other.InReplyToUser) &&
494                     (this.InReplyToStatusId == other.InReplyToStatusId) &&
495                     (this.Source == other.Source) &&
496                     (this.SourceUri == other.SourceUri) &&
497                     (this.ReplyToList.SequenceEqual(other.ReplyToList)) &&
498                     (this.IsMe == other.IsMe) &&
499                     (this.IsDm == other.IsDm) &&
500                     (this.UserId == other.UserId) &&
501                     (this.FilterHit == other.FilterHit) &&
502                     (this.RetweetedBy == other.RetweetedBy) &&
503                     (this.RetweetedId == other.RetweetedId) &&
504                     (this.IsDeleted == other.IsDeleted) &&
505                     (this.InReplyToUserId == other.InReplyToUserId);
506
507         }
508
509         public override int GetHashCode()
510         {
511             return this.StatusId.GetHashCode();
512         }
513     }
514 }