nodevk-ts
Version:
Simple Node.js module that allows you to interact with the VKontakte API.
108 lines (107 loc) • 4.29 kB
JavaScript
import Session from "./Session.js";
import { NewMessageEvent } from '../Events/NewMessageEventHandler.js';
export var EventPriority;
(function (EventPriority) {
EventPriority[EventPriority["DEFAULT"] = 10] = "DEFAULT";
EventPriority[EventPriority["MODULE"] = 5] = "MODULE";
})(EventPriority || (EventPriority = {}));
class GroupEvents {
constructor() {
this.eventList = {};
}
on(event, callback, priority = EventPriority.DEFAULT) {
if (this.eventList[event] == null)
this.eventList[event] = [];
if (this.eventList[event][priority] == null)
this.eventList[event][priority] = [];
this.eventList[event][priority].push(callback);
}
async invoke(event, ...args) {
if (this.eventList[event] != null)
for (const callList of this.eventList[event])
if (Array.isArray(callList))
for (const call of callList)
if ((await call.apply(this, args)) === true)
return true;
return false;
}
}
export default class GroupSession extends Session {
constructor(token, config) {
super(config);
this.eventList = new GroupEvents();
this.on = this.eventList.on.bind(this.eventList);
this.group_id = null;
this.token = token;
}
async invokeMethod(method, params) {
if (!params.access_token)
params.access_token = this.token;
return super.invokeMethod(method, params);
}
async invoke(event, ...args) {
const prevented = await GroupSession.globalEventList.invoke(event, ...args);
if (prevented === false)
this.eventList.invoke(event, ...args);
}
setSettingsLongPoll(group_id) {
if (group_id)
this.group_id = group_id;
}
async startLongPoll(callback) {
if (this.group_id == null)
throw new Error("Не был указан ID группы.");
let server_info = await this.getLongPollServer(this.group_id);
if (callback)
callback();
this.server = server_info.server;
this.key = server_info.key;
this.getEvents(server_info.ts);
}
async getLongPollServer(group_id) {
return (await this.invokeMethod("groups.getLongPollServer", { group_id: group_id })).response;
}
async getEvents(ts) {
let res = await this.request(this.server, {
act: "a_check",
key: this.key,
ts: ts,
wait: "25"
});
if (res.failed) {
switch (res.failed) {
case 1:
console.error("История событий устарела или была частично утеряна. LongPoll продолжает работать.");
this.getEvents(res.ts);
break;
case 2:
console.error("Истекло время действия ключа. Получаю повторно ключ.");
this.startLongPoll();
break;
case 3:
console.log("Информация утрачена. Получаю повторно ключ.");
this.startLongPoll();
break;
default:
console.log("Неизвестная ошибка. Запускаю сервер повторно.");
this.startLongPoll();
break;
}
}
else {
for (let event of res.updates) {
switch (event.type) {
case "message_new":
this.invoke(event.type, new NewMessageEvent(event.object, this));
break;
default:
this.invoke(event.type, event.object);
break;
}
}
this.getEvents(res.ts);
}
}
}
GroupSession.globalEventList = new GroupEvents();
GroupSession.on = GroupSession.globalEventList.on.bind(GroupSession.globalEventList);