11const common = require ( '../../../api/utils/common' ) ,
22 countlyCommon = require ( '../../../api/lib/countly.common.js' ) ,
3- log = common . log ( 'push:api:drill' ) ,
4- { FIELDS_TITLES } = require ( './send/platforms' ) ;
3+ log = common . log ( 'push:api:drill' ) ;
54
65module . exports . drillAddPushEvents = ( { uid, params, events, event} ) => {
76 return new Promise ( ( res , rej ) => {
@@ -95,75 +94,23 @@ function messageQuery(message) {
9594 }
9695}
9796
98- /**
99- * Find messages using particular query and return ids
100- *
101- * @param {object } q message collection query
102- * @returns {String[] } array of message ids
103- */
104- async function find ( q ) {
105- let ids = await common . db . collection ( 'messages' ) . find ( q , { projection : { _id : 1 } } ) . toArray ( ) ;
106- ids = ( ids || [ ] ) . map ( id => id . _id . toString ( ) ) ;
107- return ids . length ? ids : [ 'nope' ] ;
108- }
109-
110- const toIdsMappers = {
111- 'message.name' : ( query , app_id ) => find ( { app : common . db . ObjectID ( app_id ) , 'info.title' : query } ) ,
112- 'message.title' : ( query , app_id ) => find ( { app : common . db . ObjectID ( app_id ) , 'contents.title' : query } ) ,
113- 'message.message' : ( query , app_id ) => find ( { app : common . db . ObjectID ( app_id ) , 'contents.message' : query } ) ,
114- } ;
115-
116- module . exports . drillPreprocessQuery = async function ( { query, params} ) {
117- if ( query && params && params . qstring && params . qstring . event === '[CLY]_push_action' ) {
118- if ( query . $or ) {
119- for ( let i = 0 ; i < query . $or . length ; i ++ ) {
120- let q = query . $or [ i ] ;
121- for ( let k in q ) {
122- if ( toIdsMappers [ k ] ) {
123- let ids = await toIdsMappers [ k ] ( q [ k ] , params . app_id ) ;
124- log . d ( `replaced query.$or[${ i } ] (%j) with %j` , query . $or [ i ] , { 'sg.i' : { $in : ids } } ) ;
125- query . $or [ i ] = {
126- 'sg.i' : { $in : ids }
127- } ;
128- }
129- }
97+ module . exports . drillPreprocessQuery = ( { query, params} ) => {
98+ if ( query ) {
99+ if ( query . push ) {
100+ if ( query . push . $nin ) {
101+ query . $and = query . push . $nin . map ( tk => {
102+ return { $or : [ { [ tk ] : false } , { [ tk ] : { $exists : false } } ] } ;
103+ } ) ;
130104 }
131- }
132- for ( let k in query ) {
133- if ( toIdsMappers [ k ] ) {
134- let ids = await toIdsMappers [ k ] ( query [ k ] , params . app_id ) ;
135- if ( query [ 'sg.i' ] && query [ 'sg.i' ] . $in ) {
136- query [ 'sg.i' ] . $in = query [ 'sg.i' ] . $in . filter ( id => ids . includes ( id ) ) ;
137- }
138- else if ( query [ 'sg.i' ] ) {
139- query [ 'sg.i' ] . $in = ids ;
140- }
141- else {
142- query [ 'sg.i' ] = { $in : ids } ;
143- }
144- log . d ( `replaced query[${ k } ] (%j) with %j` , query [ k ] , query [ 'sg.i' ] ) ;
145- delete query [ k ] ;
105+ if ( query . push . $in ) {
106+ let q = query . push . $in . map ( tk => {
107+ return { [ tk ] : true } ;
108+ } ) ;
109+ query . $or = q ;
146110 }
111+ delete query . push ;
147112 }
148- if ( query [ 'sg.i' ] && query [ 'sg.i' ] . $in && ! query [ 'sg.i' ] . $in . length ) {
149- query [ 'sg.i' ] . $in = [ 'nope' ] ;
150- }
151- // if (query.push) {
152- // if (query.push.$nin) {
153- // query.$and = query.push.$nin.map(tk => {
154- // return {$or: [{[tk]: false}, {[tk]: {$exists: false}}] };
155- // });
156- // }
157- // if (query.push.$in) {
158- // let q = query.push.$in.map(tk => {
159- // return {[tk]: true};
160- // });
161- // query.$or = q;
162- // }
163- // delete query.push;
164- // }
165- }
166- else if ( query && params ) {
113+
167114 if ( query . message ) {
168115 let q = messageQuery ( query . message ) ;
169116
@@ -174,111 +121,32 @@ module.exports.drillPreprocessQuery = async function({query, params}) {
174121 log . d ( `removing message ${ JSON . stringify ( query . message ) } from queryObject` ) ;
175122 delete query . message ;
176123
177- try {
178- let ids = await common . db . collection ( `push_${ params . app_id } ` ) . find ( q , { projection : { _id : 1 } } ) . toArray ( ) ;
179- ids = ( ids || [ ] ) . map ( id => id . _id ) ;
180- query . uid = { $in : ids } ;
181- log . d ( `filtered by message: uids out of ${ ids . length } ` ) ;
182- }
183- catch ( e ) {
184- log . e ( e ) ;
185- }
186- }
187- }
188-
189- if ( query . push ) {
190- let q ;
191- if ( query . push . $nin ) {
192- q = {
193- $and : query . push . $nin . map ( tk => {
194- return { [ tk ] : { $exists : false } } ;
195- } )
196- } ;
197- }
198- if ( query . push . $in ) {
199- q = {
200- $or : query . push . $in . map ( tk => {
201- return { [ tk ] : { $exists : true } } ;
202- } )
203- } ;
204- }
205- if ( query . push . $regex ) {
206- q = Object . keys ( FIELDS_TITLES ) . filter ( k => query . push . $regex . test ( FIELDS_TITLES [ k ] ) ) . map ( tk => {
207- return { [ tk ] : { $exists : true } } ;
208- } ) ;
209- }
210-
211- delete query . push ;
212-
213- if ( q ) {
214- if ( query . $or ) {
215- query . $and = [ query . $or , q ] ;
216- }
217- else if ( query . $and ) {
218- query . $and = [ query . $and , q ] ;
219- }
220- else {
221- for ( let k in q ) {
222- query [ k ] = q [ k ] ;
124+ return new Promise ( ( res , rej ) => {
125+ try {
126+ common . db . collection ( `push_${ params . app_id } ` ) . find ( q , { projection : { _id : 1 } } ) . toArray ( ( err , ids ) => {
127+ if ( err ) {
128+ rej ( err ) ;
129+ }
130+ else {
131+ ids = ( ids || [ ] ) . map ( id => id . _id ) ;
132+ query . uid = { $in : ids } ;
133+ log . d ( `filtered by message: uids out of ${ ids . length } ` ) ;
134+ res ( ) ;
135+ }
136+ } ) ;
223137 }
224- }
138+ catch ( e ) {
139+ log . e ( e ) ;
140+ rej ( e ) ;
141+ }
142+ } ) ;
225143 }
226144 }
227145} ;
228146
229- // module.exports.drillPreprocessQuery = ({query, params}) => {
230- // if (query) {
231- // if (query.push) {
232- // if (query.push.$nin) {
233- // query.$and = query.push.$nin.map(tk => {
234- // return {$or: [{[tk]: false}, {[tk]: {$exists: false}}] };
235- // });
236- // }
237- // if (query.push.$in) {
238- // let q = query.push.$in.map(tk => {
239- // return {[tk]: true};
240- // });
241- // query.$or = q;
242- // }
243- // delete query.push;
244- // }
245-
246- // if (query.message) {
247- // let q = messageQuery(query.message);
248-
249- // if (!q) {
250- // return;
251- // }
252-
253- // log.d(`removing message ${JSON.stringify(query.message)} from queryObject`);
254- // delete query.message;
255-
256- // return new Promise((res, rej) => {
257- // try {
258- // common.db.collection(`push_${params.app_id}`).find(q, {projection: {_id: 1}}).toArray((err, ids) => {
259- // if (err) {
260- // rej(err);
261- // }
262- // else {
263- // ids = (ids || []).map(id => id._id);
264- // query.uid = {$in: ids};
265- // log.d(`filtered by message: uids out of ${ids.length}`);
266- // res();
267- // }
268- // });
269- // }
270- // catch (e) {
271- // log.e(e);
272- // rej(e);
273- // }
274- // });
275- // }
276- // }
277- // };
278-
279147module . exports . drillPostprocessUids = ( { uids, params} ) => new Promise ( ( res , rej ) => {
280- let message = params && params . initialQueryObject && params . initialQueryObject . message ;
281- if ( uids && uids . length && message ) {
148+ let message = params . initialQueryObject && params . initialQueryObject . message ;
149+ if ( uids . length && message ) {
282150 log . d ( `filtering ${ uids . length } uids by message` ) ;
283151
284152 let q = messageQuery ( message ) ;
0 commit comments