From 53854e73feeaad7e04756e0700c9c92a807a6a51 Mon Sep 17 00:00:00 2001 From: IvanBlacky Date: Mon, 26 Aug 2019 13:58:55 +0300 Subject: [PATCH 1/3] Refactor: leadersIn mechanics in find leaders method --- src/controllers/connector/Leaders.js | 10 ++++++---- src/services/Connector.js | 5 ++++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/controllers/connector/Leaders.js b/src/controllers/connector/Leaders.js index d3bf6d9..79f5496 100644 --- a/src/controllers/connector/Leaders.js +++ b/src/controllers/connector/Leaders.js @@ -12,9 +12,10 @@ class Leaders extends AbstractFeed { this._leaderFeedCache = leaderFeedCache; } - async findLeaders({ query, currentUserId, limit, app = 'gls', sequenceKey }) { + async findLeaders({ query, currentUserId, communityId, limit, app = 'gls', sequenceKey }) { const filter = { [`usernames.${app}`]: { $regex: `^${escape(query.trim().toLowerCase())}` }, + leaderIn: { $all: [communityId] }, }; if (sequenceKey) { @@ -47,12 +48,13 @@ class Leaders extends AbstractFeed { const profiles = await ProfileModel.aggregate(pipeline); - const leaders = profiles.reduce((leaders, profile) => { + const leaders = []; + + for (const profile of profiles) { if (profile.leader.length > 0) { leaders.push({ username: profile.usernames[app], ...profile.leader[0] }); } - return leaders; - }, []); + } await this._populateUsers(leaders, app); await this._tryApplyVotesForModels(leaders, currentUserId); diff --git a/src/services/Connector.js b/src/services/Connector.js index 0365855..3a7842f 100644 --- a/src/services/Connector.js +++ b/src/services/Connector.js @@ -311,11 +311,14 @@ class Connector extends BasicConnector { 'onlyWhenPublicApiEnabled', ], validation: { - required: ['query'], + required: ['query', 'communityId'], properties: { query: { type: 'string', }, + communityId: { + type: 'string', + }, }, }, }, From 9eaf3d6a48b617ded9742b78c458b15a41241e6e Mon Sep 17 00:00:00 2001 From: IvanBlacky Date: Wed, 4 Sep 2019 12:31:43 +0300 Subject: [PATCH 2/3] Fix: search sync --- src/services/SearchSync.js | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/src/services/SearchSync.js b/src/services/SearchSync.js index f445ef5..335bc69 100644 --- a/src/services/SearchSync.js +++ b/src/services/SearchSync.js @@ -176,10 +176,23 @@ class SearchSync extends BasicService { } } - async _getDocsToSync(model, from = new Date(null)) { - return await await model.find({ + async _getDocsToSync({ model, from = new Date(null), maxDocs = 200, sequenceKey }) { + const query = { updatedAt: { $gte: from }, - }); + }; + if (sequenceKey) { + query._id = { $gt: sequenceKey }; + } + const docs = await model.find(query).limit(maxDocs); + const result = { + docs, + }; + + if (docs.length === maxDocs) { + result.sequenceKey = docs[docs.length - 1]._id; + } + + return result; } async _getAllIndexes(model, offset = 0) { @@ -203,14 +216,25 @@ class SearchSync extends BasicService { return allDocs; } - async _syncModel(model, from) { - const dataToSync = await this._getDocsToSync(model, from); + async _syncModel(model, from, sequenceKey) { + const { docs: dataToSync, sequenceKey: newSequenceKey } = await this._getDocsToSync({ + model, + from, + sequenceKey, + }); + const syncPromises = []; if (dataToSync.length > 0) { for (const data of dataToSync) { - await this._syncDoc(model, data); + syncPromises.push(this._syncDoc(model, data)); } } + + await Promise.all(syncPromises); + + if (newSequenceKey) { + await this._syncModel(model, from, newSequenceKey); + } } async _syncDeleted(model) { From 04680ab7e802558b8ba97ff07dad30678dbe8bba Mon Sep 17 00:00:00 2001 From: IvanBlacky Date: Wed, 4 Sep 2019 13:18:12 +0300 Subject: [PATCH 3/3] Fix: review comments --- src/services/SearchSync.js | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/services/SearchSync.js b/src/services/SearchSync.js index 335bc69..cd4e4ee 100644 --- a/src/services/SearchSync.js +++ b/src/services/SearchSync.js @@ -176,7 +176,7 @@ class SearchSync extends BasicService { } } - async _getDocsToSync({ model, from = new Date(null), maxDocs = 200, sequenceKey }) { + async _getDocsToSync({ model, from = new Date(0), maxDocs = 200, sequenceKey }) { const query = { updatedAt: { $gte: from }, }; @@ -223,15 +223,10 @@ class SearchSync extends BasicService { sequenceKey, }); - const syncPromises = []; if (dataToSync.length > 0) { - for (const data of dataToSync) { - syncPromises.push(this._syncDoc(model, data)); - } + await Promise.all(dataToSync.map(data => this._syncDoc(model, data))); } - await Promise.all(syncPromises); - if (newSequenceKey) { await this._syncModel(model, from, newSequenceKey); }