فهرست منبع

Refactor: put ipfs+infura in own provider

Carsten Porth 5 سال پیش
والد
کامیت
284e5c797d

+ 3 - 1
app/src/app/app.module.ts

@@ -24,6 +24,7 @@ import { ProfileHeaderComponent } from '../components/profile-header/profile-hea
 import { PipesModule } from '../pipes/pipes.module';
 import { WriteTweetPage } from '../pages/write-tweet/write-tweet';
 import { QuotedStatusComponent } from '../components/quoted-status/quoted-status';
+import { P2pStorageIpfsProvider } from '../providers/p2p-storage-ipfs/p2p-storage-ipfs';
 
 @NgModule({
   declarations: [
@@ -65,7 +66,8 @@ import { QuotedStatusComponent } from '../components/quoted-status/quoted-status
     HttpClient,
     { provide: ErrorHandler, useClass: IonicErrorHandler },
     AuthProvider,
-    TwitterApiProvider
+    TwitterApiProvider,
+    P2pStorageIpfsProvider
   ]
 })
 export class AppModule { }

+ 3 - 8
app/src/pages/profile/profile.ts

@@ -6,7 +6,7 @@ import {
   InfiniteScroll
 } from "ionic-angular";
 import { TwitterApiProvider } from "../../providers/twitter-api/twitter-api";
-import { HttpClient } from "@angular/common/http";
+import { P2pStorageIpfsProvider } from "../../providers/p2p-storage-ipfs/p2p-storage-ipfs";
 import Gun from "gun";
 
 /**
@@ -32,7 +32,7 @@ export class ProfilePage {
     public navCtrl: NavController,
     public navParams: NavParams,
     private twitter: TwitterApiProvider,
-    private http: HttpClient
+    private ipfs: P2pStorageIpfsProvider
   ) {
     this.gun = Gun();
   }
@@ -74,12 +74,7 @@ export class ProfilePage {
       .get("username")
       .get("lastTweet")
       .once(lastTweet => (lastTweetHash = lastTweet));
-    const res = await this.http
-      .get("https://ipfs.infura.io:5001/api/v0/cat", {
-        params: { arg: lastTweetHash }
-      })
-      .toPromise();
-    // const tweet = JSON.parse(res);
+    const res = await this.ipfs.fetchTweet(lastTweetHash);
     console.log(res);
   }
 }

+ 8 - 14
app/src/pages/write-tweet/write-tweet.ts

@@ -10,6 +10,7 @@ import { TwitterApiProvider } from "../../providers/twitter-api/twitter-api";
 import { HttpClient } from "@angular/common/http";
 import { Storage } from "@ionic/storage";
 import Gun from "gun";
+import { P2pStorageIpfsProvider } from "../../providers/p2p-storage-ipfs/p2p-storage-ipfs";
 
 /**
  * Generated class for the WriteTweetPage page.
@@ -34,7 +35,8 @@ export class WriteTweetPage {
     private twitter: TwitterApiProvider,
     private loadingCtrl: LoadingController,
     private http: HttpClient,
-    private storage: Storage
+    private storage: Storage,
+    private ipfs: P2pStorageIpfsProvider
   ) {
     this.gun = Gun();
     this.tweet = this.formBuilder.group({
@@ -57,8 +59,9 @@ export class WriteTweetPage {
     loading.present();
 
     if (this.tweet.value.p2p) {
-      let hash = await this.postToIpfs(this.tweet.value["text"]);
-      this.gun.get("username").put({ lastTweet: hash });
+      const tweet = await this.buildPrivateTweet();
+      const res = await this.ipfs.storeTweet(tweet);
+      this.gun.get("username").put({ lastTweet: res["Hash"] });
     } else {
       await this.twitter.tweet(this.tweet.value["text"]);
     }
@@ -67,20 +70,11 @@ export class WriteTweetPage {
     this.navCtrl.pop();
   }
 
-  private async postToIpfs(text) {
-    let url = "https://ipfs.infura.io:5001/api/v0/add";
-
-    let data = {
+  private async buildPrivateTweet() {
+    return {
       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"]);
-    return res["Hash"];
   }
 }

+ 29 - 0
app/src/providers/p2p-storage-ipfs/p2p-storage-ipfs.ts

@@ -0,0 +1,29 @@
+import { HttpClient } from "@angular/common/http";
+import { Injectable } from "@angular/core";
+
+/*
+  Generated class for the P2pStorageIpfsProvider provider.
+
+  See https://angular.io/guide/dependency-injection for more info on providers
+  and Angular DI.
+*/
+@Injectable()
+export class P2pStorageIpfsProvider {
+  private infuraUrl = "https://ipfs.infura.io:5001/api/v0/";
+
+  constructor(public http: HttpClient) {}
+
+  public storeTweet(tweet) {
+    const formData = new FormData();
+    formData.append("data", JSON.stringify(tweet));
+
+    return this.http.post(this.infuraUrl + "add", formData).toPromise();
+  }
+
+  public fetchTweet(hash) {
+    const options = {
+      params: { arg: hash }
+    };
+    return this.http.get(this.infuraUrl + "cat", options).toPromise();
+  }
+}