/
puppet.ts
175 lines (160 loc) · 5.14 KB
/
puppet.ts
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
const Promise = require('bluebird');
const matrixSdk = require("matrix-js-sdk");
import { MatrixClient } from './matrix-client';
import { ThirdPartyAdapter } from './third-party-adapter';
import { Config, IdentityPair, PuppetIdentity, readConfigFile, findIdentityPair } from './config';
import { associateToken, TokenAssociationParams } from './associate-token';
export interface PuppetConfigLoadParams {
config?: Config;
jsonFile?: string;
}
/**
* Puppet class
*/
export class Puppet {
userId: string;
client: MatrixClient;
private identityPairId: string;
private configLoadStrat: PuppetConfigLoadParams;
private config : Config;
private identityPair: IdentityPair;
private identity: PuppetIdentity;
private homeserverUrl: string;
private thirdPartyRooms: any;
private adapter: ThirdPartyAdapter;
private matrixRoomMembers: any;
/**
* Constructs a Puppet
*/
constructor(identityPairId: string, configLoadStrat: PuppetConfigLoadParams) {
this.identityPairId = identityPairId;
this.configLoadStrat = configLoadStrat;
this.config = null;
this.client = null;
this.thirdPartyRooms = {};
this.adapter = null;
}
/**
* Reads the config file, creates a matrix client, connects, and waits for sync
*
* @returns {Promise} Returns a promise
*/
public startClient(config?: Config) {
// load config
if (config) {
this.config = config;
this.homeserverUrl = config.homeserverUrl;
} else if (this.configLoadStrat.jsonFile) {
return readConfigFile(this.configLoadStrat.jsonFile).then(config=> {
return this.startClient(config);
})
} else if (this.configLoadStrat.config) {
return this.startClient(this.configLoadStrat.config);
} else {
console.error(`Unable to load puppet configuration!`);
process.exit(1);
}
// end config load
// load identity
this.identityPair = findIdentityPair(this.config, this.identityPairId);
this.identity = this.identityPair.matrixPuppet;
if ( this.identityPair && this.identity ) {
this.userId = "@"+this.identity.localpart+":"+config.homeserverDomain;
} else {
console.error(`No matrix puppet identity found (looked for an identity pair with id: '${this.identityPairId}'`);
process.exit(1);
}
// end load identity
// load token
if (this.identity.token) {
return this.login(this.identity.token);
} else if (this.identity.password) {
let matrixClient = matrixSdk.createClient(this.homeserverUrl);
return matrixClient.loginWithPassword(this.userId, this.identity.password).then(accessDat => {
if (this.configLoadStrat.jsonFile) {
return associateToken({
identityPairId: this.identityPairId,
jsonFile: this.configLoadStrat.jsonFile,
token: accessDat.access_token
}).then(()=>{
return this.login(accessDat.access_token);
});
} else {
return this.login(accessDat.access_token);
}
});
} else {
console.error(`Matrix puppet '${this.identityPairId}' must have a 'token' or 'password' to login`);
process.exit(1);
}
}
private login(token: string) : Promise<void> {
this.client = matrixSdk.createClient({
baseUrl: this.homeserverUrl,
userId: this.userId,
accessToken: token
})
this.client.startClient();
return new Promise((resolve, _reject) => {
this.matrixRoomMembers = {};
this.client.on("RoomState.members", (event, state, _member) => {
this.matrixRoomMembers[state.roomId] = Object.keys(state.members);
});
this.client.on("Room.receipt", (event, room) => {
if (this.adapter && this.adapter.sendReadReceipt) {
if (room.roomId in this.thirdPartyRooms) {
let content = event.getContent();
for (var eventId in content) {
for (var userId in content[eventId]['m.read']) {
if (userId === this.userId) {
console.log("Receive a read event from ourself");
return this.adapter.sendReadReceipt(this.thirdPartyRooms[room.roomId]);
}
}
}
}
}
});
this.client.on('sync', (state) => {
if ( state === 'PREPARED' ) {
console.log('synced');
resolve();
}
});
});
}
/**
* Get the list of matrix room members
*
* @param {string} roomId matrix room id
* @returns {Array} List of room members
*/
getMatrixRoomMembers(roomId) {
return this.matrixRoomMembers[roomId] || [];
}
/**
* Returns the MatrixClient
*
* @returns {MatrixClient} an instance of MatrixClient
*/
getClient() {
return this.client;
}
/**
* Save a third party room id
*
* @param {string} matrixRoomId matrix room id
* @param {string} thirdPartyRoomId third party room id
*/
saveThirdPartyRoomId(matrixRoomId, thirdPartyRoomId) {
this.thirdPartyRooms[matrixRoomId] = thirdPartyRoomId;
}
/**
* Set the App object
*
* @param {MatrixPuppetBridgeBase} app the App object
*/
setAdapter(adapter : ThirdPartyAdapter) {
this.adapter = adapter;
}
}