123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- import { Component, Input } from "@angular/core";
- /**
- * Generated class for the TweetBodyComponent component.
- *
- * See https://angular.io/api/core/Component for more info on Angular
- * Components.
- */
- @Component({
- selector: "tweet-body",
- templateUrl: "tweet-body.html"
- })
- export class TweetBodyComponent {
- @Input()
- data: any[];
- constructor() {}
- get status(): string {
- if (this.data["retweeted_status"]) {
- return this.data["retweeted_status"]["full_text"];
- } else {
- return this.data["full_text"];
- }
- }
- get entities() {
- if (this.data["retweeted_status"]) {
- return this.data["retweeted_status"]["entities"];
- } else {
- return this.data["entities"];
- }
- }
- get extended_entities() {
- if (this.data["retweeted_status"]) {
- return this.data["retweeted_status"]["extended_entities"];
- } else {
- return this.data["extended_entities"];
- }
- }
- get range() {
- if (this.data["retweeted_status"]) {
- return this.data["retweeted_status"]["display_text_range"];
- } else {
- return this.data["display_text_range"];
- }
- }
- get hasPhoto() {
- return (
- !this.data["private_tweet"] &&
- !this.isGif &&
- (this.entities["media"] && this.entities["media"][0]["type"] == "photo")
- );
- }
- get isGif() {
- return (
- !this.data["private_tweet"] &&
- this.extended_entities &&
- this.extended_entities["media"] &&
- this.extended_entities["media"][0]["type"] === "animated_gif"
- );
- }
- get tweetArray() {
- let tweetArray = [];
- tweetArray = tweetArray.concat(this.getUserMentionsForTweetArray());
- tweetArray = tweetArray.concat(this.getHashtagsForTweetArray());
- tweetArray = tweetArray.concat(this.getUrlsForTweetArray());
- tweetArray = tweetArray.concat(
- this.getTextPartsForTweetArray(
- tweetArray.sort((a, b) => a["start"] - b["start"])
- )
- );
- tweetArray = this.cutToTextRange(tweetArray);
- return tweetArray.sort((a, b) => a["start"] - b["start"]);
- }
- private getUserMentionsForTweetArray() {
- return this.entities["user_mentions"].map(mention => ({
- start: mention["indices"][0],
- stop: mention["indices"][1],
- type: "user_mention",
- text: "@" + mention["screen_name"],
- userId: mention["id_str"]
- }));
- }
- private getHashtagsForTweetArray() {
- return this.entities["hashtags"].map(hashtag => ({
- start: hashtag["indices"][0],
- stop: hashtag["indices"][1],
- type: "hashtag",
- text: "#" + hashtag["text"]
- }));
- }
- private getUrlsForTweetArray() {
- return this.entities["urls"].map(url => ({
- start: url["indices"][0],
- stop: url["indices"][1],
- type: "url",
- text: url["display_url"],
- url: url["url"]
- }));
- }
- private getTextPartsForTweetArray(specialParts) {
- if (!specialParts.length) {
- // text only
- return [
- {
- start: 0,
- stop: this.status.length,
- type: "text",
- text: this.status
- }
- ];
- } else {
- let res = [];
- let start = 0;
- for (let i = 0; i < specialParts.length; i++) {
- if (start != specialParts[i].start) {
- res.push({
- start: start,
- stop: specialParts[i]["start"],
- type: "text",
- text: this.status.substring(start, specialParts[i]["start"])
- });
- }
- start = specialParts[i]["stop"];
- }
- const stopOfLastSpecialPart =
- specialParts[specialParts.length - 1]["stop"];
- const textUntilEnd = this.status.substring(
- stopOfLastSpecialPart,
- this.status.length
- );
- if (textUntilEnd.length) {
- res.push({
- start: stopOfLastSpecialPart,
- stop: this.status.length,
- type: "text",
- text: textUntilEnd
- });
- }
- return res;
- }
- }
- private cutToTextRange(tweetArray) {
- const res = [];
- for (let i = 0; i < tweetArray.length; i++) {
- if (
- tweetArray[i]["start"] >= this.range[0] &&
- tweetArray[i]["stop"] <= this.range[1]
- ) {
- // Inside the range
- res.push(tweetArray[i]);
- } else if (
- tweetArray[i]["start"] < this.range[0] &&
- tweetArray[i]["stop"] <= this.range[1]
- ) {
- // Beginning needs to be cut
- res.push({
- start: this.range[0],
- stop: tweetArray[i]["stop"],
- type: tweetArray[i]["type"],
- text: tweetArray[i]["text"].substring(
- this.range[0],
- tweetArray[i]["stop"]
- )
- });
- } else if (
- tweetArray[i]["start"] >= this.range[0] &&
- tweetArray[i]["stop"] > this.range[1]
- ) {
- // End needs to be cut
- if (this.range[1] - tweetArray[i]["start"] > 0) {
- res.push({
- start: tweetArray[i]["start"],
- stop: this.range[1],
- type: tweetArray[i]["type"],
- text: tweetArray[i]["text"].substring(
- 0,
- this.range[1] - tweetArray[i]["start"]
- )
- });
- }
- } else if (
- tweetArray[i]["start"] < this.range[0] &&
- tweetArray[i]["stop"] > this.range[1]
- ) {
- // Start and end needs to be cut
- res.push({
- start: this.range[0],
- stop: this.range[1],
- type: tweetArray[i]["type"],
- text: tweetArray[i]["text"].substring(
- this.range[1] - tweetArray[i]["stop"],
- tweetArray[i]["stop"] - this.range[1]
- )
- });
- }
- }
- return res;
- }
- }
|