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.Generic;
33 using System.Threading;
34 using System.Threading.Tasks;
36 namespace OpenTween.Models
38 public class PostClass : ICloneable
40 public struct StatusGeo : IEquatable<StatusGeo>
42 public double Longitude { get; }
43 public double Latitude { get; }
45 public StatusGeo(double longitude, double latitude)
47 this.Longitude = longitude;
48 this.Latitude = latitude;
51 public override int GetHashCode()
52 => this.Longitude.GetHashCode() ^ this.Latitude.GetHashCode();
54 public override bool Equals(object obj)
55 => obj is StatusGeo && this.Equals((StatusGeo)obj);
57 public bool Equals(StatusGeo other)
58 => this.Longitude == other.Longitude && this.Latitude == other.Longitude;
60 public static bool operator ==(StatusGeo left, StatusGeo right)
61 => left.Equals(right);
63 public static bool operator !=(StatusGeo left, StatusGeo right)
64 => !left.Equals(right);
66 public string Nickname { get; set; }
67 public string TextFromApi { get; set; }
69 /// <summary>スクリーンリーダーでの読み上げを考慮したテキスト</summary>
70 public string AccessibleText { get; set; }
72 public string ImageUrl { get; set; }
73 public string ScreenName { get; set; }
74 public DateTimeUtc CreatedAt { get; set; }
75 public long StatusId { get; set; }
82 if (this.expandComplatedAll)
85 var expandedHtml = this.ReplaceToExpandedUrl(this._text, out this.expandComplatedAll);
86 if (this.expandComplatedAll)
87 this._text = expandedHtml;
91 set => this._text = value;
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; }
122 /// <see cref="PostClass"/> に含まれる t.co の展開後の URL を保持するクラス
124 public class ExpandedUrlInfo : ICloneable
126 /// <summary>展開前の t.co ドメインの URL</summary>
127 public string Url { get; }
129 /// <summary>展開後の URL</summary>
131 /// <see cref="ShortUrl"/> による展開が完了するまでは Entity に含まれる expanded_url の値を返します
133 public string ExpandedUrl => this._expandedUrl;
135 /// <summary><see cref="ShortUrl"/> による展開を行うタスク</summary>
136 public Task ExpandTask { get; private set; }
138 /// <summary><see cref="DeepExpandAsync"/> による展開が完了したか否か</summary>
139 public bool ExpandedCompleted => this.ExpandTask.IsCompleted;
141 protected string _expandedUrl;
143 public ExpandedUrlInfo(string url, string expandedUrl)
144 : this(url, expandedUrl, deepExpand: true)
148 public ExpandedUrlInfo(string url, string expandedUrl, bool deepExpand)
151 this._expandedUrl = expandedUrl;
154 this.ExpandTask = this.DeepExpandAsync();
156 this.ExpandTask = Task.FromResult(0);
159 protected virtual async Task DeepExpandAsync()
161 var origUrl = this._expandedUrl;
162 var newUrl = await ShortUrl.Instance.ExpandUrlAsync(origUrl)
163 .ConfigureAwait(false);
165 Interlocked.CompareExchange(ref this._expandedUrl, newUrl, origUrl);
168 public ExpandedUrlInfo Clone()
169 => new ExpandedUrlInfo(this.Url, this.ExpandedUrl, deepExpand: false);
171 object ICloneable.Clone()
175 public int FavoritedCount { get; set; }
177 private States _states = States.None;
178 private bool expandComplatedAll = false;
193 Media = new List<MediaInfo>();
194 ReplyToList = new List<Tuple<long, string>>();
195 QuoteStatusIds = new long[0];
196 ExpandedUrls = new ExpandedUrlInfo[0];
199 public string TextSingleLine
200 => this.TextFromApi?.Replace("\n", " ");
206 if (this.RetweetedId != null)
208 var post = this.RetweetSource;
220 if (this.RetweetedId != null)
222 var post = this.RetweetSource;
231 public bool IsProtect
241 _states = _states | States.Protect;
245 _states = _states & ~States.Protect;
260 _states = _states | States.Mark;
264 _states = _states & ~States.Mark;
269 public long? InReplyToStatusId
273 return _InReplyToStatusId;
279 _states = _states | States.Reply;
283 _states = _states & ~States.Reply;
285 _InReplyToStatusId = value;
289 public bool IsDeleted
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;
310 protected virtual PostClass RetweetSource
314 return TabInformations.GetInstance().RetweetSource(this.RetweetedId.Value);
318 public StatusGeo? PostGeo
328 _states |= States.Geo;
332 _states &= ~States.Geo;
338 public int StateIndex
342 return (int)_states - 1;
347 public string SourceHtml
351 if (this.SourceUri == null)
352 return WebUtility.HtmlEncode(this.Source);
354 return string.Format("<a href=\"{0}\" rel=\"nofollow\">{1}</a>",
355 WebUtility.HtmlEncode(this.SourceUri.AbsoluteUri), WebUtility.HtmlEncode(this.Source));
360 /// このツイートが指定したユーザーによって削除可能であるかを判定します
362 /// <param name="selfUserId">ツイートを削除しようとするユーザーのID</param>
363 /// <returns>削除可能であれば true、そうでなければ false</returns>
364 public bool CanDeleteBy(long selfUserId)
366 // 自分が送った DM と自分に届いた DM のどちらも削除可能
370 // 自分のツイート or 他人に RT された自分のツイート
371 if (this.UserId == selfUserId)
375 if (this.RetweetedByUserId == selfUserId)
382 /// このツイートが指定したユーザーによってリツイート可能であるかを判定します
384 /// <param name="selfUserId">リツイートしようとするユーザーのID</param>
385 /// <returns>リツイート可能であれば true、そうでなければ false</returns>
386 public bool CanRetweetBy(long selfUserId)
392 // 自分のツイートであれば鍵垢であるかに関わらずリツイート可
393 if (this.UserId == selfUserId)
396 return !this.IsProtect;
399 public PostClass ConvertToOriginalPost()
401 if (this.RetweetedId == null)
402 throw new InvalidOperationException();
404 var originalPost = this.Clone();
406 originalPost.StatusId = this.RetweetedId.Value;
407 originalPost.RetweetedId = null;
408 originalPost.RetweetedBy = "";
409 originalPost.RetweetedByUserId = null;
410 originalPost.RetweetedCount = 1;
415 public string GetExpandedUrl(string urlStr)
417 var urlInfo = this.ExpandedUrls.FirstOrDefault(x => x.Url == urlStr);
421 return urlInfo.ExpandedUrl;
424 public string[] GetExpandedUrls()
425 => this.ExpandedUrls.Select(x => x.ExpandedUrl).ToArray();
428 /// <paramref name="html"/> に含まれる短縮 URL を展開済みの URL に置換します
430 /// <param name="html">置換する対象の HTML 文字列</param>
431 /// <param name="completedAll">全ての URL の展開が完了していれば true、未完了の URL があれば false</param>
432 private string ReplaceToExpandedUrl(string html, out bool completedAll)
434 if (this.ExpandedUrls.Length == 0)
442 foreach (var urlInfo in this.ExpandedUrls)
444 if (!urlInfo.ExpandedCompleted)
445 completedAll = false;
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)}\"");
456 public PostClass Clone()
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();
467 object ICloneable.Clone()
470 public override bool Equals(object obj)
472 if (obj == null || this.GetType() != obj.GetType()) return false;
473 return this.Equals((PostClass)obj);
476 public bool Equals(PostClass other)
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);
509 public override int GetHashCode()
511 return this.StatusId.GetHashCode();