constants-to-ts

pull/1242/head
Vincent 5 years ago
parent 506b55bfe2
commit f8cd997b13

@ -15,13 +15,13 @@ const NUM_SEND_CONNECTIONS = 3;
const getTTLForType = type => { const getTTLForType = type => {
switch (type) { switch (type) {
case 'pairing-request': case 'pairing-request':
return window.CONSTANTS.TTL_DEFAULT_PAIRING_REQUEST; return window.libsession.Constants.TTL_DEFAULT.PAIRING_REQUEST;
case 'device-unpairing': case 'device-unpairing':
return window.CONSTANTS.TTL_DEFAULT_DEVICE_UNPAIRING; return window.libsession.Constants.TTL_DEFAULT.DEVICE_UNPAIRING;
case 'onlineBroadcast': case 'onlineBroadcast':
return window.CONSTANTS.TTL_DEFAULT_ONLINE_BROADCAST; return window.libsession.Constants.TTL_DEFAULT.ONLINE_BROADCAST;
default: default:
return window.CONSTANTS.TTL_DEFAULT_REGULAR_MESSAGE; return window.libsession.Constants.TTL_DEFAULT.REGULAR_MESSAGE;
} }
}; };

@ -75,44 +75,6 @@ window.isBeforeVersion = (toCheck, baseVersion) => {
} }
}; };
window.timeAsMs = (value, unit) => {
// Converts a time to milliseconds
// Valid units: second, minute, hour, day
const unitAsSingular = unit.replace(new RegExp('s?$'), '');
switch (unitAsSingular) {
case 'second':
return value * 1000;
case 'minute':
return value * 60 * 1000;
case 'hour':
return value * 60 * 60 * 1000;
case 'day':
return value * 24 * 60 * 60 * 1000;
default:
return value;
}
};
window.msAsUnit = (value, unit) => {
// Converts milliseconds to your preferred unit
// Valid units: second(s), minute(s), hour(s), day(s)
const unitAsSingular = unit.replace(new RegExp('s?$'), '');
switch (unitAsSingular) {
case 'second':
return value / 1000;
case 'minute':
return value / 60 / 1000;
case 'hour':
return value / 60 / 60 / 1000;
case 'day':
return value / 24 / 60 / 60 / 1000;
default:
return value;
}
};
// eslint-disable-next-line func-names // eslint-disable-next-line func-names
window.CONSTANTS = new (function() { window.CONSTANTS = new (function() {
this.MAX_LOGIN_TRIES = 3; this.MAX_LOGIN_TRIES = 3;
@ -129,12 +91,6 @@ window.CONSTANTS = new (function() {
this.NOTIFICATION_ENABLE_TIMEOUT_SECONDS = 10; this.NOTIFICATION_ENABLE_TIMEOUT_SECONDS = 10;
this.SESSION_ID_LENGTH = 66; this.SESSION_ID_LENGTH = 66;
// TTL Defaults
this.TTL_DEFAULT_PAIRING_REQUEST = window.timeAsMs(2, 'minutes');
this.TTL_DEFAULT_DEVICE_UNPAIRING = window.timeAsMs(4, 'days');
this.TTL_DEFAULT_ONLINE_BROADCAST = window.timeAsMs(1, 'minute');
this.TTL_DEFAULT_REGULAR_MESSAGE = window.timeAsMs(2, 'days');
// Loki Name System (LNS) // Loki Name System (LNS)
this.LNS_DEFAULT_LOOKUP_TIMEOUT = 6000; this.LNS_DEFAULT_LOOKUP_TIMEOUT = 6000;
// Minimum nodes version for LNS lookup // Minimum nodes version for LNS lookup

@ -242,7 +242,7 @@ $session-compose-margin: 20px;
align-items: center; align-items: center;
overflow-y: auto; overflow-y: auto;
overflow-x: hidden; overflow-x: hidden;
.session-icon .exit { .session-icon .exit {
padding: 13px; padding: 13px;
} }

@ -29,7 +29,14 @@ export class SessionIdEditable extends React.PureComponent<Props> {
} }
public render() { public render() {
const { placeholder, editable, text, value, maxLength, isGroup } = this.props; const {
placeholder,
editable,
text,
value,
maxLength,
isGroup,
} = this.props;
return ( return (
<div <div
@ -40,7 +47,10 @@ export class SessionIdEditable extends React.PureComponent<Props> {
> >
<textarea <textarea
className={classNames( className={classNames(
isGroup ? 'group-id-editable-textarea' : 'session-id-editable-textarea')} isGroup
? 'group-id-editable-textarea'
: 'session-id-editable-textarea'
)}
ref={this.inputRef} ref={this.inputRef}
placeholder={placeholder} placeholder={placeholder}
disabled={!editable} disabled={!editable}

@ -10,6 +10,8 @@ import {
import { UserUtil } from '../../../util'; import { UserUtil } from '../../../util';
import { MultiDeviceProtocol } from '../../../session/protocols'; import { MultiDeviceProtocol } from '../../../session/protocols';
import { PubKey } from '../../../session/types'; import { PubKey } from '../../../session/types';
import { Constants } from '../../../session';
import { NumberUtils } from '../../../session/utils';
export enum SessionSettingCategory { export enum SessionSettingCategory {
Appearance = 'appearance', Appearance = 'appearance',
@ -455,8 +457,8 @@ export class SettingsView extends React.Component<SettingsViewProps, State> {
step: 6, step: 6,
min: 12, min: 12,
max: 96, max: 96,
defaultValue: window.msAsUnit( defaultValue: NumberUtils.msAsUnit(
window.CONSTANTS.TTL_DEFAULT_REGULAR_MESSAGE, Constants.TTL_DEFAULT.REGULAR_MESSAGE,
'hour' 'hour'
), ),
info: (value: number) => `${value} Hours`, info: (value: number) => `${value} Hours`,

@ -0,0 +1,13 @@
import { NumberUtils } from './utils';
// Default TTL
export const TTL_DEFAULT = {
PAIRING_REQUEST: NumberUtils.timeAsMs(2, 'minutes'),
DEVICE_UNPAIRING: NumberUtils.timeAsMs(4, 'days'),
SESSION_REQUEST: NumberUtils.timeAsMs(4, 'days'),
END_SESSION_MESSAGE: NumberUtils.timeAsMs(4, 'days'),
TYPING_MESSAGE: NumberUtils.timeAsMs(1, 'minute'),
ONLINE_BROADCAST: NumberUtils.timeAsMs(1, 'minute'),
SESSION_ESTABLISHED: NumberUtils.timeAsMs(5, 'minutes'),
REGULAR_MESSAGE: NumberUtils.timeAsMs(2, 'days'),
};

@ -2,7 +2,8 @@ import * as Messages from './messages';
import * as Protocols from './protocols'; import * as Protocols from './protocols';
import * as Types from './types'; import * as Types from './types';
import * as Utils from './utils'; import * as Utils from './utils';
import * as Constants from './constants';
export * from './instance'; export * from './instance';
export { Messages, Utils, Protocols, Types }; export { Messages, Utils, Protocols, Types, Constants };

@ -1,5 +1,6 @@
import { Message } from '../Message'; import { Message } from '../Message';
import { SignalService } from '../../../../protobuf'; import { SignalService } from '../../../../protobuf';
import { Constants } from '../../..';
export abstract class ContentMessage extends Message { export abstract class ContentMessage extends Message {
public plainTextBuffer(): Uint8Array { public plainTextBuffer(): Uint8Array {
@ -14,7 +15,6 @@ export abstract class ContentMessage extends Message {
* this value can be used in all child classes * this value can be used in all child classes
*/ */
protected getDefaultTTL(): number { protected getDefaultTTL(): number {
// 1 day default for any other message return Constants.TTL_DEFAULT.REGULAR_MESSAGE;
return 24 * 60 * 60 * 1000;
} }
} }

@ -1,9 +1,10 @@
import { SessionRequestMessage } from './SessionRequestMessage'; import { SessionRequestMessage } from './SessionRequestMessage';
import { SignalService } from '../../../../protobuf'; import { SignalService } from '../../../../protobuf';
import { Constants } from '../../..';
export class EndSessionMessage extends SessionRequestMessage { export class EndSessionMessage extends SessionRequestMessage {
public ttl(): number { public ttl(): number {
return 4 * 24 * 60 * 60 * 1000; // 4 days return Constants.TTL_DEFAULT.END_SESSION_MESSAGE;
} }
protected contentProto(): SignalService.Content { protected contentProto(): SignalService.Content {

@ -2,6 +2,7 @@ import { ContentMessage } from './ContentMessage';
import { SignalService } from '../../../../protobuf'; import { SignalService } from '../../../../protobuf';
import * as crypto from 'crypto'; import * as crypto from 'crypto';
import { MessageParams } from '../Message'; import { MessageParams } from '../Message';
import { Constants } from '../../..';
export class SessionEstablishedMessage extends ContentMessage { export class SessionEstablishedMessage extends ContentMessage {
public readonly padding: Buffer; public readonly padding: Buffer;
@ -18,7 +19,7 @@ export class SessionEstablishedMessage extends ContentMessage {
this.padding = crypto.randomBytes(paddingLength); this.padding = crypto.randomBytes(paddingLength);
} }
public ttl(): number { public ttl(): number {
return 5 * 60 * 1000; return Constants.TTL_DEFAULT.SESSION_ESTABLISHED;
} }
protected contentProto(): SignalService.Content { protected contentProto(): SignalService.Content {

@ -1,6 +1,7 @@
import { ContentMessage } from './ContentMessage'; import { ContentMessage } from './ContentMessage';
import { SignalService } from '../../../../protobuf'; import { SignalService } from '../../../../protobuf';
import { MessageParams } from '../Message'; import { MessageParams } from '../Message';
import { Constants } from '../../..';
export interface PreKeyBundleType { export interface PreKeyBundleType {
identityKey: Uint8Array; identityKey: Uint8Array;
@ -17,7 +18,7 @@ interface SessionRequestParams extends MessageParams {
} }
export class SessionRequestMessage extends ContentMessage { export class SessionRequestMessage extends ContentMessage {
public static readonly ttl = 4 * 24 * 60 * 60 * 1000; // 4 days public static readonly ttl = Constants.TTL_DEFAULT.SESSION_REQUEST;
private readonly preKeyBundle: PreKeyBundleType; private readonly preKeyBundle: PreKeyBundleType;
constructor(params: SessionRequestParams) { constructor(params: SessionRequestParams) {

@ -4,6 +4,7 @@ import { TextEncoder } from 'util';
import { MessageParams } from '../Message'; import { MessageParams } from '../Message';
import { StringUtils } from '../../../utils'; import { StringUtils } from '../../../utils';
import { PubKey } from '../../../types'; import { PubKey } from '../../../types';
import { Constants } from '../../..';
interface TypingMessageParams extends MessageParams { interface TypingMessageParams extends MessageParams {
isTyping: boolean; isTyping: boolean;
@ -26,7 +27,7 @@ export class TypingMessage extends ContentMessage {
} }
public ttl(): number { public ttl(): number {
return 60 * 1000; // 1 minute for typing indicators return Constants.TTL_DEFAULT.TYPING_MESSAGE;
} }
protected contentProto(): SignalService.Content { protected contentProto(): SignalService.Content {

@ -1,9 +1,10 @@
import { DataMessage } from './DataMessage'; import { DataMessage } from './DataMessage';
import { SignalService } from '../../../../../protobuf'; import { SignalService } from '../../../../../protobuf';
import { Constants } from '../../../..';
export class DeviceUnlinkMessage extends DataMessage { export class DeviceUnlinkMessage extends DataMessage {
public ttl(): number { public ttl(): number {
return 4 * 24 * 60 * 60 * 1000; // 4 days for device unlinking return Constants.TTL_DEFAULT.DEVICE_UNPAIRING;
} }
public dataProto(): SignalService.DataMessage { public dataProto(): SignalService.DataMessage {

@ -1,6 +1,7 @@
import { ContentMessage } from '../ContentMessage'; import { ContentMessage } from '../ContentMessage';
import { SignalService } from '../../../../../protobuf'; import { SignalService } from '../../../../../protobuf';
import { MessageParams } from '../../Message'; import { MessageParams } from '../../Message';
import { Constants } from '../../../..';
export interface DeviceLinkMessageParams extends MessageParams { export interface DeviceLinkMessageParams extends MessageParams {
primaryDevicePubKey: string; primaryDevicePubKey: string;
secondaryDevicePubKey: string; secondaryDevicePubKey: string;
@ -20,7 +21,7 @@ export class DeviceLinkRequestMessage extends ContentMessage {
} }
public ttl(): number { public ttl(): number {
return 2 * 60 * 1000; // 2 minutes for pairing requests return Constants.TTL_DEFAULT.PAIRING_REQUEST;
} }
protected getDataMessage(): SignalService.DataMessage | undefined { protected getDataMessage(): SignalService.DataMessage | undefined {

@ -0,0 +1,47 @@
type TimeUnit =
| 'second'
| 'seconds'
| 'minute'
| 'minutes'
| 'hour'
| 'hours'
| 'day'
| 'days';
export const timeAsMs = (value: number, unit: TimeUnit) => {
// Converts a time to milliseconds
// Valid units: second, minute, hour, day
const unitAsSingular = unit.replace(new RegExp('s?$'), '');
switch (unitAsSingular) {
case 'second':
return value * 1000;
case 'minute':
return value * 60 * 1000;
case 'hour':
return value * 60 * 60 * 1000;
case 'day':
return value * 24 * 60 * 60 * 1000;
default:
return value;
}
};
export const msAsUnit = (value: number, unit: TimeUnit) => {
// Converts milliseconds to your preferred unit
// Valid units: second(s), minute(s), hour(s), day(s)
const unitAsSingular = unit.replace(new RegExp('s?$'), '');
switch (unitAsSingular) {
case 'second':
return value / 1000;
case 'minute':
return value / 60 / 1000;
case 'hour':
return value / 60 / 60 / 1000;
case 'day':
return value / 24 / 60 / 60 / 1000;
default:
return value;
}
};

@ -2,6 +2,7 @@ import * as MessageUtils from './Messages';
import * as GroupUtils from './Groups'; import * as GroupUtils from './Groups';
import * as SyncMessageUtils from './SyncMessage'; import * as SyncMessageUtils from './SyncMessage';
import * as StringUtils from './String'; import * as StringUtils from './String';
import * as NumberUtils from './Number';
import * as PromiseUtils from './Promise'; import * as PromiseUtils from './Promise';
import * as ProtobufUtils from './Protobuf'; import * as ProtobufUtils from './Protobuf';
@ -14,6 +15,7 @@ export {
SyncMessageUtils, SyncMessageUtils,
GroupUtils, GroupUtils,
StringUtils, StringUtils,
NumberUtils,
PromiseUtils, PromiseUtils,
ProtobufUtils, ProtobufUtils,
}; };

1
ts/window.d.ts vendored

@ -19,7 +19,6 @@ If you import anything in global.d.ts, the type system won't work correctly.
declare global { declare global {
interface Window { interface Window {
CONSTANTS: any; CONSTANTS: any;
msAsUnit: any;
ConversationController: any; ConversationController: any;
Events: any; Events: any;
Lodash: any; Lodash: any;

Loading…
Cancel
Save