UNPKG

202 kBSource Map (JSON)View Raw
1{"version":3,"sources":["../src/client.js"],"names":["client","userId","deviceId","verified","blocked","known","_crypto","Error","setDeviceVerification","dev","emit","_setDeviceVerification","PushProcessor","require","EventEmitter","url","httpApi","MatrixEvent","EventStatus","EventTimeline","SearchResult","StubStore","webRtcCall","utils","contentRepo","Filter","SyncApi","MatrixBaseApis","MatrixError","ContentHelpers","SCROLLBACK_DELAY_MS","CRYPTO_ENABLED","Crypto","e","console","warn","MatrixClient","opts","baseUrl","endsWith","substr","length","idBaseUrl","call","reEmitter","store","credentials","scheduler","self","setProcessFunction","eventToSend","room","getRoom","getRoomId","status","SENDING","_updatePendingEventStatus","_sendEventHttpRequest","clientRunning","callList","createNewMatrixCall","_supportsVoip","setupCallEventHandler","_syncingRetry","_syncApi","_peekSync","_isGuest","_ongoingScrollbacks","timelineSupport","Boolean","urlPreviewCache","_notifTimelineSet","_cryptoStore","cryptoStore","_sessionStore","sessionStore","_forceTURN","forceTURN","olmVersion","getOlmVersion","_roomList","_pushProcessor","inherits","extend","prototype","clearStores","_clientRunning","promises","push","deleteAllData","all","getUserId","getDomain","replace","getUserIdLocalpart","split","substring","getDeviceId","supportsVoip","setForceTURN","getSyncState","isGuest","getScheduler","setGuest","retryImmediately","getNotifTimelineSet","setNotifTimelineSet","notifTimelineSet","initCrypto","init","crypto","reEmit","registerEventHandlers","isCryptoEnabled","getDeviceEd25519Key","uploadKeys","uploadDeviceKeys","downloadKeys","userIds","forceDownload","reject","getStoredDevicesForUser","getStoredDevice","setDeviceVerified","undefined","setDeviceBlocked","setDeviceKnown","setGlobalBlacklistUnverifiedDevices","value","getGlobalBlacklistUnverifiedDevices","getEventSenderDeviceInfo","event","isEventSenderVerified","device","isVerified","cancelAndResendEventRoomKeyRequest","cancelAndResendKeyRequest","setRoomEncryption","roomId","config","isRoomEncrypted","ev","currentState","getStateEvents","exportRoomKeys","importRoomKeys","keys","getGroup","groupId","getGroups","getRooms","getUser","getUsers","setAccountData","eventType","contents","callback","path","encodeUri","$userId","$type","_http","authedRequest","getAccountData","getIgnoredUsers","getContent","setIgnoredUsers","content","ignored_users","map","u","isUserIgnored","indexOf","joinRoom","roomIdOrAlias","isFunction","syncRoom","hasMembershipState","resolve","sign_promise","inviteSignUrl","requestOtherUrl","mxid","defer","then","signed_invite_object","data","third_party_signed","$roomid","res","room_id","syncApi","_clientOpts","createRoom","done","_resolve","err","_reject","promise","resendEvent","_sendEvent","cancelPendingEvent","QUEUED","NOT_SENT","removeEventFromQueue","CANCELLED","setRoomName","name","sendStateEvent","setRoomTopic","topic","getRoomTags","$roomId","setRoomTag","tagName","metadata","$tag","deleteRoomTag","setRoomAccountData","setPowerLevel","powerLevel","users","getType","deepCopy","sendEvent","txnId","makeTxnId","log","localEvent","event_id","user_id","type","origin_server_ts","Date","getTime","_txnId","addPendingEvent","encryptionPromise","_encryptEventIfNeeded","ENCRYPTING","queueEvent","getQueueForEvent","updatePendingEvent","SENT","error","stack","err2","isEncrypted","encryptEvent","newStatus","pathParams","$eventType","getWireType","$stateKey","getStateKey","$txnId","isState","pathTemplate","getWireContent","sendMessage","sendTextMessage","body","makeTextMessage","sendNotice","makeNotice","sendEmoteMessage","makeEmoteMessage","sendImageMessage","info","text","msgtype","sendStickerMessage","sendHtmlMessage","htmlBody","makeHtmlMessage","sendHtmlNotice","makeHtmlNotice","sendHtmlEmote","makeHtmlEmote","sendReceipt","receiptType","$receiptType","$eventId","getId","_addLocalEchoReceipt","sendReadReceipt","setRoomReadMarkers","eventId","rrEvent","rmEventId","rrEventId","setRoomReadMarkersHttpRequest","getUrlPreview","ts","key","og","authedRequestWithPrefix","PREFIX_MEDIA_R0","response","sendTyping","isTyping","timeoutMs","typing","timeout","invite","_membershipChange","inviteByEmail","email","inviteByThreePid","medium","address","identityServerUrl","getIdentityServerUrl","errcode","id_server","leave","ban","reason","forget","deleteRoom","removeRoom","unban","kick","_setMembershipState","membershipValue","membership","$room_id","$membership","getPushActionsForEvent","getPushActions","setPushActions","actionsForEvent","setProfileInfo","$info","setDisplayName","displayname","setAvatarUrl","avatar_url","mxcUrlToHttp","mxcUrl","width","height","resizeMethod","allowDirectLinks","getHttpUriForMxc","setPresence","presence","validStates","_presenceList","method","getPresenceList","inviteToPresenceList","dropFromPresenceList","scrollback","limit","timeToWaitMs","errorTs","timeWaitedMs","now","Math","max","oldState","paginationToken","numAdded","params","from","dir","delay","matrixEvents","chunk","_PojoToMatrixEventMapper","addEventsToTimeline","getLiveTimeline","end","storeEvents","paginateEventContext","eventContext","backwards","token","getPaginateToken","pendingRequest","_paginateRequests","getEvent","getEventMapper","reverse","addEvents","setPaginateToken","finally","getEventTimeline","timelineSet","getTimelineForEvent","events_after","events","concat","events_before","timeline","addTimeline","initialiseState","state","getState","FORWARDS","start","tl","paginateEventTimeline","eventTimeline","isNotifTimeline","getTimelineSet","BACKWARDS","getPaginationToken","_paginationRequests","only","PREFIX_UNSTABLE","next_token","i","notifications","notification","actionListToActionsObject","actions","setPaginationToken","filter","getFilter","getRoomTimelineFilterComponent","resetNotifTimelineSet","resetLiveTimeline","peekInRoom","stopPeeking","peek","setGuestAccess","writePromise","guest_access","allowJoin","readPromise","allowRead","history_visibility","requestRegisterEmailToken","clientSecret","sendAttempt","nextLink","_requestTokenFromEndpoint","client_secret","send_attempt","next_link","requestRegisterMsisdnToken","phoneCountry","phoneNumber","country","phone_number","requestAdd3pidEmailToken","requestAdd3pidMsisdnToken","requestPasswordEmailToken","requestPasswordMsisdnToken","endpoint","id_server_url","parse","host","postParams","request","getRoomPushRule","scope","pushRules","rule","rule_id","setRoomMutePushRule","mute","deferred","hasDontNotifyRule","roomPushRule","deletePushRule","addPushRule","ruleRefreshDeferred","getPushRules","result","searchMessageText","roomEvents","search_term","query","search","search_categories","room_events","searchRoomEvents","term","order_by","event_context","before_limit","after_limit","include_profile","searchResults","_query","results","highlights","_processRoomEventsSearch","bind","backPaginateRoomEventsSearch","next_batch","searchOpts","count","forEach","hl","sr","fromJson","syncLeftRooms","_syncedLeftRooms","_syncLeftRoomsPromise","createFilter","filter_id","storeFilter","filterId","allowCached","$filterId","getOrCreateFilter","filterName","getFilterIdByName","existingFilter","oldDef","getDefinition","newDef","deepCompare","setFilterIdByName","httpStatus","existingId","createdFilter","getOpenIdToken","turnServer","getTurnServers","_turnServers","startClient","initialSyncLimit","checkTurnServers","stop","canResetEntireTimeline","_canResetTimelineCallback","sync","stopClient","global","clearTimeout","_checkTurnServersTimeoutID","setCanResetTimelineCallback","cb","getCanResetTimelineCallback","candidatesByCall","callEventBuffer","isClientPrepared","on","ignoreCallIds","call_id","callEventHandler","onEvent","isBeingDecrypted","isDecryptionFailure","once","getSender","getAge","lifetime","callId","_initWithInvite","_gotRemoteIceCandidate","existingCall","existingCalls","values","thisCall","direction","_replacedBy","answer","hangup","_onAnsweredElsewhere","_receivedAnswer","candidates","_initWithHangup","_onHangupReceived","uris","ttl","servers","urls","username","credential","password","setTimeout","mapper","plainOldJsObject","attemptDecryption","generateClientSecret","ret","chars","charAt","floor","random","module","exports"],"mappings":"AAAA;;;;;;;;;;;;;;;;;AAiBA;;;;;;;;;;;;;;;;;;;;;;;;;;;oEA8hBA,kBACIA,MADJ,EACYC,MADZ,EACoBC,QADpB,EAC8BC,QAD9B,EACwCC,OADxC,EACiDC,KADjD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,4BAGSL,OAAOM,OAHhB;AAAA;AAAA;AAAA;;AAAA,8BAIc,IAAIC,KAAJ,CAAU,gCAAV,CAJd;;AAAA;AAAA;AAAA,sDAMsBP,OAAOM,OAAP,CAAeE,qBAAf,CACdP,MADc,EACNC,QADM,EACIC,QADJ,EACcC,OADd,EACuBC,KADvB,CANtB;;AAAA;AAMUI,2BANV;;AASIT,+BAAOU,IAAP,CAAY,2BAAZ,EAAyCT,MAAzC,EAAiDC,QAAjD,EAA2DO,GAA3D;;AATJ;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,K;;oBAAeE,sB;;;;;AAYf;;;;;;;;;AA/gBA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SAAK,IAAIzK,IAAI,CAAb,EAAgBA,IAAI,EAApB,EAAwBA,GAAxB,EAA6B;AACzBwK,eAAOC,MAAMC,MAAN,CAAahO,KAAKiO,KAAL,CAAWjO,KAAKkO,MAAL,KAAgBH,MAAM7hB,MAAjC,CAAb,CAAP;AACH;;AAED,WAAO4hB,GAAP;AACH,CATD;;AAWA;AACAK,OAAOC,OAAP,CAAeviB,YAAf,GAA8BA,YAA9B;AACA;AACAsiB,OAAOC,OAAP,CAAe5iB,cAAf,GAAgCA,cAAhC;;AAEA;;AAEA;;;;;;;;;;;;;;AAcA;;;;;;;;;;AAUA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqHC;;;;;;;;;;;AAWA;;;;;;;;;;;;AAYA;;;;;;;;;;;AAWD;;;;;;;;;;AAUA;;;;;;;;;;;;AAYA;;;;;;;;;;AAUA;;;;;;;;;;;AAWA;;;;;;;;;;AAWA;;AAEA;;;;;;AAMA;;;;;;;;;;;AAWA;;;;;;;;AAQA;;;;;;;;;AASA;;;;;;;;;;AAUA;;;;;;;;;AASA;;;;;;;;AAQA;;;;;;;;;;AAUA;;AAEA;;;;;;;;;AASC","file":"client.js","sourcesContent":["/*\nCopyright 2015, 2016 OpenMarket Ltd\nCopyright 2017 Vector Creations Ltd\nCopyright 2018 New Vector Ltd\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n*/\n\"use strict\";\n\nconst PushProcessor = require('./pushprocessor');\n\n/**\n * This is an internal module. See {@link MatrixClient} for the public class.\n * @module client\n */\nconst EventEmitter = require(\"events\").EventEmitter;\nimport Promise from 'bluebird';\nconst url = require('url');\n\nconst httpApi = require(\"./http-api\");\nconst MatrixEvent = require(\"./models/event\").MatrixEvent;\nconst EventStatus = require(\"./models/event\").EventStatus;\nconst EventTimeline = require(\"./models/event-timeline\");\nconst SearchResult = require(\"./models/search-result\");\nconst StubStore = require(\"./store/stub\");\nconst webRtcCall = require(\"./webrtc/call\");\nconst utils = require(\"./utils\");\nconst contentRepo = require(\"./content-repo\");\nconst Filter = require(\"./filter\");\nconst SyncApi = require(\"./sync\");\nconst MatrixBaseApis = require(\"./base-apis\");\nconst MatrixError = httpApi.MatrixError;\nconst ContentHelpers = require(\"./content-helpers\");\n\nimport ReEmitter from './ReEmitter';\nimport RoomList from './crypto/RoomList';\n\nconst SCROLLBACK_DELAY_MS = 3000;\nlet CRYPTO_ENABLED = false;\n\ntry {\n var Crypto = require(\"./crypto\");\n CRYPTO_ENABLED = true;\n} catch (e) {\n console.warn(\"Unable to load crypto module: crypto will be disabled: \" + e);\n}\n\n/**\n * Construct a Matrix Client. Only directly construct this if you want to use\n * custom modules. Normally, {@link createClient} should be used\n * as it specifies 'sensible' defaults for these modules.\n * @constructor\n * @extends {external:EventEmitter}\n * @extends {module:base-apis~MatrixBaseApis}\n *\n * @param {Object} opts The configuration options for this client.\n * @param {string} opts.baseUrl Required. The base URL to the client-server\n * HTTP API.\n * @param {string} opts.idBaseUrl Optional. The base identity server URL for\n * identity server requests.\n * @param {Function} opts.request Required. The function to invoke for HTTP\n * requests. The value of this property is typically <code>require(\"request\")\n * </code> as it returns a function which meets the required interface. See\n * {@link requestFunction} for more information.\n *\n * @param {string} opts.accessToken The access_token for this user.\n *\n * @param {string} opts.userId The user ID for this user.\n *\n * @param {Object=} opts.store The data store to use. If not specified,\n * this client will not store any HTTP responses.\n *\n * @param {string=} opts.deviceId A unique identifier for this device; used for\n * tracking things like crypto keys and access tokens. If not specified,\n * end-to-end crypto will be disabled.\n *\n * @param {Object=} opts.sessionStore A store to be used for end-to-end crypto\n * session data. This should be a {@link\n * module:store/session/webstorage~WebStorageSessionStore|WebStorageSessionStore},\n * or an object implementing the same interface. If not specified,\n * end-to-end crypto will be disabled.\n *\n * @param {Object} opts.scheduler Optional. The scheduler to use. If not\n * specified, this client will not retry requests on failure. This client\n * will supply its own processing function to\n * {@link module:scheduler~MatrixScheduler#setProcessFunction}.\n *\n * @param {Object} opts.queryParams Optional. Extra query parameters to append\n * to all requests with this client. Useful for application services which require\n * <code>?user_id=</code>.\n *\n * @param {Number=} opts.localTimeoutMs Optional. The default maximum amount of\n * time to wait before timing out HTTP requests. If not specified, there is no timeout.\n *\n * @param {boolean} [opts.useAuthorizationHeader = false] Set to true to use\n * Authorization header instead of query param to send the access token to the server.\n *\n * @param {boolean} [opts.timelineSupport = false] Set to true to enable\n * improved timeline support ({@link\n * module:client~MatrixClient#getEventTimeline getEventTimeline}). It is\n * disabled by default for compatibility with older clients - in particular to\n * maintain support for back-paginating the live timeline after a '/sync'\n * result with a gap.\n *\n * @param {module:crypto.store.base~CryptoStore} opts.cryptoStore\n * crypto store implementation.\n */\nfunction MatrixClient(opts) {\n // Allow trailing slash in HS url\n if (opts.baseUrl && opts.baseUrl.endsWith(\"/\")) {\n opts.baseUrl = opts.baseUrl.substr(0, opts.baseUrl.length - 1);\n }\n\n // Allow trailing slash in IS url\n if (opts.idBaseUrl && opts.idBaseUrl.endsWith(\"/\")) {\n opts.idBaseUrl = opts.idBaseUrl.substr(0, opts.idBaseUrl.length - 1);\n }\n\n MatrixBaseApis.call(this, opts);\n\n this.reEmitter = new ReEmitter(this);\n\n this.store = opts.store || new StubStore();\n\n this.deviceId = opts.deviceId || null;\n\n const userId = (opts.userId || null);\n this.credentials = {\n userId: userId,\n };\n\n this.scheduler = opts.scheduler;\n if (this.scheduler) {\n const self = this;\n this.scheduler.setProcessFunction(function(eventToSend) {\n const room = self.getRoom(eventToSend.getRoomId());\n if (eventToSend.status !== EventStatus.SENDING) {\n _updatePendingEventStatus(room, eventToSend,\n EventStatus.SENDING);\n }\n return _sendEventHttpRequest(self, eventToSend);\n });\n }\n this.clientRunning = false;\n\n this.callList = {\n // callId: MatrixCall\n };\n\n // try constructing a MatrixCall to see if we are running in an environment\n // which has WebRTC. If we are, listen for and handle m.call.* events.\n const call = webRtcCall.createNewMatrixCall(this);\n this._supportsVoip = false;\n if (call) {\n setupCallEventHandler(this);\n this._supportsVoip = true;\n }\n this._syncingRetry = null;\n this._syncApi = null;\n this._peekSync = null;\n this._isGuest = false;\n this._ongoingScrollbacks = {};\n this.timelineSupport = Boolean(opts.timelineSupport);\n this.urlPreviewCache = {};\n this._notifTimelineSet = null;\n\n this._crypto = null;\n this._cryptoStore = opts.cryptoStore;\n this._sessionStore = opts.sessionStore;\n\n this._forceTURN = opts.forceTURN || false;\n\n if (CRYPTO_ENABLED) {\n this.olmVersion = Crypto.getOlmVersion();\n }\n\n // List of which rooms have encryption enabled: separate from crypto because\n // we still want to know which rooms are encrypted even if crypto is disabled:\n // we don't want to start sending unencrypted events to them.\n this._roomList = new RoomList(this._cryptoStore, this._sessionStore);\n\n // The pushprocessor caches useful things, so keep one and re-use it\n this._pushProcessor = new PushProcessor(this);\n}\nutils.inherits(MatrixClient, EventEmitter);\nutils.extend(MatrixClient.prototype, MatrixBaseApis.prototype);\n\n/**\n * Clear any data out of the persistent stores used by the client.\n *\n * @returns {Promise} Promise which resolves when the stores have been cleared.\n */\nMatrixClient.prototype.clearStores = function() {\n if (this._clientRunning) {\n throw new Error(\"Cannot clear stores while client is running\");\n }\n\n const promises = [];\n\n promises.push(this.store.deleteAllData());\n if (this._cryptoStore) {\n promises.push(this._cryptoStore.deleteAllData());\n }\n return Promise.all(promises);\n};\n\n/**\n * Get the user-id of the logged-in user\n *\n * @return {?string} MXID for the logged-in user, or null if not logged in\n */\nMatrixClient.prototype.getUserId = function() {\n if (this.credentials && this.credentials.userId) {\n return this.credentials.userId;\n }\n return null;\n};\n\n/**\n * Get the domain for this client's MXID\n * @return {?string} Domain of this MXID\n */\nMatrixClient.prototype.getDomain = function() {\n if (this.credentials && this.credentials.userId) {\n return this.credentials.userId.replace(/^.*?:/, '');\n }\n return null;\n};\n\n/**\n * Get the local part of the current user ID e.g. \"foo\" in \"@foo:bar\".\n * @return {?string} The user ID localpart or null.\n */\nMatrixClient.prototype.getUserIdLocalpart = function() {\n if (this.credentials && this.credentials.userId) {\n return this.credentials.userId.split(\":\")[0].substring(1);\n }\n return null;\n};\n\n/**\n * Get the device ID of this client\n * @return {?string} device ID\n */\nMatrixClient.prototype.getDeviceId = function() {\n return this.deviceId;\n};\n\n\n/**\n * Check if the runtime environment supports VoIP calling.\n * @return {boolean} True if VoIP is supported.\n */\nMatrixClient.prototype.supportsVoip = function() {\n return this._supportsVoip;\n};\n\n/**\n * Set whether VoIP calls are forced to use only TURN\n * candidates. This is the same as the forceTURN option\n * when creating the client.\n * @param {bool} forceTURN True to force use of TURN servers\n */\nMatrixClient.prototype.setForceTURN = function(forceTURN) {\n this._forceTURN = forceTURN;\n};\n\n/**\n * Get the current sync state.\n * @return {?string} the sync state, which may be null.\n * @see module:client~MatrixClient#event:\"sync\"\n */\nMatrixClient.prototype.getSyncState = function() {\n if (!this._syncApi) {\n return null;\n }\n return this._syncApi.getSyncState();\n};\n\n/**\n * Return whether the client is configured for a guest account.\n * @return {boolean} True if this is a guest access_token (or no token is supplied).\n */\nMatrixClient.prototype.isGuest = function() {\n return this._isGuest;\n};\n\n/**\n * Return the provided scheduler, if any.\n * @return {?module:scheduler~MatrixScheduler} The scheduler or null\n */\nMatrixClient.prototype.getScheduler = function() {\n return this.scheduler;\n};\n\n/**\n * Set whether this client is a guest account. <b>This method is experimental\n * and may change without warning.</b>\n * @param {boolean} isGuest True if this is a guest account.\n */\nMatrixClient.prototype.setGuest = function(isGuest) {\n // EXPERIMENTAL:\n // If the token is a macaroon, it should be encoded in it that it is a 'guest'\n // access token, which means that the SDK can determine this entirely without\n // the dev manually flipping this flag.\n this._isGuest = isGuest;\n};\n\n/**\n * Retry a backed off syncing request immediately. This should only be used when\n * the user <b>explicitly</b> attempts to retry their lost connection.\n * @return {boolean} True if this resulted in a request being retried.\n */\nMatrixClient.prototype.retryImmediately = function() {\n return this._syncApi.retryImmediately();\n};\n\n/**\n * Return the global notification EventTimelineSet, if any\n *\n * @return {EventTimelineSet} the globl notification EventTimelineSet\n */\nMatrixClient.prototype.getNotifTimelineSet = function() {\n return this._notifTimelineSet;\n};\n\n/**\n * Set the global notification EventTimelineSet\n *\n * @param {EventTimelineSet} notifTimelineSet\n */\nMatrixClient.prototype.setNotifTimelineSet = function(notifTimelineSet) {\n this._notifTimelineSet = notifTimelineSet;\n};\n\n// Crypto bits\n// ===========\n\n/**\n * Initialise support for end-to-end encryption in this client\n *\n * You should call this method after creating the matrixclient, but *before*\n * calling `startClient`, if you want to support end-to-end encryption.\n *\n * It will return a Promise which will resolve when the crypto layer has been\n * successfully initialised.\n */\nMatrixClient.prototype.initCrypto = async function() {\n if (this._crypto) {\n console.warn(\"Attempt to re-initialise e2e encryption on MatrixClient\");\n return;\n }\n\n if (!this._sessionStore) {\n // this is temporary, the sessionstore is supposed to be going away\n throw new Error(`Cannot enable encryption: no sessionStore provided`);\n }\n if (!this._cryptoStore) {\n // the cryptostore is provided by sdk.createClient, so this shouldn't happen\n throw new Error(`Cannot enable encryption: no cryptoStore provided`);\n }\n\n // initialise the list of encrypted rooms (whether or not crypto is enabled)\n await this._roomList.init();\n\n if (!CRYPTO_ENABLED) {\n throw new Error(\n `End-to-end encryption not supported in this js-sdk build: did ` +\n `you remember to load the olm library?`,\n );\n }\n\n const userId = this.getUserId();\n if (userId === null) {\n throw new Error(\n `Cannot enable encryption on MatrixClient with unknown userId: ` +\n `ensure userId is passed in createClient().`,\n );\n }\n if (this.deviceId === null) {\n throw new Error(\n `Cannot enable encryption on MatrixClient with unknown deviceId: ` +\n `ensure deviceId is passed in createClient().`,\n );\n }\n\n const crypto = new Crypto(\n this,\n this._sessionStore,\n userId, this.deviceId,\n this.store,\n this._cryptoStore,\n this._roomList,\n );\n\n this.reEmitter.reEmit(crypto, [\n \"crypto.roomKeyRequest\",\n \"crypto.roomKeyRequestCancellation\",\n \"crypto.warning\",\n ]);\n\n await crypto.init();\n\n // if crypto initialisation was successful, tell it to attach its event\n // handlers.\n crypto.registerEventHandlers(this);\n this._crypto = crypto;\n};\n\n\n/**\n * Is end-to-end crypto enabled for this client.\n * @return {boolean} True if end-to-end is enabled.\n */\nMatrixClient.prototype.isCryptoEnabled = function() {\n return this._crypto !== null;\n};\n\n\n/**\n * Get the Ed25519 key for this device\n *\n * @return {?string} base64-encoded ed25519 key. Null if crypto is\n * disabled.\n */\nMatrixClient.prototype.getDeviceEd25519Key = function() {\n if (!this._crypto) {\n return null;\n }\n return this._crypto.getDeviceEd25519Key();\n};\n\n/**\n * Upload the device keys to the homeserver.\n * @return {object} A promise that will resolve when the keys are uploaded.\n */\nMatrixClient.prototype.uploadKeys = function() {\n if (this._crypto === null) {\n throw new Error(\"End-to-end encryption disabled\");\n }\n\n return this._crypto.uploadDeviceKeys();\n};\n\n/**\n * Download the keys for a list of users and stores the keys in the session\n * store.\n * @param {Array} userIds The users to fetch.\n * @param {bool} forceDownload Always download the keys even if cached.\n *\n * @return {Promise} A promise which resolves to a map userId->deviceId->{@link\n * module:crypto~DeviceInfo|DeviceInfo}.\n */\nMatrixClient.prototype.downloadKeys = function(userIds, forceDownload) {\n if (this._crypto === null) {\n return Promise.reject(new Error(\"End-to-end encryption disabled\"));\n }\n return this._crypto.downloadKeys(userIds, forceDownload);\n};\n\n/**\n * Get the stored device keys for a user id\n *\n * @param {string} userId the user to list keys for.\n *\n * @return {Promise<module:crypto-deviceinfo[]>} list of devices\n */\nMatrixClient.prototype.getStoredDevicesForUser = async function(userId) {\n if (this._crypto === null) {\n throw new Error(\"End-to-end encryption disabled\");\n }\n return this._crypto.getStoredDevicesForUser(userId) || [];\n};\n\n/**\n * Get the stored device key for a user id and device id\n *\n * @param {string} userId the user to list keys for.\n * @param {string} deviceId unique identifier for the device\n *\n * @return {Promise<?module:crypto-deviceinfo>} device or null\n */\nMatrixClient.prototype.getStoredDevice = async function(userId, deviceId) {\n if (this._crypto === null) {\n throw new Error(\"End-to-end encryption disabled\");\n }\n return this._crypto.getStoredDevice(userId, deviceId) || null;\n};\n\n/**\n * Mark the given device as verified\n *\n * @param {string} userId owner of the device\n * @param {string} deviceId unique identifier for the device\n *\n * @param {boolean=} verified whether to mark the device as verified. defaults\n * to 'true'.\n *\n * @returns {Promise}\n *\n * @fires module:client~event:MatrixClient\"deviceVerificationChanged\"\n */\nMatrixClient.prototype.setDeviceVerified = function(userId, deviceId, verified) {\n if (verified === undefined) {\n verified = true;\n }\n return _setDeviceVerification(this, userId, deviceId, verified, null);\n};\n\n/**\n * Mark the given device as blocked/unblocked\n *\n * @param {string} userId owner of the device\n * @param {string} deviceId unique identifier for the device\n *\n * @param {boolean=} blocked whether to mark the device as blocked. defaults\n * to 'true'.\n *\n * @returns {Promise}\n *\n * @fires module:client~event:MatrixClient\"deviceVerificationChanged\"\n */\nMatrixClient.prototype.setDeviceBlocked = function(userId, deviceId, blocked) {\n if (blocked === undefined) {\n blocked = true;\n }\n return _setDeviceVerification(this, userId, deviceId, null, blocked);\n};\n\n/**\n * Mark the given device as known/unknown\n *\n * @param {string} userId owner of the device\n * @param {string} deviceId unique identifier for the device\n *\n * @param {boolean=} known whether to mark the device as known. defaults\n * to 'true'.\n *\n * @returns {Promise}\n *\n * @fires module:client~event:MatrixClient\"deviceVerificationChanged\"\n */\nMatrixClient.prototype.setDeviceKnown = function(userId, deviceId, known) {\n if (known === undefined) {\n known = true;\n }\n return _setDeviceVerification(this, userId, deviceId, null, null, known);\n};\n\nasync function _setDeviceVerification(\n client, userId, deviceId, verified, blocked, known,\n) {\n if (!client._crypto) {\n throw new Error(\"End-to-End encryption disabled\");\n }\n const dev = await client._crypto.setDeviceVerification(\n userId, deviceId, verified, blocked, known,\n );\n client.emit(\"deviceVerificationChanged\", userId, deviceId, dev);\n}\n\n/**\n * Set the global override for whether the client should ever send encrypted\n * messages to unverified devices. This provides the default for rooms which\n * do not specify a value.\n *\n * @param {boolean} value whether to blacklist all unverified devices by default\n */\nMatrixClient.prototype.setGlobalBlacklistUnverifiedDevices = function(value) {\n if (this._crypto === null) {\n throw new Error(\"End-to-end encryption disabled\");\n }\n this._crypto.setGlobalBlacklistUnverifiedDevices(value);\n};\n\n/**\n * @return {boolean} whether to blacklist all unverified devices by default\n */\nMatrixClient.prototype.getGlobalBlacklistUnverifiedDevices = function() {\n if (this._crypto === null) {\n throw new Error(\"End-to-end encryption disabled\");\n }\n return this._crypto.getGlobalBlacklistUnverifiedDevices();\n};\n\n/**\n * Get e2e information on the device that sent an event\n *\n * @param {MatrixEvent} event event to be checked\n *\n * @return {Promise<module:crypto/deviceinfo?>}\n */\nMatrixClient.prototype.getEventSenderDeviceInfo = async function(event) {\n if (!this._crypto) {\n return null;\n }\n\n return this._crypto.getEventSenderDeviceInfo(event);\n};\n\n/**\n * Check if the sender of an event is verified\n *\n * @param {MatrixEvent} event event to be checked\n *\n * @return {boolean} true if the sender of this event has been verified using\n * {@link module:client~MatrixClient#setDeviceVerified|setDeviceVerified}.\n */\nMatrixClient.prototype.isEventSenderVerified = async function(event) {\n const device = await this.getEventSenderDeviceInfo(event);\n if (!device) {\n return false;\n }\n return device.isVerified();\n};\n\n/**\n * Cancel a room key request for this event if one is ongoing and resend the\n * request.\n * @param {MatrxEvent} event event of which to cancel and resend the room\n * key request.\n */\nMatrixClient.prototype.cancelAndResendEventRoomKeyRequest = function(event) {\n event.cancelAndResendKeyRequest(this._crypto);\n};\n\n/**\n * Enable end-to-end encryption for a room.\n * @param {string} roomId The room ID to enable encryption in.\n * @param {object} config The encryption config for the room.\n * @return {Promise} A promise that will resolve when encryption is set up.\n */\nMatrixClient.prototype.setRoomEncryption = function(roomId, config) {\n if (!this._crypto) {\n throw new Error(\"End-to-End encryption disabled\");\n }\n return this._crypto.setRoomEncryption(roomId, config);\n};\n\n/**\n * Whether encryption is enabled for a room.\n * @param {string} roomId the room id to query.\n * @return {bool} whether encryption is enabled.\n */\nMatrixClient.prototype.isRoomEncrypted = function(roomId) {\n const room = this.getRoom(roomId);\n if (!room) {\n // we don't know about this room, so can't determine if it should be\n // encrypted. Let's assume not.\n return false;\n }\n\n // if there is an 'm.room.encryption' event in this room, it should be\n // encrypted (independently of whether we actually support encryption)\n const ev = room.currentState.getStateEvents(\"m.room.encryption\", \"\");\n if (ev) {\n return true;\n }\n\n // we don't have an m.room.encrypted event, but that might be because\n // the server is hiding it from us. Check the store to see if it was\n // previously encrypted.\n return this._roomList.isRoomEncrypted(roomId);\n};\n\n/**\n * Get a list containing all of the room keys\n *\n * This should be encrypted before returning it to the user.\n *\n * @return {module:client.Promise} a promise which resolves to a list of\n * session export objects\n */\nMatrixClient.prototype.exportRoomKeys = function() {\n if (!this._crypto) {\n return Promise.reject(new Error(\"End-to-end encryption disabled\"));\n }\n return this._crypto.exportRoomKeys();\n};\n\n/**\n * Import a list of room keys previously exported by exportRoomKeys\n *\n * @param {Object[]} keys a list of session export objects\n *\n * @return {module:client.Promise} a promise which resolves when the keys\n * have been imported\n */\nMatrixClient.prototype.importRoomKeys = function(keys) {\n if (!this._crypto) {\n throw new Error(\"End-to-end encryption disabled\");\n }\n return this._crypto.importRoomKeys(keys);\n};\n\n// Group ops\n// =========\n// Operations on groups that come down the sync stream (ie. ones the\n// user is a member of or invited to)\n\n/**\n * Get the group for the given group ID.\n * This function will return a valid group for any group for which a Group event\n * has been emitted.\n * @param {string} groupId The group ID\n * @return {Group} The Group or null if the group is not known or there is no data store.\n */\nMatrixClient.prototype.getGroup = function(groupId) {\n return this.store.getGroup(groupId);\n};\n\n/**\n * Retrieve all known groups.\n * @return {Groups[]} A list of groups, or an empty list if there is no data store.\n */\nMatrixClient.prototype.getGroups = function() {\n return this.store.getGroups();\n};\n\n// Room ops\n// ========\n\n/**\n * Get the room for the given room ID.\n * This function will return a valid room for any room for which a Room event\n * has been emitted. Note in particular that other events, eg. RoomState.members\n * will be emitted for a room before this function will return the given room.\n * @param {string} roomId The room ID\n * @return {Room} The Room or null if it doesn't exist or there is no data store.\n */\nMatrixClient.prototype.getRoom = function(roomId) {\n return this.store.getRoom(roomId);\n};\n\n/**\n * Retrieve all known rooms.\n * @return {Room[]} A list of rooms, or an empty list if there is no data store.\n */\nMatrixClient.prototype.getRooms = function() {\n return this.store.getRooms();\n};\n\n/**\n * Retrieve a user.\n * @param {string} userId The user ID to retrieve.\n * @return {?User} A user or null if there is no data store or the user does\n * not exist.\n */\nMatrixClient.prototype.getUser = function(userId) {\n return this.store.getUser(userId);\n};\n\n/**\n * Retrieve all known users.\n * @return {User[]} A list of users, or an empty list if there is no data store.\n */\nMatrixClient.prototype.getUsers = function() {\n return this.store.getUsers();\n};\n\n// User Account Data operations\n// ============================\n\n/**\n * Set account data event for the current user.\n * @param {string} eventType The event type\n * @param {Object} contents the contents object for the event\n * @param {module:client.callback} callback Optional.\n * @return {module:client.Promise} Resolves: TODO\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.setAccountData = function(eventType, contents, callback) {\n const path = utils.encodeUri(\"/user/$userId/account_data/$type\", {\n $userId: this.credentials.userId,\n $type: eventType,\n });\n return this._http.authedRequest(\n callback, \"PUT\", path, undefined, contents,\n );\n};\n\n/**\n * Get account data event of given type for the current user.\n * @param {string} eventType The event type\n * @return {?object} The contents of the given account data event\n */\nMatrixClient.prototype.getAccountData = function(eventType) {\n return this.store.getAccountData(eventType);\n};\n\n/**\n * Gets the users that are ignored by this client\n * @returns {string[]} The array of users that are ignored (empty if none)\n */\nMatrixClient.prototype.getIgnoredUsers = function() {\n const event = this.getAccountData(\"m.ignored_user_list\");\n if (!event || !event.getContent() || !event.getContent()[\"ignored_users\"]) return [];\n return Object.keys(event.getContent()[\"ignored_users\"]);\n};\n\n/**\n * Sets the users that the current user should ignore.\n * @param {string[]} userIds the user IDs to ignore\n * @param {module:client.callback} [callback] Optional.\n * @return {module:client.Promise} Resolves: Account data event\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.setIgnoredUsers = function(userIds, callback) {\n const content = {ignored_users: {}};\n userIds.map((u) => content.ignored_users[u] = {});\n return this.setAccountData(\"m.ignored_user_list\", content, callback);\n};\n\n/**\n * Gets whether or not a specific user is being ignored by this client.\n * @param {string} userId the user ID to check\n * @returns {boolean} true if the user is ignored, false otherwise\n */\nMatrixClient.prototype.isUserIgnored = function(userId) {\n return this.getIgnoredUsers().indexOf(userId) !== -1;\n};\n\n// Room operations\n// ===============\n\n/**\n * Join a room. If you have already joined the room, this will no-op.\n * @param {string} roomIdOrAlias The room ID or room alias to join.\n * @param {Object} opts Options when joining the room.\n * @param {boolean} opts.syncRoom True to do a room initial sync on the resulting\n * room. If false, the <strong>returned Room object will have no current state.\n * </strong> Default: true.\n * @param {boolean} opts.inviteSignUrl If the caller has a keypair 3pid invite,\n * the signing URL is passed in this parameter.\n * @param {module:client.callback} callback Optional.\n * @return {module:client.Promise} Resolves: Room object.\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.joinRoom = function(roomIdOrAlias, opts, callback) {\n // to help people when upgrading..\n if (utils.isFunction(opts)) {\n throw new Error(\"Expected 'opts' object, got function.\");\n }\n opts = opts || {};\n if (opts.syncRoom === undefined) {\n opts.syncRoom = true;\n }\n\n const room = this.getRoom(roomIdOrAlias);\n if (room && room.hasMembershipState(this.credentials.userId, \"join\")) {\n return Promise.resolve(room);\n }\n\n let sign_promise = Promise.resolve();\n\n if (opts.inviteSignUrl) {\n sign_promise = this._http.requestOtherUrl(\n undefined, 'POST',\n opts.inviteSignUrl, { mxid: this.credentials.userId },\n );\n }\n\n const defer = Promise.defer();\n\n const self = this;\n sign_promise.then(function(signed_invite_object) {\n const data = {};\n if (signed_invite_object) {\n data.third_party_signed = signed_invite_object;\n }\n\n const path = utils.encodeUri(\"/join/$roomid\", { $roomid: roomIdOrAlias});\n return self._http.authedRequest(undefined, \"POST\", path, undefined, data);\n }).then(function(res) {\n const roomId = res.room_id;\n const syncApi = new SyncApi(self, self._clientOpts);\n const room = syncApi.createRoom(roomId);\n if (opts.syncRoom) {\n // v2 will do this for us\n // return syncApi.syncRoom(room);\n }\n return Promise.resolve(room);\n }).done(function(room) {\n _resolve(callback, defer, room);\n }, function(err) {\n _reject(callback, defer, err);\n });\n return defer.promise;\n};\n\n/**\n * Resend an event.\n * @param {MatrixEvent} event The event to resend.\n * @param {Room} room Optional. The room the event is in. Will update the\n * timeline entry if provided.\n * @return {module:client.Promise} Resolves: TODO\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.resendEvent = function(event, room) {\n _updatePendingEventStatus(room, event, EventStatus.SENDING);\n return _sendEvent(this, room, event);\n};\n\n/**\n * Cancel a queued or unsent event.\n *\n * @param {MatrixEvent} event Event to cancel\n * @throws Error if the event is not in QUEUED or NOT_SENT state\n */\nMatrixClient.prototype.cancelPendingEvent = function(event) {\n if ([EventStatus.QUEUED, EventStatus.NOT_SENT].indexOf(event.status) < 0) {\n throw new Error(\"cannot cancel an event with status \" + event.status);\n }\n\n // first tell the scheduler to forget about it, if it's queued\n if (this.scheduler) {\n this.scheduler.removeEventFromQueue(event);\n }\n\n // then tell the room about the change of state, which will remove it\n // from the room's list of pending events.\n const room = this.getRoom(event.getRoomId());\n _updatePendingEventStatus(room, event, EventStatus.CANCELLED);\n};\n\n/**\n * @param {string} roomId\n * @param {string} name\n * @param {module:client.callback} callback Optional.\n * @return {module:client.Promise} Resolves: TODO\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.setRoomName = function(roomId, name, callback) {\n return this.sendStateEvent(roomId, \"m.room.name\", {name: name},\n undefined, callback);\n};\n\n/**\n * @param {string} roomId\n * @param {string} topic\n * @param {module:client.callback} callback Optional.\n * @return {module:client.Promise} Resolves: TODO\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.setRoomTopic = function(roomId, topic, callback) {\n return this.sendStateEvent(roomId, \"m.room.topic\", {topic: topic},\n undefined, callback);\n};\n\n/**\n * @param {string} roomId\n * @param {module:client.callback} callback Optional.\n * @return {module:client.Promise} Resolves: TODO\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.getRoomTags = function(roomId, callback) {\n const path = utils.encodeUri(\"/user/$userId/rooms/$roomId/tags/\", {\n $userId: this.credentials.userId,\n $roomId: roomId,\n });\n return this._http.authedRequest(\n callback, \"GET\", path, undefined,\n );\n};\n\n/**\n * @param {string} roomId\n * @param {string} tagName name of room tag to be set\n * @param {object} metadata associated with that tag to be stored\n * @param {module:client.callback} callback Optional.\n * @return {module:client.Promise} Resolves: TODO\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.setRoomTag = function(roomId, tagName, metadata, callback) {\n const path = utils.encodeUri(\"/user/$userId/rooms/$roomId/tags/$tag\", {\n $userId: this.credentials.userId,\n $roomId: roomId,\n $tag: tagName,\n });\n return this._http.authedRequest(\n callback, \"PUT\", path, undefined, metadata,\n );\n};\n\n/**\n * @param {string} roomId\n * @param {string} tagName name of room tag to be removed\n * @param {module:client.callback} callback Optional.\n * @return {module:client.Promise} Resolves: TODO\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.deleteRoomTag = function(roomId, tagName, callback) {\n const path = utils.encodeUri(\"/user/$userId/rooms/$roomId/tags/$tag\", {\n $userId: this.credentials.userId,\n $roomId: roomId,\n $tag: tagName,\n });\n return this._http.authedRequest(\n callback, \"DELETE\", path, undefined, undefined,\n );\n};\n\n/**\n * @param {string} roomId\n * @param {string} eventType event type to be set\n * @param {object} content event content\n * @param {module:client.callback} callback Optional.\n * @return {module:client.Promise} Resolves: TODO\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.setRoomAccountData = function(roomId, eventType,\n content, callback) {\n const path = utils.encodeUri(\"/user/$userId/rooms/$roomId/account_data/$type\", {\n $userId: this.credentials.userId,\n $roomId: roomId,\n $type: eventType,\n });\n return this._http.authedRequest(\n callback, \"PUT\", path, undefined, content,\n );\n};\n\n/**\n * Set a user's power level.\n * @param {string} roomId\n * @param {string} userId\n * @param {Number} powerLevel\n * @param {MatrixEvent} event\n * @param {module:client.callback} callback Optional.\n * @return {module:client.Promise} Resolves: TODO\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.setPowerLevel = function(roomId, userId, powerLevel,\n event, callback) {\n let content = {\n users: {},\n };\n if (event && event.getType() === \"m.room.power_levels\") {\n // take a copy of the content to ensure we don't corrupt\n // existing client state with a failed power level change\n content = utils.deepCopy(event.getContent());\n }\n content.users[userId] = powerLevel;\n const path = utils.encodeUri(\"/rooms/$roomId/state/m.room.power_levels\", {\n $roomId: roomId,\n });\n return this._http.authedRequest(\n callback, \"PUT\", path, undefined, content,\n );\n};\n\n/**\n * @param {string} roomId\n * @param {string} eventType\n * @param {Object} content\n * @param {string} txnId Optional.\n * @param {module:client.callback} callback Optional.\n * @return {module:client.Promise} Resolves: TODO\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.sendEvent = function(roomId, eventType, content, txnId,\n callback) {\n if (utils.isFunction(txnId)) {\n callback = txnId; txnId = undefined;\n }\n\n if (!txnId) {\n txnId = this.makeTxnId();\n }\n\n console.log(`sendEvent of type ${eventType} in ${roomId} with txnId ${txnId}`);\n\n // we always construct a MatrixEvent when sending because the store and\n // scheduler use them. We'll extract the params back out if it turns out\n // the client has no scheduler or store.\n const room = this.getRoom(roomId);\n const localEvent = new MatrixEvent({\n event_id: \"~\" + roomId + \":\" + txnId,\n user_id: this.credentials.userId,\n room_id: roomId,\n type: eventType,\n origin_server_ts: new Date().getTime(),\n content: content,\n });\n localEvent._txnId = txnId;\n localEvent.status = EventStatus.SENDING;\n\n // add this event immediately to the local store as 'sending'.\n if (room) {\n room.addPendingEvent(localEvent, txnId);\n }\n\n return _sendEvent(this, room, localEvent, callback);\n};\n\n\n// encrypts the event if necessary\n// adds the event to the queue, or sends it\n// marks the event as sent/unsent\n// returns a promise which resolves with the result of the send request\nfunction _sendEvent(client, room, event, callback) {\n // Add an extra Promise.resolve() to turn synchronous exceptions into promise rejections,\n // so that we can handle synchronous and asynchronous exceptions with the\n // same code path.\n return Promise.resolve().then(function() {\n const encryptionPromise = _encryptEventIfNeeded(client, event, room);\n\n if (!encryptionPromise) {\n return null;\n }\n\n _updatePendingEventStatus(room, event, EventStatus.ENCRYPTING);\n return encryptionPromise.then(() => {\n _updatePendingEventStatus(room, event, EventStatus.SENDING);\n });\n }).then(function() {\n let promise;\n // this event may be queued\n if (client.scheduler) {\n // if this returns a promsie then the scheduler has control now and will\n // resolve/reject when it is done. Internally, the scheduler will invoke\n // processFn which is set to this._sendEventHttpRequest so the same code\n // path is executed regardless.\n promise = client.scheduler.queueEvent(event);\n if (promise && client.scheduler.getQueueForEvent(event).length > 1) {\n // event is processed FIFO so if the length is 2 or more we know\n // this event is stuck behind an earlier event.\n _updatePendingEventStatus(room, event, EventStatus.QUEUED);\n }\n }\n\n if (!promise) {\n promise = _sendEventHttpRequest(client, event);\n }\n return promise;\n }).then(function(res) { // the request was sent OK\n if (room) {\n room.updatePendingEvent(event, EventStatus.SENT, res.event_id);\n }\n if (callback) {\n callback(null, res);\n }\n return res;\n }, function(err) {\n // the request failed to send.\n console.error(\"Error sending event\", err.stack || err);\n\n try {\n // set the error on the event before we update the status:\n // updating the status emits the event, so the state should be\n // consistent at that point.\n event.error = err;\n _updatePendingEventStatus(room, event, EventStatus.NOT_SENT);\n // also put the event object on the error: the caller will need this\n // to resend or cancel the event\n err.event = event;\n\n if (callback) {\n callback(err);\n }\n } catch (err2) {\n console.error(\"Exception in error handler!\", err2.stack || err);\n }\n throw err;\n });\n}\n\n/**\n * Encrypt an event according to the configuration of the room, if necessary.\n *\n * @param {MatrixClient} client\n *\n * @param {module:models/event.MatrixEvent} event event to be sent\n *\n * @param {module:models/room?} room destination room. Null if the destination\n * is not a room we have seen over the sync pipe.\n *\n * @return {module:client.Promise?} Promise which resolves when the event has been\n * encrypted, or null if nothing was needed\n */\n\nfunction _encryptEventIfNeeded(client, event, room) {\n if (event.isEncrypted()) {\n // this event has already been encrypted; this happens if the\n // encryption step succeeded, but the send step failed on the first\n // attempt.\n return null;\n }\n\n if (!client.isRoomEncrypted(event.getRoomId())) {\n // looks like this room isn't encrypted.\n return null;\n }\n\n if (!client._crypto) {\n throw new Error(\n \"This room is configured to use encryption, but your client does \" +\n \"not support encryption.\",\n );\n }\n\n return client._crypto.encryptEvent(event, room);\n}\n\nfunction _updatePendingEventStatus(room, event, newStatus) {\n if (room) {\n room.updatePendingEvent(event, newStatus);\n } else {\n event.status = newStatus;\n }\n}\n\nfunction _sendEventHttpRequest(client, event) {\n const txnId = event._txnId ? event._txnId : client.makeTxnId();\n\n const pathParams = {\n $roomId: event.getRoomId(),\n $eventType: event.getWireType(),\n $stateKey: event.getStateKey(),\n $txnId: txnId,\n };\n\n let path;\n\n if (event.isState()) {\n let pathTemplate = \"/rooms/$roomId/state/$eventType\";\n if (event.getStateKey() && event.getStateKey().length > 0) {\n pathTemplate = \"/rooms/$roomId/state/$eventType/$stateKey\";\n }\n path = utils.encodeUri(pathTemplate, pathParams);\n } else {\n path = utils.encodeUri(\n \"/rooms/$roomId/send/$eventType/$txnId\", pathParams,\n );\n }\n\n return client._http.authedRequest(\n undefined, \"PUT\", path, undefined, event.getWireContent(),\n ).then((res) => {\n console.log(\n `Event sent to ${event.getRoomId()} with event id ${res.event_id}`,\n );\n return res;\n });\n}\n\n/**\n * @param {string} roomId\n * @param {Object} content\n * @param {string} txnId Optional.\n * @param {module:client.callback} callback Optional.\n * @return {module:client.Promise} Resolves: TODO\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.sendMessage = function(roomId, content, txnId, callback) {\n if (utils.isFunction(txnId)) {\n callback = txnId; txnId = undefined;\n }\n return this.sendEvent(\n roomId, \"m.room.message\", content, txnId, callback,\n );\n};\n\n/**\n * @param {string} roomId\n * @param {string} body\n * @param {string} txnId Optional.\n * @param {module:client.callback} callback Optional.\n * @return {module:client.Promise} Resolves: TODO\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.sendTextMessage = function(roomId, body, txnId, callback) {\n const content = ContentHelpers.makeTextMessage(body);\n return this.sendMessage(roomId, content, txnId, callback);\n};\n\n/**\n * @param {string} roomId\n * @param {string} body\n * @param {string} txnId Optional.\n * @param {module:client.callback} callback Optional.\n * @return {module:client.Promise} Resolves: TODO\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.sendNotice = function(roomId, body, txnId, callback) {\n const content = ContentHelpers.makeNotice(body);\n return this.sendMessage(roomId, content, txnId, callback);\n};\n\n/**\n * @param {string} roomId\n * @param {string} body\n * @param {string} txnId Optional.\n * @param {module:client.callback} callback Optional.\n * @return {module:client.Promise} Resolves: TODO\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.sendEmoteMessage = function(roomId, body, txnId, callback) {\n const content = ContentHelpers.makeEmoteMessage(body);\n return this.sendMessage(roomId, content, txnId, callback);\n};\n\n/**\n * @param {string} roomId\n * @param {string} url\n * @param {Object} info\n * @param {string} text\n * @param {module:client.callback} callback Optional.\n * @return {module:client.Promise} Resolves: TODO\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.sendImageMessage = function(roomId, url, info, text, callback) {\n if (utils.isFunction(text)) {\n callback = text; text = undefined;\n }\n if (!text) {\n text = \"Image\";\n }\n const content = {\n msgtype: \"m.image\",\n url: url,\n info: info,\n body: text,\n };\n return this.sendMessage(roomId, content, callback);\n};\n\n/**\n * @param {string} roomId\n * @param {string} url\n * @param {Object} info\n * @param {string} text\n * @param {module:client.callback} callback Optional.\n * @return {module:client.Promise} Resolves: TODO\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.sendStickerMessage = function(roomId, url, info, text, callback) {\n if (utils.isFunction(text)) {\n callback = text; text = undefined;\n }\n if (!text) {\n text = \"Sticker\";\n }\n const content = {\n url: url,\n info: info,\n body: text,\n };\n return this.sendEvent(\n roomId, \"m.sticker\", content, callback, undefined,\n );\n};\n\n/**\n * @param {string} roomId\n * @param {string} body\n * @param {string} htmlBody\n * @param {module:client.callback} callback Optional.\n * @return {module:client.Promise} Resolves: TODO\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.sendHtmlMessage = function(roomId, body, htmlBody, callback) {\n const content = ContentHelpers.makeHtmlMessage(body, htmlBody);\n return this.sendMessage(roomId, content, callback);\n};\n\n/**\n * @param {string} roomId\n * @param {string} body\n * @param {string} htmlBody\n * @param {module:client.callback} callback Optional.\n * @return {module:client.Promise} Resolves: TODO\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.sendHtmlNotice = function(roomId, body, htmlBody, callback) {\n const content = ContentHelpers.makeHtmlNotice(body, htmlBody);\n return this.sendMessage(roomId, content, callback);\n};\n\n/**\n * @param {string} roomId\n * @param {string} body\n * @param {string} htmlBody\n * @param {module:client.callback} callback Optional.\n * @return {module:client.Promise} Resolves: TODO\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.sendHtmlEmote = function(roomId, body, htmlBody, callback) {\n const content = ContentHelpers.makeHtmlEmote(body, htmlBody);\n return this.sendMessage(roomId, content, callback);\n};\n\n/**\n * Send a receipt.\n * @param {Event} event The event being acknowledged\n * @param {string} receiptType The kind of receipt e.g. \"m.read\"\n * @param {module:client.callback} callback Optional.\n * @return {module:client.Promise} Resolves: TODO\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.sendReceipt = function(event, receiptType, callback) {\n if (this.isGuest()) {\n return Promise.resolve({}); // guests cannot send receipts so don't bother.\n }\n\n const path = utils.encodeUri(\"/rooms/$roomId/receipt/$receiptType/$eventId\", {\n $roomId: event.getRoomId(),\n $receiptType: receiptType,\n $eventId: event.getId(),\n });\n const promise = this._http.authedRequest(\n callback, \"POST\", path, undefined, {},\n );\n\n const room = this.getRoom(event.getRoomId());\n if (room) {\n room._addLocalEchoReceipt(this.credentials.userId, event, receiptType);\n }\n return promise;\n};\n\n/**\n * Send a read receipt.\n * @param {Event} event The event that has been read.\n * @param {module:client.callback} callback Optional.\n * @return {module:client.Promise} Resolves: TODO\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.sendReadReceipt = function(event, callback) {\n return this.sendReceipt(event, \"m.read\", callback);\n};\n\n/**\n * Set a marker to indicate the point in a room before which the user has read every\n * event. This can be retrieved from room account data (the event type is `m.fully_read`)\n * and displayed as a horizontal line in the timeline that is visually distinct to the\n * position of the user's own read receipt.\n * @param {string} roomId ID of the room that has been read\n * @param {string} eventId ID of the event that has been read\n * @param {string} rrEvent the event tracked by the read receipt. This is here for\n * convenience because the RR and the RM are commonly updated at the same time as each\n * other. The local echo of this receipt will be done if set. Optional.\n * @return {module:client.Promise} Resolves: the empty object, {}.\n */\nMatrixClient.prototype.setRoomReadMarkers = function(roomId, eventId, rrEvent) {\n const rmEventId = eventId;\n let rrEventId;\n\n // Add the optional RR update, do local echo like `sendReceipt`\n if (rrEvent) {\n rrEventId = rrEvent.getId();\n const room = this.getRoom(roomId);\n if (room) {\n room._addLocalEchoReceipt(this.credentials.userId, rrEvent, \"m.read\");\n }\n }\n\n return this.setRoomReadMarkersHttpRequest(roomId, rmEventId, rrEventId);\n};\n\n/**\n * Get a preview of the given URL as of (roughly) the given point in time,\n * described as an object with OpenGraph keys and associated values.\n * Attributes may be synthesized where actual OG metadata is lacking.\n * Caches results to prevent hammering the server.\n * @param {string} url The URL to get preview data for\n * @param {Number} ts The preferred point in time that the preview should\n * describe (ms since epoch). The preview returned will either be the most\n * recent one preceding this timestamp if available, or failing that the next\n * most recent available preview.\n * @param {module:client.callback} callback Optional.\n * @return {module:client.Promise} Resolves: Object of OG metadata.\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n * May return synthesized attributes if the URL lacked OG meta.\n */\nMatrixClient.prototype.getUrlPreview = function(url, ts, callback) {\n const key = ts + \"_\" + url;\n const og = this.urlPreviewCache[key];\n if (og) {\n return Promise.resolve(og);\n }\n\n const self = this;\n return this._http.authedRequestWithPrefix(\n callback, \"GET\", \"/preview_url\", {\n url: url,\n ts: ts,\n }, undefined, httpApi.PREFIX_MEDIA_R0,\n ).then(function(response) {\n // TODO: expire cache occasionally\n self.urlPreviewCache[key] = response;\n return response;\n });\n};\n\n/**\n * @param {string} roomId\n * @param {boolean} isTyping\n * @param {Number} timeoutMs\n * @param {module:client.callback} callback Optional.\n * @return {module:client.Promise} Resolves: TODO\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.sendTyping = function(roomId, isTyping, timeoutMs, callback) {\n if (this.isGuest()) {\n return Promise.resolve({}); // guests cannot send typing notifications so don't bother.\n }\n\n const path = utils.encodeUri(\"/rooms/$roomId/typing/$userId\", {\n $roomId: roomId,\n $userId: this.credentials.userId,\n });\n const data = {\n typing: isTyping,\n };\n if (isTyping) {\n data.timeout = timeoutMs ? timeoutMs : 20000;\n }\n return this._http.authedRequest(\n callback, \"PUT\", path, undefined, data,\n );\n};\n\n/**\n * @param {string} roomId\n * @param {string} userId\n * @param {module:client.callback} callback Optional.\n * @return {module:client.Promise} Resolves: TODO\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.invite = function(roomId, userId, callback) {\n return _membershipChange(this, roomId, userId, \"invite\", undefined,\n callback);\n};\n\n/**\n * Invite a user to a room based on their email address.\n * @param {string} roomId The room to invite the user to.\n * @param {string} email The email address to invite.\n * @param {module:client.callback} callback Optional.\n * @return {module:client.Promise} Resolves: TODO\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.inviteByEmail = function(roomId, email, callback) {\n return this.inviteByThreePid(\n roomId, \"email\", email, callback,\n );\n};\n\n/**\n * Invite a user to a room based on a third-party identifier.\n * @param {string} roomId The room to invite the user to.\n * @param {string} medium The medium to invite the user e.g. \"email\".\n * @param {string} address The address for the specified medium.\n * @param {module:client.callback} callback Optional.\n * @return {module:client.Promise} Resolves: TODO\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.inviteByThreePid = function(roomId, medium, address, callback) {\n const path = utils.encodeUri(\n \"/rooms/$roomId/invite\",\n { $roomId: roomId },\n );\n\n const identityServerUrl = this.getIdentityServerUrl(true);\n if (!identityServerUrl) {\n return Promise.reject(new MatrixError({\n error: \"No supplied identity server URL\",\n errcode: \"ORG.MATRIX.JSSDK_MISSING_PARAM\",\n }));\n }\n\n return this._http.authedRequest(callback, \"POST\", path, undefined, {\n id_server: identityServerUrl,\n medium: medium,\n address: address,\n });\n};\n\n/**\n * @param {string} roomId\n * @param {module:client.callback} callback Optional.\n * @return {module:client.Promise} Resolves: TODO\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.leave = function(roomId, callback) {\n return _membershipChange(this, roomId, undefined, \"leave\", undefined,\n callback);\n};\n\n/**\n * @param {string} roomId\n * @param {string} userId\n * @param {string} reason Optional.\n * @param {module:client.callback} callback Optional.\n * @return {module:client.Promise} Resolves: TODO\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.ban = function(roomId, userId, reason, callback) {\n return _membershipChange(this, roomId, userId, \"ban\", reason,\n callback);\n};\n\n/**\n * @param {string} roomId\n * @param {boolean} deleteRoom True to delete the room from the store on success.\n * Default: true.\n * @param {module:client.callback} callback Optional.\n * @return {module:client.Promise} Resolves: TODO\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.forget = function(roomId, deleteRoom, callback) {\n if (deleteRoom === undefined) {\n deleteRoom = true;\n }\n const promise = _membershipChange(this, roomId, undefined, \"forget\", undefined,\n callback);\n if (!deleteRoom) {\n return promise;\n }\n const self = this;\n return promise.then(function(response) {\n self.store.removeRoom(roomId);\n self.emit(\"deleteRoom\", roomId);\n return response;\n });\n};\n\n/**\n * @param {string} roomId\n * @param {string} userId\n * @param {module:client.callback} callback Optional.\n * @return {module:client.Promise} Resolves: Object (currently empty)\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.unban = function(roomId, userId, callback) {\n // unbanning != set their state to leave: this used to be\n // the case, but was then changed so that leaving was always\n // a revoking of priviledge, otherwise two people racing to\n // kick / ban someone could end up banning and then un-banning\n // them.\n const path = utils.encodeUri(\"/rooms/$roomId/unban\", {\n $roomId: roomId,\n });\n const data = {\n user_id: userId,\n };\n return this._http.authedRequest(\n callback, \"POST\", path, undefined, data,\n );\n};\n\n/**\n * @param {string} roomId\n * @param {string} userId\n * @param {string} reason Optional.\n * @param {module:client.callback} callback Optional.\n * @return {module:client.Promise} Resolves: TODO\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.kick = function(roomId, userId, reason, callback) {\n return _setMembershipState(\n this, roomId, userId, \"leave\", reason, callback,\n );\n};\n\n/**\n * This is an internal method.\n * @param {MatrixClient} client\n * @param {string} roomId\n * @param {string} userId\n * @param {string} membershipValue\n * @param {string} reason\n * @param {module:client.callback} callback Optional.\n * @return {module:client.Promise} Resolves: TODO\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nfunction _setMembershipState(client, roomId, userId, membershipValue, reason,\n callback) {\n if (utils.isFunction(reason)) {\n callback = reason; reason = undefined;\n }\n\n const path = utils.encodeUri(\n \"/rooms/$roomId/state/m.room.member/$userId\",\n { $roomId: roomId, $userId: userId},\n );\n\n return client._http.authedRequest(callback, \"PUT\", path, undefined, {\n membership: membershipValue,\n reason: reason,\n });\n}\n\n/**\n * This is an internal method.\n * @param {MatrixClient} client\n * @param {string} roomId\n * @param {string} userId\n * @param {string} membership\n * @param {string} reason\n * @param {module:client.callback} callback Optional.\n * @return {module:client.Promise} Resolves: TODO\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nfunction _membershipChange(client, roomId, userId, membership, reason, callback) {\n if (utils.isFunction(reason)) {\n callback = reason; reason = undefined;\n }\n\n const path = utils.encodeUri(\"/rooms/$room_id/$membership\", {\n $room_id: roomId,\n $membership: membership,\n });\n return client._http.authedRequest(\n callback, \"POST\", path, undefined, {\n user_id: userId, // may be undefined e.g. on leave\n reason: reason,\n },\n );\n}\n\n/**\n * Obtain a dict of actions which should be performed for this event according\n * to the push rules for this user. Caches the dict on the event.\n * @param {MatrixEvent} event The event to get push actions for.\n * @return {module:pushprocessor~PushAction} A dict of actions to perform.\n */\nMatrixClient.prototype.getPushActionsForEvent = function(event) {\n if (!event.getPushActions()) {\n event.setPushActions(this._pushProcessor.actionsForEvent(event));\n }\n return event.getPushActions();\n};\n\n// Profile operations\n// ==================\n\n/**\n * @param {string} info The kind of info to set (e.g. 'avatar_url')\n * @param {Object} data The JSON object to set.\n * @param {module:client.callback} callback Optional.\n * @return {module:client.Promise} Resolves: TODO\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.setProfileInfo = function(info, data, callback) {\n const path = utils.encodeUri(\"/profile/$userId/$info\", {\n $userId: this.credentials.userId,\n $info: info,\n });\n return this._http.authedRequest(\n callback, \"PUT\", path, undefined, data,\n );\n};\n\n/**\n * @param {string} name\n * @param {module:client.callback} callback Optional.\n * @return {module:client.Promise} Resolves: TODO\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.setDisplayName = function(name, callback) {\n return this.setProfileInfo(\n \"displayname\", { displayname: name }, callback,\n );\n};\n\n/**\n * @param {string} url\n * @param {module:client.callback} callback Optional.\n * @return {module:client.Promise} Resolves: TODO\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.setAvatarUrl = function(url, callback) {\n return this.setProfileInfo(\n \"avatar_url\", { avatar_url: url }, callback,\n );\n};\n\n/**\n * Turn an MXC URL into an HTTP one. <strong>This method is experimental and\n * may change.</strong>\n * @param {string} mxcUrl The MXC URL\n * @param {Number} width The desired width of the thumbnail.\n * @param {Number} height The desired height of the thumbnail.\n * @param {string} resizeMethod The thumbnail resize method to use, either\n * \"crop\" or \"scale\".\n * @param {Boolean} allowDirectLinks If true, return any non-mxc URLs\n * directly. Fetching such URLs will leak information about the user to\n * anyone they share a room with. If false, will return null for such URLs.\n * @return {?string} the avatar URL or null.\n */\nMatrixClient.prototype.mxcUrlToHttp =\n function(mxcUrl, width, height, resizeMethod, allowDirectLinks) {\n return contentRepo.getHttpUriForMxc(\n this.baseUrl, mxcUrl, width, height, resizeMethod, allowDirectLinks,\n );\n};\n\n/**\n * @param {Object} opts Options to apply\n * @param {string} opts.presence One of \"online\", \"offline\" or \"unavailable\"\n * @param {string} opts.status_msg The status message to attach.\n * @param {module:client.callback} callback Optional.\n * @return {module:client.Promise} Resolves: TODO\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n * @throws If 'presence' isn't a valid presence enum value.\n */\nMatrixClient.prototype.setPresence = function(opts, callback) {\n const path = utils.encodeUri(\"/presence/$userId/status\", {\n $userId: this.credentials.userId,\n });\n\n if (typeof opts === \"string\") {\n opts = { presence: opts };\n }\n\n const validStates = [\"offline\", \"online\", \"unavailable\"];\n if (validStates.indexOf(opts.presence) == -1) {\n throw new Error(\"Bad presence value: \" + opts.presence);\n }\n return this._http.authedRequest(\n callback, \"PUT\", path, undefined, opts,\n );\n};\n\nfunction _presenceList(callback, client, opts, method) {\n const path = utils.encodeUri(\"/presence/list/$userId\", {\n $userId: client.credentials.userId,\n });\n return client._http.authedRequest(callback, method, path, undefined, opts);\n}\n\n/**\n* Retrieve current user presence list.\n* @param {module:client.callback} callback Optional.\n* @return {module:client.Promise} Resolves: TODO\n* @return {module:http-api.MatrixError} Rejects: with an error response.\n*/\nMatrixClient.prototype.getPresenceList = function(callback) {\n return _presenceList(callback, this, undefined, \"GET\");\n};\n\n/**\n* Add users to the current user presence list.\n* @param {module:client.callback} callback Optional.\n* @param {string[]} userIds\n* @return {module:client.Promise} Resolves: TODO\n* @return {module:http-api.MatrixError} Rejects: with an error response.\n*/\nMatrixClient.prototype.inviteToPresenceList = function(callback, userIds) {\n const opts = {\"invite\": userIds};\n return _presenceList(callback, this, opts, \"POST\");\n};\n\n/**\n* Drop users from the current user presence list.\n* @param {module:client.callback} callback Optional.\n* @param {string[]} userIds\n* @return {module:client.Promise} Resolves: TODO\n* @return {module:http-api.MatrixError} Rejects: with an error response.\n**/\nMatrixClient.prototype.dropFromPresenceList = function(callback, userIds) {\n const opts = {\"drop\": userIds};\n return _presenceList(callback, this, opts, \"POST\");\n};\n\n/**\n * Retrieve older messages from the given room and put them in the timeline.\n *\n * If this is called multiple times whilst a request is ongoing, the <i>same</i>\n * Promise will be returned. If there was a problem requesting scrollback, there\n * will be a small delay before another request can be made (to prevent tight-looping\n * when there is no connection).\n *\n * @param {Room} room The room to get older messages in.\n * @param {Integer} limit Optional. The maximum number of previous events to\n * pull in. Default: 30.\n * @param {module:client.callback} callback Optional.\n * @return {module:client.Promise} Resolves: Room. If you are at the beginning\n * of the timeline, <code>Room.oldState.paginationToken</code> will be\n * <code>null</code>.\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.scrollback = function(room, limit, callback) {\n if (utils.isFunction(limit)) {\n callback = limit; limit = undefined;\n }\n limit = limit || 30;\n let timeToWaitMs = 0;\n\n let info = this._ongoingScrollbacks[room.roomId] || {};\n if (info.promise) {\n return info.promise;\n } else if (info.errorTs) {\n const timeWaitedMs = Date.now() - info.errorTs;\n timeToWaitMs = Math.max(SCROLLBACK_DELAY_MS - timeWaitedMs, 0);\n }\n\n if (room.oldState.paginationToken === null) {\n return Promise.resolve(room); // already at the start.\n }\n // attempt to grab more events from the store first\n const numAdded = this.store.scrollback(room, limit).length;\n if (numAdded === limit) {\n // store contained everything we needed.\n return Promise.resolve(room);\n }\n // reduce the required number of events appropriately\n limit = limit - numAdded;\n\n const path = utils.encodeUri(\n \"/rooms/$roomId/messages\", {$roomId: room.roomId},\n );\n const params = {\n from: room.oldState.paginationToken,\n limit: limit,\n dir: 'b',\n };\n const defer = Promise.defer();\n info = {\n promise: defer.promise,\n errorTs: null,\n };\n const self = this;\n // wait for a time before doing this request\n // (which may be 0 in order not to special case the code paths)\n Promise.delay(timeToWaitMs).then(function() {\n return self._http.authedRequest(callback, \"GET\", path, params);\n }).done(function(res) {\n const matrixEvents = utils.map(res.chunk, _PojoToMatrixEventMapper(self));\n room.addEventsToTimeline(matrixEvents, true, room.getLiveTimeline());\n room.oldState.paginationToken = res.end;\n if (res.chunk.length === 0) {\n room.oldState.paginationToken = null;\n }\n self.store.storeEvents(room, matrixEvents, res.end, true);\n self._ongoingScrollbacks[room.roomId] = null;\n _resolve(callback, defer, room);\n }, function(err) {\n self._ongoingScrollbacks[room.roomId] = {\n errorTs: Date.now(),\n };\n _reject(callback, defer, err);\n });\n this._ongoingScrollbacks[room.roomId] = info;\n return defer.promise;\n};\n\n/**\n * Take an EventContext, and back/forward-fill results.\n *\n * @param {module:models/event-context.EventContext} eventContext context\n * object to be updated\n * @param {Object} opts\n * @param {boolean} opts.backwards true to fill backwards, false to go forwards\n * @param {boolean} opts.limit number of events to request\n *\n * @return {module:client.Promise} Resolves: updated EventContext object\n * @return {Error} Rejects: with an error response.\n */\nMatrixClient.prototype.paginateEventContext = function(eventContext, opts) {\n // TODO: we should implement a backoff (as per scrollback()) to deal more\n // nicely with HTTP errors.\n opts = opts || {};\n const backwards = opts.backwards || false;\n\n const token = eventContext.getPaginateToken(backwards);\n if (!token) {\n // no more results.\n return Promise.reject(new Error(\"No paginate token\"));\n }\n\n const dir = backwards ? 'b' : 'f';\n const pendingRequest = eventContext._paginateRequests[dir];\n\n if (pendingRequest) {\n // already a request in progress - return the existing promise\n return pendingRequest;\n }\n\n const path = utils.encodeUri(\n \"/rooms/$roomId/messages\", {$roomId: eventContext.getEvent().getRoomId()},\n );\n const params = {\n from: token,\n limit: ('limit' in opts) ? opts.limit : 30,\n dir: dir,\n };\n\n const self = this;\n const promise =\n self._http.authedRequest(undefined, \"GET\", path, params,\n ).then(function(res) {\n let token = res.end;\n if (res.chunk.length === 0) {\n token = null;\n } else {\n const matrixEvents = utils.map(res.chunk, self.getEventMapper());\n if (backwards) {\n // eventContext expects the events in timeline order, but\n // back-pagination returns them in reverse order.\n matrixEvents.reverse();\n }\n eventContext.addEvents(matrixEvents, backwards);\n }\n eventContext.setPaginateToken(token, backwards);\n return eventContext;\n }).finally(function() {\n eventContext._paginateRequests[dir] = null;\n });\n eventContext._paginateRequests[dir] = promise;\n\n return promise;\n};\n\n/**\n * Get an EventTimeline for the given event\n *\n * <p>If the EventTimelineSet object already has the given event in its store, the\n * corresponding timeline will be returned. Otherwise, a /context request is\n * made, and used to construct an EventTimeline.\n *\n * @param {EventTimelineSet} timelineSet The timelineSet to look for the event in\n * @param {string} eventId The ID of the event to look for\n *\n * @return {module:client.Promise} Resolves:\n * {@link module:models/event-timeline~EventTimeline} including the given\n * event\n */\nMatrixClient.prototype.getEventTimeline = function(timelineSet, eventId) {\n // don't allow any timeline support unless it's been enabled.\n if (!this.timelineSupport) {\n throw new Error(\"timeline support is disabled. Set the 'timelineSupport'\" +\n \" parameter to true when creating MatrixClient to enable\" +\n \" it.\");\n }\n\n if (timelineSet.getTimelineForEvent(eventId)) {\n return Promise.resolve(timelineSet.getTimelineForEvent(eventId));\n }\n\n const path = utils.encodeUri(\n \"/rooms/$roomId/context/$eventId\", {\n $roomId: timelineSet.room.roomId,\n $eventId: eventId,\n },\n );\n\n // TODO: we should implement a backoff (as per scrollback()) to deal more\n // nicely with HTTP errors.\n const self = this;\n const promise =\n self._http.authedRequest(undefined, \"GET\", path,\n ).then(function(res) {\n if (!res.event) {\n throw new Error(\"'event' not in '/context' result - homeserver too old?\");\n }\n\n // by the time the request completes, the event might have ended up in\n // the timeline.\n if (timelineSet.getTimelineForEvent(eventId)) {\n return timelineSet.getTimelineForEvent(eventId);\n }\n\n // we start with the last event, since that's the point at which we\n // have known state.\n // events_after is already backwards; events_before is forwards.\n res.events_after.reverse();\n const events = res.events_after\n .concat([res.event])\n .concat(res.events_before);\n const matrixEvents = utils.map(events, self.getEventMapper());\n\n let timeline = timelineSet.getTimelineForEvent(matrixEvents[0].getId());\n if (!timeline) {\n timeline = timelineSet.addTimeline();\n timeline.initialiseState(utils.map(res.state,\n self.getEventMapper()));\n timeline.getState(EventTimeline.FORWARDS).paginationToken = res.end;\n }\n timelineSet.addEventsToTimeline(matrixEvents, true, timeline, res.start);\n\n // there is no guarantee that the event ended up in \"timeline\" (we\n // might have switched to a neighbouring timeline) - so check the\n // room's index again. On the other hand, there's no guarantee the\n // event ended up anywhere, if it was later redacted, so we just\n // return the timeline we first thought of.\n const tl = timelineSet.getTimelineForEvent(eventId) || timeline;\n return tl;\n });\n return promise;\n};\n\n\n/**\n * Take an EventTimeline, and back/forward-fill results.\n *\n * @param {module:models/event-timeline~EventTimeline} eventTimeline timeline\n * object to be updated\n * @param {Object} [opts]\n * @param {bool} [opts.backwards = false] true to fill backwards,\n * false to go forwards\n * @param {number} [opts.limit = 30] number of events to request\n *\n * @return {module:client.Promise} Resolves to a boolean: false if there are no\n * events and we reached either end of the timeline; else true.\n */\nMatrixClient.prototype.paginateEventTimeline = function(eventTimeline, opts) {\n const isNotifTimeline = (eventTimeline.getTimelineSet() === this._notifTimelineSet);\n\n // TODO: we should implement a backoff (as per scrollback()) to deal more\n // nicely with HTTP errors.\n opts = opts || {};\n const backwards = opts.backwards || false;\n\n if (isNotifTimeline) {\n if (!backwards) {\n throw new Error(\"paginateNotifTimeline can only paginate backwards\");\n }\n }\n\n const dir = backwards ? EventTimeline.BACKWARDS : EventTimeline.FORWARDS;\n\n const token = eventTimeline.getPaginationToken(dir);\n if (!token) {\n // no token - no results.\n return Promise.resolve(false);\n }\n\n const pendingRequest = eventTimeline._paginationRequests[dir];\n\n if (pendingRequest) {\n // already a request in progress - return the existing promise\n return pendingRequest;\n }\n\n let path, params, promise;\n const self = this;\n\n if (isNotifTimeline) {\n path = \"/notifications\";\n params = {\n limit: ('limit' in opts) ? opts.limit : 30,\n only: 'highlight',\n };\n\n if (token && token !== \"end\") {\n params.from = token;\n }\n\n promise =\n this._http.authedRequestWithPrefix(undefined, \"GET\", path, params,\n undefined, httpApi.PREFIX_UNSTABLE,\n ).then(function(res) {\n const token = res.next_token;\n const matrixEvents = [];\n\n for (let i = 0; i < res.notifications.length; i++) {\n const notification = res.notifications[i];\n const event = self.getEventMapper()(notification.event);\n event.setPushActions(\n PushProcessor.actionListToActionsObject(notification.actions),\n );\n event.event.room_id = notification.room_id; // XXX: gutwrenching\n matrixEvents[i] = event;\n }\n\n eventTimeline.getTimelineSet()\n .addEventsToTimeline(matrixEvents, backwards, eventTimeline, token);\n\n // if we've hit the end of the timeline, we need to stop trying to\n // paginate. We need to keep the 'forwards' token though, to make sure\n // we can recover from gappy syncs.\n if (backwards && !res.next_token) {\n eventTimeline.setPaginationToken(null, dir);\n }\n return res.next_token ? true : false;\n }).finally(function() {\n eventTimeline._paginationRequests[dir] = null;\n });\n eventTimeline._paginationRequests[dir] = promise;\n } else {\n const room = this.getRoom(eventTimeline.getRoomId());\n if (!room) {\n throw new Error(\"Unknown room \" + eventTimeline.getRoomId());\n }\n\n path = utils.encodeUri(\n \"/rooms/$roomId/messages\", {$roomId: eventTimeline.getRoomId()},\n );\n params = {\n from: token,\n limit: ('limit' in opts) ? opts.limit : 30,\n dir: dir,\n };\n\n const filter = eventTimeline.getFilter();\n if (filter) {\n // XXX: it's horrific that /messages' filter parameter doesn't match\n // /sync's one - see https://matrix.org/jira/browse/SPEC-451\n params.filter = JSON.stringify(filter.getRoomTimelineFilterComponent());\n }\n\n promise =\n this._http.authedRequest(undefined, \"GET\", path, params,\n ).then(function(res) {\n const token = res.end;\n const matrixEvents = utils.map(res.chunk, self.getEventMapper());\n eventTimeline.getTimelineSet()\n .addEventsToTimeline(matrixEvents, backwards, eventTimeline, token);\n\n // if we've hit the end of the timeline, we need to stop trying to\n // paginate. We need to keep the 'forwards' token though, to make sure\n // we can recover from gappy syncs.\n if (backwards && res.end == res.start) {\n eventTimeline.setPaginationToken(null, dir);\n }\n return res.end != res.start;\n }).finally(function() {\n eventTimeline._paginationRequests[dir] = null;\n });\n eventTimeline._paginationRequests[dir] = promise;\n }\n\n return promise;\n};\n\n/**\n * Reset the notifTimelineSet entirely, paginating in some historical notifs as\n * a starting point for subsequent pagination.\n */\nMatrixClient.prototype.resetNotifTimelineSet = function() {\n if (!this._notifTimelineSet) {\n return;\n }\n\n // FIXME: This thing is a total hack, and results in duplicate events being\n // added to the timeline both from /sync and /notifications, and lots of\n // slow and wasteful processing and pagination. The correct solution is to\n // extend /messages or /search or something to filter on notifications.\n\n // use the fictitious token 'end'. in practice we would ideally give it\n // the oldest backwards pagination token from /sync, but /sync doesn't\n // know about /notifications, so we have no choice but to start paginating\n // from the current point in time. This may well overlap with historical\n // notifs which are then inserted into the timeline by /sync responses.\n this._notifTimelineSet.resetLiveTimeline('end', null);\n\n // we could try to paginate a single event at this point in order to get\n // a more valid pagination token, but it just ends up with an out of order\n // timeline. given what a mess this is and given we're going to have duplicate\n // events anyway, just leave it with the dummy token for now.\n /*\n this.paginateNotifTimeline(this._notifTimelineSet.getLiveTimeline(), {\n backwards: true,\n limit: 1\n });\n */\n};\n\n/**\n * Peek into a room and receive updates about the room. This only works if the\n * history visibility for the room is world_readable.\n * @param {String} roomId The room to attempt to peek into.\n * @return {module:client.Promise} Resolves: Room object\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.peekInRoom = function(roomId) {\n if (this._peekSync) {\n this._peekSync.stopPeeking();\n }\n this._peekSync = new SyncApi(this, this._clientOpts);\n return this._peekSync.peek(roomId);\n};\n\n/**\n * Stop any ongoing room peeking.\n */\nMatrixClient.prototype.stopPeeking = function() {\n if (this._peekSync) {\n this._peekSync.stopPeeking();\n this._peekSync = null;\n }\n};\n\n/**\n * Set r/w flags for guest access in a room.\n * @param {string} roomId The room to configure guest access in.\n * @param {Object} opts Options\n * @param {boolean} opts.allowJoin True to allow guests to join this room. This\n * implicitly gives guests write access. If false or not given, guests are\n * explicitly forbidden from joining the room.\n * @param {boolean} opts.allowRead True to set history visibility to\n * be world_readable. This gives guests read access *from this point forward*.\n * If false or not given, history visibility is not modified.\n * @return {module:client.Promise} Resolves: TODO\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.setGuestAccess = function(roomId, opts) {\n const writePromise = this.sendStateEvent(roomId, \"m.room.guest_access\", {\n guest_access: opts.allowJoin ? \"can_join\" : \"forbidden\",\n });\n\n let readPromise = Promise.resolve();\n if (opts.allowRead) {\n readPromise = this.sendStateEvent(roomId, \"m.room.history_visibility\", {\n history_visibility: \"world_readable\",\n });\n }\n\n return Promise.all([readPromise, writePromise]);\n};\n\n// Registration/Login operations\n// =============================\n\n/**\n * Requests an email verification token for the purposes of registration.\n * This API proxies the Identity Server /validate/email/requestToken API,\n * adding registration-specific behaviour. Specifically, if an account with\n * the given email address already exists, it will either send an email\n * to the address informing them of this or return M_THREEPID_IN_USE\n * (which one is up to the Home Server).\n *\n * requestEmailToken calls the equivalent API directly on the ID server,\n * therefore bypassing the registration-specific logic.\n *\n * Parameters and return value are as for requestEmailToken\n\n * @param {string} email As requestEmailToken\n * @param {string} clientSecret As requestEmailToken\n * @param {number} sendAttempt As requestEmailToken\n * @param {string} nextLink As requestEmailToken\n * @return {module:client.Promise} Resolves: As requestEmailToken\n */\nMatrixClient.prototype.requestRegisterEmailToken = function(email, clientSecret,\n sendAttempt, nextLink) {\n return this._requestTokenFromEndpoint(\n \"/register/email/requestToken\",\n {\n email: email,\n client_secret: clientSecret,\n send_attempt: sendAttempt,\n next_link: nextLink,\n },\n );\n};\n\n/**\n * Requests a text message verification token for the purposes of registration.\n * This API proxies the Identity Server /validate/msisdn/requestToken API,\n * adding registration-specific behaviour, as with requestRegisterEmailToken.\n *\n * @param {string} phoneCountry The ISO 3166-1 alpha-2 code for the country in which\n * phoneNumber should be parsed relative to.\n * @param {string} phoneNumber The phone number, in national or international format\n * @param {string} clientSecret As requestEmailToken\n * @param {number} sendAttempt As requestEmailToken\n * @param {string} nextLink As requestEmailToken\n * @return {module:client.Promise} Resolves: As requestEmailToken\n */\nMatrixClient.prototype.requestRegisterMsisdnToken = function(phoneCountry, phoneNumber,\n clientSecret, sendAttempt, nextLink) {\n return this._requestTokenFromEndpoint(\n \"/register/msisdn/requestToken\",\n {\n country: phoneCountry,\n phone_number: phoneNumber,\n client_secret: clientSecret,\n send_attempt: sendAttempt,\n next_link: nextLink,\n },\n );\n};\n\n/**\n * Requests an email verification token for the purposes of adding a\n * third party identifier to an account.\n * This API proxies the Identity Server /validate/email/requestToken API,\n * adding specific behaviour for the addition of email addresses to an\n * account. Specifically, if an account with\n * the given email address already exists, it will either send an email\n * to the address informing them of this or return M_THREEPID_IN_USE\n * (which one is up to the Home Server).\n *\n * requestEmailToken calls the equivalent API directly on the ID server,\n * therefore bypassing the email addition specific logic.\n *\n * @param {string} email As requestEmailToken\n * @param {string} clientSecret As requestEmailToken\n * @param {number} sendAttempt As requestEmailToken\n * @param {string} nextLink As requestEmailToken\n * @return {module:client.Promise} Resolves: As requestEmailToken\n */\nMatrixClient.prototype.requestAdd3pidEmailToken = function(email, clientSecret,\n sendAttempt, nextLink) {\n return this._requestTokenFromEndpoint(\n \"/account/3pid/email/requestToken\",\n {\n email: email,\n client_secret: clientSecret,\n send_attempt: sendAttempt,\n next_link: nextLink,\n },\n );\n};\n\n/**\n * Requests a text message verification token for the purposes of adding a\n * third party identifier to an account.\n * This API proxies the Identity Server /validate/email/requestToken API,\n * adding specific behaviour for the addition of phone numbers to an\n * account, as requestAdd3pidEmailToken.\n *\n * @param {string} phoneCountry As requestRegisterMsisdnToken\n * @param {string} phoneNumber As requestRegisterMsisdnToken\n * @param {string} clientSecret As requestEmailToken\n * @param {number} sendAttempt As requestEmailToken\n * @param {string} nextLink As requestEmailToken\n * @return {module:client.Promise} Resolves: As requestEmailToken\n */\nMatrixClient.prototype.requestAdd3pidMsisdnToken = function(phoneCountry, phoneNumber,\n clientSecret, sendAttempt, nextLink) {\n return this._requestTokenFromEndpoint(\n \"/account/3pid/msisdn/requestToken\",\n {\n country: phoneCountry,\n phone_number: phoneNumber,\n client_secret: clientSecret,\n send_attempt: sendAttempt,\n next_link: nextLink,\n },\n );\n};\n\n/**\n * Requests an email verification token for the purposes of resetting\n * the password on an account.\n * This API proxies the Identity Server /validate/email/requestToken API,\n * adding specific behaviour for the password resetting. Specifically,\n * if no account with the given email address exists, it may either\n * return M_THREEPID_NOT_FOUND or send an email\n * to the address informing them of this (which one is up to the Home Server).\n *\n * requestEmailToken calls the equivalent API directly on the ID server,\n * therefore bypassing the password reset specific logic.\n *\n * @param {string} email As requestEmailToken\n * @param {string} clientSecret As requestEmailToken\n * @param {number} sendAttempt As requestEmailToken\n * @param {string} nextLink As requestEmailToken\n * @param {module:client.callback} callback Optional. As requestEmailToken\n * @return {module:client.Promise} Resolves: As requestEmailToken\n */\nMatrixClient.prototype.requestPasswordEmailToken = function(email, clientSecret,\n sendAttempt, nextLink) {\n return this._requestTokenFromEndpoint(\n \"/account/password/email/requestToken\",\n {\n email: email,\n client_secret: clientSecret,\n send_attempt: sendAttempt,\n next_link: nextLink,\n },\n );\n};\n\n/**\n * Requests a text message verification token for the purposes of resetting\n * the password on an account.\n * This API proxies the Identity Server /validate/email/requestToken API,\n * adding specific behaviour for the password resetting, as requestPasswordEmailToken.\n *\n * @param {string} phoneCountry As requestRegisterMsisdnToken\n * @param {string} phoneNumber As requestRegisterMsisdnToken\n * @param {string} clientSecret As requestEmailToken\n * @param {number} sendAttempt As requestEmailToken\n * @param {string} nextLink As requestEmailToken\n * @return {module:client.Promise} Resolves: As requestEmailToken\n */\nMatrixClient.prototype.requestPasswordMsisdnToken = function(phoneCountry, phoneNumber,\n clientSecret, sendAttempt, nextLink) {\n return this._requestTokenFromEndpoint(\n \"/account/password/msisdn/requestToken\",\n {\n country: phoneCountry,\n phone_number: phoneNumber,\n client_secret: clientSecret,\n send_attempt: sendAttempt,\n next_link: nextLink,\n },\n );\n};\n\n/**\n * Internal utility function for requesting validation tokens from usage-specific\n * requestToken endpoints.\n *\n * @param {string} endpoint The endpoint to send the request to\n * @param {object} params Parameters for the POST request\n * @return {module:client.Promise} Resolves: As requestEmailToken\n */\nMatrixClient.prototype._requestTokenFromEndpoint = function(endpoint, params) {\n const id_server_url = url.parse(this.idBaseUrl);\n if (id_server_url.host === null) {\n throw new Error(\"Invalid ID server URL: \" + this.idBaseUrl);\n }\n\n const postParams = Object.assign({}, params, {\n id_server: id_server_url.host,\n });\n return this._http.request(\n undefined, \"POST\", endpoint, undefined,\n postParams,\n );\n};\n\n\n// Push operations\n// ===============\n\n/**\n * Get the room-kind push rule associated with a room.\n * @param {string} scope \"global\" or device-specific.\n * @param {string} roomId the id of the room.\n * @return {object} the rule or undefined.\n */\nMatrixClient.prototype.getRoomPushRule = function(scope, roomId) {\n // There can be only room-kind push rule per room\n // and its id is the room id.\n if (this.pushRules) {\n for (let i = 0; i < this.pushRules[scope].room.length; i++) {\n const rule = this.pushRules[scope].room[i];\n if (rule.rule_id === roomId) {\n return rule;\n }\n }\n } else {\n throw new Error(\n \"SyncApi.sync() must be done before accessing to push rules.\",\n );\n }\n};\n\n/**\n * Set a room-kind muting push rule in a room.\n * The operation also updates MatrixClient.pushRules at the end.\n * @param {string} scope \"global\" or device-specific.\n * @param {string} roomId the id of the room.\n * @param {string} mute the mute state.\n * @return {module:client.Promise} Resolves: result object\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.setRoomMutePushRule = function(scope, roomId, mute) {\n const self = this;\n let deferred, hasDontNotifyRule;\n\n // Get the existing room-kind push rule if any\n const roomPushRule = this.getRoomPushRule(scope, roomId);\n if (roomPushRule) {\n if (0 <= roomPushRule.actions.indexOf(\"dont_notify\")) {\n hasDontNotifyRule = true;\n }\n }\n\n if (!mute) {\n // Remove the rule only if it is a muting rule\n if (hasDontNotifyRule) {\n deferred = this.deletePushRule(scope, \"room\", roomPushRule.rule_id);\n }\n } else {\n if (!roomPushRule) {\n deferred = this.addPushRule(scope, \"room\", roomId, {\n actions: [\"dont_notify\"],\n });\n } else if (!hasDontNotifyRule) {\n // Remove the existing one before setting the mute push rule\n // This is a workaround to SYN-590 (Push rule update fails)\n deferred = Promise.defer();\n this.deletePushRule(scope, \"room\", roomPushRule.rule_id)\n .done(function() {\n self.addPushRule(scope, \"room\", roomId, {\n actions: [\"dont_notify\"],\n }).done(function() {\n deferred.resolve();\n }, function(err) {\n deferred.reject(err);\n });\n }, function(err) {\n deferred.reject(err);\n });\n\n deferred = deferred.promise;\n }\n }\n\n if (deferred) {\n // Update this.pushRules when the operation completes\n const ruleRefreshDeferred = Promise.defer();\n deferred.done(function() {\n self.getPushRules().done(function(result) {\n self.pushRules = result;\n ruleRefreshDeferred.resolve();\n }, function(err) {\n ruleRefreshDeferred.reject(err);\n });\n }, function(err) {\n // Update it even if the previous operation fails. This can help the\n // app to recover when push settings has been modifed from another client\n self.getPushRules().done(function(result) {\n self.pushRules = result;\n ruleRefreshDeferred.reject(err);\n }, function(err2) {\n ruleRefreshDeferred.reject(err);\n });\n });\n return ruleRefreshDeferred.promise;\n }\n};\n\n// Search\n// ======\n\n/**\n * Perform a server-side search for messages containing the given text.\n * @param {Object} opts Options for the search.\n * @param {string} opts.query The text to query.\n * @param {string=} opts.keys The keys to search on. Defaults to all keys. One\n * of \"content.body\", \"content.name\", \"content.topic\".\n * @param {module:client.callback} callback Optional.\n * @return {module:client.Promise} Resolves: TODO\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.searchMessageText = function(opts, callback) {\n const roomEvents = {\n search_term: opts.query,\n };\n\n if ('keys' in opts) {\n roomEvents.keys = opts.keys;\n }\n\n return this.search({\n body: {\n search_categories: {\n room_events: roomEvents,\n },\n },\n }, callback);\n};\n\n/**\n * Perform a server-side search for room events.\n *\n * The returned promise resolves to an object containing the fields:\n *\n * * {number} count: estimate of the number of results\n * * {string} next_batch: token for back-pagination; if undefined, there are\n * no more results\n * * {Array} highlights: a list of words to highlight from the stemming\n * algorithm\n * * {Array} results: a list of results\n *\n * Each entry in the results list is a {module:models/search-result.SearchResult}.\n *\n * @param {Object} opts\n * @param {string} opts.term the term to search for\n * @param {Object} opts.filter a JSON filter object to pass in the request\n * @return {module:client.Promise} Resolves: result object\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.searchRoomEvents = function(opts) {\n // TODO: support groups\n\n const body = {\n search_categories: {\n room_events: {\n search_term: opts.term,\n filter: opts.filter,\n order_by: \"recent\",\n event_context: {\n before_limit: 1,\n after_limit: 1,\n include_profile: true,\n },\n },\n },\n };\n\n const searchResults = {\n _query: body,\n results: [],\n highlights: [],\n };\n\n return this.search({body: body}).then(\n this._processRoomEventsSearch.bind(this, searchResults),\n );\n};\n\n/**\n * Take a result from an earlier searchRoomEvents call, and backfill results.\n *\n * @param {object} searchResults the results object to be updated\n * @return {module:client.Promise} Resolves: updated result object\n * @return {Error} Rejects: with an error response.\n */\nMatrixClient.prototype.backPaginateRoomEventsSearch = function(searchResults) {\n // TODO: we should implement a backoff (as per scrollback()) to deal more\n // nicely with HTTP errors.\n\n if (!searchResults.next_batch) {\n return Promise.reject(new Error(\"Cannot backpaginate event search any further\"));\n }\n\n if (searchResults.pendingRequest) {\n // already a request in progress - return the existing promise\n return searchResults.pendingRequest;\n }\n\n const searchOpts = {\n body: searchResults._query,\n next_batch: searchResults.next_batch,\n };\n\n const promise = this.search(searchOpts).then(\n this._processRoomEventsSearch.bind(this, searchResults),\n ).finally(function() {\n searchResults.pendingRequest = null;\n });\n searchResults.pendingRequest = promise;\n\n return promise;\n};\n\n/**\n * helper for searchRoomEvents and backPaginateRoomEventsSearch. Processes the\n * response from the API call and updates the searchResults\n *\n * @param {Object} searchResults\n * @param {Object} response\n * @return {Object} searchResults\n * @private\n */\nMatrixClient.prototype._processRoomEventsSearch = function(searchResults, response) {\n const room_events = response.search_categories.room_events;\n\n searchResults.count = room_events.count;\n searchResults.next_batch = room_events.next_batch;\n\n // combine the highlight list with our existing list; build an object\n // to avoid O(N^2) fail\n const highlights = {};\n room_events.highlights.forEach(function(hl) {\n highlights[hl] = 1;\n });\n searchResults.highlights.forEach(function(hl) {\n highlights[hl] = 1;\n });\n\n // turn it back into a list.\n searchResults.highlights = Object.keys(highlights);\n\n // append the new results to our existing results\n for (let i = 0; i < room_events.results.length; i++) {\n const sr = SearchResult.fromJson(room_events.results[i], this.getEventMapper());\n searchResults.results.push(sr);\n }\n return searchResults;\n};\n\n\n/**\n * Populate the store with rooms the user has left.\n * @return {module:client.Promise} Resolves: TODO - Resolved when the rooms have\n * been added to the data store.\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.syncLeftRooms = function() {\n // Guard against multiple calls whilst ongoing and multiple calls post success\n if (this._syncedLeftRooms) {\n return Promise.resolve([]); // don't call syncRooms again if it succeeded.\n }\n if (this._syncLeftRoomsPromise) {\n return this._syncLeftRoomsPromise; // return the ongoing request\n }\n const self = this;\n const syncApi = new SyncApi(this, this._clientOpts);\n this._syncLeftRoomsPromise = syncApi.syncLeftRooms();\n\n // cleanup locks\n this._syncLeftRoomsPromise.then(function(res) {\n console.log(\"Marking success of sync left room request\");\n self._syncedLeftRooms = true; // flip the bit on success\n }).finally(function() {\n self._syncLeftRoomsPromise = null; // cleanup ongoing request state\n });\n\n return this._syncLeftRoomsPromise;\n};\n\n// Filters\n// =======\n\n/**\n * Create a new filter.\n * @param {Object} content The HTTP body for the request\n * @return {Filter} Resolves to a Filter object.\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.createFilter = function(content) {\n const self = this;\n const path = utils.encodeUri(\"/user/$userId/filter\", {\n $userId: this.credentials.userId,\n });\n return this._http.authedRequest(\n undefined, \"POST\", path, undefined, content,\n ).then(function(response) {\n // persist the filter\n const filter = Filter.fromJson(\n self.credentials.userId, response.filter_id, content,\n );\n self.store.storeFilter(filter);\n return filter;\n });\n};\n\n/**\n * Retrieve a filter.\n * @param {string} userId The user ID of the filter owner\n * @param {string} filterId The filter ID to retrieve\n * @param {boolean} allowCached True to allow cached filters to be returned.\n * Default: True.\n * @return {module:client.Promise} Resolves: TODO\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.getFilter = function(userId, filterId, allowCached) {\n if (allowCached) {\n const filter = this.store.getFilter(userId, filterId);\n if (filter) {\n return Promise.resolve(filter);\n }\n }\n\n const self = this;\n const path = utils.encodeUri(\"/user/$userId/filter/$filterId\", {\n $userId: userId,\n $filterId: filterId,\n });\n\n return this._http.authedRequest(\n undefined, \"GET\", path, undefined, undefined,\n ).then(function(response) {\n // persist the filter\n const filter = Filter.fromJson(\n userId, filterId, response,\n );\n self.store.storeFilter(filter);\n return filter;\n });\n};\n\n/**\n * @param {string} filterName\n * @param {Filter} filter\n * @return {Promise<String>} Filter ID\n */\nMatrixClient.prototype.getOrCreateFilter = function(filterName, filter) {\n const filterId = this.store.getFilterIdByName(filterName);\n let promise = Promise.resolve();\n const self = this;\n\n if (filterId) {\n // check that the existing filter matches our expectations\n promise = self.getFilter(self.credentials.userId,\n filterId, true,\n ).then(function(existingFilter) {\n const oldDef = existingFilter.getDefinition();\n const newDef = filter.getDefinition();\n\n if (utils.deepCompare(oldDef, newDef)) {\n // super, just use that.\n // debuglog(\"Using existing filter ID %s: %s\", filterId,\n // JSON.stringify(oldDef));\n return Promise.resolve(filterId);\n }\n // debuglog(\"Existing filter ID %s: %s; new filter: %s\",\n // filterId, JSON.stringify(oldDef), JSON.stringify(newDef));\n self.store.setFilterIdByName(filterName, undefined);\n return undefined;\n }, function(error) {\n // Synapse currently returns the following when the filter cannot be found:\n // {\n // errcode: \"M_UNKNOWN\",\n // name: \"M_UNKNOWN\",\n // message: \"No row found\",\n // data: Object, httpStatus: 404\n // }\n if (error.httpStatus === 404 &&\n (error.errcode === \"M_UNKNOWN\" || error.errcode === \"M_NOT_FOUND\")) {\n // Clear existing filterId from localStorage\n // if it no longer exists on the server\n self.store.setFilterIdByName(filterName, undefined);\n // Return a undefined value for existingId further down the promise chain\n return undefined;\n } else {\n throw error;\n }\n });\n }\n\n return promise.then(function(existingId) {\n if (existingId) {\n return existingId;\n }\n\n // create a new filter\n return self.createFilter(filter.getDefinition(),\n ).then(function(createdFilter) {\n // debuglog(\"Created new filter ID %s: %s\", createdFilter.filterId,\n // JSON.stringify(createdFilter.getDefinition()));\n self.store.setFilterIdByName(filterName, createdFilter.filterId);\n return createdFilter.filterId;\n });\n });\n};\n\n\n/**\n * Gets a bearer token from the Home Server that the user can\n * present to a third party in order to prove their ownership\n * of the Matrix account they are logged into.\n * @return {module:client.Promise} Resolves: Token object\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.getOpenIdToken = function() {\n const path = utils.encodeUri(\"/user/$userId/openid/request_token\", {\n $userId: this.credentials.userId,\n });\n\n return this._http.authedRequest(\n undefined, \"POST\", path, undefined, {},\n );\n};\n\n\n// VoIP operations\n// ===============\n\n/**\n * @param {module:client.callback} callback Optional.\n * @return {module:client.Promise} Resolves: TODO\n * @return {module:http-api.MatrixError} Rejects: with an error response.\n */\nMatrixClient.prototype.turnServer = function(callback) {\n return this._http.authedRequest(callback, \"GET\", \"/voip/turnServer\");\n};\n\n/**\n * Get the TURN servers for this home server.\n * @return {Array<Object>} The servers or an empty list.\n */\nMatrixClient.prototype.getTurnServers = function() {\n return this._turnServers || [];\n};\n\n// Higher level APIs\n// =================\n\n// TODO: stuff to handle:\n// local echo\n// event dup suppression? - apparently we should still be doing this\n// tracking current display name / avatar per-message\n// pagination\n// re-sending (including persisting pending messages to be sent)\n// - Need a nice way to callback the app for arbitrary events like\n// displayname changes\n// due to ambiguity (or should this be on a chat-specific layer)?\n// reconnect after connectivity outages\n\n\n/**\n * High level helper method to begin syncing and poll for new events. To listen for these\n * events, add a listener for {@link module:client~MatrixClient#event:\"event\"}\n * via {@link module:client~MatrixClient#on}. Alternatively, listen for specific\n * state change events.\n * @param {Object=} opts Options to apply when syncing.\n * @param {Number=} opts.initialSyncLimit The event <code>limit=</code> to apply\n * to initial sync. Default: 8.\n * @param {Boolean=} opts.includeArchivedRooms True to put <code>archived=true</code>\n * on the <code>/initialSync</code> request. Default: false.\n * @param {Boolean=} opts.resolveInvitesToProfiles True to do /profile requests\n * on every invite event if the displayname/avatar_url is not known for this user ID.\n * Default: false.\n *\n * @param {String=} opts.pendingEventOrdering Controls where pending messages\n * appear in a room's timeline. If \"<b>chronological</b>\", messages will appear\n * in the timeline when the call to <code>sendEvent</code> was made. If\n * \"<b>detached</b>\", pending messages will appear in a separate list,\n * accessbile via {@link module:models/room#getPendingEvents}. Default:\n * \"chronological\".\n *\n * @param {Number=} opts.pollTimeout The number of milliseconds to wait on /sync.\n * Default: 30000 (30 seconds).\n *\n * @param {Filter=} opts.filter The filter to apply to /sync calls. This will override\n * the opts.initialSyncLimit, which would normally result in a timeline limit filter.\n *\n * @param {Boolean=} opts.disablePresence True to perform syncing without automatically\n * updating presence.\n */\nMatrixClient.prototype.startClient = function(opts) {\n if (this.clientRunning) {\n // client is already running.\n return;\n }\n this.clientRunning = true;\n // backwards compat for when 'opts' was 'historyLen'.\n if (typeof opts === \"number\") {\n opts = {\n initialSyncLimit: opts,\n };\n }\n\n if (this._crypto) {\n this._crypto.uploadDeviceKeys().done();\n this._crypto.start();\n }\n\n // periodically poll for turn servers if we support voip\n checkTurnServers(this);\n\n if (this._syncApi) {\n // This shouldn't happen since we thought the client was not running\n console.error(\"Still have sync object whilst not running: stopping old one\");\n this._syncApi.stop();\n }\n\n // shallow-copy the opts dict before modifying and storing it\n opts = Object.assign({}, opts);\n\n opts.crypto = this._crypto;\n opts.canResetEntireTimeline = (roomId) => {\n if (!this._canResetTimelineCallback) {\n return false;\n }\n return this._canResetTimelineCallback(roomId);\n };\n this._clientOpts = opts;\n\n this._syncApi = new SyncApi(this, opts);\n this._syncApi.sync();\n};\n\n/**\n * High level helper method to stop the client from polling and allow a\n * clean shutdown.\n */\nMatrixClient.prototype.stopClient = function() {\n console.log('stopping MatrixClient');\n\n this.clientRunning = false;\n // TODO: f.e. Room => self.store.storeRoom(room) ?\n if (this._syncApi) {\n this._syncApi.stop();\n this._syncApi = null;\n }\n if (this._crypto) {\n this._crypto.stop();\n }\n if (this._peekSync) {\n this._peekSync.stopPeeking();\n }\n global.clearTimeout(this._checkTurnServersTimeoutID);\n};\n\n/*\n * Set a function which is called when /sync returns a 'limited' response.\n * It is called with a room ID and returns a boolean. It should return 'true' if the SDK\n * can SAFELY remove events from this room. It may not be safe to remove events if there\n * are other references to the timelines for this room, e.g because the client is\n * actively viewing events in this room.\n * Default: returns false.\n * @param {Function} cb The callback which will be invoked.\n */\nMatrixClient.prototype.setCanResetTimelineCallback = function(cb) {\n this._canResetTimelineCallback = cb;\n};\n\n/**\n * Get the callback set via `setCanResetTimelineCallback`.\n * @return {?Function} The callback or null\n */\nMatrixClient.prototype.getCanResetTimelineCallback = function() {\n return this._canResetTimelineCallback;\n};\n\nfunction setupCallEventHandler(client) {\n const candidatesByCall = {\n // callId: [Candidate]\n };\n\n // Maintain a buffer of events before the client has synced for the first time.\n // This buffer will be inspected to see if we should send incoming call\n // notifications. It needs to be buffered to correctly determine if an\n // incoming call has had a matching answer/hangup.\n let callEventBuffer = [];\n let isClientPrepared = false;\n client.on(\"sync\", function(state) {\n if (state === \"PREPARED\") {\n isClientPrepared = true;\n const ignoreCallIds = {}; // Set<String>\n // inspect the buffer and mark all calls which have been answered\n // or hung up before passing them to the call event handler.\n for (let i = callEventBuffer.length - 1; i >= 0; i--) {\n const ev = callEventBuffer[i];\n if (ev.getType() === \"m.call.answer\" ||\n ev.getType() === \"m.call.hangup\") {\n ignoreCallIds[ev.getContent().call_id] = \"yep\";\n }\n }\n // now loop through the buffer chronologically and inject them\n callEventBuffer.forEach(function(e) {\n if (ignoreCallIds[e.getContent().call_id]) {\n // This call has previously been ansered or hung up: ignore it\n return;\n }\n callEventHandler(e);\n });\n callEventBuffer = [];\n }\n });\n\n client.on(\"event\", onEvent);\n\n function onEvent(event) {\n if (event.getType().indexOf(\"m.call.\") !== 0) {\n // not a call event\n if (event.isBeingDecrypted() || event.isDecryptionFailure()) {\n // not *yet* a call event, but might become one...\n event.once(\"Event.decrypted\", onEvent);\n }\n return;\n }\n if (!isClientPrepared) {\n callEventBuffer.push(event);\n return;\n }\n callEventHandler(event);\n }\n\n function callEventHandler(event) {\n const content = event.getContent();\n let call = content.call_id ? client.callList[content.call_id] : undefined;\n let i;\n //console.log(\"RECV %s content=%s\", event.getType(), JSON.stringify(content));\n\n if (event.getType() === \"m.call.invite\") {\n if (event.getSender() === client.credentials.userId) {\n return; // ignore invites you send\n }\n\n if (event.getAge() > content.lifetime) {\n return; // expired call\n }\n\n if (call && call.state === \"ended\") {\n return; // stale/old invite event\n }\n if (call) {\n console.log(\n \"WARN: Already have a MatrixCall with id %s but got an \" +\n \"invite. Clobbering.\",\n content.call_id,\n );\n }\n\n call = webRtcCall.createNewMatrixCall(client, event.getRoomId(), {\n forceTURN: client._forceTURN,\n });\n if (!call) {\n console.log(\n \"Incoming call ID \" + content.call_id + \" but this client \" +\n \"doesn't support WebRTC\",\n );\n // don't hang up the call: there could be other clients\n // connected that do support WebRTC and declining the\n // the call on their behalf would be really annoying.\n return;\n }\n\n call.callId = content.call_id;\n call._initWithInvite(event);\n client.callList[call.callId] = call;\n\n // if we stashed candidate events for that call ID, play them back now\n if (candidatesByCall[call.callId]) {\n for (i = 0; i < candidatesByCall[call.callId].length; i++) {\n call._gotRemoteIceCandidate(\n candidatesByCall[call.callId][i],\n );\n }\n }\n\n // Were we trying to call that user (room)?\n let existingCall;\n const existingCalls = utils.values(client.callList);\n for (i = 0; i < existingCalls.length; ++i) {\n const thisCall = existingCalls[i];\n if (call.roomId === thisCall.roomId &&\n thisCall.direction === 'outbound' &&\n ([\"wait_local_media\", \"create_offer\", \"invite_sent\"].indexOf(\n thisCall.state) !== -1)) {\n existingCall = thisCall;\n break;\n }\n }\n\n if (existingCall) {\n // If we've only got to wait_local_media or create_offer and\n // we've got an invite, pick the incoming call because we know\n // we haven't sent our invite yet otherwise, pick whichever\n // call has the lowest call ID (by string comparison)\n if (existingCall.state === 'wait_local_media' ||\n existingCall.state === 'create_offer' ||\n existingCall.callId > call.callId) {\n console.log(\n \"Glare detected: answering incoming call \" + call.callId +\n \" and canceling outgoing call \" + existingCall.callId,\n );\n existingCall._replacedBy(call);\n call.answer();\n } else {\n console.log(\n \"Glare detected: rejecting incoming call \" + call.callId +\n \" and keeping outgoing call \" + existingCall.callId,\n );\n call.hangup();\n }\n } else {\n client.emit(\"Call.incoming\", call);\n }\n } else if (event.getType() === 'm.call.answer') {\n if (!call) {\n return;\n }\n if (event.getSender() === client.credentials.userId) {\n if (call.state === 'ringing') {\n call._onAnsweredElsewhere(content);\n }\n } else {\n call._receivedAnswer(content);\n }\n } else if (event.getType() === 'm.call.candidates') {\n if (event.getSender() === client.credentials.userId) {\n return;\n }\n if (!call) {\n // store the candidates; we may get a call eventually.\n if (!candidatesByCall[content.call_id]) {\n candidatesByCall[content.call_id] = [];\n }\n candidatesByCall[content.call_id] = candidatesByCall[\n content.call_id\n ].concat(content.candidates);\n } else {\n for (i = 0; i < content.candidates.length; i++) {\n call._gotRemoteIceCandidate(content.candidates[i]);\n }\n }\n } else if (event.getType() === 'm.call.hangup') {\n // Note that we also observe our own hangups here so we can see\n // if we've already rejected a call that would otherwise be valid\n if (!call) {\n // if not live, store the fact that the call has ended because\n // we're probably getting events backwards so\n // the hangup will come before the invite\n call = webRtcCall.createNewMatrixCall(client, event.getRoomId());\n if (call) {\n call.callId = content.call_id;\n call._initWithHangup(event);\n client.callList[content.call_id] = call;\n }\n } else {\n if (call.state !== 'ended') {\n call._onHangupReceived(content);\n delete client.callList[content.call_id];\n }\n }\n }\n }\n}\n\nfunction checkTurnServers(client) {\n if (!client._supportsVoip) {\n return;\n }\n if (client.isGuest()) {\n return; // guests can't access TURN servers\n }\n\n client.turnServer().done(function(res) {\n if (res.uris) {\n console.log(\"Got TURN URIs: \" + res.uris + \" refresh in \" +\n res.ttl + \" secs\");\n // map the response to a format that can be fed to\n // RTCPeerConnection\n const servers = {\n urls: res.uris,\n username: res.username,\n credential: res.password,\n };\n client._turnServers = [servers];\n // re-fetch when we're about to reach the TTL\n client._checkTurnServersTimeoutID = setTimeout(() => {\n checkTurnServers(client);\n }, (res.ttl || (60 * 60)) * 1000 * 0.9);\n }\n }, function(err) {\n console.error(\"Failed to get TURN URIs\");\n client._checkTurnServersTimeoutID =\n setTimeout(function() {\n checkTurnServers(client);\n}, 60000);\n });\n}\n\nfunction _reject(callback, defer, err) {\n if (callback) {\n callback(err);\n }\n defer.reject(err);\n}\n\nfunction _resolve(callback, defer, res) {\n if (callback) {\n callback(null, res);\n }\n defer.resolve(res);\n}\n\nfunction _PojoToMatrixEventMapper(client) {\n function mapper(plainOldJsObject) {\n const event = new MatrixEvent(plainOldJsObject);\n if (event.isEncrypted()) {\n client.reEmitter.reEmit(event, [\n \"Event.decrypted\",\n ]);\n event.attemptDecryption(client._crypto);\n }\n return event;\n }\n return mapper;\n}\n\n/**\n * @return {Function}\n */\nMatrixClient.prototype.getEventMapper = function() {\n return _PojoToMatrixEventMapper(this);\n};\n\n// Identity Server Operations\n// ==========================\n\n/**\n * Generates a random string suitable for use as a client secret. <strong>This\n * method is experimental and may change.</strong>\n * @return {string} A new client secret\n */\nMatrixClient.prototype.generateClientSecret = function() {\n let ret = \"\";\n const chars = \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\";\n\n for (let i = 0; i < 32; i++) {\n ret += chars.charAt(Math.floor(Math.random() * chars.length));\n }\n\n return ret;\n};\n\n/** */\nmodule.exports.MatrixClient = MatrixClient;\n/** */\nmodule.exports.CRYPTO_ENABLED = CRYPTO_ENABLED;\n\n// MatrixClient Event JSDocs\n\n/**\n * Fires whenever the SDK receives a new event.\n * <p>\n * This is only fired for live events received via /sync - it is not fired for\n * events received over context, search, or pagination APIs.\n *\n * @event module:client~MatrixClient#\"event\"\n * @param {MatrixEvent} event The matrix event which caused this event to fire.\n * @example\n * matrixClient.on(\"event\", function(event){\n * var sender = event.getSender();\n * });\n */\n\n/**\n * Fires whenever the SDK receives a new to-device event.\n * @event module:client~MatrixClient#\"toDeviceEvent\"\n * @param {MatrixEvent} event The matrix event which caused this event to fire.\n * @example\n * matrixClient.on(\"toDeviceEvent\", function(event){\n * var sender = event.getSender();\n * });\n */\n\n/**\n * Fires whenever the SDK's syncing state is updated. The state can be one of:\n * <ul>\n *\n * <li>PREPARED: The client has synced with the server at least once and is\n * ready for methods to be called on it. This will be immediately followed by\n * a state of SYNCING. <i>This is the equivalent of \"syncComplete\" in the\n * previous API.</i></li>\n *\n * <li>SYNCING : The client is currently polling for new events from the server.\n * This will be called <i>after</i> processing latest events from a sync.</li>\n *\n * <li>ERROR : The client has had a problem syncing with the server. If this is\n * called <i>before</i> PREPARED then there was a problem performing the initial\n * sync. If this is called <i>after</i> PREPARED then there was a problem polling\n * the server for updates. This may be called multiple times even if the state is\n * already ERROR. <i>This is the equivalent of \"syncError\" in the previous\n * API.</i></li>\n *\n * <li>RECONNECTING: The sync connection has dropped, but not (yet) in a way that\n * should be considered erroneous.\n * </li>\n *\n * <li>STOPPED: The client has stopped syncing with server due to stopClient\n * being called.\n * </li>\n * </ul>\n * State transition diagram:\n * <pre>\n * +---->STOPPED\n * |\n * +----->PREPARED -------> SYNCING <--+\n * | ^ | ^ |\n * | | | | |\n * | | V | |\n * null ------+ | +--------RECONNECTING |\n * | | V |\n * +------->ERROR ---------------------+\n *\n * NB: 'null' will never be emitted by this event.\n *\n * </pre>\n * Transitions:\n * <ul>\n *\n * <li><code>null -> PREPARED</code> : Occurs when the initial sync is completed\n * first time. This involves setting up filters and obtaining push rules.\n *\n * <li><code>null -> ERROR</code> : Occurs when the initial sync failed first time.\n *\n * <li><code>ERROR -> PREPARED</code> : Occurs when the initial sync succeeds\n * after previously failing.\n *\n * <li><code>PREPARED -> SYNCING</code> : Occurs immediately after transitioning\n * to PREPARED. Starts listening for live updates rather than catching up.\n *\n * <li><code>SYNCING -> RECONNECTING</code> : Occurs when the live update fails.\n *\n * <li><code>RECONNECTING -> RECONNECTING</code> : Can occur if the update calls\n * continue to fail, but the keepalive calls (to /versions) succeed.\n *\n * <li><code>RECONNECTING -> ERROR</code> : Occurs when the keepalive call also fails\n *\n * <li><code>ERROR -> SYNCING</code> : Occurs when the client has performed a\n * live update after having previously failed.\n *\n * <li><code>ERROR -> ERROR</code> : Occurs when the client has failed to keepalive\n * for a second time or more.</li>\n *\n * <li><code>SYNCING -> SYNCING</code> : Occurs when the client has performed a live\n * update. This is called <i>after</i> processing.</li>\n *\n * <li><code>* -> STOPPED</code> : Occurs once the client has stopped syncing or\n * trying to sync after stopClient has been called.</li>\n * </ul>\n *\n * @event module:client~MatrixClient#\"sync\"\n *\n * @param {string} state An enum representing the syncing state. One of \"PREPARED\",\n * \"SYNCING\", \"ERROR\", \"STOPPED\".\n *\n * @param {?string} prevState An enum representing the previous syncing state.\n * One of \"PREPARED\", \"SYNCING\", \"ERROR\", \"STOPPED\" <b>or null</b>.\n *\n * @param {?Object} data Data about this transition.\n *\n * @param {MatrixError} data.err The matrix error if <code>state=ERROR</code>.\n *\n * @param {String} data.oldSyncToken The 'since' token passed to /sync.\n * <code>null</code> for the first successful sync since this client was\n * started. Only present if <code>state=PREPARED</code> or\n * <code>state=SYNCING</code>.\n *\n * @param {String} data.nextSyncToken The 'next_batch' result from /sync, which\n * will become the 'since' token for the next call to /sync. Only present if\n * <code>state=PREPARED</code> or <code>state=SYNCING</code>.\n *\n * @param {boolean} data.catchingUp True if we are working our way through a\n * backlog of events after connecting. Only present if <code>state=SYNCING</code>.\n *\n * @example\n * matrixClient.on(\"sync\", function(state, prevState, data) {\n * switch (state) {\n * case \"ERROR\":\n * // update UI to say \"Connection Lost\"\n * break;\n * case \"SYNCING\":\n * // update UI to remove any \"Connection Lost\" message\n * break;\n * case \"PREPARED\":\n * // the client instance is ready to be queried.\n * var rooms = matrixClient.getRooms();\n * break;\n * }\n * });\n */\n\n /**\n * Fires whenever the sdk learns about a new group. <strong>This event\n * is experimental and may change.</strong>\n * @event module:client~MatrixClient#\"Group\"\n * @param {Group} group The newly created, fully populated group.\n * @example\n * matrixClient.on(\"Group\", function(group){\n * var groupId = group.groupId;\n * });\n */\n\n /**\n * Fires whenever a new Room is added. This will fire when you are invited to a\n * room, as well as when you join a room. <strong>This event is experimental and\n * may change.</strong>\n * @event module:client~MatrixClient#\"Room\"\n * @param {Room} room The newly created, fully populated room.\n * @example\n * matrixClient.on(\"Room\", function(room){\n * var roomId = room.roomId;\n * });\n */\n\n /**\n * Fires whenever a Room is removed. This will fire when you forget a room.\n * <strong>This event is experimental and may change.</strong>\n * @event module:client~MatrixClient#\"deleteRoom\"\n * @param {string} roomId The deleted room ID.\n * @example\n * matrixClient.on(\"deleteRoom\", function(roomId){\n * // update UI from getRooms()\n * });\n */\n\n/**\n * Fires whenever an incoming call arrives.\n * @event module:client~MatrixClient#\"Call.incoming\"\n * @param {module:webrtc/call~MatrixCall} call The incoming call.\n * @example\n * matrixClient.on(\"Call.incoming\", function(call){\n * call.answer(); // auto-answer\n * });\n */\n\n/**\n * Fires whenever the login session the JS SDK is using is no\n * longer valid and the user must log in again.\n * NB. This only fires when action is required from the user, not\n * when then login session can be renewed by using a refresh token.\n * @event module:client~MatrixClient#\"Session.logged_out\"\n * @example\n * matrixClient.on(\"Session.logged_out\", function(call){\n * // show the login screen\n * });\n */\n\n/**\n * Fires when the JS SDK receives a M_CONSENT_NOT_GIVEN error in response\n * to a HTTP request.\n * @event module:client~MatrixClient#\"no_consent\"\n * @example\n * matrixClient.on(\"no_consent\", function(message, contentUri) {\n * console.info(message + ' Go to ' + contentUri);\n * });\n */\n\n/**\n * Fires when a device is marked as verified/unverified/blocked/unblocked by\n * {@link module:client~MatrixClient#setDeviceVerified|MatrixClient.setDeviceVerified} or\n * {@link module:client~MatrixClient#setDeviceBlocked|MatrixClient.setDeviceBlocked}.\n *\n * @event module:client~MatrixClient#\"deviceVerificationChanged\"\n * @param {string} userId the owner of the verified device\n * @param {string} deviceId the id of the verified device\n * @param {module:crypto/deviceinfo} deviceInfo updated device information\n */\n\n/**\n * Fires whenever new user-scoped account_data is added.\n * @event module:client~MatrixClient#\"accountData\"\n * @param {MatrixEvent} event The event describing the account_data just added\n * @example\n * matrixClient.on(\"accountData\", function(event){\n * myAccountData[event.type] = event.content;\n * });\n */\n\n\n// EventEmitter JSDocs\n\n/**\n * The {@link https://nodejs.org/api/events.html|EventEmitter} class.\n * @external EventEmitter\n * @see {@link https://nodejs.org/api/events.html}\n */\n\n/**\n * Adds a listener to the end of the listeners array for the specified event.\n * No checks are made to see if the listener has already been added. Multiple\n * calls passing the same combination of event and listener will result in the\n * listener being added multiple times.\n * @function external:EventEmitter#on\n * @param {string} event The event to listen for.\n * @param {Function} listener The function to invoke.\n * @return {EventEmitter} for call chaining.\n */\n\n/**\n * Alias for {@link external:EventEmitter#on}.\n * @function external:EventEmitter#addListener\n * @param {string} event The event to listen for.\n * @param {Function} listener The function to invoke.\n * @return {EventEmitter} for call chaining.\n */\n\n/**\n * Adds a <b>one time</b> listener for the event. This listener is invoked only\n * the next time the event is fired, after which it is removed.\n * @function external:EventEmitter#once\n * @param {string} event The event to listen for.\n * @param {Function} listener The function to invoke.\n * @return {EventEmitter} for call chaining.\n */\n\n/**\n * Remove a listener from the listener array for the specified event.\n * <b>Caution:</b> changes array indices in the listener array behind the\n * listener.\n * @function external:EventEmitter#removeListener\n * @param {string} event The event to listen for.\n * @param {Function} listener The function to invoke.\n * @return {EventEmitter} for call chaining.\n */\n\n/**\n * Removes all listeners, or those of the specified event. It's not a good idea\n * to remove listeners that were added elsewhere in the code, especially when\n * it's on an emitter that you didn't create (e.g. sockets or file streams).\n * @function external:EventEmitter#removeAllListeners\n * @param {string} event Optional. The event to remove listeners for.\n * @return {EventEmitter} for call chaining.\n */\n\n/**\n * Execute each of the listeners in order with the supplied arguments.\n * @function external:EventEmitter#emit\n * @param {string} event The event to emit.\n * @param {Function} listener The function to invoke.\n * @return {boolean} true if event had listeners, false otherwise.\n */\n\n/**\n * By default EventEmitters will print a warning if more than 10 listeners are\n * added for a particular event. This is a useful default which helps finding\n * memory leaks. Obviously not all Emitters should be limited to 10. This\n * function allows that to be increased. Set to zero for unlimited.\n * @function external:EventEmitter#setMaxListeners\n * @param {Number} n The max number of listeners.\n * @return {EventEmitter} for call chaining.\n */\n\n// MatrixClient Callback JSDocs\n\n/**\n * The standard MatrixClient callback interface. Functions which accept this\n * will specify 2 return arguments. These arguments map to the 2 parameters\n * specified in this callback.\n * @callback module:client.callback\n * @param {Object} err The error value, the \"rejected\" value or null.\n * @param {Object} data The data returned, the \"resolved\" value.\n */\n\n /**\n * {@link https://github.com/kriskowal/q|A promise implementation (Q)}. Functions\n * which return this will specify 2 return arguments. These arguments map to the\n * \"onFulfilled\" and \"onRejected\" values of the Promise.\n * @typedef {Object} Promise\n * @static\n * @property {Function} then promise.then(onFulfilled, onRejected, onProgress)\n * @property {Function} catch promise.catch(onRejected)\n * @property {Function} finally promise.finally(callback)\n * @property {Function} done promise.done(onFulfilled, onRejected, onProgress)\n */\n"]}
\No newline at end of file