1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-28 05:03:59 +03:00

implement account data

This commit is contained in:
Matthew Hodgson
2016-01-08 03:22:08 +00:00
parent 446faed9b5
commit 387ad09c5f
4 changed files with 103 additions and 11 deletions

View File

@@ -722,6 +722,42 @@ MatrixClient.prototype.deleteRoomTag = function(roomId, tagName, callback) {
); );
}; };
/**
* @param {MatrixEvent} account_data event
* @param {string} event type to be set
* @param {object} content event content
* @return {module:client.Promise} Resolves: TODO
* @return {module:http-api.MatrixError} Rejects: with an error response.
*/
MatrixClient.prototype.setAccountData = function(event, eventType, content) {
var path = utils.encodeUri("/user/$userId/account_data/$type", {
$userId: this.credentials.userId,
$type: event.getType(),
});
return this._http.authedRequestWithPrefix(
callback, "PUT", path, undefined, content, httpApi.PREFIX_V2_ALPHA
);
};
/**
* @param {string} roomId
* @param {string} event type to be set
* @param {object} content event content
* @param {module:client.callback} callback Optional.
* @return {module:client.Promise} Resolves: TODO
* @return {module:http-api.MatrixError} Rejects: with an error response.
*/
MatrixClient.prototype.setRoomAccountData = function(roomId, eventType, content, callback) {
var path = utils.encodeUri("/user/$userId/rooms/$roomId/account_data/$type", {
$userId: this.credentials.userId,
$roomId: roomId,
$type: eventType,
});
return this._http.authedRequestWithPrefix(
callback, "PUT", path, undefined, content, httpApi.PREFIX_V2_ALPHA
);
};
/** /**
* Set a user's power level. * Set a user's power level.
* @param {string} roomId * @param {string} roomId
@@ -1782,7 +1818,7 @@ MatrixClient.prototype.setPresence = function(presence, callback) {
* @return {module:http-api.MatrixError} Rejects: with an error response. * @return {module:http-api.MatrixError} Rejects: with an error response.
*/ */
MatrixClient.prototype.publicRooms = function(callback) { MatrixClient.prototype.publicRooms = function(callback) {
return this._http.request(callback, "GET", "/publicRooms"); return this._http.authedRequest(callback, "GET", "/publicRooms");
}; };
/** /**

View File

@@ -53,6 +53,9 @@ module.exports.EventStatus = {
* @prop {boolean} forwardLooking True if this event is 'forward looking', meaning * @prop {boolean} forwardLooking True if this event is 'forward looking', meaning
* that getDirectionalContent() will return event.content and not event.prev_content. * that getDirectionalContent() will return event.content and not event.prev_content.
* Default: true. <strong>This property is experimental and may change.</strong> * Default: true. <strong>This property is experimental and may change.</strong>
* @prop {String} section The type of /sync section this event came from, if any
* one of 'state', 'account_data', 'ephemeral', or 'timeline'. Useful for recognising
* 'account_data' events.
*/ */
module.exports.MatrixEvent = function MatrixEvent(event, encrypted) { module.exports.MatrixEvent = function MatrixEvent(event, encrypted) {
this.event = event || {}; this.event = event || {};
@@ -61,6 +64,7 @@ module.exports.MatrixEvent = function MatrixEvent(event, encrypted) {
this.status = null; this.status = null;
this.forwardLooking = true; this.forwardLooking = true;
this.encrypted = Boolean(encrypted); this.encrypted = Boolean(encrypted);
this.section = null;
}; };
module.exports.MatrixEvent.prototype = { module.exports.MatrixEvent.prototype = {
@@ -178,6 +182,14 @@ module.exports.MatrixEvent.prototype = {
return this.event.state_key !== undefined; return this.event.state_key !== undefined;
}, },
/**
* Check if this event is account_data.
* @return {boolean} True if this event describes account_data
*/
isAccountData: function() {
return this.section === "account_data";
},
/** /**
* Check if the event is encrypted. * Check if the event is encrypted.
* @return {boolean} True if this event is encrypted. * @return {boolean} True if this event is encrypted.

View File

@@ -62,6 +62,9 @@ function synthesizeReceipt(userId, event, receiptType) {
* this room. * this room.
* @prop {object} tags Dict of room tags; the keys are the tag name and the values * @prop {object} tags Dict of room tags; the keys are the tag name and the values
* are any metadata associated with the tag - e.g. { "fav" : { order: 1 } } * are any metadata associated with the tag - e.g. { "fav" : { order: 1 } }
* @prop {object} accountData Dict of per-room account_data events; the keys are the
* event type and the values are the events.
* are any metadata associated with the tag - e.g. { "fav" : { order: 1 } }
* @prop {RoomState} oldState The state of the room at the time of the oldest * @prop {RoomState} oldState The state of the room at the time of the oldest
* event in the timeline. * event in the timeline.
* @prop {RoomState} currentState The state of the room at the time of the * @prop {RoomState} currentState The state of the room at the time of the
@@ -88,6 +91,9 @@ function Room(roomId, opts) {
// $tagName: { $metadata: $value }, // $tagName: { $metadata: $value },
// $tagName: { $metadata: $value }, // $tagName: { $metadata: $value },
}; };
this.accountData = {
// $eventType: $event
};
this.oldState = new RoomState(roomId); this.oldState = new RoomState(roomId);
this.currentState = new RoomState(roomId); this.currentState = new RoomState(roomId);
this.summary = null; this.summary = null;
@@ -336,6 +342,10 @@ Room.prototype.addEvents = function(events, duplicateStrategy) {
else if (events[i].getType() === "m.tag") { else if (events[i].getType() === "m.tag") {
this.addTags(events[i]); this.addTags(events[i]);
} }
else if (events[i].isAccountData()) {
this.addAccountData(events[i]);
continue;
}
else { else {
if (duplicateStrategy) { if (duplicateStrategy) {
// is there a duplicate? // is there a duplicate?
@@ -600,6 +610,24 @@ Room.prototype.addTags = function(event) {
this.emit("Room.tags", event, this); this.emit("Room.tags", event, this);
}; };
/**
* Update the account_data events for this room, overwriting events of the same type.
* @param {MatrixEvent} event the account_data event
*/
Room.prototype.addAccountData = function(event) {
this.accountData[event.getType()] = event;
this.emit("Room.accountData", event, this);
};
/**
* Access account_data event of given event type for this room
* @param {string} type the type of account_data event to be accessed
* @return {MatrixEvent} the account_data event in question
*/
Room.prototype.getAccountData = function(type) {
return this.accountData[type];
};
function setEventMetadata(event, stateContext, toStartOfTimeline) { function setEventMetadata(event, stateContext, toStartOfTimeline) {
// set sender and target properties // set sender and target properties
event.sender = stateContext.getSentinelMember( event.sender = stateContext.getSentinelMember(
@@ -750,3 +778,16 @@ module.exports = Room;
* if (newTags["favourite"]) showStar(room); * if (newTags["favourite"]) showStar(room);
* }); * });
*/ */
/**
* Fires whenever a room's account_data is updated.
* @event module:client~MatrixClient#"Room.accountData"
* @param {event} event The account_data event
* @param {Room} room The room whose account_data was updated.
* @example
* matrixClient.on("Room.accountData", function(event, room){
* if (event.getType() === "m.room.colorscheme") {
* applyColorScheme(event.getContents());
* }
* });
*/

View File

@@ -138,8 +138,8 @@ SyncApi.prototype.syncLeftRooms = function() {
return; return;
} }
leaveObj.timeline = leaveObj.timeline || {}; leaveObj.timeline = leaveObj.timeline || {};
var timelineEvents = self._mapSyncEventsFormat(leaveObj.timeline, room); var timelineEvents = self._mapSyncEventsFormat(leaveObj.timeline, room, 'timeline');
var stateEvents = self._mapSyncEventsFormat(leaveObj.state, room); var stateEvents = self._mapSyncEventsFormat(leaveObj.state, room, 'state');
var paginationToken = ( var paginationToken = (
leaveObj.timeline.limited ? leaveObj.timeline.prev_batch : null leaveObj.timeline.limited ? leaveObj.timeline.prev_batch : null
); );
@@ -324,7 +324,7 @@ SyncApi.prototype._sync = function(syncOptions, attempt) {
// Handle invites // Handle invites
inviteRooms.forEach(function(inviteObj) { inviteRooms.forEach(function(inviteObj) {
var room = inviteObj.room; var room = inviteObj.room;
var stateEvents = self._mapSyncEventsFormat(inviteObj.invite_state, room); var stateEvents = self._mapSyncEventsFormat(inviteObj.invite_state, room, 'state');
self._processRoomEvents(room, stateEvents); self._processRoomEvents(room, stateEvents);
if (inviteObj.isBrandNewRoom) { if (inviteObj.isBrandNewRoom) {
room.recalculate(client.credentials.userId); room.recalculate(client.credentials.userId);
@@ -337,10 +337,10 @@ SyncApi.prototype._sync = function(syncOptions, attempt) {
// Handle joins // Handle joins
joinRooms.forEach(function(joinObj) { joinRooms.forEach(function(joinObj) {
var room = joinObj.room; var room = joinObj.room;
var stateEvents = self._mapSyncEventsFormat(joinObj.state, room); var stateEvents = self._mapSyncEventsFormat(joinObj.state, room, 'state');
var timelineEvents = self._mapSyncEventsFormat(joinObj.timeline, room); var timelineEvents = self._mapSyncEventsFormat(joinObj.timeline, room, 'timeline');
var ephemeralEvents = self._mapSyncEventsFormat(joinObj.ephemeral); var ephemeralEvents = self._mapSyncEventsFormat(joinObj.ephemeral, undefined, 'ephemeral');
var accountDataEvents = self._mapSyncEventsFormat(joinObj.account_data); var accountDataEvents = self._mapSyncEventsFormat(joinObj.account_data, undefined, 'account_data');
// we do this first so it's correct when any of the events fire // we do this first so it's correct when any of the events fire
room.unread_notification_count = joinObj.unread_notification_count; room.unread_notification_count = joinObj.unread_notification_count;
@@ -379,7 +379,7 @@ SyncApi.prototype._sync = function(syncOptions, attempt) {
leaveRooms.forEach(function(leaveObj) { leaveRooms.forEach(function(leaveObj) {
// Do the bear minimum to register rejected invites / you leaving rooms // Do the bear minimum to register rejected invites / you leaving rooms
var room = leaveObj.room; var room = leaveObj.room;
var timelineEvents = self._mapSyncEventsFormat(leaveObj.timeline, room); var timelineEvents = self._mapSyncEventsFormat(leaveObj.timeline, room, 'timeline');
room.addEvents(timelineEvents); room.addEvents(timelineEvents);
timelineEvents.forEach(function(e) { client.emit("event", e); }); timelineEvents.forEach(function(e) { client.emit("event", e); });
}); });
@@ -457,9 +457,10 @@ SyncApi.prototype._mapSyncResponseToRoomArray = function(obj) {
/** /**
* @param {Object} obj * @param {Object} obj
* @param {Room} room * @param {Room} room
* @param {String} /sync section (timeline, state, ephemeral, account_data)
* @return {MatrixEvent[]} * @return {MatrixEvent[]}
*/ */
SyncApi.prototype._mapSyncEventsFormat = function(obj, room) { SyncApi.prototype._mapSyncEventsFormat = function(obj, room, section) {
if (!obj || !utils.isArray(obj.events)) { if (!obj || !utils.isArray(obj.events)) {
return []; return [];
} }
@@ -468,7 +469,9 @@ SyncApi.prototype._mapSyncEventsFormat = function(obj, room) {
if (room) { if (room) {
e.room_id = room.roomId; e.room_id = room.roomId;
} }
return mapper(e); var event = mapper(e);
event.section = section;
return event;
}); });
}; };