@Override public Map<MessageUid, Flags> setFlags(final Flags flags, final FlagsUpdateMode flagsUpdateMode, final MessageRange set, MailboxSession mailboxSession) throws MailboxException { if (!isWriteable(mailboxSession)) { throw new ReadOnlyException(getMailboxPath(), mailboxSession.getPathDelimiter()); } final SortedMap<MessageUid, Flags> newFlagsByUid = new TreeMap<>(); trimFlags(flags, mailboxSession); final MessageMapper messageMapper = mapperFactory.getMessageMapper(mailboxSession); Iterator<UpdatedFlags> it = messageMapper.execute(() -> messageMapper.updateFlags(getMailboxEntity(), new FlagsUpdateCalculator(flags, flagsUpdateMode), set)); final SortedMap<MessageUid, UpdatedFlags> uFlags = new TreeMap<>(); while (it.hasNext()) { UpdatedFlags flag = it.next(); newFlagsByUid.put(flag.getUid(), flag.getNewFlags()); uFlags.put(flag.getUid(), flag); } dispatcher.flagsUpdated(mailboxSession, new ArrayList<>(uFlags.keySet()), getMailboxEntity(), new ArrayList<>(uFlags.values())); return newFlagsByUid; }
/** * Return a List which holds all uids of recent messages and optional reset * the recent flag on the messages for the uids */ protected List<MessageUid> recent(final boolean reset, MailboxSession mailboxSession) throws MailboxException { if (reset) { if (!isWriteable(mailboxSession)) { throw new ReadOnlyException(getMailboxPath(), mailboxSession.getPathDelimiter()); } } final MessageMapper messageMapper = mapperFactory.getMessageMapper(mailboxSession); return messageMapper.execute(() -> { final List<MessageUid> members = messageMapper.findRecentMessageUidsInMailbox(getMailboxEntity()); // Convert to MessageRanges so we may be able to optimize the // flag update List<MessageRange> ranges = MessageRange.toRanges(members); for (MessageRange range : ranges) { if (reset) { // only call save if we need to messageMapper.updateFlags(getMailboxEntity(), new FlagsUpdateCalculator(new Flags(Flag.RECENT), FlagsUpdateMode.REMOVE), range); } } return members; }); }