({ userId, chatId, batchNumberForMessages, limit })
| 146 | |
| 147 | class MessageClass extends mongoose.Model { |
| 148 | public static async getList({ userId, chatId, batchNumberForMessages, limit }) { |
| 149 | await this.checkPermission({ userId, chatId }); |
| 150 | |
| 151 | const filter = { chatId, parentMessageId: null }; |
| 152 | |
| 153 | const messages = await this.find(filter) |
| 154 | .sort({ createdAt: -1, _id: 1 }) |
| 155 | .skip((batchNumberForMessages - 1) * limit) |
| 156 | .limit(limit) |
| 157 | .setOptions({ lean: true }); |
| 158 | |
| 159 | // use for...of instead of forEach |
| 160 | // forEach does not care about promises from await |
| 161 | for (const message of messages) { |
| 162 | const threadMessages = await this.find({ |
| 163 | parentMessageId: message._id.toString(), |
| 164 | }); |
| 165 | |
| 166 | message.countOfThreadMessages = threadMessages.length; |
| 167 | } |
| 168 | |
| 169 | return messages; |
| 170 | } |
| 171 | |
| 172 | public static async getListForThread({ userId, chatId, messageId }) { |
| 173 | await this.checkPermission({ userId, chatId }); |
nothing calls this directly
no test coverage detected