(Context context, JSONArray jargs, EntityAccount account, EntityFolder folder, EntityMessage message, IMAPStore istore, IMAPFolder ifolder, State state)
| 2265 | } |
| 2266 | |
| 2267 | private static void onDetach(Context context, JSONArray jargs, EntityAccount account, EntityFolder folder, EntityMessage message, IMAPStore istore, IMAPFolder ifolder, State state) throws JSONException, MessagingException, IOException { |
| 2268 | DB db = DB.getInstance(context); |
| 2269 | |
| 2270 | JSONArray jids = jargs.getJSONArray(0); |
| 2271 | List<Long> ids = new ArrayList<>(); |
| 2272 | for (int i = 0; i < jids.length(); i++) |
| 2273 | ids.add(jids.getLong(i)); |
| 2274 | |
| 2275 | if (message.uid == null) |
| 2276 | throw new IllegalArgumentException("Delete attachments uid missing"); |
| 2277 | |
| 2278 | EntityFolder trash = db.folder().getFolderByType(message.account, EntityFolder.TRASH); |
| 2279 | |
| 2280 | // Get message |
| 2281 | Message imessage = ifolder.getMessageByUID(message.uid); |
| 2282 | if (imessage == null) |
| 2283 | throw new MessageRemovedException(); |
| 2284 | |
| 2285 | String msgid = EntityMessage.generateMessageId(); |
| 2286 | String ref = (TextUtils.isEmpty(message.references) |
| 2287 | ? message.msgid |
| 2288 | : message.references + " " + message.msgid); |
| 2289 | MimeMessage icopy = new MimeMessageEx((MimeMessage) imessage, msgid); |
| 2290 | icopy.addHeader("References", MessageHelper.limitReferences(ref)); |
| 2291 | icopy.addHeader(MessageHelper.HEADER_MODIFIED_TIME, Long.toString(new Date().getTime())); |
| 2292 | MessageHelper helper = new MessageHelper(icopy, context); |
| 2293 | MessageHelper.MessageParts parts = helper.getMessageParts(); |
| 2294 | List<MessageHelper.AttachmentPart> aparts = parts.getAttachmentParts(); |
| 2295 | |
| 2296 | List<EntityAttachment> attachments = db.attachment().getAttachments(message.id); |
| 2297 | for (EntityAttachment attachment : attachments) |
| 2298 | if (ids.contains(attachment.id)) { |
| 2299 | Part apart = aparts.get(attachment.sequence - 1).part; |
| 2300 | if (!deletePart(icopy, apart)) |
| 2301 | throw new IllegalArgumentException("Attachment part not found"); |
| 2302 | } |
| 2303 | |
| 2304 | ifolder.appendMessages(new Message[]{icopy}); |
| 2305 | |
| 2306 | Long uid = findUid(context, account, ifolder, msgid, null); |
| 2307 | if (uid != null) { |
| 2308 | JSONArray fargs = new JSONArray(); |
| 2309 | fargs.put(uid); |
| 2310 | onFetch(context, fargs, folder, istore, ifolder, state); |
| 2311 | } |
| 2312 | |
| 2313 | if (trash == null) { |
| 2314 | imessage.setFlag(Flags.Flag.DELETED, true); |
| 2315 | expunge(context, ifolder, Arrays.asList(imessage)); |
| 2316 | } else |
| 2317 | EntityOperation.queue(context, message, EntityOperation.MOVE, trash.id); |
| 2318 | } |
| 2319 | |
| 2320 | static boolean deletePart(Part part, Part attachment) throws MessagingException, IOException { |
| 2321 | boolean deleted = false; |
no test coverage detected