mirror of
https://github.com/nodemailer/wildduck.git
synced 2025-01-09 09:27:38 +08:00
Merge pull request #353 from louis-lau/threadcounters
Add optional thread message counters to listing
This commit is contained in:
commit
ceab52944b
2 changed files with 55 additions and 0 deletions
|
@ -1503,6 +1503,11 @@ paths:
|
|||
description: 'If true, then includes metaData in the response'
|
||||
schema:
|
||||
type: boolean
|
||||
- name: threadCounters
|
||||
in: query
|
||||
description: 'If true, then includes threadMessageCount in the response. Counters come with some overhead'
|
||||
schema:
|
||||
type: boolean
|
||||
- name: limit
|
||||
in: query
|
||||
description: How many records to return
|
||||
|
@ -1828,6 +1833,11 @@ paths:
|
|||
description: Maximal message size in bytes
|
||||
schema:
|
||||
type: number
|
||||
- name: threadCounters
|
||||
in: query
|
||||
description: 'If true, then includes threadMessageCount in the response. Counters come with some overhead'
|
||||
schema:
|
||||
type: boolean
|
||||
- name: limit
|
||||
in: query
|
||||
description: How many records to return
|
||||
|
@ -6379,6 +6389,9 @@ components:
|
|||
thread:
|
||||
type: string
|
||||
description: ID of the Thread
|
||||
threadMessageCount:
|
||||
type: number
|
||||
description: Amount of messages in the Thread. Included if threadCounters query argument was true
|
||||
from:
|
||||
$ref: '#/components/schemas/Address'
|
||||
to:
|
||||
|
|
|
@ -61,6 +61,35 @@ module.exports = (db, server, messageHandler, userHandler, storageHandler, setti
|
|||
});
|
||||
});
|
||||
|
||||
const addThreadCountersToMessageList = async (user, list) => {
|
||||
const threadIdsToCount = list.map(message => message.thread);
|
||||
const threadCounts = await db.database
|
||||
.collection('messages')
|
||||
.aggregate([
|
||||
{
|
||||
$match: {
|
||||
user: new ObjectId(user),
|
||||
thread: { $in: threadIdsToCount }
|
||||
}
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
_id: '$thread',
|
||||
count: {
|
||||
$sum: 1
|
||||
}
|
||||
}
|
||||
}
|
||||
])
|
||||
.toArray();
|
||||
|
||||
return list.map(message => {
|
||||
const matchingThreadCount = threadCounts.find(thread => thread._id.toString() === message.thread.toString());
|
||||
message.threadMessageCount = matchingThreadCount ? matchingThreadCount.count : undefined
|
||||
return message
|
||||
});
|
||||
};
|
||||
|
||||
const putMessageHandler = async (req, res, next) => {
|
||||
res.charSet('utf-8');
|
||||
|
||||
|
@ -267,6 +296,7 @@ module.exports = (db, server, messageHandler, userHandler, storageHandler, setti
|
|||
mailbox: Joi.string().hex().lowercase().length(24).required(),
|
||||
unseen: booleanSchema,
|
||||
metaData: booleanSchema.default(false),
|
||||
threadCounters: booleanSchema.default(false),
|
||||
limit: Joi.number().empty('').default(20).min(1).max(250),
|
||||
order: Joi.any().empty('').allow('asc', 'desc').default('desc'),
|
||||
next: nextPageCursorSchema,
|
||||
|
@ -302,6 +332,7 @@ module.exports = (db, server, messageHandler, userHandler, storageHandler, setti
|
|||
let user = new ObjectId(result.value.user);
|
||||
let mailbox = new ObjectId(result.value.mailbox);
|
||||
let limit = result.value.limit;
|
||||
let threadCounters = result.value.threadCounters;
|
||||
let page = result.value.page;
|
||||
let pageNext = result.value.next;
|
||||
let pagePrevious = result.value.previous;
|
||||
|
@ -410,6 +441,10 @@ module.exports = (db, server, messageHandler, userHandler, storageHandler, setti
|
|||
page = 1;
|
||||
}
|
||||
|
||||
if (threadCounters) {
|
||||
listing.results = await addThreadCountersToMessageList(user, listing.results);
|
||||
}
|
||||
|
||||
let response = {
|
||||
success: true,
|
||||
total,
|
||||
|
@ -454,6 +489,7 @@ module.exports = (db, server, messageHandler, userHandler, storageHandler, setti
|
|||
flagged: booleanSchema,
|
||||
unseen: booleanSchema,
|
||||
searchable: booleanSchema,
|
||||
threadCounters: booleanSchema.default(false),
|
||||
limit: Joi.number().default(20).min(1).max(250),
|
||||
next: nextPageCursorSchema,
|
||||
previous: previousPageCursorSchema,
|
||||
|
@ -505,6 +541,7 @@ module.exports = (db, server, messageHandler, userHandler, storageHandler, setti
|
|||
let filterMinSize = result.value.minSize;
|
||||
let filterMaxSize = result.value.maxSize;
|
||||
|
||||
let threadCounters = result.value.threadCounters;
|
||||
let limit = result.value.limit;
|
||||
let page = result.value.page;
|
||||
let pageNext = result.value.next;
|
||||
|
@ -819,6 +856,10 @@ module.exports = (db, server, messageHandler, userHandler, storageHandler, setti
|
|||
page = 1;
|
||||
}
|
||||
|
||||
if (threadCounters) {
|
||||
listing.results = await addThreadCountersToMessageList(user, listing.results);
|
||||
}
|
||||
|
||||
let response = {
|
||||
success: true,
|
||||
query,
|
||||
|
@ -3139,6 +3180,7 @@ function formatMessageListing(messageData) {
|
|||
id: messageData.uid,
|
||||
mailbox: messageData.mailbox,
|
||||
thread: messageData.thread,
|
||||
threadMessageCount: messageData.threadMessageCount,
|
||||
from: from && from[0],
|
||||
to,
|
||||
cc,
|
||||
|
|
Loading…
Reference in a new issue