Browse Source

tweet to ipfs

Carsten Porth 5 years ago
parent
commit
65d0eba219
1 changed files with 29 additions and 12 deletions
  1. 29 12
      app/src/pages/write-tweet/write-tweet.ts

+ 29 - 12
app/src/pages/write-tweet/write-tweet.ts

@@ -3,10 +3,12 @@ import {
   IonicPage,
   NavController,
   NavParams,
-  AlertController
+  LoadingController
 } from "ionic-angular";
 import { FormBuilder, Validators, FormGroup } from "@angular/forms";
 import { TwitterApiProvider } from "../../providers/twitter-api/twitter-api";
+import { HttpClient } from "@angular/common/http";
+import { Storage } from "@ionic/storage";
 
 /**
  * Generated class for the WriteTweetPage page.
@@ -28,7 +30,9 @@ export class WriteTweetPage {
     public navParams: NavParams,
     private formBuilder: FormBuilder,
     private twitter: TwitterApiProvider,
-    private alertCtrl: AlertController
+    private loadingCtrl: LoadingController,
+    private http: HttpClient,
+    private storage: Storage
   ) {
     this.tweet = this.formBuilder.group({
       text: ["", Validators.maxLength(140)],
@@ -45,20 +49,33 @@ export class WriteTweetPage {
     return progress * circumference;
   }
 
-  submitTweet() {
+  async submitTweet() {
+    let loading = this.loadingCtrl.create();
+    loading.present();
+
     if (this.tweet.value.p2p) {
-      this.alertCtrl
-        .create({
-          title: "Private Mode",
-          subTitle:
-            "Your tweet will be encrypted and send to the private network. TODO!",
-          buttons: ["OK"]
-        })
-        .present();
+      await this.postToIpfs(this.tweet.value["text"]);
     } else {
-      this.twitter.tweet(this.tweet.value["text"]);
+      await this.twitter.tweet(this.tweet.value["text"]);
     }
 
+    loading.dismiss();
     this.navCtrl.pop();
   }
+
+  private async postToIpfs(text) {
+    let url = "https://ipfs.infura.io:5001/api/v0/add";
+
+    let data = {
+      text: this.tweet.value["text"].trim(),
+      userId: await this.storage.get("userId"),
+      timestamp: Date.now()
+    };
+
+    let formData = new FormData();
+    formData.append("data", JSON.stringify(data));
+
+    let res = await this.http.post(url, formData).toPromise();
+    console.log(res["Hash"]);
+  }
 }