diff --git a/src/matrix/net/HomeServerApi.js b/src/matrix/net/HomeServerApi.js index 8e7a110b..a6acaf74 100644 --- a/src/matrix/net/HomeServerApi.js +++ b/src/matrix/net/HomeServerApi.js @@ -254,6 +254,10 @@ export class HomeServerApi { uploadAttachment(blob, filename, options = null) { return this._authedRequest("POST", `${this._homeserver}/_matrix/media/r0/upload`, {filename}, blob, options); } + + setPusher(pusher, options = null) { + return this._post("/pushers/set", null, pusher, options); + } } export function tests() { diff --git a/src/matrix/push/Pusher.js b/src/matrix/push/Pusher.js new file mode 100644 index 00000000..a8ac1b21 --- /dev/null +++ b/src/matrix/push/Pusher.js @@ -0,0 +1,35 @@ +export class Pusher { + constructor(description) { + this._description = description; + } + + static httpPusher(host, appId, pushkey, data) { + return new Pusher({ + kind: "http", + append: true, // as pushkeys are shared between multiple users on one origin + data: Object.assign({}, data, {url: host + "/_matrix/push/v1/notify"}), + pushkey, + app_id: appId, + app_display_name: "Hydrogen", + device_display_name: "Hydrogen", + lang: "en" + }); + } + + static createDefaultPayload(sessionId) { + return {session_id: sessionId}; + } + + async enable(hsApi, log) { + await hsApi.setPusher(this._description, {log}).response(); + } + + async disable(hsApi, log) { + const deleteDescription = Object.assign({}, this._description, {kind: null}); + await hsApi.setPusher(deleteDescription, {log}).response(); + } + + serialize() { + return this._description; + } +}