|
1 | 1 | const common = require('../../../api/utils/common'), |
2 | 2 | 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'); |
5 | 4 |
|
6 | 5 | module.exports.drillAddPushEvents = ({uid, params, events, event}) => { |
7 | 6 | return new Promise((res, rej) => { |
@@ -95,190 +94,59 @@ function messageQuery(message) { |
95 | 94 | } |
96 | 95 | } |
97 | 96 |
|
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 | + }); |
130 | 104 | } |
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; |
146 | 110 | } |
| 111 | + delete query.push; |
147 | 112 | } |
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 | + |
167 | 114 | if (query.message) { |
168 | 115 | let q = messageQuery(query.message); |
169 | 116 |
|
170 | 117 | if (!q) { |
171 | | - return; |
| 118 | + return Promise.resolve(); |
172 | 119 | } |
173 | 120 |
|
174 | 121 | log.d(`removing message ${JSON.stringify(query.message)} from queryObject`); |
175 | 122 | delete query.message; |
176 | 123 |
|
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 | + }); |
223 | 137 | } |
224 | | - } |
| 138 | + catch (e) { |
| 139 | + log.e(e); |
| 140 | + rej(e); |
| 141 | + } |
| 142 | + }); |
225 | 143 | } |
226 | 144 | } |
227 | 145 | }; |
228 | 146 |
|
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 | | - |
279 | 147 | module.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) { |
282 | 150 | log.d(`filtering ${uids.length} uids by message`); |
283 | 151 |
|
284 | 152 | let q = messageQuery(message); |
|
0 commit comments