Skip to content

Commit

Permalink
Prevent send and show toast for invalid conversations
Browse files Browse the repository at this point in the history
  • Loading branch information
scottnonnenberg-signal committed Nov 16, 2020
1 parent 34be074 commit 3ee830a
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 44 deletions.
4 changes: 4 additions & 0 deletions _locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,10 @@
"message": "You are no longer a member of the group.",
"description": "Displayed when a user can't send a message because they have left the group"
},
"invalidConversation": {
"message": "This group is invalid. Please create a new group.",
"description": "Displayed when a user can't send a message because something has gone wrong in the conversation."
},
"scrollDown": {
"message": "Scroll to bottom of conversation",
"description": "Alt text for button to take user down to bottom of conversation, shown when user scrolls up"
Expand Down
2 changes: 2 additions & 0 deletions ts/groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ type UpdatesResultType = {
// Constants

export const MASTER_KEY_LENGTH = 32;
export const ID_V1_LENGTH = 16;
export const ID_LENGTH = 32;
const TEMPORAL_AUTH_REJECTED_CODE = 401;
const GROUP_ACCESS_DENIED_CODE = 403;
const SUPPORTED_CHANGE_EPOCH = 0;
Expand Down
12 changes: 10 additions & 2 deletions ts/models/conversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,8 @@ export class ConversationModel extends window.Backbone.Model<
return false;
}

return fromEncodedBinaryToArrayBuffer(groupId).byteLength === 16;
const buffer = fromEncodedBinaryToArrayBuffer(groupId);
return buffer.byteLength === window.Signal.Groups.ID_V1_LENGTH;
}

isGroupV2(): boolean {
Expand All @@ -277,7 +278,10 @@ export class ConversationModel extends window.Backbone.Model<

const groupVersion = this.get('groupVersion') || 0;

return groupVersion === 2 && base64ToArrayBuffer(groupId).byteLength === 32;
return (
groupVersion === 2 &&
base64ToArrayBuffer(groupId).byteLength === window.Signal.Groups.ID_LENGTH
);
}

isMemberPending(conversationId: string): boolean {
Expand Down Expand Up @@ -822,6 +826,10 @@ export class ConversationModel extends window.Backbone.Model<
});
}

isValid(): boolean {
return this.isPrivate() || this.isGroupV1() || this.isGroupV2();
}

