Added strict type checking to group id

pull/1188/head
Mikunj 5 years ago
parent 3d7b36aa57
commit 4d0d2727bd

@ -2,26 +2,28 @@ import { DataMessage } from '../DataMessage';
import { SignalService } from '../../../../../../protobuf'; import { SignalService } from '../../../../../../protobuf';
import { TextEncoder } from 'util'; import { TextEncoder } from 'util';
import { MessageParams } from '../../../Message'; import { MessageParams } from '../../../Message';
import { PubKey } from '../../../../../types';
interface ClosedGroupMessageParams extends MessageParams { interface ClosedGroupMessageParams extends MessageParams {
groupId: string; groupId: string | PubKey;
} }
export abstract class ClosedGroupMessage extends DataMessage { export abstract class ClosedGroupMessage extends DataMessage {
public readonly groupId: string; public readonly groupId: PubKey;
constructor(params: ClosedGroupMessageParams) { constructor(params: ClosedGroupMessageParams) {
super({ super({
timestamp: params.timestamp, timestamp: params.timestamp,
identifier: params.identifier, identifier: params.identifier,
}); });
this.groupId = params.groupId; const { groupId } = params;
this.groupId = typeof groupId === 'string' ? new PubKey(groupId) : groupId;
} }
protected abstract groupContextType(): SignalService.GroupContext.Type; protected abstract groupContextType(): SignalService.GroupContext.Type;
protected groupContext(): SignalService.GroupContext { protected groupContext(): SignalService.GroupContext {
const id = new TextEncoder().encode(this.groupId); const id = new TextEncoder().encode(this.groupId.key);
const type = this.groupContextType(); const type = this.groupContextType();
return new SignalService.GroupContext({ id, type }); return new SignalService.GroupContext({ id, type });

@ -139,7 +139,7 @@ export class MessageQueue implements MessageQueueInterface {
public async processPending(device: PubKey) { public async processPending(device: PubKey) {
const messages = await this.pendingMessageCache.getForDevice(device); const messages = await this.pendingMessageCache.getForDevice(device);
const isMediumGroup = GroupUtils.isMediumGroup(device.key); const isMediumGroup = GroupUtils.isMediumGroup(device);
const hasSession = await SessionProtocol.hasSession(device); const hasSession = await SessionProtocol.hasSession(device);
if (!isMediumGroup && !hasSession) { if (!isMediumGroup && !hasSession) {

@ -1,11 +1,11 @@
import _ from 'lodash'; import _ from 'lodash';
import { PrimaryPubKey } from '../types'; import { PrimaryPubKey, PubKey } from '../types';
import { MultiDeviceProtocol } from '../protocols'; import { MultiDeviceProtocol } from '../protocols';
export async function getGroupMembers( export async function getGroupMembers(
groupId: string groupId: PubKey
): Promise<Array<PrimaryPubKey>> { ): Promise<Array<PrimaryPubKey>> {
const groupConversation = window.ConversationController.get(groupId); const groupConversation = window.ConversationController.get(groupId.key);
const groupMembers = groupConversation const groupMembers = groupConversation
? groupConversation.attributes.members ? groupConversation.attributes.members
: undefined; : undefined;
@ -22,8 +22,8 @@ export async function getGroupMembers(
return _.uniqWith(primaryDevices, (a, b) => a.isEqual(b)); return _.uniqWith(primaryDevices, (a, b) => a.isEqual(b));
} }
export function isMediumGroup(groupId: string): boolean { export function isMediumGroup(groupId: PubKey): boolean {
const conversation = window.ConversationController.get(groupId); const conversation = window.ConversationController.get(groupId.key);
if (!conversation) { if (!conversation) {
return false; return false;

@ -43,7 +43,7 @@ describe('MessageQueue', () => {
// Message Sender Stubs // Message Sender Stubs
let sendStub: sinon.SinonStub<[RawMessage, (number | undefined)?]>; let sendStub: sinon.SinonStub<[RawMessage, (number | undefined)?]>;
// Utils Stubs // Utils Stubs
let isMediumGroupStub: sinon.SinonStub<[string], boolean>; let isMediumGroupStub: sinon.SinonStub<[PubKey], boolean>;
// Session Protocol Stubs // Session Protocol Stubs
let hasSessionStub: sinon.SinonStub<[PubKey]>; let hasSessionStub: sinon.SinonStub<[PubKey]>;
let sendSessionRequestIfNeededStub: sinon.SinonStub<[PubKey], Promise<void>>; let sendSessionRequestIfNeededStub: sinon.SinonStub<[PubKey], Promise<void>>;

Loading…
Cancel
Save