Skip to content

Commit

Permalink
feat(updates): migrate to 19 polling version
Browse files Browse the repository at this point in the history
  • Loading branch information
negezor committed Jan 9, 2024
1 parent 8cfac86 commit f874b7a
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 41 deletions.
4 changes: 1 addition & 3 deletions packages/vk-io/src/structures/contexts/dialog-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ import { kSerializeData } from '../../utils/constants';
export type DialogFlagsContextType = 'dialog_flags';

export type DialogFlagsContextSubType =
'dialog_flags_replace'
| 'dialog_flags_add'
'dialog_flags_add'
| 'dialog_flags_delete';

const subTypes: Record<string, DialogFlagsContextSubType> = {
10: 'dialog_flags_delete',
11: 'dialog_flags_replace',
12: 'dialog_flags_add',
};

Expand Down
12 changes: 4 additions & 8 deletions packages/vk-io/src/structures/contexts/dialog-messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@ import { kSerializeData } from '../../utils/constants';

export type DialogMessagesContextType = 'dialog_messages';

export type DialogMessagesContextSubType =
'dialog_messages_delete'
| 'dialog_messages_restore';
export type DialogMessagesContextSubType = 'dialog_messages_delete';

const subTypes: Record<number, DialogMessagesContextSubType> = {
13: 'dialog_messages_delete',
14: 'dialog_messages_restore',
10013: 'dialog_messages_delete',
};
export interface IDialogMessagesContextPayload {
local_id: number;
Expand Down Expand Up @@ -55,10 +52,9 @@ export class DialogMessagesContext<S = ContextDefaultState>

/**
* Checks if messages are restored
* @deprecated
*/
public get isRestore(): boolean {
return this.subTypes.includes('dialog_messages_restore');
}
public readonly isRestore = false;

/**
* Returns the peer identifier
Expand Down
48 changes: 35 additions & 13 deletions packages/vk-io/src/structures/contexts/helpers/transform-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,25 +81,24 @@ const attachmentHandlers = {
/**
* Transform message to Object
*/
export function transformMessage({
1: id,
2: flags,
3: peer_id,
4: date,
5: text,
6: extra,
7: attachments,
8: random_id,
9: conversation_message_id,
10: update_time,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
}: [
export function transformMessage(rawMessage: [
// type
number,
// conversation message id
number,
// flags
number,
// minor id
number,
// peer id
number,
// timestamp
number,

// text
string,

// additional
{
title?: string;
from?: string;
Expand All @@ -115,16 +114,39 @@ export function transformMessage({

payload?: string;
},
// attachments
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Record<string, any> & {
fwd?: string;
reply?: string;
},
// randomId
number,
// messageId
number,
// updateTimestamp
number
// eslint-disable-next-line @typescript-eslint/no-explicit-any,
]): IMessageContextPayload['message'] {
if (rawMessage[0] !== 10004) {
// set minor id to 0
rawMessage.splice(3, 0, 0);
}

const {
1: conversation_message_id,
2: flags,
// 3: minor_id,
4: peer_id,
5: date,
6: text,
7: extra,
8: attachments,
9: random_id,
10: id,
11: update_time,
} = rawMessage;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const message = {
id,
Expand Down
14 changes: 10 additions & 4 deletions packages/vk-io/src/structures/contexts/message-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ import { kSerializeData, UpdateSource } from '../../utils/constants';
export type MessageFlagsContextType = 'message_flags';

export type MessageFlagsContextSubType =
'message_flags_replace'
| 'message_flags_add'
'message_flags_add'
| 'message_flags_delete';

const subTypes: Record<number, MessageFlagsContextSubType> = {
1: 'message_flags_replace',
2: 'message_flags_add',
3: 'message_flags_delete',
};
Expand All @@ -34,7 +32,8 @@ enum MessageFlag {
DELETED_FOR_ALL = 1 << 17,
INBOX_FROM_CHAT = 1 << 19,
SILENT = 1 << 20,
REPLIED = 1 << 21
REPLIED = 1 << 21,
AUTO_READ = 1 << 23
}
/* eslint-enable no-bitwise */

Expand Down Expand Up @@ -191,6 +190,13 @@ export class MessageFlagsContext<S = ContextDefaultState>
return this.hasFlag(MessageFlag.REPLIED);
}

/**
* Checks a reply has been created to the message
*/
public get isAutoRead(): boolean {
return this.hasFlag(MessageFlag.AUTO_READ);
}

/**
* Returns the message ID
*/
Expand Down
8 changes: 5 additions & 3 deletions packages/vk-io/src/structures/contexts/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ export type MessageContextSubType =
| MessageContextPayloadEventType;

const subTypesEnum: Record<string | number, MessageContextSubType> = {
4: 'message_new',
5: 'message_edit',
18: 'message_edit',
10004: 'message_new',
10005: 'message_edit',
10018: 'message_edit',
};

const kForwards = Symbol('forwards');
Expand Down Expand Up @@ -821,6 +821,8 @@ class MessageContext<S = ContextDefaultState>
}
: payload;

console.log(payload);

const { message } = this;

this.text = message.text
Expand Down
4 changes: 2 additions & 2 deletions packages/vk-io/src/structures/contexts/messages-read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export type MessagesReadContextSubType =
| 'messages_read_outbox';

const subTypes: Record<number, MessagesReadContextSubType> = {
6: 'messages_read_inbox',
7: 'messages_read_outbox',
10006: 'messages_read_inbox',
10007: 'messages_read_outbox',
};

export interface IMessagesReadContextPayload {
Expand Down
11 changes: 8 additions & 3 deletions packages/vk-io/src/updates/transports/polling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const debug = createDebug('vk-io:updates');
/**
* Version polling
*/
const POLLING_VERSION = 10;
const POLLING_VERSION = 19;

export class PollingTransport {
public started = false;
Expand Down Expand Up @@ -164,7 +164,12 @@ export class PollingTransport {

const interval = setTimeout(() => controller.abort(), 30e3);

let result;
let result:
| { ts: number, pts?: number, updates: unknown[] }
| { failed: 1, ts: number }
| { failed: 2, error: string }
| { failed: 4, min_version: 0, max_version: 19 };

try {
const response = await fetch(this.url, {
agent: this.options.agent,
Expand All @@ -191,7 +196,7 @@ export class PollingTransport {
clearTimeout(interval);
}

if (result.failed !== undefined) {
if ('failed' in result) {
if (result.failed === 1) {
this.ts = result.ts;

Expand Down
10 changes: 5 additions & 5 deletions packages/vk-io/src/updates/updates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,27 +214,27 @@ const webhookContextsEvents: [string[], Constructor<any>][] = [
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const pollingContextsEvents: [number[], Constructor<any>][] = [
[
[1, 2, 3],
[10002, 10003],
MessageFlagsContext,
],
[
[4, 5, 18],
[10004, 10005, 10018],
MessageContext,
],
[
[6, 7],
[10006, 10007],
MessagesReadContext,
],
[
[8, 9, 81],
FriendActivityContext,
],
[
[10, 11, 12],
[10, 12],
DialogFlagsContext,
],
[
[13, 14],
[10013],
DialogMessagesContext,
],
[
Expand Down

0 comments on commit f874b7a

Please sign in to comment.