write-tweet.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. @IonicPage()
  14. @Component({
  15. selector: "page-write-tweet",
  16. templateUrl: "write-tweet.html"
  17. })
  18. export class WriteTweetPage {
  19. tweet: FormGroup;
  20. retweetId: string;
  21. replyToStatusId: string;
  22. retweet;
  23. replyTweet;
  24. constructor(
  25. public navCtrl: NavController,
  26. public navParams: NavParams,
  27. private formBuilder: FormBuilder,
  28. private twitter: TwitterApiProvider,
  29. private loadingCtrl: LoadingController,
  30. private storage: Storage,
  31. private ipfs: P2pStorageIpfsProvider,
  32. private gun: P2pDatabaseGunProvider
  33. ) {
  34. this.retweetId = this.navParams.get("tweetId");
  35. this.replyToStatusId = this.navParams.get("replyToStatus");
  36. this.tweet = this.formBuilder.group({
  37. text: ["", Validators.maxLength(140)],
  38. p2p: [false]
  39. });
  40. }
  41. async ionViewDidLoad() {
  42. if (this.retweetId) {
  43. this.retweet = await this.twitter.fetchTweet(this.retweetId);
  44. }
  45. if (this.replyToStatusId) {
  46. this.replyTweet = await this.twitter.fetchTweet(this.replyToStatusId);
  47. }
  48. }
  49. get tweetCharProgress() {
  50. let progress = 1 - this.tweet.value["text"].length / 140;
  51. let radius = 8;
  52. let circumference = Math.PI * radius * 2;
  53. return progress * circumference;
  54. }
  55. async submitTweet() {
  56. const loading = this.loadingCtrl.create();
  57. loading.present();
  58. if (this.tweet.value.p2p) {
  59. const tweet = await this.buildPrivateTweet();
  60. const res = await this.ipfs.storeTweet(tweet);
  61. this.gun.storeLastTweetHashForUser(
  62. tweet.user_id,
  63. res["Hash"],
  64. tweet.created_at
  65. );
  66. } else {
  67. await this.twitter.tweet(
  68. this.tweet.value["text"],
  69. this.retweet,
  70. this.replyToStatusId
  71. );
  72. }
  73. loading.dismiss();
  74. this.navCtrl.pop();
  75. }
  76. private async buildPrivateTweet() {
  77. const status = this.tweet.value["text"].trim();
  78. return {
  79. full_text: status,
  80. user_id: await this.storage.get("userId"),
  81. created_at: Date.now(),
  82. private_tweet: true,
  83. display_text_range: [0, status.length],
  84. entities: {}
  85. };
  86. }
  87. }