@amelix/phoenix.js
Version:
A feature-rich API wrapper for the critically acclaimed chatting app Phoenix.. or something.
119 lines (118 loc) • 4.97 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const config_1 = require("../config");
const Message_1 = require("./Message");
const ServerChannel_1 = require("./ServerChannel");
class TextChannel extends ServerChannel_1.default {
constructor(client, data) {
super(client, data);
this.messageQueries = {};
this.fullyCached = false;
this.messages = new Map();
this.messageIDs = [];
this.usersTyping = [];
this.clientTyping = false;
}
/** Send a message to the channel. */
send(content) {
return __awaiter(this, void 0, void 0, function* () {
if (!content)
throw new Error("Cannot send empty messages!");
this.stopTyping();
const res = yield this.client.request("POST", "/channels/" + this.id, {}, { content: content });
return new Message_1.default(this.client, res.body);
});
}
addMessage([id, message], before = false) {
return __awaiter(this, void 0, void 0, function* () {
if (message.author && !this.client.users.has(message.author)) {
yield this.client.fetchUser(message.author);
}
const newMessage = new Message_1.default(this.client, message);
this.messages.set(id, newMessage);
before ? this.messageIDs.push(id) : this.messageIDs.unshift(id);
return newMessage;
});
}
/** Fetch a single message from this channel. */
fetchMessage(id) {
return __awaiter(this, void 0, void 0, function* () {
let msg = this.messages.get(id);
if (msg)
return msg;
const res = yield this.client.request("GET", `/channels/${this.id}/messages/${id}`);
msg = yield this.addMessage([id, res.body]);
return msg;
});
}
/** Fetch messages from this channel. */
fetchMessages(options = {}) {
return __awaiter(this, void 0, void 0, function* () {
if (!options.around && !options.before && !options.after && this.fullyCached) {
return new Map(Array.from(this.messages).slice(-(options.limit || config_1.messageQueryLimit)));
}
let query = "?", option;
for (option in options) {
query += `${option}=${options[option]}&`;
}
const path = `/channels/${this.id}/messages${query}`;
if (this.messageQueries[path])
return this.messageQueries[path];
const res = yield this.client.request("GET", path);
let arr = res.body, out = new Map();
if (options.before) {
for (let i = arr.length - 1; i >= 0; i--) {
out.set(arr[i][0], yield this.addMessage(arr[i]));
}
}
else {
for (let i = 0; i <= arr.length - 1; i++) {
out.set(arr[i][0], yield this.addMessage(arr[i], true));
}
}
this.messageQueries[path] = out;
if (out.size < (options.limit || config_1.messageQueryLimit))
this.fullyCached = true;
return out;
});
}
/**
* Check if a user is typing in this channel.
* @param {string} id User ID
*/
isTyping(id) {
if (id === this.client.user.id)
return this.clientTyping;
return this.usersTyping.includes(id);
}
/**
* Start a typing indicator in this channel.
*/
startTyping() {
var _a;
if (!this.isTyping(this.client.user.id)) {
this.clientTyping = true;
(_a = this.client.socket) === null || _a === void 0 ? void 0 : _a.emit('typingStart', this.id);
}
}
/**
* Stop the typing indicator in this channel.
*/
stopTyping() {
var _a;
if (this.isTyping(this.client.user.id)) {
this.clientTyping = false;
(_a = this.client.socket) === null || _a === void 0 ? void 0 : _a.emit('typingStop', this.id);
}
}
}
exports.default = TextChannel;