Kaynağa Gözat

Implement retweet and quote tweet

Carsten Porth 5 yıl önce
ebeveyn
işleme
44995300b5

+ 1 - 1
app/src/components/quoted-status/quoted-status.scss

@@ -3,7 +3,7 @@ quoted-status {
     border: 1px solid #ccc;
     border-radius: 3px;
     padding: 5px;
-    margin: 5px 0 0 10px;
+    margin: 5px 0;
   }
   .header {
     display: flex;

+ 1 - 1
app/src/components/tweet-actions/tweet-actions.html

@@ -1,6 +1,6 @@
 <!-- Generated template for the TweetActionsComponent component -->
 <div class="actions-container">
-  <div class="retweets" *ngIf="!data.user.protected">
+  <div class="retweets" *ngIf="!data.user.protected" (click)="retweetStatus(data.id_str)">
     <ion-icon name="ios-git-compare-outline"></ion-icon>
     <span>{{ data.retweet_count | friendlyNumber }}</span>
   </div>

+ 10 - 3
app/src/components/tweet-actions/tweet-actions.ts

@@ -1,5 +1,7 @@
 import { Component, Input, ChangeDetectorRef } from "@angular/core";
 import { TwitterApiProvider } from "../../providers/twitter-api/twitter-api";
+import { NavController } from "ionic-angular";
+import { WriteTweetPage } from "../../pages/write-tweet/write-tweet";
 
 /**
  * Generated class for the TweetActionsComponent component.
@@ -17,10 +19,11 @@ export class TweetActionsComponent {
 
   constructor(
     private twitter: TwitterApiProvider,
-    private ref: ChangeDetectorRef
+    private ref: ChangeDetectorRef,
+    private navCtrl: NavController
   ) {}
 
-  like(id) {
+  like(id: string): void {
     if (!this.data["private_tweet"]) {
       this.twitter.likeTweet(id).then(() => {
         this.data["favorited"] = true;
@@ -30,7 +33,7 @@ export class TweetActionsComponent {
     }
   }
 
-  removeLike(id) {
+  removeLike(id: string): void {
     if (!this.data["private_tweet"]) {
       this.twitter.unlikeTweet(id).then(() => {
         this.data["favorited"] = false;
@@ -39,4 +42,8 @@ export class TweetActionsComponent {
       });
     }
   }
+
+  retweetStatus(id: string): void {
+    this.navCtrl.push(WriteTweetPage, { tweetId: id });
+  }
 }

+ 5 - 1
app/src/pages/write-tweet/write-tweet.html

@@ -14,6 +14,9 @@
 
 
 <ion-content padding>
+  <ion-label color="primary" *ngIf="retweet">Retweet</ion-label>
+  <quoted-status *ngIf="retweet" [data]="retweet.data"></quoted-status>
+
   <form [formGroup]="tweet" (ngSubmit)="submitTweet()">
     <ion-item class="padding-0">
       <ion-label color="primary" stacked>Your tweet</ion-label>
@@ -24,7 +27,8 @@
       <span class="progress">
         <svg width="20" height="20" class="progress-circle">
           <circle class="background-stroke" cx="10" cy="10" r="8"></circle>
-          <circle class="progress-stroke" cx="10" cy="10" r="8" transform="rotate(-90, 10, 10)" [style.strokeDashoffset]="tweetCharProgress"></circle>
+          <circle class="progress-stroke" cx="10" cy="10" r="8" transform="rotate(-90, 10, 10)"
+            [style.strokeDashoffset]="tweetCharProgress"></circle>
         </svg>
 
         <span class="progress-stats">{{ (tweet.value.text).length }}/140</span>

+ 9 - 2
app/src/pages/write-tweet/write-tweet.ts

@@ -25,6 +25,8 @@ import { P2pDatabaseGunProvider } from "../../providers/p2p-database-gun/p2p-dat
 })
 export class WriteTweetPage {
   tweet: FormGroup;
+  retweetId: string;
+  retweet;
 
   constructor(
     public navCtrl: NavController,
@@ -36,13 +38,18 @@ export class WriteTweetPage {
     private ipfs: P2pStorageIpfsProvider,
     private gun: P2pDatabaseGunProvider
   ) {
+    this.retweetId = this.navParams.get("tweetId");
     this.tweet = this.formBuilder.group({
       text: ["", Validators.maxLength(140)],
       p2p: [false]
     });
   }
 
-  ionViewDidLoad() {}
+  async ionViewDidLoad() {
+    if (this.retweetId) {
+      this.retweet = await this.twitter.fetchTweet(this.retweetId);
+    }
+  }
 
   get tweetCharProgress() {
     let progress = 1 - this.tweet.value["text"].length / 140;
@@ -64,7 +71,7 @@ export class WriteTweetPage {
         tweet.created_at
       );
     } else {
-      await this.twitter.tweet(this.tweet.value["text"]);
+      await this.twitter.tweet(this.tweet.value["text"], this.retweet);
     }
 
     loading.dismiss();

+ 30 - 2
app/src/providers/twitter-api/twitter-api.ts

@@ -72,8 +72,29 @@ export class TwitterApiProvider {
     return await this.client.post("blocks/destroy", { user_id: userId });
   }
 
-  public async tweet(status) {
-    return await this.client.post("statuses/update", { status: status });
+  public async tweet(status, retweet?) {
+    if (status.length === 0 && retweet) {
+      // Simple retweet
+      return await this.client.post("statuses/retweet", {
+        id: retweet.data.id_str
+      });
+    } else if (!retweet) {
+      // Simple tweet
+      return await this.client.post("statuses/update", { status: status });
+    } else if (status.length > 0 && retweet) {
+      // Quoted tweet
+      const url =
+        "https://twitter.com/" +
+        retweet.data.user.screen_name +
+        "/status/" +
+        retweet.data.id_str;
+      return await this.client.post("statuses/update", {
+        status: status,
+        attachment_url: url
+      });
+    } else {
+      return;
+    }
   }
 
   public async fetchFriends(userId) {
@@ -102,4 +123,11 @@ export class TwitterApiProvider {
   public async unlikeTweet(tweetId) {
     return await this.client.post("favorites/destroy", { id: tweetId });
   }
+
+  public async fetchTweet(tweetId) {
+    return await this.client.get("statuses/show", {
+      id: tweetId,
+      tweet_mode: "extended"
+    });
+  }
 }