write-tweet.ts 2.1 KB

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