Skip to content

Commit 8f434df

Browse files
committed
return general and original message object in listenTo callback
1 parent 61ae426 commit 8f434df

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

js/build/embark.bundle.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ var EmbarkJS =
250250
var ipfs;
251251
if (provider === 'whisper') {
252252
this.currentMessages = EmbarkJS.Messages.Whisper;
253+
this.currentMessages.identity = web3.shh.newIdentity();
253254
} else if (provider === 'orbit') {
254255
this.currentMessages = EmbarkJS.Messages.Orbit;
255256
if (options === undefined) {
@@ -278,7 +279,7 @@ var EmbarkJS =
278279
EmbarkJS.Messages.Whisper.sendMessage = function(options) {
279280
var topics = options.topic || options.topics;
280281
var data = options.data || options.payload;
281-
var identity = options.identity || web3.shh.newIdentity();
282+
var identity = options.identity || this.identity || web3.shh.newIdentity();
282283
var ttl = options.ttl || 100;
283284
var priority = options.priority || 1000;
284285

@@ -319,7 +320,7 @@ var EmbarkJS =
319320
var topics = options.topic || options.topics;
320321

321322
if (typeof topics === 'string') {
322-
topics = [topics];
323+
topics = [web3.fromAscii(topics)];
323324
} else {
324325
// TODO: replace with es6 + babel;
325326
var _topics = [];
@@ -349,10 +350,17 @@ var EmbarkJS =
349350

350351
var filter = web3.shh.filter(filterOptions, function(err, result) {
351352
var payload = JSON.parse(web3.toAscii(result.payload));
353+
var data;
352354
if (err) {
353355
promise.error(err);
354356
} else {
355-
promise.cb(payload);
357+
data = {
358+
topic: topics.map((t) => web3.toAscii(t)),
359+
data: payload,
360+
from: result.from,
361+
time: (new Date(result.sent * 1000))
362+
};
363+
promise.cb(payload, data, result);
356364
}
357365
});
358366

@@ -418,9 +426,14 @@ var EmbarkJS =
418426
var promise = new messageEvents();
419427

420428
this.orbit.events.on('message', (topics, message) => {
421-
// Get the actual content of the message
422429
self.orbit.getPost(message.payload.value, true).then((post) => {
423-
promise.cb(post);
430+
var data = {
431+
topic: topics,
432+
data: post.content,
433+
from: post.meta.from.name,
434+
time: (new Date(post.meta.ts))
435+
};
436+
promise.cb(post.content, data, post);
424437
});
425438
});
426439

js/embark.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ EmbarkJS.Messages.setProvider = function(provider, options) {
203203
var ipfs;
204204
if (provider === 'whisper') {
205205
this.currentMessages = EmbarkJS.Messages.Whisper;
206+
this.currentMessages.identity = web3.shh.newIdentity();
206207
} else if (provider === 'orbit') {
207208
this.currentMessages = EmbarkJS.Messages.Orbit;
208209
if (options === undefined) {
@@ -231,7 +232,7 @@ EmbarkJS.Messages.Whisper = {
231232
EmbarkJS.Messages.Whisper.sendMessage = function(options) {
232233
var topics = options.topic || options.topics;
233234
var data = options.data || options.payload;
234-
var identity = options.identity || web3.shh.newIdentity();
235+
var identity = options.identity || this.identity || web3.shh.newIdentity();
235236
var ttl = options.ttl || 100;
236237
var priority = options.priority || 1000;
237238

@@ -272,7 +273,7 @@ EmbarkJS.Messages.Whisper.listenTo = function(options) {
272273
var topics = options.topic || options.topics;
273274

274275
if (typeof topics === 'string') {
275-
topics = [topics];
276+
topics = [web3.fromAscii(topics)];
276277
} else {
277278
// TODO: replace with es6 + babel;
278279
var _topics = [];
@@ -302,10 +303,17 @@ EmbarkJS.Messages.Whisper.listenTo = function(options) {
302303

303304
var filter = web3.shh.filter(filterOptions, function(err, result) {
304305
var payload = JSON.parse(web3.toAscii(result.payload));
306+
var data;
305307
if (err) {
306308
promise.error(err);
307309
} else {
308-
promise.cb(payload);
310+
data = {
311+
topic: topics.map((t) => web3.toAscii(t)),
312+
data: payload,
313+
from: result.from,
314+
time: (new Date(result.sent * 1000))
315+
};
316+
promise.cb(payload, data, result);
309317
}
310318
});
311319

@@ -371,9 +379,14 @@ EmbarkJS.Messages.Orbit.listenTo = function(options) {
371379
var promise = new messageEvents();
372380

373381
this.orbit.events.on('message', (topics, message) => {
374-
// Get the actual content of the message
375382
self.orbit.getPost(message.payload.value, true).then((post) => {
376-
promise.cb(post.content);
383+
var data = {
384+
topic: topics,
385+
data: post.content,
386+
from: post.meta.from.name,
387+
time: (new Date(post.meta.ts))
388+
};
389+
promise.cb(post.content, data, post);
377390
});
378391
});
379392

0 commit comments

Comments
 (0)