Skip to content

Commit

Permalink
Merge pull request #18 from vector-im/bwindels/ignore-empty-timelines
Browse files Browse the repository at this point in the history
ignore rooms with empty timelines during initial sync
  • Loading branch information
bwindels authored Aug 17, 2020
2 parents 4f5609f + 59588dc commit bbd5217
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/matrix/Sync.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2020 Bruno Windels <[email protected]>
Copyright 2020 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -17,6 +18,7 @@ limitations under the License.
import {AbortError} from "./error.js";
import {ObservableValue} from "../observable/ObservableValue.js";
import {createEnum} from "../utils/enum.js";
import {readPath, Type} from "../utils/validate.js";

const INCREMENTAL_TIMEOUT = 30000;
const SYNC_EVENT_LIMIT = 10;
Expand All @@ -43,6 +45,15 @@ function parseRooms(roomsSection, roomCallback) {
return [];
}

function timelineIsEmpty(roomResponse) {
try {
const events = readPath(roomResponse, ["timeline", "events"], Type.Array);
return events.length === 0;
} catch (err) {
return true;
}
}

export class Sync {
constructor({hsApi, session, storage}) {
this._hsApi = hsApi;
Expand Down Expand Up @@ -102,6 +113,7 @@ export class Sync {
const totalRequestTimeout = timeout + (80 * 1000); // same as riot-web, don't get stuck on wedged long requests
this._currentRequest = this._hsApi.sync(syncToken, syncFilterId, timeout, {timeout: totalRequestTimeout});
const response = await this._currentRequest.response();
const isInitialSync = !syncToken;
syncToken = response.next_batch;
const storeNames = this._storage.storeNames;
const syncTxn = await this._storage.readWriteTxn([
Expand All @@ -120,6 +132,11 @@ export class Sync {
// presence
if (response.rooms) {
const promises = parseRooms(response.rooms, async (roomId, roomResponse, membership) => {
// ignore rooms with empty timelines during initial sync,
// see https://github.com/vector-im/hydrogen-web/issues/15
if (isInitialSync && timelineIsEmpty(roomResponse)) {
return;
}
let room = this._session.rooms.get(roomId);
if (!room) {
room = this._session.createRoom(roomId);
Expand Down
117 changes: 117 additions & 0 deletions src/utils/validate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
Copyright 2020 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

export class InvalidPathError extends Error {
constructor(obj, path, field) {
super(`Could not read path ${path.join("/")}, stopped at ${field}. Base value is ${obj}`);
}

get name() {
return "InvalidPathError";
}
}

export class InvalidTypeError extends Error {
constructor(path, fieldValue, validator) {
super(`Value ${path.join("/")} is not of type ${getTypeName(validator)} but is: ${fieldValue}`);
}

get name() {
return "InvalidTypeError";
}
}

function getTypeName(validator) {
if (validator === Type.Array) {
return "Array";
}
if (validator === Type.Integer) {
return "Integer";
}
if (validator === Type.String) {
return "String";
}
if (validator === Type.Object) {
return "Object";
}
if (typeof validator === "function") {
return "Custom";
}
return "None";
}

export function readPath(obj, path, typeOrDefaultValue) {
if (!obj) {
throw new InvalidPathError(obj, path);
}
const hasDefaultValue = typeof typeOrDefaultValue !== "function";
let currentValue = obj;
for (const field of path) {
currentValue = currentValue[field];
if (typeof currentValue === "undefined") {
if (hasDefaultValue) {
return typeOrDefaultValue;
} else {
throw new InvalidPathError(obj, path, field);
}
}
}
if (!hasDefaultValue) {
const validator = typeOrDefaultValue;
if (!validator(currentValue)) {
throw new InvalidTypeError(path, currentValue, validator);
}
}
return currentValue;
}

export const Type = Object.freeze({
"Array": Array.isArray,
"Integer": Number.isSafeInteger,
"Boolean": value => value === true || value === false,
"String": value => typeof value === "string",
"Object": value => value !== null && typeof value === "object",
});

export function tests() {
return {
"readPath value at top level": assert => {
assert.strictEqual(readPath({a: 5}, ["a"]), 5);
},
"readPath value at deep level": assert => {
assert.strictEqual(readPath({a: {b: {c: 5}}}, ["a", "b", "c"]), 5);
},
"readPath value with correct type": assert => {
assert.strictEqual(readPath({a: 5}, ["a"], Type.Integer), 5);
},
"readPath value with failing type": assert => {
assert.throws(
() => readPath({a: 5}, ["a"], Type.String),
{name: "InvalidTypeError"}
);
},
"readPath value with failing path with intermediate field not being an object": assert => {
assert.throws(
() => readPath({a: {b: "bar"}}, ["a", "b", "c"], Type.Integer),
{name: "InvalidPathError"}
);
},
"readPath returns default value for incomplete path": assert => {
assert.strictEqual(readPath({a: {b: "bar"}}, ["a", "b", "c"], 5), 5);
},

}
}

0 comments on commit bbd5217

Please sign in to comment.