Add required metadata to sending pipeline and send to public channels

pull/447/head
Beaudan Brown 6 years ago
parent 1cabc8d02b
commit b5fd01a468

@ -1374,8 +1374,9 @@
const options = this.getSendOptions(); const options = this.getSendOptions();
options.messageType = message.get('type'); options.messageType = message.get('type');
options.isPublic = this.isPublic();
if (this.isPublic()) { if (this.isPublic()) {
options.publicEndpoint = this.getEndpoint(); options.channelSettings = this.getPublicSource();
} }
const groupNumbers = this.getRecipients(); const groupNumbers = this.getRecipients();
@ -2064,18 +2065,9 @@
return { return {
server: this.get('server'), server: this.get('server'),
channelId: this.get('channelId'), channelId: this.get('channelId'),
conversationId: this.get('id'),
}; };
}, },
// FIXME: remove or add public and/or "sending" hint to name...
getEndpoint() {
if (!this.isPublic()) {
return null;
}
const server = this.get('server');
const channelId = this.get('channelId');
const endpoint = `${server}/channels/${channelId}/messages`;
return endpoint;
},
// SIGNAL PROFILES // SIGNAL PROFILES

@ -1,6 +1,7 @@
/* eslint-disable no-await-in-loop */ /* eslint-disable no-await-in-loop */
/* eslint-disable no-loop-func */ /* eslint-disable no-loop-func */
/* global log, dcodeIO, window, callWorker, lokiP2pAPI, lokiSnodeAPI, textsecure */ /* global log, dcodeIO, window, callWorker,
lokiP2pAPI, lokiSnodeAPI, lokiPublicChatAPI, textsecure */
const _ = require('lodash'); const _ = require('lodash');
const { rpc } = require('./loki_rpc'); const { rpc } = require('./loki_rpc');
@ -78,8 +79,9 @@ class LokiMessageAPI {
async sendMessage(pubKey, data, messageTimeStamp, ttl, options = {}) { async sendMessage(pubKey, data, messageTimeStamp, ttl, options = {}) {
const { const {
isPing = false, isPing = false,
isPublic = false,
numConnections = DEFAULT_CONNECTIONS, numConnections = DEFAULT_CONNECTIONS,
publicEndpoint = null, channelSettings = null,
} = options; } = options;
// Data required to identify a message in a conversation // Data required to identify a message in a conversation
const messageEventData = { const messageEventData = {
@ -87,10 +89,22 @@ class LokiMessageAPI {
timestamp: messageTimeStamp, timestamp: messageTimeStamp,
}; };
// FIXME: should have public/sending(ish hint) in the option to make if (isPublic) {
// this more obvious...
if (publicEndpoint) {
// could we emit back to LokiPublicChannelAPI somehow? // could we emit back to LokiPublicChannelAPI somehow?
const { server, channelId, conversationId } = channelSettings;
const serverAPI = lokiPublicChatAPI.findOrCreateServer(server);
const token = await serverAPI.getServerToken();
if (!token) {
throw new window.textsecure.PublicChatError(
`Failed to retrieve valid token for ${conversationId}`
);
}
const channelAPI = serverAPI.findOrCreateChannel(
channelId,
conversationId
);
const publicEndpoint = channelAPI.getEndpoint(conversationId);
const { profile } = data; const { profile } = data;
let displayName = 'Anonymous'; let displayName = 'Anonymous';
if (profile && profile.displayName) { if (profile && profile.displayName) {
@ -109,24 +123,34 @@ class LokiMessageAPI {
}, },
], ],
}; };
let result;
try { try {
const result = await nodeFetch(publicEndpoint, { result = await nodeFetch(publicEndpoint, {
method: 'post', method: 'post',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
Authorization: 'Bearer loki', Authorization: `Bearer ${token}`,
}, },
body: JSON.stringify(payload), body: JSON.stringify(payload),
}); });
const body = await result.json();
messageEventData.serverId = body.data.id;
window.Whisper.events.trigger('publicMessageSent', messageEventData);
return;
} catch (e) { } catch (e) {
throw new window.textsecure.PublicChatError( throw new window.textsecure.PublicChatError(
'Failed to send public chat message.' `Failed to send public chat message: ${e}`
); );
} }
const body = await result.json();
if (!result.ok) {
if (result.status === 401) {
// TODO: Handle token timeout
}
const error = body.meta.error_message;
throw new window.textsecure.PublicChatError(
`Failed to send public chat message: ${error}`
);
}
messageEventData.serverId = body.data.id;
window.Whisper.events.trigger('publicMessageSent', messageEventData);
return;
} }
const data64 = dcodeIO.ByteBuffer.wrap(data).toString('base64'); const data64 = dcodeIO.ByteBuffer.wrap(data).toString('base64');

@ -49,11 +49,13 @@ function OutgoingMessage(
online, online,
messageType, messageType,
isPing, isPing,
publicEndpoint, isPublic,
channelSettings,
} = } =
options || {}; options || {};
this.numberInfo = numberInfo; this.numberInfo = numberInfo;
this.publicEndpoint = publicEndpoint; this.isPublic = isPublic;
this.channelSettings = channelSettings;
this.senderCertificate = senderCertificate; this.senderCertificate = senderCertificate;
this.online = online; this.online = online;
this.messageType = messageType || 'outgoing'; this.messageType = messageType || 'outgoing';
@ -201,8 +203,9 @@ OutgoingMessage.prototype = {
numConnections: NUM_SEND_CONNECTIONS, numConnections: NUM_SEND_CONNECTIONS,
isPing: this.isPing, isPing: this.isPing,
}; };
if (this.publicEndpoint) { options.isPublic = this.isPublic;
options.publicEndpoint = this.publicEndpoint; if (this.isPublic) {
options.channelSettings = this.channelSettings;
} }
await lokiMessageAPI.sendMessage(pubKey, data, timestamp, ttl, options); await lokiMessageAPI.sendMessage(pubKey, data, timestamp, ttl, options);
} catch (e) { } catch (e) {
@ -270,7 +273,7 @@ OutgoingMessage.prototype = {
}, },
doSendMessage(number, deviceIds, recurse) { doSendMessage(number, deviceIds, recurse) {
const ciphers = {}; const ciphers = {};
if (this.publicEndpoint) { if (this.isPublic) {
return this.transmitMessage( return this.transmitMessage(
number, number,
this.message.dataMessage, this.message.dataMessage,

@ -943,7 +943,7 @@ MessageSender.prototype = {
) { ) {
const me = textsecure.storage.user.getNumber(); const me = textsecure.storage.user.getNumber();
let numbers = groupNumbers.filter(number => number !== me); let numbers = groupNumbers.filter(number => number !== me);
if (options.publicEndpoint) { if (options.isPublic) {
numbers = [groupId]; numbers = [groupId];
} }
const profile = textsecure.storage.impl.getLocalProfile(); const profile = textsecure.storage.impl.getLocalProfile();

Loading…
Cancel
Save