tweet-body.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import { Component, Input } from "@angular/core";
  2. /**
  3. * Generated class for the TweetBodyComponent component.
  4. *
  5. * See https://angular.io/api/core/Component for more info on Angular
  6. * Components.
  7. */
  8. @Component({
  9. selector: "tweet-body",
  10. templateUrl: "tweet-body.html"
  11. })
  12. export class TweetBodyComponent {
  13. @Input()
  14. data: any[];
  15. constructor() {}
  16. get status(): string {
  17. if (this.data["retweeted_status"]) {
  18. return this.data["retweeted_status"]["full_text"];
  19. } else {
  20. return this.data["full_text"];
  21. }
  22. }
  23. get entities() {
  24. if (this.data["retweeted_status"]) {
  25. return this.data["retweeted_status"]["entities"];
  26. } else {
  27. return this.data["entities"];
  28. }
  29. }
  30. get extended_entities() {
  31. if (this.data["retweeted_status"]) {
  32. return this.data["retweeted_status"]["extended_entities"];
  33. } else {
  34. return this.data["extended_entities"];
  35. }
  36. }
  37. get range() {
  38. if (this.data["retweeted_status"]) {
  39. return this.data["retweeted_status"]["display_text_range"];
  40. } else {
  41. return this.data["display_text_range"];
  42. }
  43. }
  44. get hasPhoto() {
  45. return (
  46. !this.data["private_tweet"] &&
  47. !this.isGif &&
  48. (this.entities["media"] && this.entities["media"][0]["type"] == "photo")
  49. );
  50. }
  51. get isGif() {
  52. return (
  53. !this.data["private_tweet"] &&
  54. this.extended_entities &&
  55. this.extended_entities["media"] &&
  56. this.extended_entities["media"][0]["type"] === "animated_gif"
  57. );
  58. }
  59. get tweetArray() {
  60. let tweetArray = [];
  61. tweetArray = tweetArray.concat(this.getUserMentionsForTweetArray());
  62. tweetArray = tweetArray.concat(this.getHashtagsForTweetArray());
  63. tweetArray = tweetArray.concat(this.getUrlsForTweetArray());
  64. tweetArray = tweetArray.concat(
  65. this.getTextPartsForTweetArray(
  66. tweetArray.sort((a, b) => a["start"] - b["start"])
  67. )
  68. );
  69. tweetArray = this.cutToTextRange(tweetArray);
  70. return tweetArray.sort((a, b) => a["start"] - b["start"]);
  71. }
  72. private getUserMentionsForTweetArray() {
  73. return this.entities["user_mentions"].map(mention => ({
  74. start: mention["indices"][0],
  75. stop: mention["indices"][1],
  76. type: "user_mention",
  77. text: "@" + mention["screen_name"],
  78. userId: mention["id_str"]
  79. }));
  80. }
  81. private getHashtagsForTweetArray() {
  82. return this.entities["hashtags"].map(hashtag => ({
  83. start: hashtag["indices"][0],
  84. stop: hashtag["indices"][1],
  85. type: "hashtag",
  86. text: "#" + hashtag["text"]
  87. }));
  88. }
  89. private getUrlsForTweetArray() {
  90. return this.entities["urls"].map(url => ({
  91. start: url["indices"][0],
  92. stop: url["indices"][1],
  93. type: "url",
  94. text: url["display_url"],
  95. url: url["url"]
  96. }));
  97. }
  98. private getTextPartsForTweetArray(specialParts) {
  99. if (!specialParts.length) {
  100. // text only
  101. return [
  102. {
  103. start: 0,
  104. stop: this.status.length,
  105. type: "text",
  106. text: this.status
  107. }
  108. ];
  109. } else {
  110. let res = [];
  111. let start = 0;
  112. for (let i = 0; i < specialParts.length; i++) {
  113. if (start != specialParts[i].start) {
  114. res.push({
  115. start: start,
  116. stop: specialParts[i]["start"],
  117. type: "text",
  118. text: this.status.substring(start, specialParts[i]["start"])
  119. });
  120. }
  121. start = specialParts[i]["stop"];
  122. }
  123. const stopOfLastSpecialPart =
  124. specialParts[specialParts.length - 1]["stop"];
  125. const textUntilEnd = this.status.substring(
  126. stopOfLastSpecialPart,
  127. this.status.length
  128. );
  129. if (textUntilEnd.length) {
  130. res.push({
  131. start: stopOfLastSpecialPart,
  132. stop: this.status.length,
  133. type: "text",
  134. text: textUntilEnd
  135. });
  136. }
  137. return res;
  138. }
  139. }
  140. private cutToTextRange(tweetArray) {
  141. const res = [];
  142. for (let i = 0; i < tweetArray.length; i++) {
  143. if (
  144. tweetArray[i]["start"] >= this.range[0] &&
  145. tweetArray[i]["stop"] <= this.range[1]
  146. ) {
  147. // Inside the range
  148. res.push(tweetArray[i]);
  149. } else if (
  150. tweetArray[i]["start"] < this.range[0] &&
  151. tweetArray[i]["stop"] <= this.range[1]
  152. ) {
  153. // Beginning needs to be cut
  154. res.push({
  155. start: this.range[0],
  156. stop: tweetArray[i]["stop"],
  157. type: tweetArray[i]["type"],
  158. text: tweetArray[i]["text"].substring(
  159. this.range[0],
  160. tweetArray[i]["stop"]
  161. )
  162. });
  163. } else if (
  164. tweetArray[i]["start"] >= this.range[0] &&
  165. tweetArray[i]["stop"] > this.range[1]
  166. ) {
  167. // End needs to be cut
  168. if (this.range[1] - tweetArray[i]["start"] > 0) {
  169. res.push({
  170. start: tweetArray[i]["start"],
  171. stop: this.range[1],
  172. type: tweetArray[i]["type"],
  173. text: tweetArray[i]["text"].substring(
  174. 0,
  175. this.range[1] - tweetArray[i]["start"]
  176. )
  177. });
  178. }
  179. } else if (
  180. tweetArray[i]["start"] < this.range[0] &&
  181. tweetArray[i]["stop"] > this.range[1]
  182. ) {
  183. // Start and end needs to be cut
  184. res.push({
  185. start: this.range[0],
  186. stop: this.range[1],
  187. type: tweetArray[i]["type"],
  188. text: tweetArray[i]["text"].substring(
  189. this.range[1] - tweetArray[i]["stop"],
  190. tweetArray[i]["stop"] - this.range[1]
  191. )
  192. });
  193. }
  194. }
  195. return res;
  196. }
  197. }