import {Chat, ChatCreationData, ChatData, ChatResourceFilters} from "./chat"; import {CurrentClientResource} from "../../resources/client.resource"; import {APICursorHttpResponse} from "../../helpers/apiCursor.helper"; import {keys, merge, union} from "lodash"; import {mc_log} from '../../settings'; export type ClientCredentials = { id: number; email?: string; auth_token: string; session: string; } export type ClientData = ClientCredentials & { id: number, first_name: string, last_name: string, profile_picture: string, gender: string, birthday: string, status: string } /** * A single chat. Uses StreamListeners to keep tabs on the Chat's status. * @constructor */ export class Client extends CurrentClientResource { uuid: string; id: number; sessionKey: string; protected sortKey: string; // The key to use to order messages on auth_token: string; chat: Chat; resourceIdentifyingMetadata: { id: number, auth_token: string }; protected _subResources = { 'chats': { url: this.baseResource + 'chats/', resource: 'chats' }, }; /** * @param data */ constructor(data?: ClientCredentials) { super(); if (data) { this.sessionKey = data.session; this.stream_endpoint = '/users/'; this.id = data.id; this.auth_token = data.auth_token; this.resourceIdentifyingMetadata = { id: data.id, auth_token: data.auth_token }; } } getChats(filters?: ChatResourceFilters): Promise { return new Promise((resolve, reject) => { this.getChatData(filters).then((response: APICursorHttpResponse) => { let chats: Chat[] = []; response.results.forEach((data: ChatData) => { chats.push(new Chat(data, this.sessionKey)); }); resolve(chats); }).catch(reject); }) } getChatData(filters?: ChatResourceFilters): Promise> { return this.requestSubResource('chats', undefined, filters); } getChatsOrCreateChat(filters?: ChatResourceFilters, metadata?: any): Promise { let that = this; return new Promise(function (resolve, reject) { that.getChats(filters).then((chats: Chat[]) => { if (chats.length === 0) { that.createChat({ client: that.id, type: (filters ? filters.type : undefined), metadata: metadata }).then(function (chat: Chat) { that.chat = chat; // TODO remove references to local chat instance resolve([that.chat]); }).catch(reject); } else { resolve(chats); } }).catch(reject); }); } getChat(uuid: string): Promise { return this.request(`/chats/${uuid}/`); } async createChat(data?: ChatCreationData): Promise { let chat = new Chat(await this.post('/chats/', data), this.sessionKey); return chat; } /** * Listen for updates on one specific Client instance * @return {Client} */ listenForClientUpdates() { this.ensureListeningOnStream('client', ['create', 'received']); return this; }; /** * Listen for updates on one generic updates to all clients * @return {Client} */ listenForGenericClientsUpdates() { this.ensureListeningOnStream('clients', ['update']); return this; }; public login(email: string, password: string): Promise { let promise = this.post('/auth/login/', {email: email, password: password}); promise.then(function (resp: ClientData) { mc_log(resp); }); return promise; } public logout() { return this.post('/auth/logout') } }