maybeRepairGroupV2(data: {
masterKey: string;
secretParams: string;
Expand Down
74 changes: 48 additions & 26 deletions ts/views/conversation_view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ Whisper.LeftGroupToast = Whisper.ToastView.extend({
},
});

Whisper.InvalidConversationToast = Whisper.ToastView.extend({
render_attributes() {
return { toastMessage: window.i18n('invalidConversation') };
},
});

Whisper.OriginalNotFoundToast = Whisper.ToastView.extend({
render_attributes() {
return { toastMessage: window.i18n('originalMessageNotFound') };
Expand Down Expand Up @@ -2897,6 +2903,10 @@ Whisper.ConversationView = Whisper.View.extend({
this.model.set({ profileSharing: true });
}

if (this.showInvalidMessageToast()) {
return;
}

const { packId, stickerId } = options;
this.model.sendStickerMessage(packId, stickerId);
} catch (error) {
Expand Down Expand Up @@ -3030,6 +3040,43 @@ Whisper.ConversationView = Whisper.View.extend({
});
},

showInvalidMessageToast(messageText?: string): boolean {
let ToastView;

if (window.reduxStore.getState().expiration.hasExpired) {
ToastView = Whisper.ExpiredToast;
}
if (!this.model.isValid()) {
ToastView = Whisper.InvalidConversationToast;
}
if (
this.model.isPrivate() &&
(window.storage.isBlocked(this.model.get('e164')) ||
window.storage.isUuidBlocked(this.model.get('uuid')))
) {
ToastView = Whisper.BlockedToast;
}
if (
!this.model.isPrivate() &&
window.storage.isGroupBlocked(this.model.get('groupId'))
) {
ToastView = Whisper.BlockedGroupToast;
}
if (!this.model.isPrivate() && this.model.get('left')) {
ToastView = Whisper.LeftGroupToast;
}
if (messageText && messageText.length > MAX_MESSAGE_BODY_LENGTH) {
ToastView = Whisper.MessageBodyTooLongToast;
}

if (ToastView) {
this.showToast(ToastView);
return true;
}

return false;
},

async sendMessage(message = '', mentions = [], options = {}) {
this.sendStart = Date.now();

Expand Down Expand Up @@ -3058,32 +3105,7 @@ Whisper.ConversationView = Whisper.View.extend({

this.model.clearTypingTimers();

let ToastView;
if (window.reduxStore.getState().expiration.hasExpired) {
ToastView = Whisper.ExpiredToast;
}
if (
this.model.isPrivate() &&
(window.storage.isBlocked(this.model.get('e164')) ||
window.storage.isUuidBlocked(this.model.get('uuid')))
) {
ToastView = Whisper.BlockedToast;
}
if (
!this.model.isPrivate() &&
window.storage.isGroupBlocked(this.model.get('groupId'))
) {
ToastView = Whisper.BlockedGroupToast;
}
if (!this.model.isPrivate() && this.model.get('left')) {
ToastView = Whisper.LeftGroupToast;
}
if (message.length > MAX_MESSAGE_BODY_LENGTH) {
ToastView = Whisper.MessageBodyTooLongToast;
}

if (ToastView) {
this.showToast(ToastView);
if (this.showInvalidMessageToast(message)) {
this.enableMessageField();
return;
}
Expand Down
34 changes: 18 additions & 16 deletions ts/window.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -674,28 +674,30 @@ export type WhisperType = {
deliveryReceiptBatcher: BatcherType<WhatIsThis>;
RotateSignedPreKeyListener: WhatIsThis;

ExpiredToast: typeof Whisper.ToastView;
BlockedToast: typeof Whisper.ToastView;
BlockedGroupToast: typeof Whisper.ToastView;
BlockedToast: typeof Whisper.ToastView;
CannotMixImageAndNonImageAttachmentsToast: typeof Whisper.ToastView;
DangerousFileTypeToast: typeof Whisper.ToastView;
ExpiredToast: typeof Whisper.ToastView;
FileSavedToast: typeof Whisper.ToastView;
FileSizeToast: any;
FoundButNotLoadedToast: typeof Whisper.ToastView;
InvalidConversationToast: typeof Whisper.ToastView;
LeftGroupToast: typeof Whisper.ToastView;
OriginalNotFoundToast: typeof Whisper.ToastView;
MaxAttachmentsToast: typeof Whisper.ToastView;
MessageBodyTooLongToast: typeof Whisper.ToastView;
OneNonImageAtATimeToast: typeof Whisper.ToastView;
OriginalNoLongerAvailableToast: typeof Whisper.ToastView;
FoundButNotLoadedToast: typeof Whisper.ToastView;
VoiceNoteLimit: typeof Whisper.ToastView;
VoiceNoteMustBeOnlyAttachmentToast: typeof Whisper.ToastView;
OriginalNotFoundToast: typeof Whisper.ToastView;
PinnedConversationsFullToast: typeof Whisper.ToastView;
ReactionFailedToast: typeof Whisper.ToastView;
TapToViewExpiredIncomingToast: typeof Whisper.ToastView;
TapToViewExpiredOutgoingToast: typeof Whisper.ToastView;
FileSavedToast: typeof Whisper.ToastView;
ReactionFailedToast: typeof Whisper.ToastView;
MessageBodyTooLongToast: typeof Whisper.ToastView;
FileSizeToast: any;
UnableToLoadToast: typeof Whisper.ToastView;
DangerousFileTypeToast: typeof Whisper.ToastView;
OneNonImageAtATimeToast: typeof Whisper.ToastView;
CannotMixImageAndNonImageAttachmentsToast: typeof Whisper.ToastView;
MaxAttachmentsToast: typeof Whisper.ToastView;
TimerConflictToast: typeof Whisper.ToastView;
PinnedConversationsFullToast: typeof Whisper.ToastView;
UnableToLoadToast: typeof Whisper.ToastView;
VoiceNoteLimit: typeof Whisper.ToastView;
VoiceNoteMustBeOnlyAttachmentToast: typeof Whisper.ToastView;

ConversationLoadingScreen: typeof Whisper.View;
ConversationView: typeof Whisper.View;
View: typeof Backbone.View;
Expand Down

0 comments on commit 3ee830a

Please sign in to comment.