forked from linagora/jmap-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
ddolcimascolo
committed
Feb 10, 2016
1 parent
23ae75f
commit 8c03835
Showing
4 changed files
with
250 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -494,28 +494,21 @@ export default class Client { | |
* @see http://jmap.io/spec.html#saving-a-draft | ||
*/ | ||
saveAsDraft(message) { | ||
Utils.assertRequiredParameterHasType(message, 'message', OutboundMessage); | ||
|
||
var clientId = this._generateClientId(); | ||
|
||
return this | ||
.getMailboxWithRole(MailboxRole.DRAFTS) | ||
.then(mailbox => { | ||
message.mailboxIds = [mailbox.id]; | ||
message.isDraft = true; | ||
|
||
return this.setMessages({ | ||
create: { | ||
[clientId]: message.toJSONObject() | ||
} | ||
}); | ||
}).then(response => { | ||
if (!response.created[clientId]) { | ||
throw new Error('Failed to save the message as draft, clientId: ' + clientId + ', message: ' + (response.notCreated[clientId] || 'none')); | ||
} | ||
return this._createMessage(message, MailboxRole.DRAFTS, true); | ||
} | ||
|
||
return new CreateMessageAck(this, response.created[clientId]); | ||
}); | ||
/** | ||
* Sends a message by issuing a _setMessages_ JMAP request.<br /> | ||
* The _mailboxIds_ and _isDraft_ properties of the given _message_ will be overridden by this method. | ||
* | ||
* @param message {OutboundMessage} The message to save. | ||
* | ||
* @return {Promise} A {@link Promise} that eventually resolves to a {@link CreateMessageAck}. | ||
* | ||
* @see http://jmap.io/spec.html#sending-messages | ||
*/ | ||
send(message) { | ||
return this._createMessage(message, MailboxRole.OUTBOX, null); | ||
} | ||
|
||
/** | ||
|
@@ -536,6 +529,31 @@ export default class Client { | |
return this.updateMessage(id, { mailboxIds: mailboxIds }); | ||
} | ||
|
||
_createMessage(message, role, isDraft) { | ||
Utils.assertRequiredParameterHasType(message, 'message', OutboundMessage); | ||
|
||
var clientId = this._generateClientId(); | ||
|
||
return this | ||
.getMailboxWithRole(role) | ||
.then(mailbox => { | ||
message.mailboxIds = [mailbox.id]; | ||
message.isDraft = isDraft; | ||
|
||
return this.setMessages({ | ||
create: { | ||
[clientId]: message.toJSONObject() | ||
} | ||
}); | ||
}).then(response => { | ||
if (!response.created[clientId]) { | ||
throw new Error('Failed to store message with clientId ' + clientId + '. Error: ' + (response.notCreated[clientId] || 'none')); | ||
} | ||
|
||
return new CreateMessageAck(this, response.created[clientId]); | ||
}); | ||
} | ||
|
||
_generateClientId() { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
ddolcimascolo
Owner
|
||
return Date.now(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict'; | ||
|
||
var jmap = require('../../dist/jmap-client.min'), | ||
q = require('q'), | ||
options = require('node-getopt').create([ | ||
['', 'token=ARG', ''], | ||
['', 'url=ARG', ''], | ||
['', 'from=ARG', ''], | ||
['', 'to=ARG', ''], | ||
['', 'cc=ARG', ''], | ||
['', 'subject=ARG', ''], | ||
['', 'body=ARG', ''] | ||
]).parseSystem().options; | ||
|
||
var client = new jmap.Client(new jmap.RequestTransport(), new jmap.QPromiseProvider()); | ||
|
||
client | ||
.withAPIUrl(options.url) | ||
.withAuthenticationToken(options.token) | ||
.getAccounts() | ||
.then(function(accounts) { | ||
console.log('Sending message to ' + options.to + ' using account: ' + accounts[0].name); | ||
|
||
return client.send(new jmap.OutboundMessage(client, { | ||
from: { email: options.from }, | ||
to: [{ email: options.to }], | ||
cc: [{ email: options.cc }], | ||
subject: options.subject, | ||
textBody: options.body | ||
})); | ||
}) | ||
.then(function() { console.log('Message sent !'); }, console.log); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 comment
on commit 8c03835
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
great !
come on, you can do better that Date.now !