1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-08-18 05:42:00 +03:00

Merge pull request #32 from matrix-org/member-info-for-invites

Retrieving profile info for invites
This commit is contained in:
Kegsay
2015-10-26 16:42:21 +00:00
3 changed files with 214 additions and 6 deletions

View File

@@ -141,6 +141,7 @@ function MatrixClient(opts) {
this.callList = {
// callId: MatrixCall
};
this._config = {}; // see startClient()
// try constructing a MatrixCall to see if we are running in an environment
// which has WebRTC. If we are, listen for and handle m.call.* events.
@@ -1986,6 +1987,9 @@ function doInitialSync(client, historyLen, includeArchived) {
* @param {Number} opts.initialSyncLimit The event <code>limit=</code> to apply
* to initial sync. Default: 8.
* @param {Boolean} opts.includeArchivedRooms True to put <code>archived=true</code>
* @param {Boolean} opts.resolveInvitesToProfiles True to do /profile requests
* on every invite event if the displayname/avatar_url is not known for this user ID.
* Default: false.
* on the <code>/initialSync</code> request. Default: false.
*/
MatrixClient.prototype.startClient = function(opts) {
@@ -2003,6 +2007,8 @@ MatrixClient.prototype.startClient = function(opts) {
opts = opts || {};
opts.initialSyncLimit = opts.initialSyncLimit || 8;
opts.includeArchivedRooms = opts.includeArchivedRooms || false;
opts.resolveInvitesToProfiles = opts.resolveInvitesToProfiles || false;
this._config = opts;
if (CRYPTO_ENABLED && this.sessionStore !== null) {
this.uploadKeys(5);
@@ -2057,6 +2063,7 @@ function _pollForEvents(client) {
events = utils.map(data.chunk, _PojoToMatrixEventMapper(self));
}
if (!(self.store instanceof StubStore)) {
var roomIdsWithNewInvites = {};
// bucket events based on room.
var i = 0;
var roomIdToEvents = {};
@@ -2068,6 +2075,10 @@ function _pollForEvents(client) {
roomIdToEvents[roomId] = [];
}
roomIdToEvents[roomId].push(events[i]);
if (events[i].getType() === "m.room.member" &&
events[i].getContent().membership === "invite") {
roomIdsWithNewInvites[roomId] = true;
}
}
else if (events[i].getType() === "m.presence") {
var usr = self.store.getUser(events[i].getContent().user_id);
@@ -2081,6 +2092,7 @@ function _pollForEvents(client) {
}
}
}
// add events to room
var roomIds = utils.keys(roomIdToEvents);
utils.forEach(roomIds, function(roomId) {
@@ -2115,6 +2127,10 @@ function _pollForEvents(client) {
_syncRoom(self, room);
}
});
Object.keys(roomIdsWithNewInvites).forEach(function(inviteRoomId) {
_resolveInvites(self, self.store.getRoom(inviteRoomId));
});
}
if (data) {
self.store.setSyncToken(data.end);
@@ -2173,6 +2189,8 @@ function _processRoomEvents(client, room, stateEventList, messageChunk) {
room.oldState.setStateEvents(oldStateEvents);
room.currentState.setStateEvents(stateEvents);
_resolveInvites(client, room);
// add events to the timeline *after* setting the state
// events so messages use the right display names. Initial sync
// returns messages in chronological order, so we need to reverse
@@ -2217,6 +2235,47 @@ function reEmit(reEmitEntity, emittableEntity, eventNames) {
});
}
function _resolveInvites(client, room) {
if (!room || !client._config.resolveInvitesToProfiles) {
return;
}
// For each invited room member we want to give them a displayname/avatar url
// if they have one (the m.room.member invites don't contain this).
room.getMembersWithMembership("invite").forEach(function(member) {
if (member._requestedProfileInfo) {
return;
}
member._requestedProfileInfo = true;
// try to get a cached copy first.
var user = client.getUser(member.userId);
var promise;
if (user) {
promise = q({
avatar_url: user.avatarUrl,
displayname: user.displayName
});
}
else {
promise = client.getProfileInfo(member.userId);
}
promise.done(function(info) {
// slightly naughty by doctoring the invite event but this means all
// the code paths remain the same between invite/join display name stuff
// which is a worthy trade-off for some minor pollution.
var inviteEvent = member.events.member;
if (inviteEvent.getContent().membership !== "invite") {
// between resolving and now they have since joined, so don't clobber
return;
}
inviteEvent.getContent().avatar_url = info.avatar_url;
inviteEvent.getContent().displayname = info.displayname;
member.setMembershipEvent(inviteEvent, room.currentState); // fire listeners
}, function(err) {
// OH WELL.
});
});
}
function setupCallEventHandler(client) {
var candidatesByCall = {
// callId: [Candidate]