ignore own messages for unread state, and don't set unread while open

This commit is contained in:
Bruno Windels 2020-08-21 14:26:51 +02:00
parent 879c4ff951
commit d3ea8c747a
2 changed files with 24 additions and 10 deletions

View File

@ -31,7 +31,7 @@ export class Room extends EventEmitter {
this._roomId = roomId; this._roomId = roomId;
this._storage = storage; this._storage = storage;
this._hsApi = hsApi; this._hsApi = hsApi;
this._summary = new RoomSummary(roomId); this._summary = new RoomSummary(roomId, user.id);
this._fragmentIdComparer = new FragmentIdComparer([]); this._fragmentIdComparer = new FragmentIdComparer([]);
this._syncWriter = new SyncWriter({roomId, fragmentIdComparer: this._fragmentIdComparer}); this._syncWriter = new SyncWriter({roomId, fragmentIdComparer: this._fragmentIdComparer});
this._emitCollectionChange = emitCollectionChange; this._emitCollectionChange = emitCollectionChange;
@ -43,7 +43,12 @@ export class Room extends EventEmitter {
/** @package */ /** @package */
async writeSync(roomResponse, membership, isInitialSync, txn) { async writeSync(roomResponse, membership, isInitialSync, txn) {
const summaryChanges = this._summary.writeSync(roomResponse, membership, isInitialSync, txn); const isTimelineOpen = !!this._timeline;
const summaryChanges = this._summary.writeSync(
roomResponse,
membership,
isInitialSync, isTimelineOpen,
txn);
const {entries, newLiveKey, changedMembers} = await this._syncWriter.writeSync(roomResponse, txn); const {entries, newLiveKey, changedMembers} = await this._syncWriter.writeSync(roomResponse, txn);
let removedPendingEvents; let removedPendingEvents;
if (roomResponse.timeline && roomResponse.timeline.events) { if (roomResponse.timeline && roomResponse.timeline.events) {

View File

@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
function applySyncResponse(data, roomResponse, membership, isInitialSync) { function applySyncResponse(data, roomResponse, membership, isInitialSync, isTimelineOpen, ownUserId) {
if (roomResponse.summary) { if (roomResponse.summary) {
data = updateSummary(data, roomResponse.summary); data = updateSummary(data, roomResponse.summary);
} }
@ -25,7 +25,7 @@ function applySyncResponse(data, roomResponse, membership, isInitialSync) {
// 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((data, event) => {
return processEvent(data, event, isInitialSync); return processEvent(data, event, isInitialSync, isTimelineOpen, ownUserId);
}, data); }, data);
} }
if (roomResponse.timeline) { if (roomResponse.timeline) {
@ -35,7 +35,7 @@ function applySyncResponse(data, roomResponse, membership, isInitialSync) {
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); return processEvent(data, event, isInitialSync, isTimelineOpen, ownUserId);
}, data); }, data);
} }
const unreadNotifications = roomResponse.unread_notifications; const unreadNotifications = roomResponse.unread_notifications;
@ -48,7 +48,7 @@ function applySyncResponse(data, roomResponse, membership, isInitialSync) {
return data; return data;
} }
function processEvent(data, event, isInitialSync) { function processEvent(data, event, isInitialSync, isTimelineOpen, ownUserId) {
if (event.type === "m.room.encryption") { if (event.type === "m.room.encryption") {
if (!data.isEncrypted) { if (!data.isEncrypted) {
data = data.cloneIfNeeded(); data = data.cloneIfNeeded();
@ -69,7 +69,7 @@ function processEvent(data, event, isInitialSync) {
} else if (event.type === "m.room.message") { } else 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) { if (!isInitialSync && event.sender !== ownUserId && !isTimelineOpen) {
data.isUnread = true; data.isUnread = true;
} }
const {content} = event; const {content} = event;
@ -145,7 +145,8 @@ class SummaryData {
} }
export class RoomSummary { export class RoomSummary {
constructor(roomId) { constructor(roomId, ownUserId) {
this._ownUserId = ownUserId;
this._data = new SummaryData(null, roomId); this._data = new SummaryData(null, roomId);
} }
@ -169,6 +170,10 @@ export class RoomSummary {
return this._data.isUnread; return this._data.isUnread;
} }
get notificationCount() {
return this._data.notificationCount;
}
get lastMessage() { get lastMessage() {
return this._data.lastMessageBody; return this._data.lastMessageBody;
} }
@ -213,11 +218,15 @@ export class RoomSummary {
return data; return data;
} }
writeSync(roomResponse, membership, isInitialSync, txn) { writeSync(roomResponse, membership, isInitialSync, isTimelineOpen, txn) {
// clear cloned flag, so cloneIfNeeded makes a copy and // clear cloned flag, so cloneIfNeeded makes a copy and
// this._data is not modified if any field is changed. // this._data is not modified if any field is changed.
this._data.cloned = false; this._data.cloned = false;
const data = applySyncResponse(this._data, roomResponse, membership, isInitialSync); const data = applySyncResponse(
this._data, roomResponse,
membership,
isInitialSync, isTimelineOpen,
this._ownUserId);
if (data !== this._data) { if (data !== this._data) {
// need to think here how we want to persist // need to think here how we want to persist
// things like unread status (as read marker, or unread count)? // things like unread status (as read marker, or unread count)?