Skip to content
This repository has been archived by the owner on Jul 4, 2023. It is now read-only.

Commit

Permalink
Merge c4c3355 into fd97a47
Browse files Browse the repository at this point in the history
  • Loading branch information
ralphtheninja committed Oct 8, 2018
2 parents fd97a47 + c4c3355 commit 6b5ff1d
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 71 deletions.
48 changes: 26 additions & 22 deletions README.md
Expand Up @@ -88,7 +88,7 @@ The high level JavaScript API is a collection of classes wrapping most context t
* [<code><b>class Lot</b></code>](#class_lot)
* [<code><b>class Message</b></code>](#class_message)
* [<code><b>class MessageState</b></code>](#class_message_state)
* [<code><b>class MessageType</b></code>](#class_message_type)
* [<code><b>class MessageViewType</b></code>](#class_message_view_type)

<a name="deltachat_ctor"></a>
### `dc = DeltaChat()`
Expand Down Expand Up @@ -371,9 +371,17 @@ Mark all messages sent by the given contact as _noticed_. Corresponds to [`dc_ma

Mark a message as _seen_, updates the IMAP state and sends MDNs. Corresponds to [`dc_markseen_msgs()`](https://c.delta.chat/classdc__context__t.html#ae5305a90c09380dffe54e68c2a709128).

#### `dc.messageNew()`
#### `dc.messageNew([viewType])`

Create a new [`Message`](#class_message) object. Corresponds to [`dc_msg_new()`](https://c.delta.chat/classdc__msg__t.html#a3d5e65374c014990c35a0cee9b0ddf87).
Create a new [`Message`](#class_message) object. Corresponds to [`dc_msg_new()`](https://c.delta.chat/classdc__msg__t.html#aa694c4f707ad51918703218cc8887143). The `viewType` parameter is optional and defaults to `DC_MSG_TEXT`. Pick from one of the following values:

* `DC_MSG_TEXT`
* `DC_MSG_AUDIO`
* `DC_MSG_FILE`
* `DC_MSG_GIF`
* `DC_MSG_IMAGE`
* `DC_MSG_VIDEO`
* `DC_MSG_VOICE`

#### `dc.open([cwd], callback)`

Expand Down Expand Up @@ -677,9 +685,9 @@ Get the text of the message. Corresponds to [`dc_msg_get_text()`](https://c.delt

Get message sending time. Corresponds to [`dc_msg_get_timestamp()`](https://c.delta.chat/classdc__msg__t.html#af667c538fd07771eb94d6c5d1879b906).

#### `message.getType()`
#### `message.getViewType()`

Get the type of the message. Returns a [`MessageType`](#class_message_type) object. Corresponds to [`dc_msg_get_type()`](https://c.delta.chat/classdc__msg__t.html#aa1b0e553c44a8df5e9f725087a5186f2).
Get the view type of the message. Returns a [`MessageViewType`](#class_message_view_type) object. Corresponds to [`dc_msg_get_viewtype()`](https://c.delta.chat/classdc__msg__t.html#abbe7ce82d642e217363aa27bcc6274b3).

#### `message.getWidth()`

Expand Down Expand Up @@ -737,10 +745,6 @@ Set the media information assocated with the message object. Corresponds to [`dc

Set the test of a message object. Corresponds to [`dc_msg_set_text()`](https://c.delta.chat/classdc__msg__t.html#a352d9e2cc2fcacac43bb540550a578a1).

#### `message.setType(type)`

Set the type of a message object. Corresponds to [`dc_msg_set_type()`](https://c.delta.chat/classdc__msg__t.html#a5d5568d88453a31a1379299d9c155c4f).

#### `message.toJson()`

Returns the object state as a JavaScript serializable object.
Expand Down Expand Up @@ -790,46 +794,46 @@ Internal `state` property.

------------------------------------

<a name="class_message_type"></a>
### `class MessageType`
<a name="class_message_view_type"></a>
### `class MessageViewType`

An object representing a [`Message`](#class_message) type.
An object representing a [`Message`](#class_message) view type.

#### `type.isUndefined()`
#### `viewType.isUndefined()`

Message type is `DC_MSG_UNDEFINED`.

#### `type.isText()`
#### `viewType.isText()`

Message type is `DC_MSG_TEXT`.

#### `type.isImage()`
#### `viewType.isImage()`

Message type has `DC_MSG_IMAGE` bits set.

#### `type.isGif()`
#### `viewType.isGif()`

Message type is `DC_MSG_GIF`.

#### `type.isAudio()`
#### `viewType.isAudio()`

Message type has `DC_MSG_AUDIO` bits set.

#### `type.isVoice()`
#### `viewType.isVoice()`

Message type is `DC_MSG_VOICE`.

#### `type.isVideo()`
#### `viewType.isVideo()`

Message type has `DC_MSG_VIDEO` bits set.

#### `type.isFile()`
#### `viewType.isFile()`

Message type is `DC_MSG_FILE`.

#### `type.type`
#### `viewType.viewType`

Internal `type` property.
Internal `viewType` property.

## Events

Expand Down
4 changes: 2 additions & 2 deletions index.js
Expand Up @@ -436,8 +436,8 @@ class DeltaChat extends EventEmitter {
binding.dcn_markseen_msgs(this.dcn_context, messageIds)
}

messageNew () {
return new Message(binding.dcn_msg_new(this.dcn_context))
messageNew (viewType = C.DC_MSG_TEXT) {
return new Message(binding.dcn_msg_new(this.dcn_context, viewType))
}

open (cwd, cb) {
Expand Down
34 changes: 15 additions & 19 deletions message.js
Expand Up @@ -51,44 +51,44 @@ class MessageState {
/**
* Helper class for message types so you can do e.g.
*
* if (msg.getType().isVideo()) { .. }
* if (msg.getViewType().isVideo()) { .. }
*
*/
class MessageType {
constructor (type) {
this.type = type
class MessageViewType {
constructor (viewType) {
this.viewType = viewType
}

isUndefined () {
return this.type === C.DC_MSG_UNDEFINED
return this.viewType === C.DC_MSG_UNDEFINED
}

isText () {
return this.type === C.DC_MSG_TEXT
return this.viewType === C.DC_MSG_TEXT
}

isImage () {
return this.type === C.DC_MSG_IMAGE || this.type === C.DC_MSG_GIF
return this.viewType === C.DC_MSG_IMAGE || this.viewType === C.DC_MSG_GIF
}

isGif () {
return this.type === C.DC_MSG_GIF
return this.viewType === C.DC_MSG_GIF
}

isAudio () {
return this.type === C.DC_MSG_AUDIO || this.type === C.DC_MSG_VOICE
return this.viewType === C.DC_MSG_AUDIO || this.viewType === C.DC_MSG_VOICE
}

isVoice () {
return this.type === C.DC_MSG_VOICE
return this.viewType === C.DC_MSG_VOICE
}

isVideo () {
return this.type === C.DC_MSG_VIDEO
return this.viewType === C.DC_MSG_VIDEO
}

isFile () {
return this.type === C.DC_MSG_FILE
return this.viewType === C.DC_MSG_FILE
}
}

Expand All @@ -108,7 +108,7 @@ class Message {
fromId: this.getFromId(),
text: this.getText(),
timestamp: this.getTimestamp(),
type: binding.dcn_msg_get_type(this.dc_msg),
viewType: binding.dcn_msg_get_viewtype(this.dc_msg),
state: binding.dcn_msg_get_state(this.dc_msg),
summary: this.getSummary().toJson(),
isSetupmessage: this.isSetupmessage(),
Expand Down Expand Up @@ -187,8 +187,8 @@ class Message {
return binding.dcn_msg_get_timestamp(this.dc_msg)
}

getType () {
return new MessageType(binding.dcn_msg_get_type(this.dc_msg))
getViewType () {
return new MessageViewType(binding.dcn_msg_get_viewtype(this.dc_msg))
}

getWidth () {
Expand Down Expand Up @@ -247,10 +247,6 @@ class Message {
setText (text) {
binding.dcn_msg_set_text(this.dc_msg, text)
}

setType (type) {
binding.dcn_msg_set_type(this.dc_msg, type)
}
}

module.exports = Message
20 changes: 5 additions & 15 deletions src/module.c
Expand Up @@ -988,9 +988,10 @@ NAPI_METHOD(dcn_markseen_msgs) {
NAPI_METHOD(dcn_msg_new) {
NAPI_ARGV(2);
NAPI_DCN_CONTEXT();
NAPI_ARGV_INT32(viewtype, 1);

napi_value result;
dc_msg_t* msg = dc_msg_new(dcn_context->dc_context);
dc_msg_t* msg = dc_msg_new(dcn_context->dc_context, viewtype);

NAPI_STATUS_THROWS(napi_create_external(env, msg, finalize_msg,
NULL, &result));
Expand Down Expand Up @@ -1936,11 +1937,11 @@ NAPI_METHOD(dcn_msg_get_timestamp) {
NAPI_RETURN_INT32(timestamp);
}

NAPI_METHOD(dcn_msg_get_type) {
NAPI_METHOD(dcn_msg_get_viewtype) {
NAPI_ARGV(1);
NAPI_DC_MSG();

int type = dc_msg_get_type(dc_msg);
int type = dc_msg_get_viewtype(dc_msg);

NAPI_RETURN_INT32(type);
}
Expand Down Expand Up @@ -2084,16 +2085,6 @@ NAPI_METHOD(dcn_msg_set_text) {
NAPI_RETURN_UNDEFINED();
}

NAPI_METHOD(dcn_msg_set_type) {
NAPI_ARGV(2);
NAPI_DC_MSG();
NAPI_ARGV_INT32(type, 1);

dc_msg_set_type(dc_msg, type);

NAPI_RETURN_UNDEFINED();
}

NAPI_INIT() {
/**
* Main context
Expand Down Expand Up @@ -2250,7 +2241,7 @@ NAPI_INIT() {
NAPI_EXPORT_FUNCTION(dcn_msg_get_summarytext);
NAPI_EXPORT_FUNCTION(dcn_msg_get_text);
NAPI_EXPORT_FUNCTION(dcn_msg_get_timestamp);
NAPI_EXPORT_FUNCTION(dcn_msg_get_type);
NAPI_EXPORT_FUNCTION(dcn_msg_get_viewtype);
NAPI_EXPORT_FUNCTION(dcn_msg_get_width);
NAPI_EXPORT_FUNCTION(dcn_msg_is_forwarded);
NAPI_EXPORT_FUNCTION(dcn_msg_is_increation);
Expand All @@ -2264,5 +2255,4 @@ NAPI_INIT() {
NAPI_EXPORT_FUNCTION(dcn_msg_set_file);
NAPI_EXPORT_FUNCTION(dcn_msg_set_mediainfo);
NAPI_EXPORT_FUNCTION(dcn_msg_set_text);
NAPI_EXPORT_FUNCTION(dcn_msg_set_type);
}
20 changes: 8 additions & 12 deletions test/integration.js
Expand Up @@ -233,15 +233,14 @@ test('new message and Message methods', t => {
t.is(msg.getText(), text, 'msg text set correctly')
t.is(msg.getTimestamp(), 0, 'no timestamp')

let type = msg.getType()
t.is(type.isUndefined(), true, 'no message type set')
t.is(type.isText(), false, 'no message type set')
t.is(type.isImage(), false, 'no message type set')
t.is(type.isGif(), false, 'no message type set')
t.is(type.isAudio(), false, 'no message type set')
t.is(type.isVoice(), false, 'no message type set')
t.is(type.isVideo(), false, 'no message type set')
t.is(type.isFile(), false, 'no message type set')
const viewType = msg.getViewType()
t.is(viewType.isText(), true)
t.is(viewType.isImage(), false)
t.is(viewType.isGif(), false)
t.is(viewType.isAudio(), false)
t.is(viewType.isVoice(), false)
t.is(viewType.isVideo(), false)
t.is(viewType.isFile(), false)

t.is(msg.getWidth(), 0, 'no message width')
t.is(msg.isDeadDrop(), false, 'not deaddrop')
Expand Down Expand Up @@ -288,9 +287,6 @@ test('new message and Message methods', t => {
t.is(mi.getText1(), 'deltaX', 'text1 set')
t.is(mi.getText2(), 'rules', 'text2 set')

msg.setType(c.DC_MSG_AUDIO)
t.is(msg.getType().isAudio(), true, 'type set correctly')

const json = msg.toJson()
t.notEqual(json, null, 'not null')
t.is(typeof json, 'object', 'json object')
Expand Down

0 comments on commit 6b5ff1d

Please sign in to comment.