This repository has been archived by the owner on Dec 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnodeconnect.js
121 lines (106 loc) · 3.55 KB
/
nodeconnect.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const fs = require('fs');
// Set up module aliases for development environment.
const alias = require('module-alias');
if (fs.existsSync('../client-node/package.json')) {
alias.addAliases({
'nymph-client-node': __dirname + '/../client-node/index.js',
'nymph-client': __dirname + '/../client/dist/NymphClient.js',
'tilmeld-client': __dirname + '/../tilmeld-client/dist/TilmeldClient.js',
});
}
// Nymph Node client for the win.
const { Nymph, PubSub } = require('nymph-client-node');
// Set up Nymph.
const nymphOptions = {
restURL: 'http://localhost:8080/examples/examples/rest.php',
pubsubURL: 'ws://localhost:8081',
};
Nymph.init(nymphOptions);
const { Todo } = require('./examples/todo/Todo');
const { User } = require('tilmeld-client');
main();
async function main() {
let user;
// Check if we can login.
try {
let data = await User.loginUser({
username: 'user@example.com',
password: 'password',
});
if (data.result) {
user = data.user;
} else if (data.message === 'Incorrect login/password.') {
// Register a new user.
user = new User();
user.username = 'user@example.com';
user.email = 'user@example.com';
user.nameFirst = 'User';
user.nameLast = 'McUserface';
data = await user.$register({ password: 'password' });
if (data.result) {
if (!data.loggedin) {
console.log(
"\n\nThe Node user in Tilmeld, 'User McUserface', needs to be enabled.",
);
return;
}
} else {
console.log(
"\n\nI can't register the Node user in Tilmeld, 'User McUserface': ",
data.message,
);
}
}
console.log('\n\nCurrent User: ', (await User.current()).toJSON());
// Listen to Foobar entities, and show them on the console.
Nymph.getEntities(
{ class: Todo.class },
{ type: '&', strict: ['name', 'Foobar'] },
).subscribe(update => {
console.log(
'\n\nReceived Todo Updates: ',
Array.isArray(update) ? update.map(todo => todo.joJSON()) : update,
);
});
// Make a new todo.
const todo = new Todo();
todo.name = 'Foobar';
console.log('\n\ntodo: ', todo.toJSON());
console.log('\n\nNew Todo todo.$save(): ', (await todo.$save()).toJSON());
// Wait 5 seconds, set it to done.
console.log('\n\nWait 5 seconds...');
await new Promise(r => setTimeout(() => r(), 5000));
todo.done = true;
console.log('Set to Done todo.$save(): ', (await todo.$save()).toJSON());
// Wait 5 seconds, archive it.
console.log('\n\nWait 5 seconds...');
await new Promise(r => setTimeout(() => r(), 5000));
console.log('Archive todo.$archive(): ', await todo.$archive());
// Wait 5 seconds, delete it.
console.log('\n\nWait 5 seconds...');
await new Promise(r => setTimeout(() => r(), 5000));
console.log('Delete todo.$delete(): ', await todo.$delete());
// Logout.
console.log('\n\nWait 5 seconds...');
await new Promise(r => setTimeout(() => r(), 5000));
console.log('Logout user.$logout(): ', await user.$logout());
// Close the PubSub connection.
PubSub.close();
} catch (err) {
console.log('err: ', err);
}
return;
}
// This function would try to get a user, then edit it. If no one is logged in,
// it should fail.
async function userEditTest() {
let user = await User.byUsername('user@example.com');
user.nameFirst = 'Doofus';
try {
await user.$save();
} catch (err) {
console.log('err: ', err);
}
await user.$refresh();
console.log('user: ', user.toJSON());
}