write-tweet.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import { Component } from "@angular/core";
  2. import {
  3. IonicPage,
  4. NavController,
  5. NavParams,
  6. LoadingController,
  7. AlertController
  8. } from "ionic-angular";
  9. import {
  10. FormBuilder,
  11. Validators,
  12. FormGroup,
  13. ValidatorFn,
  14. AbstractControl
  15. } from "@angular/forms";
  16. import { MockProvider } from "../../providers/mock-provider/mock-provider";
  17. import { TwitterApiProvider } from "../../providers/twitter-api/twitter-api";
  18. import { Storage } from "@ionic/storage";
  19. import { P2pStorageIpfsProvider } from "../../providers/p2p-storage-ipfs/p2p-storage-ipfs";
  20. import { P2pDatabaseGunProvider } from "../../providers/p2p-database-gun/p2p-database-gun";
  21. import twittertext from "twitter-text";
  22. import { CryptoProvider } from "../../providers/crypto/crypto";
  23. @IonicPage()
  24. @Component({
  25. selector: "page-write-tweet",
  26. templateUrl: "write-tweet.html"
  27. })
  28. export class WriteTweetPage {
  29. tweet: FormGroup;
  30. retweetId: string;
  31. replyToStatusId: string;
  32. retweet;
  33. replyTweet;
  34. constructor(
  35. public navCtrl: NavController,
  36. public navParams: NavParams,
  37. private formBuilder: FormBuilder,
  38. private twitter: TwitterApiProvider,
  39. private loadingCtrl: LoadingController,
  40. private storage: Storage,
  41. private ipfs: P2pStorageIpfsProvider,
  42. private gun: P2pDatabaseGunProvider,
  43. private cryptoUtils: CryptoProvider,
  44. private alertCtrl: AlertController,
  45. private mockProv: MockProvider
  46. ) {
  47. this.retweetId = this.navParams.get("tweetId");
  48. this.replyToStatusId = this.navParams.get("replyToStatus");
  49. console.log('retweeting now? retweetId:', this.retweetId, 'replyToStatusId: ', this.replyToStatusId);
  50. this.tweet = this.formBuilder.group({
  51. text: [""],
  52. p2p: [false]
  53. });
  54. this.addValidators();
  55. }
  56. private async addValidators() {
  57. const triggerWords = await this.storage.get("keywords");
  58. const validators = [
  59. Validators.maxLength(140),
  60. this.containsTriggerWord(triggerWords)
  61. ];
  62. this.tweet.controls["text"].setValidators(validators);
  63. }
  64. private containsTriggerWord(triggerWords: string): ValidatorFn {
  65. return (control: AbstractControl): {
  66. [key: string]: any
  67. } | null => {
  68. if (triggerWords) {
  69. const regexList = triggerWords
  70. .toLowerCase()
  71. .split(", ")
  72. .join("|");
  73. const regex = new RegExp(regexList);
  74. const containsTriggerWord = regex.test(control.value.toLowerCase());
  75. return containsTriggerWord ? { containsTriggerWord: { value: control.value } } :
  76. null;
  77. } else {
  78. return null;
  79. }
  80. };
  81. }
  82. async ionViewDidLoad() {
  83. if (this.retweetId) {
  84. if (this.storage.get("mockup")) {
  85. this.retweet = this.mockProv.fetchTweet(this.retweetId);
  86. console.log("retweeted tweet", this.retweet);
  87. } else {
  88. this.retweet = await this.twitter.fetchTweet(this.retweetId);
  89. }
  90. }
  91. if (this.replyToStatusId) {
  92. if (this.storage.get("mockup")){
  93. console.log("replying in mockup");
  94. this.replyTweet = await this.mockProv.fetchTweet(this.replyToStatusId);
  95. }
  96. else
  97. this.replyTweet = await this.twitter.fetchTweet(this.replyToStatusId);
  98. console.log('this.replyTweet is:', this.replyTweet);
  99. }
  100. }
  101. get tweetCharProgress() {
  102. const progress = 1 - this.tweet.value["text"].length / 140;
  103. const radius = 8;
  104. const circumference = Math.PI * radius * 2;
  105. return progress * circumference;
  106. }
  107. get showTrigger(): boolean {
  108. return (
  109. this.tweet &&
  110. this.tweet.controls &&
  111. this.tweet.controls.text &&
  112. this.tweet.controls.text.errors &&
  113. this.tweet.controls.text.errors["containsTriggerWord"] &&
  114. !this.tweet.controls.p2p.value
  115. );
  116. }
  117. showTriggerInfo() {
  118. this.alertCtrl
  119. .create({
  120. title: "Watch Out!",
  121. message: "Your tweet contains words you have previously defined to only share securely via P2P. Currently P2P mode is not selected.",
  122. buttons: ["OK"]
  123. })
  124. .present();
  125. }
  126. async submitTweet() {
  127. const loading = this.loadingCtrl.create();
  128. loading.present();
  129. if (await this.storage.get("mockup")) {
  130. console.log('value of the tweet is:', this.tweet.value);
  131. await this.mockProv.writeTweet(this.tweet.value["text"],
  132. this.tweet.value.p2p,
  133. this.retweet,
  134. this.replyTweet);
  135. } else {
  136. if (this.tweet.value.p2p) {
  137. loading.setContent("Validate keys...");
  138. if (
  139. (await this.cryptoUtils.isPrivateKeySet()) &&
  140. (await this.cryptoUtils.isPublicKeyPublished())
  141. ) {
  142. loading.setContent("Publish private tweet...");
  143. await this.tweetPrivate();
  144. } else {
  145. loading.dismiss();
  146. const alert = this.alertCtrl.create({
  147. title: "Oooops...",
  148. message: "Please verify that you have set a private and public key in the settings and that your latest public key was published."
  149. });
  150. alert.present();
  151. return;
  152. }
  153. } else {
  154. loading.setContent("Publish on Twitter...");
  155. await this.twitter.tweet(
  156. this.tweet.value["text"],
  157. this.retweet,
  158. this.replyToStatusId
  159. );
  160. }
  161. }
  162. loading.dismiss();
  163. this.navCtrl.pop();
  164. }
  165. private async tweetPrivate() {
  166. const tweet = await this.buildPrivateTweet();
  167. const privateKey = await this.storage.get("privateKey");
  168. const encryptedTweet = this.cryptoUtils.encrypt(
  169. JSON.stringify(tweet),
  170. privateKey
  171. );
  172. const res = await this.ipfs.storeTweet(encryptedTweet);
  173. this.gun.storeLastTweetHashForUser(
  174. tweet.user_id,
  175. res["Hash"],
  176. tweet.created_at
  177. );
  178. this.gun.publishHashtags(tweet.entities.hashtags);
  179. }
  180. private async buildPrivateTweet() {
  181. console.log('building private tweet');
  182. const status = this.tweet.value["text"].trim();
  183. const entities = await this.getEntities(status);
  184. return {
  185. full_text: status,
  186. user_id: await this.storage.get("userId"),
  187. created_at: Date.now(),
  188. private_tweet: true,
  189. in_reply_to_status_id: this.replyToStatusId,
  190. quoted_status_id: this.retweetId,
  191. display_text_range: [0, status.length],
  192. entities: entities
  193. };
  194. }
  195. private async getEntities(status: string) {
  196. return {
  197. hashtags: twittertext.extractHashtagsWithIndices(status),
  198. urls: twittertext.extractUrlsWithIndices(status),
  199. user_mentions: await this.getMentions(status)
  200. };
  201. }
  202. private async getMentions(status: string) {
  203. // extract mentions
  204. const entities = twittertext.extractMentionsWithIndices(status);
  205. // add user_id
  206. const entitiesWithPromises = entities.map(async mention => {
  207. try {
  208. const user = await this.twitter.fetchUserFromScreenName(
  209. mention.screenName
  210. );
  211. mention["id_str"] = user[0]["id_str"];
  212. mention["screen_name"] = mention.screenName;
  213. delete mention.screenName;
  214. } catch (err) {
  215. console.error(
  216. "There is no user signed up to twitter with username: " +
  217. mention.screenName
  218. );
  219. }
  220. return mention;
  221. });
  222. // filter for valid users and return
  223. return (await Promise.all(entitiesWithPromises)).filter(el =>
  224. el.hasOwnProperty("id_str")
  225. );
  226. }
  227. }