write-tweet.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { Component } from "@angular/core";
  2. import {
  3. IonicPage,
  4. NavController,
  5. NavParams,
  6. LoadingController
  7. } from "ionic-angular";
  8. import { FormBuilder, Validators, FormGroup } from "@angular/forms";
  9. import { TwitterApiProvider } from "../../providers/twitter-api/twitter-api";
  10. import { HttpClient } from "@angular/common/http";
  11. import { Storage } from "@ionic/storage";
  12. import Gun from "gun";
  13. import { P2pStorageIpfsProvider } from "../../providers/p2p-storage-ipfs/p2p-storage-ipfs";
  14. /**
  15. * Generated class for the WriteTweetPage page.
  16. *
  17. * See https://ionicframework.com/docs/components/#navigation for more info on
  18. * Ionic pages and navigation.
  19. */
  20. @IonicPage()
  21. @Component({
  22. selector: "page-write-tweet",
  23. templateUrl: "write-tweet.html"
  24. })
  25. export class WriteTweetPage {
  26. tweet: FormGroup;
  27. gun;
  28. constructor(
  29. public navCtrl: NavController,
  30. public navParams: NavParams,
  31. private formBuilder: FormBuilder,
  32. private twitter: TwitterApiProvider,
  33. private loadingCtrl: LoadingController,
  34. private http: HttpClient,
  35. private storage: Storage,
  36. private ipfs: P2pStorageIpfsProvider
  37. ) {
  38. this.gun = Gun();
  39. this.tweet = this.formBuilder.group({
  40. text: ["", Validators.maxLength(140)],
  41. p2p: [false]
  42. });
  43. }
  44. ionViewDidLoad() {}
  45. get tweetCharProgress() {
  46. let progress = 1 - this.tweet.value["text"].length / 140;
  47. let radius = 8;
  48. let circumference = Math.PI * radius * 2;
  49. return progress * circumference;
  50. }
  51. async submitTweet() {
  52. let loading = this.loadingCtrl.create();
  53. loading.present();
  54. if (this.tweet.value.p2p) {
  55. const tweet = await this.buildPrivateTweet();
  56. const res = await this.ipfs.storeTweet(tweet);
  57. this.gun.get("username").put({ lastTweet: res["Hash"] });
  58. } else {
  59. await this.twitter.tweet(this.tweet.value["text"]);
  60. }
  61. loading.dismiss();
  62. this.navCtrl.pop();
  63. }
  64. private async buildPrivateTweet() {
  65. return {
  66. text: this.tweet.value["text"].trim(),
  67. userId: await this.storage.get("userId"),
  68. timestamp: Date.now()
  69. };
  70. }
  71. }