import { Component } from "@angular/core"; import { IonicPage, NavController, NavParams, 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"; import Gun from "gun"; /** * Generated class for the WriteTweetPage page. * * See https://ionicframework.com/docs/components/#navigation for more info on * Ionic pages and navigation. */ @IonicPage() @Component({ selector: "page-write-tweet", templateUrl: "write-tweet.html" }) export class WriteTweetPage { tweet: FormGroup; gun; constructor( public navCtrl: NavController, public navParams: NavParams, private formBuilder: FormBuilder, private twitter: TwitterApiProvider, private loadingCtrl: LoadingController, private http: HttpClient, private storage: Storage ) { this.gun = Gun(); this.tweet = this.formBuilder.group({ text: ["", Validators.maxLength(140)], p2p: [false] }); } ionViewDidLoad() {} get tweetCharProgress() { let progress = 1 - this.tweet.value["text"].length / 140; let radius = 8; let circumference = Math.PI * radius * 2; return progress * circumference; } async submitTweet() { let loading = this.loadingCtrl.create(); loading.present(); if (this.tweet.value.p2p) { 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"]); } 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"]); return res["Hash"]; } }