Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions src/MatrixClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1287,6 +1287,21 @@ export class MatrixClient extends EventEmitter {
});
}

/**
* A function that replicates @see sendNotice, but edits the passed message instead.
* @param {string} roomId the room ID to send the notice to
* @param {string} eventId the event ID to edit
* @param {string} text the text to send
* @returns {Promise<string>} resolves to the event ID that represents the edit
*/
@timedMatrixClientFunctionCall()
public editNotice(roomId: string, eventId: string, text: string): Promise<string> {
return this.editMessage(roomId, eventId, {
body: text,
msgtype: "m.notice",
});
}

/**
* Sends a notice to the given room with HTML content. The message will be encrypted if the client supports
* encryption and the room is encrypted.
Expand All @@ -1304,6 +1319,23 @@ export class MatrixClient extends EventEmitter {
});
}

/**
* A function that replicates @see sendHtmlNotice, but edits the passed message instead.
* @param {string} roomId the room ID to send the notice to
* @param {string} eventId the event ID to edit
* @param {string} html the HTML to send
* @returns {Promise<string>} resolves to the event ID that represents the edit
*/
@timedMatrixClientFunctionCall()
public editHtmlNotice(roomId: string, eventId: string, html: string): Promise<string> {
return this.editMessage(roomId, eventId, {
body: htmlToText(html, { wordwrap: false }),
msgtype: "m.notice",
format: "org.matrix.custom.html",
formatted_body: html,
});
}

/**
* Sends a text message to the given room. The message will be encrypted if the client supports
* encryption and the room is encrypted.
Expand All @@ -1319,6 +1351,21 @@ export class MatrixClient extends EventEmitter {
});
}

/**
* A function that replicates @see sendText, but edits the passed message instead.
* @param {string} roomId the room ID to send the notice to
* @param {string} eventId the event ID to edit
* @param {string} text the text to send
* @returns {Promise<string>} resolves to the event ID that represents the edit
*/
@timedMatrixClientFunctionCall()
public editText(roomId: string, eventId: string, text: string): Promise<string> {
return this.editMessage(roomId, eventId, {
body: text,
msgtype: "m.text",
});
}

/**
* Sends a text message to the given room with HTML content. The message will be encrypted if the client supports
* encryption and the room is encrypted.
Expand All @@ -1336,6 +1383,23 @@ export class MatrixClient extends EventEmitter {
});
}

/**
* A function that replicates @see sendHtmlText, but edits the passed message instead.
* @param {string} roomId the room ID to send the notice to
* @param {string} eventId the event ID to edit
* @param {string} html the HTML to send
* @returns {Promise<string>} resolves to the event ID that represents the edit
*/
@timedMatrixClientFunctionCall()
public editHtmlText(roomId: string, eventId: string, html: string): Promise<string> {
return this.editMessage(roomId, eventId, {
body: htmlToText(html, { wordwrap: false }),
msgtype: "m.text",
format: "org.matrix.custom.html",
formatted_body: html,
});
}

/**
* Sends a message to the given room. The message will be encrypted if the client supports
* encryption and the room is encrypted.
Expand All @@ -1348,6 +1412,25 @@ export class MatrixClient extends EventEmitter {
return this.sendEvent(roomId, "m.room.message", content);
}

/**
* A function that replicates @see sendMessage, but modifies the passed content so that an edit
* is made instead of a new message.
* @param {string} roomId the room ID to send the notice to
* @param {string} eventId the event ID to edit
* @param {object} content the event content to send
* @returns {Promise<string>} resolves to the event ID that represents the edit
*/
@timedMatrixClientFunctionCall()
public editMessage(roomId: string, eventId: string, content: any): Promise<string> {
content["m.new_content"] = { ...content };
content.body = "* " + content.body;
content["m.relates_to"] = {
rel_type: "m.replace",
event_id: eventId,
};
return this.sendEvent(roomId, "m.room.message", content);
}

/**
* Sends an event to the given room. This will encrypt the event before sending if the room is
* encrypted and the client supports encryption. Use sendRawEvent() to avoid this behaviour.
Expand Down