process state events separately from timeline events

This commit is contained in:
Bruno Windels 2020-08-21 14:35:23 +02:00
parent 5837aed346
commit 1a61752ace

View file

@ -24,9 +24,7 @@ function applySyncResponse(data, roomResponse, membership, isInitialSync, isTime
} }
// state comes before timeline // state comes before timeline
if (roomResponse.state) { if (roomResponse.state) {
data = roomResponse.state.events.reduce((data, event) => { data = roomResponse.state.events.reduce(processStateEvent, data);
return processEvent(data, event, isInitialSync, isTimelineOpen, ownUserId);
}, data);
} }
if (roomResponse.timeline) { if (roomResponse.timeline) {
const {timeline} = roomResponse; const {timeline} = roomResponse;
@ -35,7 +33,12 @@ function applySyncResponse(data, roomResponse, membership, isInitialSync, isTime
data.lastPaginationToken = timeline.prev_batch; data.lastPaginationToken = timeline.prev_batch;
} }
data = timeline.events.reduce((data, event) => { data = timeline.events.reduce((data, event) => {
return processEvent(data, event, isInitialSync, isTimelineOpen, ownUserId); if (typeof event.state_key === "string") {
return processStateEvent(data, event);
} else {
return processTimelineEvent(data, event,
isInitialSync, isTimelineOpen, ownUserId);
}
}, data); }, data);
} }
const unreadNotifications = roomResponse.unread_notifications; const unreadNotifications = roomResponse.unread_notifications;
@ -48,7 +51,7 @@ function applySyncResponse(data, roomResponse, membership, isInitialSync, isTime
return data; return data;
} }
function processEvent(data, event, isInitialSync, isTimelineOpen, ownUserId) { function processStateEvent(data, event) {
if (event.type === "m.room.encryption") { if (event.type === "m.room.encryption") {
if (!data.isEncrypted) { if (!data.isEncrypted) {
data = data.cloneIfNeeded(); data = data.cloneIfNeeded();
@ -66,7 +69,17 @@ function processEvent(data, event, isInitialSync, isTimelineOpen, ownUserId) {
data = data.cloneIfNeeded(); data = data.cloneIfNeeded();
data.avatarUrl = newUrl; data.avatarUrl = newUrl;
} }
} else if (event.type === "m.room.message") { } else if (event.type === "m.room.canonical_alias") {
const content = event.content;
data = data.cloneIfNeeded();
data.canonicalAlias = content.alias;
data.altAliases = content.alt_aliases;
}
return data;
}
function processTimelineEvent(data, event, isInitialSync, isTimelineOpen, ownUserId) {
if (event.type === "m.room.message") {
data = data.cloneIfNeeded(); data = data.cloneIfNeeded();
data.lastMessageTimestamp = event.origin_server_ts; data.lastMessageTimestamp = event.origin_server_ts;
if (!isInitialSync && event.sender !== ownUserId && !isTimelineOpen) { if (!isInitialSync && event.sender !== ownUserId && !isTimelineOpen) {
@ -78,11 +91,6 @@ function processEvent(data, event, isInitialSync, isTimelineOpen, ownUserId) {
if (msgtype === "m.text") { if (msgtype === "m.text") {
data.lastMessageBody = body; data.lastMessageBody = body;
} }
} else if (event.type === "m.room.canonical_alias") {
const content = event.content;
data = data.cloneIfNeeded();
data.canonicalAlias = content.alias;
data.altAliases = content.alt_aliases;
} }
return data; return data;
} }