write-tweet.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. this.tweet = this.formBuilder.group({
  50. text: [""],
  51. p2p: [false]
  52. });
  53. this.addValidators();
  54. }
  55. private async addValidators() {
  56. const triggerWords = await this.storage.get("keywords");
  57. const validators = [
  58. Validators.maxLength(140),
  59. this.containsTriggerWord(triggerWords)
  60. ];
  61. this.tweet.controls["text"].setValidators(validators);
  62. }
  63. private containsTriggerWord(triggerWords: string): ValidatorFn {
  64. return (control: AbstractControl): { [key: string]: any } | null => {
  65. if (triggerWords) {
  66. const regexList = triggerWords
  67. .toLowerCase()
  68. .split(", ")
  69. .join("|");
  70. const regex = new RegExp(regexList);
  71. const containsTriggerWord = regex.test(control.value.toLowerCase());
  72. return containsTriggerWord
  73. ? { containsTriggerWord: { value: control.value } }
  74. : null;
  75. } else {
  76. return null;
  77. }
  78. };
  79. }
  80. async ionViewDidLoad() {
  81. if (this.retweetId) {
  82. this.retweet = await this.twitter.fetchTweet(this.retweetId);
  83. }
  84. if (this.replyToStatusId) {
  85. this.replyTweet = await this.twitter.fetchTweet(this.replyToStatusId);
  86. }
  87. }
  88. get tweetCharProgress() {
  89. const progress = 1 - this.tweet.value["text"].length / 140;
  90. const radius = 8;
  91. const circumference = Math.PI * radius * 2;
  92. return progress * circumference;
  93. }
  94. get showTrigger(): boolean {
  95. return (
  96. this.tweet &&
  97. this.tweet.controls &&
  98. this.tweet.controls.text &&
  99. this.tweet.controls.text.errors &&
  100. this.tweet.controls.text.errors["containsTriggerWord"] &&
  101. !this.tweet.controls.p2p.value
  102. );
  103. }
  104. showTriggerInfo() {
  105. this.alertCtrl
  106. .create({
  107. title: "Watch Out!",
  108. message:
  109. "Your tweet contains words you have previously defined to only share securely via P2P. Currently P2P mode is not selected.",
  110. buttons: ["OK"]
  111. })
  112. .present();
  113. }
  114. async submitTweet() {
  115. // console.log('submitting tweet');
  116. const loading = this.loadingCtrl.create();
  117. loading.present();
  118. if(await this.storage.get("mockup")){
  119. console.log('value of the tweet is:',this.tweet.value);
  120. }
  121. else{
  122. // console.log('in else ');
  123. if (this.tweet.value.p2p) {
  124. loading.setContent("Validate keys...");
  125. if (
  126. (await this.cryptoUtils.isPrivateKeySet()) &&
  127. (await this.cryptoUtils.isPublicKeyPublished())
  128. ) {
  129. loading.setContent("Publish private tweet...");
  130. await this.tweetPrivate();
  131. } else {
  132. loading.dismiss();
  133. const alert = this.alertCtrl.create({
  134. title: "Oooops...",
  135. message:
  136. "Please verify that you have set a private and public key in the settings and that your latest public key was published."
  137. });
  138. alert.present();
  139. return;
  140. }
  141. }
  142. else {
  143. loading.setContent("Publish on Twitter...");
  144. await this.twitter.tweet(
  145. this.tweet.value["text"],
  146. this.retweet,
  147. this.replyToStatusId
  148. );
  149. }
  150. }
  151. // console.log('publishing tweet for mock',this.tweet.value["text"]);
  152. await this.mockProv.writeTweet(this.tweet.value["text"]);
  153. loading.dismiss();
  154. this.navCtrl.pop();
  155. }
  156. private async tweetPrivate() {
  157. const tweet = await this.buildPrivateTweet();
  158. const privateKey = await this.storage.get("privateKey");
  159. const encryptedTweet = this.cryptoUtils.encrypt(
  160. JSON.stringify(tweet),
  161. privateKey
  162. );
  163. const res = await this.ipfs.storeTweet(encryptedTweet);
  164. this.gun.storeLastTweetHashForUser(
  165. tweet.user_id,
  166. res["Hash"],
  167. tweet.created_at
  168. );
  169. this.gun.publishHashtags(tweet.entities.hashtags);
  170. }
  171. private async buildPrivateTweet() {
  172. console.log('building private tweet');
  173. const status = this.tweet.value["text"].trim();
  174. const entities = await this.getEntities(status);
  175. return {
  176. full_text: status,
  177. user_id: await this.storage.get("userId"),
  178. created_at: Date.now(),
  179. private_tweet: true,
  180. in_reply_to_status_id: this.replyToStatusId,
  181. quoted_status_id: this.retweetId,
  182. display_text_range: [0, status.length],
  183. entities: entities
  184. };
  185. }
  186. private async getEntities(status: string) {
  187. return {
  188. hashtags: twittertext.extractHashtagsWithIndices(status),
  189. urls: twittertext.extractUrlsWithIndices(status),
  190. user_mentions: await this.getMentions(status)
  191. };
  192. }
  193. private async getMentions(status: string) {
  194. // extract mentions
  195. const entities = twittertext.extractMentionsWithIndices(status);
  196. // add user_id
  197. const entitiesWithPromises = entities.map(async mention => {
  198. try {
  199. const user = await this.twitter.fetchUserFromScreenName(
  200. mention.screenName
  201. );
  202. mention["id_str"] = user[0]["id_str"];
  203. mention["screen_name"] = mention.screenName;
  204. delete mention.screenName;
  205. } catch (err) {
  206. console.error(
  207. "There is no user signed up to twitter with username: " +
  208. mention.screenName
  209. );
  210. }
  211. return mention;
  212. });
  213. // filter for valid users and return
  214. return (await Promise.all(entitiesWithPromises)).filter(el =>
  215. el.hasOwnProperty("id_str")
  216. );
  217. }
  218. }