autobind typescript class methods with autobind

pull/1495/head
Audric Ackermann 4 years ago
parent b76ce0f2ff
commit 5ec9722e00
No known key found for this signature in database
GPG Key ID: 999F434D76324AD4

@ -25,8 +25,3 @@ ReadSyncs
SyncMessage SyncMessage
sendSyncMessage needs to be rewritten sendSyncMessage needs to be rewritten
sendSyncMessageOnly to fix sendSyncMessageOnly to fix
LONG_ATTAHCMENNT

@ -169,16 +169,6 @@
this.dispatchEvent(new Event('registration')); this.dispatchEvent(new Event('registration'));
}, },
validatePubKeyHex(pubKey) {
const c = new window.models.Conversation({
id: pubKey,
type: 'private',
});
const validationError = c.validateNumber();
if (validationError) {
throw new Error(validationError);
}
},
}); });
textsecure.AccountManager = AccountManager; textsecure.AccountManager = AccountManager;
})(); })();

@ -66,6 +66,7 @@
"@types/react-mic": "^12.4.1", "@types/react-mic": "^12.4.1",
"@types/styled-components": "^5.1.4", "@types/styled-components": "^5.1.4",
"abort-controller": "3.0.0", "abort-controller": "3.0.0",
"auto-bind": "^4.0.0",
"backbone": "1.3.3", "backbone": "1.3.3",
"blob-util": "1.3.0", "blob-util": "1.3.0",
"blueimp-canvas-to-blob": "3.14.0", "blueimp-canvas-to-blob": "3.14.0",

