UNPKG

@fnlb-project/fnbr

Version:

A library to interact with Epic Games' Fortnite HTTP and XMPP services

116 lines 4.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const Endpoints_1 = tslib_1.__importDefault(require("../../resources/Endpoints")); const enums_1 = require("../../resources/enums"); const Base_1 = tslib_1.__importDefault(require("../Base")); const UserNotFoundError_1 = tslib_1.__importDefault(require("../exceptions/UserNotFoundError")); function customUUID() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => { const r = Math.random() * 16 | 0; const v = c === 'x' ? r : (r & 0x3 | 0x8); return v.toString(16); }); } // private scope const generateCustomCorrelationId = () => `EOS-${Date.now()}-${customUUID()}`; /** * Represent's the client's chat manager (dm, party chat) via eos. */ class ChatManager extends Base_1.default { /** * Returns the chat namespace, this is the eos deployment id */ get namespace() { return this.client.config.eosDeploymentId; } async getConversationId(userId) { const data = await this.client.http.epicgamesRequest({ method: 'POST', url: `${Endpoints_1.default.EOS_CHAT}/v1/public/_/conversations?createIfExists=false`, headers: { 'Content-Type': 'application/json', }, data: { title: '', type: 'dm', members: [this.client.user.self.id, userId], }, }, enums_1.AuthSessionStoreKey.FortniteEOS); return data.conversationId; } /** * Sends a private message to the specified user * @param user the account id or displayname * @param message the message object * @returns the message id */ async whisperUser(user, message) { const accountId = await this.client.user.resolveId(user); if (!accountId) throw new UserNotFoundError_1.default(user); const conversationId = await this.getConversationId(accountId); const { body, signatureString } = this.client.createSignedMessage(conversationId, message.body); const eosSession = this.client.auth.sessions.get(enums_1.AuthSessionStoreKey.FortniteEOS); await this.client.http.epicgamesRequest({ method: 'POST', url: `${Endpoints_1.default.EOS_CHAT}/v1/public/_/conversations/${conversationId}/messages?fromAccountId=${this.client.user.self.id}`, headers: { 'Content-Type': 'application/json', Authorization: `bearer ${eosSession.easAccessToken}`, }, data: { allowedRecipients: [accountId, this.client.user.self.id], message: { body, }, isReportable: false, metadata: { TmV: '2', Pub: this.client.keyData.jwt, Sig: signatureString, PlfNm: this.client.config.platform, PlfId: this.client.user.self.id, }, }, }); return conversationId; } /** * Sends a message in the specified conversation (party chat) * @param conversationId the conversation id, usually `p-[PARTYID]` * @param message the message object * @param allowedRecipients the account ids, that should receive the message * @returns the message id */ async sendMessageInConversation(conversationId, message, allowedRecipients) { const { body, signatureString } = this.client.createSignedMessage(conversationId, message.body, 'Party'); const eosSession = this.client.auth.sessions.get(enums_1.AuthSessionStoreKey.FortniteEOS); await this.client.http.epicgamesRequest({ method: 'POST', url: `${Endpoints_1.default.EOS_CHAT}/v1/public/${this.namespace}/conversations/${conversationId}/messages?fromAccountId=${this.client.user.self.id}`, headers: { 'Content-Type': 'application/json', Authorization: `bearer ${eosSession.easAccessToken}`, }, data: { allowedRecipients: allowedRecipients.filter((id) => id !== this.client.user.self.id), message: { body, }, isReportable: false, metadata: { TmV: '2', Pub: this.client.keyData.jwt, Sig: signatureString, NPM: '1', PlfNm: this.client.config.platform, PlfId: this.client.user.self.id, }, }, }); return conversationId; } } exports.default = ChatManager; //# sourceMappingURL=ChatManager.js.map