Skip to content

Commit

Permalink
Merge pull request #455 from neuroscr/public-delete
Browse files Browse the repository at this point in the history
Public delete
  • Loading branch information
Beaudan Campbell-Brown committed Sep 2, 2019
2 parents 17c2817 + 56a4a31 commit f4e76f0
Show file tree
Hide file tree
Showing 10 changed files with 409 additions and 136 deletions.
4 changes: 4 additions & 0 deletions _locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,10 @@
"delete": {
"message": "Delete"
},
"deletePublicWarning": {
"message":
"Are you sure? Clicking 'delete' will permanently remove this message for everyone in this channel."
},
"deleteWarning": {
"message":
"Are you sure? Clicking 'delete' will permanently remove this message from this device only."
Expand Down
9 changes: 6 additions & 3 deletions app/sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ async function updateToLokiSchemaVersion1(currentVersion, instance) {

await instance.run(
`ALTER TABLE messages
ADD COLUMN serverId STRING;`
ADD COLUMN serverId INTEGER;`
);

await instance.run(
Expand Down Expand Up @@ -2060,11 +2060,14 @@ async function removeMessage(id) {
);
}

async function getMessageByServerId(serverId) {
async function getMessageByServerId(serverId, conversationId) {
const row = await db.get(
'SELECT * FROM messages WHERE serverId = $serverId;',
`SELECT * FROM messages WHERE
serverId = $serverId AND
conversationId = $conversationId;`,
{
$serverId: serverId,
$conversationId: conversationId,
}
);

Expand Down
26 changes: 25 additions & 1 deletion js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,12 @@
);
publicConversations.forEach(conversation => {
const settings = conversation.getPublicSource();
window.lokiPublicChatAPI.registerChannel(
const channel = window.lokiPublicChatAPI.findOrCreateChannel(
settings.server,
settings.channelId,
conversation.id
);
channel.refreshModStatus();
});
window.lokiP2pAPI = new window.LokiP2pAPI(ourKey);
window.lokiP2pAPI.on('pingContact', pubKey => {
Expand Down Expand Up @@ -487,6 +488,28 @@
}
});

Whisper.events.on(
'deleteLocalPublicMessage',
async ({ messageServerId, conversationId }) => {
const message = await window.Signal.Data.getMessageByServerId(
messageServerId,
conversationId,
{
Message: Whisper.Message,
}
);
if (message) {
const conversation = ConversationController.get(conversationId);
if (conversation) {
conversation.removeMessage(message.id);
}
await window.Signal.Data.removeMessage(message.id, {
Message: Whisper.Message,
});
}
}
);

Whisper.events.on('setupAsNewDevice', () => {
const { appView } = window.owsDesktopApp;
if (appView) {
Expand Down Expand Up @@ -1418,6 +1441,7 @@
let messageData = {
source: data.source,
sourceDevice: data.sourceDevice,
serverId: data.serverId,
sent_at: data.timestamp,
received_at: data.receivedAt || Date.now(),
conversationId: data.source,
Expand Down
52 changes: 44 additions & 8 deletions js/models/conversations.js
Original file line number Diff line number Diff line change
Expand Up @@ -1376,7 +1376,7 @@
const options = this.getSendOptions();
options.messageType = message.get('type');
options.isPublic = this.isPublic();
if (this.isPublic()) {
if (options.isPublic) {
options.publicSendData = await this.getPublicSendData();
}

Expand Down Expand Up @@ -2073,17 +2073,28 @@
const serverAPI = lokiPublicChatAPI.findOrCreateServer(
this.get('server')
);
// Can be null if fails
const token = await serverAPI.getOrRefreshServerToken();
const channelAPI = serverAPI.findOrCreateChannel(
this.get('channelId'),
this.id
);
const publicEndpoint = channelAPI.getEndpoint();
return {
publicEndpoint,
token,
};
return channelAPI;
},
getModStatus() {
if (!this.isPublic()) {
return false;
}
return this.get('modStatus');
},
async setModStatus(newStatus) {
if (!this.isPublic()) {
return;
}
if (this.get('modStatus') !== newStatus) {
this.set({ modStatus: newStatus });
await window.Signal.Data.updateConversation(this.id, this.attributes, {
Conversation: Whisper.Conversation,
});
}
},

// SIGNAL PROFILES
Expand Down Expand Up @@ -2288,6 +2299,31 @@
});
},

async deletePublicMessage(message) {
const serverAPI = lokiPublicChatAPI.findOrCreateServer(
this.get('server')
);
const channelAPI = serverAPI.findOrCreateChannel(
this.get('channelId'),
this.id
);
const success = await channelAPI.deleteMessage(message.getServerId());
if (success) {
this.removeMessage(message.id);
}
return success;
},

removeMessage(messageId) {
const message = this.messageCollection.models.find(
msg => msg.id === messageId
);
if (message) {
message.trigger('unload');
this.messageCollection.remove(messageId);
}
},

deleteMessages() {
Whisper.events.trigger('showConfirmationDialog', {
message: i18n('deleteConversationConfirmation'),
Expand Down
10 changes: 7 additions & 3 deletions js/models/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,6 @@
onDestroy() {
this.cleanup();
},
deleteMessage() {
this.trigger('delete', this);
},
async cleanup() {
MessageController.unregister(this.id);
this.unload();
Expand Down Expand Up @@ -675,6 +672,10 @@
isP2p: !!this.get('isP2p'),
isPublic: !!this.get('isPublic'),
isRss: !!this.get('isRss'),
isDeletable:
!this.get('isPublic') ||
this.getConversation().getModStatus() ||
this.getSource() === this.OUR_NUMBER,

onCopyText: () => this.copyText(),
onReply: () => this.trigger('reply', this),
Expand Down Expand Up @@ -1243,6 +1244,9 @@
Message: Whisper.Message,
});
},
getServerId() {
return this.get('serverId');
},
async setServerId(serverId) {
if (_.isEqual(this.get('serverId'), serverId)) return;

Expand Down
4 changes: 2 additions & 2 deletions js/modules/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -908,8 +908,8 @@ async function _removeMessages(ids) {
await channels.removeMessage(ids);
}

async function getMessageByServerId(id, { Message }) {
const message = await channels.getMessageByServerId(id);
async function getMessageByServerId(serverId, conversationId, { Message }) {
const message = await channels.getMessageByServerId(serverId, conversationId);
if (!message) {
return null;
}
Expand Down
53 changes: 9 additions & 44 deletions js/modules/loki_message_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

const _ = require('lodash');
const { rpc } = require('./loki_rpc');
const nodeFetch = require('node-fetch');

const DEFAULT_CONNECTIONS = 3;
const MAX_ACCEPTABLE_FAILURES = 1;
Expand Down Expand Up @@ -89,57 +88,23 @@ class LokiMessageAPI {
};

if (isPublic) {
const { token, publicEndpoint } = publicSendData;
if (!token) {
throw new window.textsecure.PublicChatError(
`Failed to retrieve valid token for ${publicEndpoint}`
);
}

const { profile } = data;
let displayName = 'Anonymous';
if (profile && profile.displayName) {
({ displayName } = profile);
}
const payload = {
text: data.body,
annotations: [
{
type: 'network.loki.messenger.publicChat',
value: {
timestamp: messageTimeStamp,
from: displayName,
source: this.ourKey,
},
},
],
};
let result;
try {
result = await nodeFetch(publicEndpoint, {
method: 'post',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
body: JSON.stringify(payload),
});
} catch (e) {
throw new window.textsecure.PublicChatError(
`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;
const res = await publicSendData.sendMessage(
data.body,
messageTimeStamp,
displayName,
this.ourKey
);
if (res === false) {
throw new window.textsecure.PublicChatError(
`Failed to send public chat message: ${error}`
'Failed to send public chat message'
);
}
messageEventData.serverId = body.data.id;
messageEventData.serverId = res;
window.Whisper.events.trigger('publicMessageSent', messageEventData);
return;
}
Expand Down
Loading

0 comments on commit f4e76f0

Please sign in to comment.