Browse Source

include gun. write hash to gun. load hash from gun in user profile

Carsten Porth 5 years ago
parent
commit
98975bd8af
4 changed files with 257 additions and 227 deletions
  1. 225 224
      app/package-lock.json
  2. 1 0
      app/package.json
  3. 25 2
      app/src/pages/profile/profile.ts
  4. 6 1
      app/src/pages/write-tweet/write-tweet.ts

File diff suppressed because it is too large
+ 225 - 224
app/package-lock.json


+ 1 - 0
app/package.json

@@ -41,6 +41,7 @@
     "cordova-support-google-services": "^1.2.0",
     "cordova-universal-links-plugin": "git+https://github.com/walteram/cordova-universal-links-plugin.git",
     "firebase": "^5.2.0",
+    "gun": "^0.9.99996",
     "ionic-angular": "3.9.2",
     "ionicons": "3.0.0",
     "javascript-time-ago": "^1.0.31",

+ 25 - 2
app/src/pages/profile/profile.ts

@@ -6,6 +6,8 @@ import {
   InfiniteScroll
 } from "ionic-angular";
 import { TwitterApiProvider } from "../../providers/twitter-api/twitter-api";
+import { HttpClient } from "@angular/common/http";
+import Gun from "gun";
 
 /**
  * Generated class for the ProfilePage page.
@@ -22,14 +24,18 @@ import { TwitterApiProvider } from "../../providers/twitter-api/twitter-api";
 export class ProfilePage {
   userId: string;
   user: any = [];
+  gun;
 
   tweets: any[];
 
   constructor(
     public navCtrl: NavController,
     public navParams: NavParams,
-    private twitter: TwitterApiProvider
-  ) {}
+    private twitter: TwitterApiProvider,
+    private http: HttpClient
+  ) {
+    this.gun = Gun();
+  }
 
   ionViewDidLoad() {
     this.userId = this.navParams.get("userId");
@@ -39,6 +45,8 @@ export class ProfilePage {
     this.twitter
       .fetchUserTimeline(this.userId)
       .then(res => (this.tweets = res));
+
+    this.checkP2pTweets(this.userId);
   }
 
   doRefresh(refresher) {
@@ -59,4 +67,19 @@ export class ProfilePage {
         infiniteScroll.complete();
       });
   }
+
+  private async checkP2pTweets(userId) {
+    let lastTweetHash;
+    this.gun
+      .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);
+    console.log(res);
+  }
 }

+ 6 - 1
app/src/pages/write-tweet/write-tweet.ts

@@ -9,6 +9,7 @@ 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";
+import Gun from "gun";
 
 /**
  * Generated class for the WriteTweetPage page.
@@ -24,6 +25,7 @@ import { Storage } from "@ionic/storage";
 })
 export class WriteTweetPage {
   tweet: FormGroup;
+  gun;
 
   constructor(
     public navCtrl: NavController,
@@ -34,6 +36,7 @@ export class WriteTweetPage {
     private http: HttpClient,
     private storage: Storage
   ) {
+    this.gun = Gun();
     this.tweet = this.formBuilder.group({
       text: ["", Validators.maxLength(140)],
       p2p: [false]
@@ -54,7 +57,8 @@ export class WriteTweetPage {
     loading.present();
 
     if (this.tweet.value.p2p) {
-      await this.postToIpfs(this.tweet.value["text"]);
+      let hash = await this.postToIpfs(this.tweet.value["text"]);
+      this.gun.get("username").put({ lastTweet: hash });
     } else {
       await this.twitter.tweet(this.tweet.value["text"]);
     }
@@ -77,5 +81,6 @@ export class WriteTweetPage {
 
     let res = await this.http.post(url, formData).toPromise();
     console.log(res["Hash"]);
+    return res["Hash"];
   }
 }

Some files were not shown because too many files changed in this diff