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 DateTime 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<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<string>();
195 QuoteStatusIds = new long[0];
196 ExpandedUrls = new ExpandedUrlInfo[0];
199 public string TextSingleLine
203 return this.TextFromApi == null ? null : this.TextFromApi.Replace("\n", " ");
211 if (this.RetweetedId != null)
213 var post = this.RetweetSource;
225 if (this.RetweetedId != null)
227 var post = this.RetweetSource;
236 public bool IsProtect
246 _states = _states | States.Protect;
250 _states = _states & ~States.Protect;
265 _states = _states | States.Mark;
269 _states = _states & ~States.Mark;
274 public long? InReplyToStatusId
278 return _InReplyToStatusId;
284 _states = _states | States.Reply;
288 _states = _states & ~States.Reply;
290 _InReplyToStatusId = value;
294 public bool IsDeleted
304 this.InReplyToStatusId = null;
305 this.InReplyToUser = "";
306 this.InReplyToUserId = null;
307 this.IsReply = false;
308 this.ReplyToList = new List<string>();
309 this._states = States.None;
315 protected virtual PostClass RetweetSource
319 return TabInformations.GetInstance().RetweetSource(this.RetweetedId.Value);
323 public StatusGeo? PostGeo
333 _states |= States.Geo;
337 _states &= ~States.Geo;
343 public int StateIndex
347 return (int)_states - 1;
352 public string SourceHtml
356 if (this.SourceUri == null)
357 return WebUtility.HtmlEncode(this.Source);
359 return string.Format("<a href=\"{0}\" rel=\"nofollow\">{1}</a>",
360 WebUtility.HtmlEncode(this.SourceUri.AbsoluteUri), WebUtility.HtmlEncode(this.Source));
365 /// このツイートが指定したユーザーによって削除可能であるかを判定します
367 /// <param name="selfUserId">ツイートを削除しようとするユーザーのID</param>
368 /// <returns>削除可能であれば true、そうでなければ false</returns>
369 public bool CanDeleteBy(long selfUserId)
371 // 自分が送った DM と自分に届いた DM のどちらも削除可能
375 // 自分のツイート or 他人に RT された自分のツイート
376 if (this.UserId == selfUserId)
380 if (this.RetweetedByUserId == selfUserId)
387 /// このツイートが指定したユーザーによってリツイート可能であるかを判定します
389 /// <param name="selfUserId">リツイートしようとするユーザーのID</param>
390 /// <returns>リツイート可能であれば true、そうでなければ false</returns>
391 public bool CanRetweetBy(long selfUserId)
397 // 自分のツイートであれば鍵垢であるかに関わらずリツイート可
398 if (this.UserId == selfUserId)
401 return !this.IsProtect;
404 public PostClass ConvertToOriginalPost()
406 if (this.RetweetedId == null)
407 throw new InvalidOperationException();
409 var originalPost = this.Clone();
411 originalPost.StatusId = this.RetweetedId.Value;
412 originalPost.RetweetedId = null;
413 originalPost.RetweetedBy = "";
414 originalPost.RetweetedByUserId = null;
415 originalPost.RetweetedCount = 1;
420 public string GetExpandedUrl(string urlStr)
422 var urlInfo = this.ExpandedUrls.FirstOrDefault(x => x.Url == urlStr);
426 return urlInfo.ExpandedUrl;
429 public string[] GetExpandedUrls()
430 => this.ExpandedUrls.Select(x => x.ExpandedUrl).ToArray();
433 /// <paramref name="html"/> に含まれる短縮 URL を展開済みの URL に置換します
435 /// <param name="html">置換する対象の HTML 文字列</param>
436 /// <param name="completedAll">全ての URL の展開が完了していれば true、未完了の URL があれば false</param>
437 private string ReplaceToExpandedUrl(string html, out bool completedAll)
439 if (this.ExpandedUrls.Length == 0)
447 foreach (var urlInfo in this.ExpandedUrls)
449 if (!urlInfo.ExpandedCompleted)
450 completedAll = false;
452 var tcoUrl = urlInfo.Url;
453 var expandedUrl = MyCommon.ConvertToReadableUrl(urlInfo.ExpandedUrl);
454 html = html.Replace($"title=\"{WebUtility.HtmlEncode(tcoUrl)}\"",
455 $"title=\"{WebUtility.HtmlEncode(expandedUrl)}\"");
461 public PostClass Clone()
463 var clone = (PostClass)this.MemberwiseClone();
464 clone.ReplyToList = new List<string>(this.ReplyToList);
465 clone.Media = new List<MediaInfo>(this.Media);
466 clone.QuoteStatusIds = this.QuoteStatusIds.ToArray();
467 clone.ExpandedUrls = this.ExpandedUrls.Select(x => x.Clone()).ToArray();
472 object ICloneable.Clone()
475 public override bool Equals(object obj)
477 if (obj == null || this.GetType() != obj.GetType()) return false;
478 return this.Equals((PostClass)obj);
481 public bool Equals(PostClass other)
483 if (other == null) return false;
484 return (this.Nickname == other.Nickname) &&
485 (this.TextFromApi == other.TextFromApi) &&
486 (this.ImageUrl == other.ImageUrl) &&
487 (this.ScreenName == other.ScreenName) &&
488 (this.CreatedAt == other.CreatedAt) &&
489 (this.StatusId == other.StatusId) &&
490 (this.IsFav == other.IsFav) &&
491 (this.Text == other.Text) &&
492 (this.IsRead == other.IsRead) &&
493 (this.IsReply == other.IsReply) &&
494 (this.IsExcludeReply == other.IsExcludeReply) &&
495 (this.IsProtect == other.IsProtect) &&
496 (this.IsOwl == other.IsOwl) &&
497 (this.IsMark == other.IsMark) &&
498 (this.InReplyToUser == other.InReplyToUser) &&
499 (this.InReplyToStatusId == other.InReplyToStatusId) &&
500 (this.Source == other.Source) &&
501 (this.SourceUri == other.SourceUri) &&
502 (this.ReplyToList.SequenceEqual(other.ReplyToList)) &&
503 (this.IsMe == other.IsMe) &&
504 (this.IsDm == other.IsDm) &&
505 (this.UserId == other.UserId) &&
506 (this.FilterHit == other.FilterHit) &&
507 (this.RetweetedBy == other.RetweetedBy) &&
508 (this.RetweetedId == other.RetweetedId) &&
509 (this.IsDeleted == other.IsDeleted) &&
510 (this.InReplyToUserId == other.InReplyToUserId);
514 public override int GetHashCode()
516 return this.StatusId.GetHashCode();