 
 # shared functionnality : find the mail via its ID
 before ->
-    Mail.find req.params.id, (err, box) =>
-        if err or !box
+    Mail.find req.params.id, (err, mail) =>
+        if err or not mail
             send 404
         else
-            @box = box
+            @mail = mail
             next()
 , { only: ['show', 'update', 'destroy', 'getattachmentslist'] }
 

# GET '/getattachments/:id
action 'getattachmentslist', ->
    query =
        key: @box.id

    Attachment.fromMail query, (err, attachments) ->
        if err


# GET /mails/:id
action 'show', ->
    send @mail


# PUT /mails/:id
# Modify given mail. If mail read flag is changed, the remote mail version is 
# updated too.
action 'update', ->
    markRead = false
    if body.read and not @mail.read
        markRead = true

    @mail.updateAttributes body, (err) =>
        if err
            send 500
        else
            if markRead
                Mailbox.find @mail.mailbox, (err, mailbox) =>
                    if err or not mailbox?
                        send error: "unknown mailbox can't update read flag", 500
                    else
                        mailbox.markMailAsRead @mail, (err) ->
                            if err
                                send error: "can't update read flag", 500
                            else
                                send 200


# DELETE /mails/:id
action 'destroy', ->
    @mail.destroy (err) =>
        if err
            send 500
        else
