This commit is contained in:
Bruno Windels 2019-10-09 17:51:50 +02:00
parent 3e971e4880
commit 2d848bba29
25 changed files with 84 additions and 100 deletions

View file

@ -57,13 +57,13 @@ module.exports = class RestSessionCreator {
`-u ${username}`,
`-p ${password}`,
'--no-admin',
this.hsUrl
this.hsUrl,
];
const registerCmd = `./register_new_matrix_user ${registerArgs.join(' ')}`;
const allCmds = [
`cd ${this.synapseSubdir}`,
". ./activate",
registerCmd
registerCmd,
].join(' && ');
await execAsync(allCmds, {cwd: this.cwd, encoding: 'utf-8'});
@ -74,9 +74,9 @@ module.exports = class RestSessionCreator {
"type": "m.login.password",
"identifier": {
"type": "m.id.user",
"user": username
"user": username,
},
"password": password
"password": password,
};
const url = `${this.hsUrl}/_matrix/client/r0/login`;
const responseBody = await request.post({url, json: true, body: requestBody});
@ -88,4 +88,4 @@ module.exports = class RestSessionCreator {
hsUrl: this.hsUrl,
};
}
}
};

View file

@ -14,9 +14,6 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
const request = require('request-promise-native');
const RestRoom = require('./room');
const {approveConsent} = require('./consent');
const Logger = require('../logger');
module.exports = class RestMultiSession {
@ -31,7 +28,7 @@ module.exports = class RestMultiSession {
pop(userName) {
const idx = this.sessions.findIndex((s) => s.userName() === userName);
if(idx === -1) {
if (idx === -1) {
throw new Error(`user ${userName} not found`);
}
const session = this.sessions.splice(idx, 1)[0];
@ -39,7 +36,7 @@ module.exports = class RestMultiSession {
}
async setDisplayName(fn) {
this.log.step("set their display name")
this.log.step("set their display name");
await Promise.all(this.sessions.map(async (s) => {
s.log.mute();
await s.setDisplayName(fn(s));
@ -49,10 +46,10 @@ module.exports = class RestMultiSession {
}
async join(roomIdOrAlias) {
this.log.step(`join ${roomIdOrAlias}`)
this.log.step(`join ${roomIdOrAlias}`);
const rooms = await Promise.all(this.sessions.map(async (s) => {
s.log.mute();
const room = await s.join(roomIdOrAlias);
const room = await s.join(roomIdOrAlias);
s.log.unmute();
return room;
}));
@ -64,7 +61,7 @@ module.exports = class RestMultiSession {
const rooms = this.sessions.map(s => s.room(roomIdOrAlias));
return new RestMultiRoom(rooms, roomIdOrAlias, this.log);
}
}
};
class RestMultiRoom {
constructor(rooms, roomIdOrAlias, log) {
@ -74,7 +71,7 @@ class RestMultiRoom {
}
async talk(message) {
this.log.step(`say "${message}" in ${this.roomIdOrAlias}`)
this.log.step(`say "${message}" in ${this.roomIdOrAlias}`);
await Promise.all(this.rooms.map(async (r) => {
r.log.mute();
await r.talk(message);
@ -84,7 +81,7 @@ class RestMultiRoom {
}
async leave() {
this.log.step(`leave ${this.roomIdOrAlias}`)
this.log.step(`leave ${this.roomIdOrAlias}`);
await Promise.all(this.rooms.map(async (r) => {
r.log.mute();
await r.leave();

View file

@ -25,18 +25,18 @@ module.exports = class RestRoom {
}
async talk(message) {
this.log.step(`says "${message}" in ${this._roomId}`)
this.log.step(`says "${message}" in ${this._roomId}`);
const txId = uuidv4();
await this.session._put(`/rooms/${this._roomId}/send/m.room.message/${txId}`, {
"msgtype": "m.text",
"body": message
"body": message,
});
this.log.done();
return txId;
}
async leave() {
this.log.step(`leaves ${this._roomId}`)
this.log.step(`leaves ${this._roomId}`);
await this.session._post(`/rooms/${this._roomId}/leave`);
this.log.done();
}
@ -44,4 +44,4 @@ module.exports = class RestRoom {
roomId() {
return this._roomId;
}
}
};

View file

@ -43,17 +43,17 @@ module.exports = class RestSession {
this.log.step(`sets their display name to ${displayName}`);
this._displayName = displayName;
await this._put(`/profile/${this._credentials.userId}/displayname`, {
displayname: displayName
displayname: displayName,
});
this.log.done();
}
async join(roomIdOrAlias) {
this.log.step(`joins ${roomIdOrAlias}`);
const {room_id} = await this._post(`/join/${encodeURIComponent(roomIdOrAlias)}`);
const roomId = await this._post(`/join/${encodeURIComponent(roomIdOrAlias)}`).room_id;
this.log.done();
const room = new RestRoom(this, room_id, this.log);
this._rooms[room_id] = room;
const room = new RestRoom(this, roomId, this.log);
this._rooms[roomId] = room;
this._rooms[roomIdOrAlias] = room;
return room;
}
@ -86,9 +86,9 @@ module.exports = class RestSession {
body.topic = options.topic;
}
const {room_id} = await this._post(`/createRoom`, body);
const roomId = await this._post(`/createRoom`, body).room_id;
this.log.done();
return new RestRoom(this, room_id, this.log);
return new RestRoom(this, roomId, this.log);
}
_post(csApiPath, body) {
@ -105,23 +105,22 @@ module.exports = class RestSession {
url: `${this._credentials.hsUrl}/_matrix/client/r0${csApiPath}`,
method,
headers: {
"Authorization": `Bearer ${this._credentials.accessToken}`
"Authorization": `Bearer ${this._credentials.accessToken}`,
},
json: true,
body
body,
});
return responseBody;
} catch(err) {
} catch (err) {
const responseBody = err.response.body;
if (responseBody.errcode === 'M_CONSENT_NOT_GIVEN') {
await approveConsent(responseBody.consent_uri);
return this._request(method, csApiPath, body);
} else if(responseBody && responseBody.error) {
} else if (responseBody && responseBody.error) {
throw new Error(`${method} ${csApiPath}: ${responseBody.error}`);
} else {
throw new Error(`${method} ${csApiPath}: ${err.response.statusCode}`);
}
}
}
}
};