1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-26 17:03:12 +03:00

Merge pull request #61 from matrix-org/matthew/accountdata

implement account data
This commit is contained in:
Matthew Hodgson
2016-01-13 12:43:53 +00:00
3 changed files with 96 additions and 8 deletions

View File

@@ -62,6 +62,8 @@ function synthesizeReceipt(userId, event, receiptType) {
* this room, with the oldest event at index 0.
* @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 } }
* @prop {object} accountData Dict of per-room account_data events; the keys are the
* event type and the values are the events.
* @prop {RoomState} oldState The state of the room at the time of the oldest
* event in the timeline.
* @prop {RoomState} currentState The state of the room at the time of the
@@ -88,6 +90,9 @@ function Room(roomId, opts) {
// $tagName: { $metadata: $value },
// $tagName: { $metadata: $value },
};
this.accountData = {
// $eventType: $event
};
this.oldState = new RoomState(roomId);
this.currentState = new RoomState(roomId);
this.summary = null;
@@ -335,9 +340,8 @@ Room.prototype.addEvents = function(events, duplicateStrategy) {
else if (events[i].getType() === "m.receipt") {
this.addReceipt(events[i]);
}
else if (events[i].getType() === "m.tag") {
this.addTags(events[i]);
}
// N.B. account_data is added directly by /sync to avoid
// having to maintain an event.isAccountData() here
else {
if (duplicateStrategy) {
// is there a duplicate?
@@ -602,6 +606,30 @@ Room.prototype.addTags = function(event) {
this.emit("Room.tags", event, this);
};
/**
* Update the account_data events for this room, overwriting events of the same type.
* @param {Array<MatrixEvent>} events an array of account_data events to add
*/
Room.prototype.addAccountData = function(events) {
for (var i = 0; i < events.length; i++) {
var event = events[i];
if (event.getType() === "m.tag") {
this.addTags(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) {
// set sender and target properties
event.sender = stateContext.getSentinelMember(
@@ -752,3 +780,16 @@ module.exports = 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());
* }
* });
*/