@@ -37,6 +37,7 @@ import {
37
37
} from "./models/Crypto" ;
38
38
import { requiresCrypto } from "./e2ee/decorators" ;
39
39
import { ICryptoStorageProvider } from "./storage/ICryptoStorageProvider" ;
40
+ import { EncryptedRoomEvent } from "./models/events/EncryptedRoomEvent" ;
40
41
41
42
/**
42
43
* A client that is capable of interacting with a matrix homeserver.
@@ -799,6 +800,17 @@ export class MatrixClient extends EventEmitter {
799
800
800
801
for ( let event of room [ 'timeline' ] [ 'events' ] ) {
801
802
event = await this . processEvent ( event ) ;
803
+ if ( event [ 'type' ] === 'm.room.encrypted' && await this . crypto ?. isRoomEncrypted ( roomId ) ) {
804
+ await emitFn ( "room.encrypted_event" , roomId , event ) ;
805
+ try {
806
+ event = ( await this . crypto . decryptRoomEvent ( new EncryptedRoomEvent ( event ) , roomId ) ) . raw ;
807
+ event = await this . processEvent ( event ) ;
808
+ await emitFn ( "room.decrypted_event" , roomId , event ) ;
809
+ } catch ( e ) {
810
+ LogService . error ( "MatrixClientLite" , `Decryption error on ${ roomId } ${ event [ 'event_id' ] } ` , e ) ;
811
+ await emitFn ( "room.failed_decryption" , roomId , event ) ;
812
+ }
813
+ }
802
814
if ( event [ 'type' ] === 'm.room.message' ) {
803
815
await emitFn ( "room.message" , roomId , event ) ;
804
816
}
@@ -814,14 +826,29 @@ export class MatrixClient extends EventEmitter {
814
826
}
815
827
}
816
828
829
+ /**
830
+ * Gets an event for a room. If the event is encrypted, and the client supports encryption,
831
+ * and the room is encrypted, then this will return a decrypted event.
832
+ * @param {string } roomId the room ID to get the event in
833
+ * @param {string } eventId the event ID to look up
834
+ * @returns {Promise<any> } resolves to the found event
835
+ */
836
+ @timedMatrixClientFunctionCall ( )
837
+ public async getEvent ( roomId : string , eventId : string ) : Promise < any > {
838
+ const event = await this . getRawEvent ( roomId , eventId ) ;
839
+ if ( event [ 'type' ] === 'm.room.encrypted' && await this . crypto ?. isRoomEncrypted ( roomId ) ) {
840
+ return this . processEvent ( ( await this . crypto . decryptRoomEvent ( new EncryptedRoomEvent ( event ) , roomId ) ) . raw ) ;
841
+ }
842
+ }
843
+
817
844
/**
818
845
* Gets an event for a room. Returned as a raw event.
819
846
* @param {string } roomId the room ID to get the event in
820
847
* @param {string } eventId the event ID to look up
821
848
* @returns {Promise<any> } resolves to the found event
822
849
*/
823
850
@timedMatrixClientFunctionCall ( )
824
- public getEvent ( roomId : string , eventId : string ) : Promise < any > {
851
+ public getRawEvent ( roomId : string , eventId : string ) : Promise < any > {
825
852
return this . doRequest ( "GET" , "/_matrix/client/r0/rooms/" + encodeURIComponent ( roomId ) + "/event/" + encodeURIComponent ( eventId ) )
826
853
. then ( ev => this . processEvent ( ev ) ) ;
827
854
}
@@ -1030,6 +1057,7 @@ export class MatrixClient extends EventEmitter {
1030
1057
1031
1058
/**
1032
1059
* Replies to a given event with the given text. The event is sent with a msgtype of m.text.
1060
+ * The message will be encrypted if the client supports encryption and the room is encrypted.
1033
1061
* @param {string } roomId the room ID to reply in
1034
1062
* @param {any } event the event to reply to
1035
1063
* @param {string } text the text to reply with
@@ -1046,6 +1074,7 @@ export class MatrixClient extends EventEmitter {
1046
1074
1047
1075
/**
1048
1076
* Replies to a given event with the given HTML. The event is sent with a msgtype of m.text.
1077
+ * The message will be encrypted if the client supports encryption and the room is encrypted.
1049
1078
* @param {string } roomId the room ID to reply in
1050
1079
* @param {any } event the event to reply to
1051
1080
* @param {string } html the HTML to reply with.
@@ -1060,6 +1089,7 @@ export class MatrixClient extends EventEmitter {
1060
1089
1061
1090
/**
1062
1091
* Replies to a given event with the given text. The event is sent with a msgtype of m.notice.
1092
+ * The message will be encrypted if the client supports encryption and the room is encrypted.
1063
1093
* @param {string } roomId the room ID to reply in
1064
1094
* @param {any } event the event to reply to
1065
1095
* @param {string } text the text to reply with
@@ -1077,6 +1107,7 @@ export class MatrixClient extends EventEmitter {
1077
1107
1078
1108
/**
1079
1109
* Replies to a given event with the given HTML. The event is sent with a msgtype of m.notice.
1110
+ * The message will be encrypted if the client supports encryption and the room is encrypted.
1080
1111
* @param {string } roomId the room ID to reply in
1081
1112
* @param {any } event the event to reply to
1082
1113
* @param {string } html the HTML to reply with.
@@ -1091,7 +1122,8 @@ export class MatrixClient extends EventEmitter {
1091
1122
}
1092
1123
1093
1124
/**
1094
- * Sends a notice to the given room
1125
+ * Sends a notice to the given room. The message will be encrypted if the client supports
1126
+ * encryption and the room is encrypted.
1095
1127
* @param {string } roomId the room ID to send the notice to
1096
1128
* @param {string } text the text to send
1097
1129
* @returns {Promise<string> } resolves to the event ID that represents the message
@@ -1105,7 +1137,8 @@ export class MatrixClient extends EventEmitter {
1105
1137
}
1106
1138
1107
1139
/**
1108
- * Sends a notice to the given room with HTML content
1140
+ * Sends a notice to the given room with HTML content. The message will be encrypted if the client supports
1141
+ * encryption and the room is encrypted.
1109
1142
* @param {string } roomId the room ID to send the notice to
1110
1143
* @param {string } html the HTML to send
1111
1144
* @returns {Promise<string> } resolves to the event ID that represents the message
@@ -1121,7 +1154,8 @@ export class MatrixClient extends EventEmitter {
1121
1154
}
1122
1155
1123
1156
/**
1124
- * Sends a text message to the given room
1157
+ * Sends a text message to the given room. The message will be encrypted if the client supports
1158
+ * encryption and the room is encrypted.
1125
1159
* @param {string } roomId the room ID to send the text to
1126
1160
* @param {string } text the text to send
1127
1161
* @returns {Promise<string> } resolves to the event ID that represents the message
@@ -1135,7 +1169,8 @@ export class MatrixClient extends EventEmitter {
1135
1169
}
1136
1170
1137
1171
/**
1138
- * Sends a text message to the given room with HTML content
1172
+ * Sends a text message to the given room with HTML content. The message will be encrypted if the client supports
1173
+ * encryption and the room is encrypted.
1139
1174
* @param {string } roomId the room ID to send the text to
1140
1175
* @param {string } html the HTML to send
1141
1176
* @returns {Promise<string> } resolves to the event ID that represents the message
@@ -1151,7 +1186,8 @@ export class MatrixClient extends EventEmitter {
1151
1186
}
1152
1187
1153
1188
/**
1154
- * Sends a message to the given room
1189
+ * Sends a message to the given room. The message will be encrypted if the client supports
1190
+ * encryption and the room is encrypted.
1155
1191
* @param {string } roomId the room ID to send the message to
1156
1192
* @param {object } content the event content to send
1157
1193
* @returns {Promise<string> } resolves to the event ID that represents the message
@@ -1162,14 +1198,31 @@ export class MatrixClient extends EventEmitter {
1162
1198
}
1163
1199
1164
1200
/**
1165
- * Sends an event to the given room
1201
+ * Sends an event to the given room. This will encrypt the event before sending if the room is
1202
+ * encrypted and the client supports encryption. Use sendRawEvent() to avoid this behaviour.
1166
1203
* @param {string } roomId the room ID to send the event to
1167
1204
* @param {string } eventType the type of event to send
1168
1205
* @param {string } content the event body to send
1169
1206
* @returns {Promise<string> } resolves to the event ID that represents the event
1170
1207
*/
1171
1208
@timedMatrixClientFunctionCall ( )
1172
1209
public async sendEvent ( roomId : string , eventType : string , content : any ) : Promise < string > {
1210
+ if ( await this . crypto ?. isRoomEncrypted ( roomId ) ) {
1211
+ content = await this . crypto . encryptRoomEvent ( roomId , eventType , content ) ;
1212
+ eventType = "m.room.encrypted" ;
1213
+ }
1214
+ return this . sendRawEvent ( roomId , eventType , content ) ;
1215
+ }
1216
+
1217
+ /**
1218
+ * Sends an event to the given room.
1219
+ * @param {string } roomId the room ID to send the event to
1220
+ * @param {string } eventType the type of event to send
1221
+ * @param {string } content the event body to send
1222
+ * @returns {Promise<string> } resolves to the event ID that represents the event
1223
+ */
1224
+ @timedMatrixClientFunctionCall ( )
1225
+ public async sendRawEvent ( roomId : string , eventType : string , content : any ) : Promise < string > {
1173
1226
const txnId = ( new Date ( ) . getTime ( ) ) + "__inc" + ( ++ this . requestId ) ;
1174
1227
return this . doRequest ( "PUT" , "/_matrix/client/r0/rooms/" + encodeURIComponent ( roomId ) + "/send/" + encodeURIComponent ( eventType ) + "/" + encodeURIComponent ( txnId ) , null , content ) . then ( response => {
1175
1228
return response [ 'event_id' ] ;
0 commit comments