write-tweet.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. import twittertext from "twitter-text";
  14. @IonicPage()
  15. @Component({
  16. selector: "page-write-tweet",
  17. templateUrl: "write-tweet.html"
  18. })
  19. export class WriteTweetPage {
  20. tweet: FormGroup;
  21. retweetId: string;
  22. replyToStatusId: string;
  23. retweet;
  24. replyTweet;
  25. constructor(
  26. public navCtrl: NavController,
  27. public navParams: NavParams,
  28. private formBuilder: FormBuilder,
  29. private twitter: TwitterApiProvider,
  30. private loadingCtrl: LoadingController,
  31. private storage: Storage,
  32. private ipfs: P2pStorageIpfsProvider,
  33. private gun: P2pDatabaseGunProvider
  34. ) {
  35. this.retweetId = this.navParams.get("tweetId");
  36. this.replyToStatusId = this.navParams.get("replyToStatus");
  37. this.tweet = this.formBuilder.group({
  38. text: ["", Validators.maxLength(140)],
  39. p2p: [false]
  40. });
  41. }
  42. async ionViewDidLoad() {
  43. if (this.retweetId) {
  44. this.retweet = await this.twitter.fetchTweet(this.retweetId);
  45. }
  46. if (this.replyToStatusId) {
  47. this.replyTweet = await this.twitter.fetchTweet(this.replyToStatusId);
  48. }
  49. }
  50. get tweetCharProgress() {
  51. let progress = 1 - this.tweet.value["text"].length / 140;
  52. let radius = 8;
  53. let circumference = Math.PI * radius * 2;
  54. return progress * circumference;
  55. }
  56. async submitTweet() {
  57. const loading = this.loadingCtrl.create();
  58. loading.present();
  59. if (this.tweet.value.p2p) {
  60. const tweet = await this.buildPrivateTweet();
  61. const res = await this.ipfs.storeTweet(tweet);
  62. this.gun.storeLastTweetHashForUser(
  63. tweet.user_id,
  64. res["Hash"],
  65. tweet.created_at
  66. );
  67. } else {
  68. await this.twitter.tweet(
  69. this.tweet.value["text"],
  70. this.retweet,
  71. this.replyToStatusId
  72. );
  73. }
  74. loading.dismiss();
  75. this.navCtrl.pop();
  76. }
  77. private async buildPrivateTweet() {
  78. const status = this.tweet.value["text"].trim();
  79. const entities = await this.getEntities(status);
  80. return {
  81. full_text: status,
  82. user_id: await this.storage.get("userId"),
  83. created_at: Date.now(),
  84. private_tweet: true,
  85. display_text_range: [0, status.length],
  86. entities: entities
  87. };
  88. }
  89. private async getEntities(status: string) {
  90. return {
  91. hashtags: twittertext.extractHashtagsWithIndices(status),
  92. urls: twittertext.extractUrlsWithIndices(status),
  93. user_mentions: await this.getMentions(status)
  94. };
  95. }
  96. private async getMentions(status: string) {
  97. // extract mentions
  98. const entities = twittertext.extractMentionsWithIndices(status);
  99. // add user_id
  100. const entitiesWithPromises = entities.map(async mention => {
  101. try {
  102. const user = await this.twitter.fetchUserFromScreenName(
  103. mention.screenName
  104. );
  105. mention["id_str"] = user[0]["id_str"];
  106. mention["screen_name"] = mention.screenName;
  107. delete mention.screenName;
  108. } catch (err) {
  109. console.error(
  110. "There is no user signed up to twitter with username: " +
  111. mention.screenName
  112. );
  113. }
  114. return mention;
  115. });
  116. // filter for valid users and return
  117. return (await Promise.all(entitiesWithPromises)).filter(el =>
  118. el.hasOwnProperty("id_str")
  119. );
  120. }
  121. }