@ -21,6 +21,7 @@ import { SignalService } from '../protobuf';
import { MessageCollection, MessageModel } from './message'; import { MessageCollection, MessageModel } from './message';
import * as Data from '../../js/modules/data'; import * as Data from '../../js/modules/data';
import { MessageAttributesOptionals } from './messageType'; import { MessageAttributesOptionals } from './messageType';
import autoBind from 'auto-bind';
export interface OurLokiProfile { export interface OurLokiProfile {
displayName: string; displayName: string;
@ -152,6 +153,7 @@ export class ConversationModel extends Backbone.Model<ConversationAttributes> {
this.messageCollection = new MessageCollection([], { this.messageCollection = new MessageCollection([], {
conversation: this, conversation: this,
}); });
autoBind(this);
this.throttledBumpTyping = _.throttle(this.bumpTyping, 300); this.throttledBumpTyping = _.throttle(this.bumpTyping, 300);
this.updateLastMessage = _.throttle( this.updateLastMessage = _.throttle(
@ -177,8 +179,6 @@ export class ConversationModel extends Backbone.Model<ConversationAttributes> {
if (this.isPublic()) { if (this.isPublic()) {
this.set('profileSharing', true); this.set('profileSharing', true);
} }
this.unset('hasFetchedProfile');
this.unset('tokens');
this.typingRefreshTimer = null; this.typingRefreshTimer = null;
this.typingPauseTimer = null; this.typingPauseTimer = null;
@ -549,49 +549,6 @@ export class ConversationModel extends Backbone.Model<ConversationAttributes> {
return window.Signal.Data.getUnreadCountByConversation(this.id); return window.Signal.Data.getUnreadCountByConversation(this.id);
} }
public validate(attributes: any) {
const required = ['id', 'type'];
const missing = _.filter(required, attr => !attributes[attr]);
if (missing.length) {
return `Conversation must have ${missing}`;
}
if (attributes.type !== 'private' && attributes.type !== 'group') {
return `Invalid conversation type: ${attributes.type}`;
}
const error = this.validateNumber();
if (error) {
return error;
}
return null;
}
public validateNumber() {
if (!this.id) {
return 'Invalid ID';
}
if (!this.isPrivate()) {
return null;
}
// Check if it's hex
const isHex = this.id.replace(/[\s]*/g, '').match(/^[0-9a-fA-F]+$/);
if (!isHex) {
return 'Invalid Hex ID';
}
// Check if the pubkey length is 33 and leading with 05 or of length 32
const len = this.id.length;
if ((len !== 33 * 2 || !/^05/.test(this.id)) && len !== 32 * 2) {
return 'Invalid Pubkey Format';
}
this.set({ id: this.id });
return null;
}
public queueJob(callback: any) { public queueJob(callback: any) {
// tslint:disable-next-line: no-promise-as-boolean // tslint:disable-next-line: no-promise-as-boolean
const previous = this.pending || Promise.resolve(); const previous = this.pending || Promise.resolve();

@ -20,6 +20,7 @@ import {
MessageAttributesOptionals, MessageAttributesOptionals,
} from './messageType'; } from './messageType';
import autoBind from 'auto-bind';
export class MessageModel extends Backbone.Model<MessageAttributes> { export class MessageModel extends Backbone.Model<MessageAttributes> {
public propsForTimerNotification: any; public propsForTimerNotification: any;
public propsForGroupNotification: any; public propsForGroupNotification: any;
@ -45,6 +46,8 @@ export class MessageModel extends Backbone.Model<MessageAttributes> {
this.on('change:expireTimer', this.setToExpire); this.on('change:expireTimer', this.setToExpire);
// this.on('expired', this.onExpired); // this.on('expired', this.onExpired);
void this.setToExpire(); void this.setToExpire();
autoBind(this);
this.markRead = this.markRead.bind(this);
// Keep props ready // Keep props ready
const generateProps = (triggerEvent = true) => { const generateProps = (triggerEvent = true) => {
if (this.isExpirationTimerUpdate()) { if (this.isExpirationTimerUpdate()) {
@ -95,7 +98,7 @@ export class MessageModel extends Backbone.Model<MessageAttributes> {
return !!this.get('unread'); return !!this.get('unread');
} }
// Important to allow for this.unset('unread'), save to db, then fetch() // Important to allow for this.set({ unread}), save to db, then fetch()
// to propagate. We don't want the unset key in the db so our unread index // to propagate. We don't want the unset key in the db so our unread index
// stays small. // stays small.
public merge(model: any) { public merge(model: any) {
@ -103,7 +106,7 @@ export class MessageModel extends Backbone.Model<MessageAttributes> {
const { unread } = attributes; const { unread } = attributes;
if (unread === undefined) { if (unread === undefined) {
this.unset('unread'); this.set({ unread: false });
} }
this.set(attributes); this.set(attributes);
@ -1323,7 +1326,7 @@ export class MessageModel extends Backbone.Model<MessageAttributes> {
} }
public async markRead(readAt: number) { public async markRead(readAt: number) {
this.unset('unread'); this.set({ unread: false });
if (this.get('expireTimer') && !this.get('expirationStartTimestamp')) { if (this.get('expireTimer') && !this.get('expirationStartTimestamp')) {
const expirationStartTimestamp = Math.min( const expirationStartTimestamp = Math.min(

@ -294,7 +294,8 @@ function updateReadStatus(
} }
} }
if (readSync || message.isExpirationTimerUpdate()) { if (readSync || message.isExpirationTimerUpdate()) {
message.unset('unread'); message.set({ unread: false });
// This is primarily to allow the conversation to mark all older // This is primarily to allow the conversation to mark all older
// messages as read, as is done when we receive a read sync for // messages as read, as is done when we receive a read sync for
// a message we already know about. // a message we already know about.

@ -1,6 +1,5 @@
import { createSelector } from 'reselect'; import { createSelector } from 'reselect';
import { LocalizerType } from '../../types/Util';
import { StateType } from '../reducer'; import { StateType } from '../reducer';
import { import {
ConversationLookupType, ConversationLookupType,
@ -11,6 +10,7 @@ import {
import { getIntl, getOurNumber } from './user'; import { getIntl, getOurNumber } from './user';
import { BlockedNumberController } from '../../util'; import { BlockedNumberController } from '../../util';
import { LocalizerType } from '../../types/Util';
export const getConversations = (state: StateType): ConversationsStateType => export const getConversations = (state: StateType): ConversationsStateType =>
state.conversations; state.conversations;
@ -49,13 +49,15 @@ export const getMessagesOfSelectedConversation = createSelector(
(state: ConversationsStateType): Array<MessageTypeInConvo> => state.messages (state: ConversationsStateType): Array<MessageTypeInConvo> => state.messages
); );
function getConversationTitle(conversation: ConversationType): string { function getConversationTitle(
conversation: ConversationType,
i18n: LocalizerType
): string {
if (conversation.name) { if (conversation.name) {
return conversation.name; return conversation.name;
} }
if (conversation.type === 'group') { if (conversation.type === 'group') {
const { i18n } = window;
return i18n('unknown'); return i18n('unknown');
} }
return conversation.id; return conversation.id;
@ -65,19 +67,25 @@ const collator = new Intl.Collator();
export const _getConversationComparator = (i18n: LocalizerType) => { export const _getConversationComparator = (i18n: LocalizerType) => {
return (left: ConversationType, right: ConversationType): number => { return (left: ConversationType, right: ConversationType): number => {
const leftTimestamp = left.timestamp; const leftActiveAt = left.activeAt;
const rightTimestamp = right.timestamp; const rightActiveAt = right.activeAt;
if (leftTimestamp && !rightTimestamp) { if (leftActiveAt && !rightActiveAt) {
return -1; return -1;
} }
if (rightTimestamp && !leftTimestamp) { if (rightActiveAt && !leftActiveAt) {
return 1; return 1;
} }
if (leftTimestamp && rightTimestamp && leftTimestamp !== rightTimestamp) { if (leftActiveAt && rightActiveAt && leftActiveAt !== rightActiveAt) {
return rightTimestamp - leftTimestamp; return rightActiveAt - leftActiveAt;
} }
const leftTitle = getConversationTitle(left).toLowerCase(); const leftTitle = getConversationTitle(
const rightTitle = getConversationTitle(right).toLowerCase(); left,
i18n || window?.i18n
).toLowerCase();
const rightTitle = getConversationTitle(
right,
i18n || window?.i18n
).toLowerCase();
return collator.compare(leftTitle, rightTitle); return collator.compare(leftTitle, rightTitle);
}; };

@ -1308,6 +1308,11 @@ atob@^2.1.2:
resolved "https://registry.yarnpkg.com/atob/-/atob-2.1.2.tgz#6d9517eb9e030d2436666651e86bd9f6f13533c9" resolved "https://registry.yarnpkg.com/atob/-/atob-2.1.2.tgz#6d9517eb9e030d2436666651e86bd9f6f13533c9"
integrity sha512-Wm6ukoaOGJi/73p/cl2GvLjTI5JM1k/O14isD73YML8StrH/7/lRFgmg8nICZgD3bZZvjwCGxtMOD3wWNAu8cg== integrity sha512-Wm6ukoaOGJi/73p/cl2GvLjTI5JM1k/O14isD73YML8StrH/7/lRFgmg8nICZgD3bZZvjwCGxtMOD3wWNAu8cg==
auto-bind@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/auto-bind/-/auto-bind-4.0.0.tgz#e3589fc6c2da8f7ca43ba9f84fa52a744fc997fb"
integrity sha512-Hdw8qdNiqdJ8LqT0iK0sVzkFbzg6fhnQqqfWhBDxcHZvU75+B+ayzTy8x+k5Ix0Y92XOhOUlx74ps+bA6BeYMQ==
autoprefixer@^6.3.1: autoprefixer@^6.3.1:
version "6.7.7" version "6.7.7"
resolved "https://registry.yarnpkg.com/autoprefixer/-/autoprefixer-6.7.7.tgz#1dbd1c835658e35ce3f9984099db00585c782014" resolved "https://registry.yarnpkg.com/autoprefixer/-/autoprefixer-6.7.7.tgz#1dbd1c835658e35ce3f9984099db00585c782014"

Loading…
Cancel
Save