|
@@ -6,7 +6,13 @@ import {
|
|
|
LoadingController,
|
|
|
AlertController
|
|
|
} from "ionic-angular";
|
|
|
-import { FormBuilder, Validators, FormGroup } from "@angular/forms";
|
|
|
+import {
|
|
|
+ FormBuilder,
|
|
|
+ Validators,
|
|
|
+ FormGroup,
|
|
|
+ ValidatorFn,
|
|
|
+ AbstractControl
|
|
|
+} from "@angular/forms";
|
|
|
import { TwitterApiProvider } from "../../providers/twitter-api/twitter-api";
|
|
|
import { Storage } from "@ionic/storage";
|
|
|
import { P2pStorageIpfsProvider } from "../../providers/p2p-storage-ipfs/p2p-storage-ipfs";
|
|
@@ -42,9 +48,34 @@ export class WriteTweetPage {
|
|
|
this.replyToStatusId = this.navParams.get("replyToStatus");
|
|
|
|
|
|
this.tweet = this.formBuilder.group({
|
|
|
- text: ["", Validators.maxLength(140)],
|
|
|
+ text: [""],
|
|
|
p2p: [false]
|
|
|
});
|
|
|
+
|
|
|
+ this.addValidators();
|
|
|
+ }
|
|
|
+
|
|
|
+ private async addValidators() {
|
|
|
+ const triggerWords = await this.storage.get("keywords");
|
|
|
+ const validators = [
|
|
|
+ Validators.maxLength(140),
|
|
|
+ this.containsTriggerWord(triggerWords)
|
|
|
+ ];
|
|
|
+ this.tweet.controls["text"].setValidators(validators);
|
|
|
+ }
|
|
|
+
|
|
|
+ private containsTriggerWord(triggerWords: string): ValidatorFn {
|
|
|
+ return (control: AbstractControl): { [key: string]: any } | null => {
|
|
|
+ const regexList = triggerWords
|
|
|
+ .toLowerCase()
|
|
|
+ .split(", ")
|
|
|
+ .join("|");
|
|
|
+ const regex = new RegExp(regexList);
|
|
|
+ const containsTriggerWord = regex.test(control.value.toLowerCase());
|
|
|
+ return containsTriggerWord
|
|
|
+ ? { containsTriggerWord: { value: control.value } }
|
|
|
+ : null;
|
|
|
+ };
|
|
|
}
|
|
|
|
|
|
async ionViewDidLoad() {
|
|
@@ -57,12 +88,34 @@ export class WriteTweetPage {
|
|
|
}
|
|
|
|
|
|
get tweetCharProgress() {
|
|
|
- let progress = 1 - this.tweet.value["text"].length / 140;
|
|
|
- let radius = 8;
|
|
|
- let circumference = Math.PI * radius * 2;
|
|
|
+ const progress = 1 - this.tweet.value["text"].length / 140;
|
|
|
+ const radius = 8;
|
|
|
+ const circumference = Math.PI * radius * 2;
|
|
|
return progress * circumference;
|
|
|
}
|
|
|
|
|
|
+ get showTrigger(): boolean {
|
|
|
+ return (
|
|
|
+ this.tweet &&
|
|
|
+ this.tweet.controls &&
|
|
|
+ this.tweet.controls.text &&
|
|
|
+ this.tweet.controls.text.errors &&
|
|
|
+ this.tweet.controls.text.errors["containsTriggerWord"] &&
|
|
|
+ !this.tweet.controls.p2p.value
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ showTriggerInfo() {
|
|
|
+ this.alertCtrl
|
|
|
+ .create({
|
|
|
+ title: "Watch Out!",
|
|
|
+ message:
|
|
|
+ "Your tweet contains words you have previously defined to only share securely via P2P. Currently P2P mode is not selected.",
|
|
|
+ buttons: ["OK"]
|
|
|
+ })
|
|
|
+ .present();
|
|
|
+ }
|
|
|
+
|
|
|
async submitTweet() {
|
|
|
const loading = this.loadingCtrl.create();
|
|
|
loading.present();
|