This repository has been archived by the owner on Jun 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
setup_dialog.js
93 lines (80 loc) · 2.89 KB
/
setup_dialog.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// -*- mode: js; indent-tabs-mode: nil; js-basic-offset: 4 -*-
//
// This file is part of Almond
//
// Copyright 2016 Giovanni Campagna <gcampagn@cs.stanford.edu>
//
// See COPYING for details
"use strict";
const Q = require('q');
const ThingTalk = require('thingtalk');
const Ast = ThingTalk.Ast;
const Type = ThingTalk.Type;
const SEMPRESyntax = ThingTalk.SEMPRESyntax;
const Describe = ThingTalk.Describe;
const ValueCategory = require('./semantic').ValueCategory;
const Dialog = require('./dialog');
const ContactSearchDialog = require('./contact_search_dialog');
module.exports = class SetupDialog extends Dialog {
constructor() {
super();
this.person = null;
this.principal = null;
this.program = null;
this.reconstructed = null;
this.contactSearch = null;
}
handle(command) {
return this.handleGeneric(command).then((handled) => {
if (handled)
return true;
if (this.program === null) {
this.program = command.program;
this.person = command.person;
this.reconstructed = Describe.describeProgram(this.manager.gettext, this.program);
return this._continue(command);
} else {
return this._continue(command);
}
});
}
_resolvePerson() {
if (this.principal !== null)
return Q(false);
if (this.contactSearch !== null && this.contactSearch.value.type !== 'tt:contact_name') {
this.principal = this.contactSearch.value;
return Q(false);
}
this.contactSearch = Ast.InputParam('__principal', this.person);
return ContactSearchDialog.resolve(this, Type.Entity('tt:contact'), this.contactSearch).then((waiting) => {
if (waiting)
return waiting;
else
return this._resolvePerson();
});
}
execute() {
var json = SEMPRESyntax.toSEMPRE(this.program);
this.manager.remote.installRuleRemote(this.principal, json).catch((e) => {
console.log('Ignored error from permission control request: ' + e.code + ': ' + e.message);
});
return this.done();
}
_continue(command) {
return this._resolvePerson().then((waiting) => {
if (waiting)
return waiting;
if (this.expecting === ValueCategory.YesNo) {
if (command.isYes)
return this.execute();
else if (command.isNo)
return this.reset();
else
return this.fail();
} else {
return this.ask(ValueCategory.YesNo, this._("Ok, so you want me to tell %s: %s. Is that right?")
.format(Describe.describeArg(this.manager.gettext, this.principal), this.reconstructed));
}
});
}
}