Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
xml('presence', {from: stanza.attrs.to, to: stanza.attrs.from},
xml('x', {xmlns: 'http://jabber.org/protocol/muc#user'},
xml('item', {affiliation: 'owner', role: 'moderator'}),
xml('status', {code: '110'}),
xml('status', {code: '201'})
)
)
);
} else {
throw new Error('unknown stanza: ' + stanza.toString());
}
});
// when
const myOccupantJid = parseJid('chatroom@conference.example.com/me');
const room = await multiUserChatPlugin.joinRoom(myOccupantJid);
await multiUserChatPlugin.sendMessage(room, 'message body');
// then
expect(room.messages.length).toEqual(1);
expect(room.messages[0].body).toEqual('message body');
expect(room.messages[0].direction).toEqual(Direction.out);
expect(room.messages[0].id).not.toBeUndefined();
expect(room.messages[0].from).toEqual(myOccupantJid);
});
const mockClientFactory = new MockClientFactory();
xmppClientMock = mockClientFactory.clientInstance;
TestBed.configureTestingModule({
providers: [
XmppChatConnectionService,
{provide: XmppClientFactoryService, useValue: mockClientFactory},
XmppChatAdapter,
{provide: LogService, useValue: testLogService()},
ContactFactoryService
]
});
chatConnectionService = TestBed.get(XmppChatConnectionService);
chatConnectionService.client = xmppClientMock;
chatConnectionService.userJid = parseJid('me', 'example.com', 'something');
chatAdapter = TestBed.get(XmppChatAdapter);
const conferenceService = {
jid: 'conference.jabber.example.com',
};
const serviceDiscoveryPluginMock: any = {
findService: () => conferenceService
};
logService = TestBed.get(LogService);
chatAdapter.addPlugins([
new MultiUserChatPlugin(chatAdapter, logService, serviceDiscoveryPluginMock),
new MessageUuidPlugin()
]);
private async joinRoomInternal(roomJid: JID, name?: string) {
const userJid = this.xmppChatAdapter.chatConnectionService.userJid;
const occupantJid = parseJid(roomJid.local, roomJid.domain, roomJid.resource || userJid.local);
const roomJoinedPromise = new Promise(resolve => this.roomJoinPromises[occupantJid.toString()] = resolve);
await this.xmppChatAdapter.chatConnectionService.send(
xml('presence', {from: userJid.toString(), to: occupantJid.toString()},
xml('x', {xmlns: 'http://jabber.org/protocol/muc'})
)
);
const presenceResponse = await roomJoinedPromise;
if (presenceResponse.getChild('error')) {
throw new Error('error joining room: ' + presenceResponse.toString());
}
let room;
try {
room = this.getRoomByJid(roomJid);
} catch {
private handleRoomMessageStanza(stanza: Stanza) {
let datetime;
const delay = stanza.getChild('delay');
if (delay && delay.attrs.stamp) {
datetime = new Date(delay.attrs.stamp);
} else {
datetime = new Date();
}
const from = parseJid(stanza.attrs.from);
const room = this.getRoomByJid(from.bare());
const message = {
body: stanza.getChildText('body'),
datetime,
id: stanza.attrs.id,
from,
direction: from.equals(room.occupantJid) ? Direction.out : Direction.in,
delayed: !!stanza.getChild('delay')
};
const messageReceivedEvent = new MessageReceivedEvent();
for (const plugin of this.xmppChatAdapter.plugins) {
plugin.afterReceiveMessage(message, stanza, messageReceivedEvent);
}
if (!messageReceivedEvent.discard) {
async createRoom(request: RoomCreationOptions): Promise {
const roomId = request.roomId;
const service = await this.serviceDiscoveryPlugin.findService('conference', 'text');
const occupantJid = parseJid(roomId, service.jid, request.nick);
const {presenceResponse, room} = await this.joinRoomInternal(occupantJid, request.name);
const itemElement = presenceResponse.getChild('x').getChild('item');
if (itemElement.attrs.affiliation !== 'owner') {
throw new Error('error creating room, user is not owner: ' + presenceResponse.toString());
}
const configurationForm = await this.xmppChatAdapter.chatConnectionService.sendIq(
xml('iq', {type: 'get', to: room.roomJid.toString()},
xml('query', {xmlns: 'http://jabber.org/protocol/muc#owner'})
)
);
const configurationListElement = configurationForm.getChild('query').getChild('x');
if (!configurationListElement) {
throw new Error('room not configurable');
const xmppClientMock = mockClientFactory.clientInstance;
const logService = testLogService();
TestBed.configureTestingModule({
providers: [
XmppChatConnectionService,
{provide: XmppClientFactoryService, useValue: mockClientFactory},
{provide: ChatServiceToken, useClass: XmppChatAdapter},
{provide: LogService, useValue: logService},
ContactFactoryService
]
});
chatConnectionService = TestBed.get(XmppChatConnectionService);
chatConnectionService.client = xmppClientMock;
chatConnectionService.userJid = parseJid('me', 'example.com', 'something');
chatService = TestBed.get(ChatServiceToken);
chatService.addPlugins([new MessagePlugin(chatService, logService)]);
});
async afterSendMessage(message: Message, messageStanza: Element) {
const {type, to} = messageStanza.attrs;
if (type === 'chat') {
await this.publishSubscribePlugin.privateNotify(NGX_CHAT_MESSAGESENT);
this.updateContactMessageState(parseJid(to).bare().toString(), MessageState.SENT, new Date());
delete message.state;
}
}
{provide: LogService, useValue: logService},
ContactFactoryService
]
});
chatConnectionService = TestBed.get(XmppChatConnectionService);
chatConnectionService.client = xmppClientMock;
contactFactory = TestBed.get(ContactFactoryService);
chatService = TestBed.get(ChatServiceToken);
chatService.addPlugins([new MessageUuidPlugin(), new MessagePlugin(chatService, logService)]);
contact1 = contactFactory.createContact('test@example.com', 'jon doe');
contact2 = contactFactory.createContact('test2@example.com', 'jane dane');
contacts = [contact1, contact2];
chatConnectionService.userJid = parseJid('me', 'example.com', 'something');
});
constructor(jidPlain: string,
public name: string,
logService?: LogService,
avatar?: string) {
if (avatar) {
this.avatar = avatar;
}
const jid = parseJid(jidPlain);
this.jidFull = jid;
this.jidBare = jid.bare();
this.messageStore = new MessageStore(logService);
}
describe('message archive plugin', () => {
let chatConnectionService: XmppChatConnectionService;
let chatAdapter: XmppChatAdapter;
let contactFactory: ContactFactoryService;
let xmppClientMock: SpyObj;
let contact1: Contact;
const userJid = parseJid('me@example.com/myresource');
const validArchiveStanza =
xml('message', {},
xml('result', {xmlns: 'urn:xmpp:mam:2'},
xml('forwarded', {},
xml('delay', {stamp: '2018-07-18T08:47:44.233057Z'}),
xml('message', {to: userJid.toString(), from: 'someone@else.com/resource'},
xml('origin-id', {id: 'id'}),
xml('body', {}, 'message text')))));
beforeEach(() => {
const mockClientFactory = new MockClientFactory();
xmppClientMock = mockClientFactory.clientInstance;
TestBed.configureTestingModule({
providers: [