chamesu
Version:
This package contains all packages of the chat application.
49 lines (38 loc) • 1.31 kB
text/typescript
/*
* Copyright (c) 2022.
* Author Peter Placzek (tada5hi)
* For the full copyright and license information,
* view the LICENSE file that was distributed with this source code.
*/
import {ChatRoom, ChatRoomType} from "../../chat/room";
import {getRepository} from "typeorm";
import {ChatMember} from "../../chat/member";
import {AuthContact} from "./index";
export async function createContactChatRoom(contactId: string, userOne: number, userTwo: number) : Promise<ChatRoom> {
const roomRepository = getRepository(ChatRoom);
const room = roomRepository.create({
type: ChatRoomType.PRIVATE,
contact_id: contactId,
protected: true
});
await roomRepository.save(room);
const memberRepository = getRepository(ChatMember);
const memberOne = memberRepository.create({
user_id: userOne,
room_id: room.id,
creator: true
});
const memberTwo = memberRepository.create({
user_id: userTwo,
room_id: room.id
});
await memberRepository.insert([memberOne, memberTwo]);
return room;
}
export function getContactOtherUserId(contact: AuthContact, ownUserId: number) {
if(contact.user_one_id === ownUserId) {
return contact.user_two_id;
} else {
return contact.user_one_id;
}
}