From 149ed04a4f30997b328a463fe0a22139076e8e7c Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Wed, 4 Nov 2015 02:24:36 +0000 Subject: [PATCH] fix some review feedback; add initial api for setting & deleting tags; still a WIP --- lib/client.js | 39 ++++++++++++++++++++++++++++++++++++++- lib/models/room.js | 6 +++--- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/lib/client.js b/lib/client.js index 004a79d05..fb1999e97 100644 --- a/lib/client.js +++ b/lib/client.js @@ -608,6 +608,43 @@ MatrixClient.prototype.setRoomTopic = function(roomId, topic, callback) { undefined, callback); }; +/** + * @param {string} roomId + * @param {string} name of room tag to be set + * @param {object} metadata associated with that tag to be stored + * @param {module:client.callback} callback Optional. + * @return {module:client.Promise} Resolves: TODO + * @return {module:http-api.MatrixError} Rejects: with an error response. + */ +MatrixClient.prototype.setRoomTag = function(roomId, tagName, metadata, callback) { + var path = utils.encodeUri("/user/$userId/rooms/$roomId/tags/$tag", { + $userId: this.credentials.userId, + $roomId: roomId, + $tag: tagName, + }); + return this._http.authedRequestWithPrefix( + callback, "PUT", path, undefined, metadata, httpApi.PREFIX_V2_ALPHA + ); +}; + +/** + * @param {string} roomId + * @param {string} name of room tag to be removed + * @param {module:client.callback} callback Optional. + * @return {module:client.Promise} Resolves: TODO + * @return {module:http-api.MatrixError} Rejects: with an error response. + */ +MatrixClient.prototype.deleteRoomTag = function(roomId, tagName, callback) { + var path = utils.encodeUri("/user/$userId/rooms/$roomId/tags/$tag", { + $userId: this.credentials.userId, + $roomId: roomId, + $tag: tagName, + }); + return this._http.authedRequestWithPrefix( + callback, "DELETE", path, undefined, undefined, httpApi.PREFIX_V2_ALPHA + ); +}; + /** * Set a user's power level. * @param {string} roomId @@ -1945,7 +1982,7 @@ function doInitialSync(client, historyLen, includeArchived) { var privateUserData = data.rooms[i].private_user_data || []; for (j = 0; j < privateUserData.length; j++) { var event = _PojoToMatrixEventMapper(client)(privateUserData[j]); - if (event.type === "m.tag") { + if (event.getType() === "m.tag") { room.addTags(event); } // XXX: unhandled private user data event - we should probably diff --git a/lib/models/room.js b/lib/models/room.js index d2e7f0ba3..e99a083a0 100644 --- a/lib/models/room.js +++ b/lib/models/room.js @@ -423,12 +423,12 @@ Room.prototype.addTags = function(event) { // } // } + // XXX: do we need to deep copy here? + this.tags = event.getContent().tags; + // XXX: we could do a deep-comparison to see if the tags have really // changed - but do we want to bother? this.emit("Room.tags", this); - - // XXX: do we need to deep copy here? - this.tags = event.content.tags; }; function setEventMetadata(event, stateContext, toStartOfTimeline) {