@mtcute/dispatcher 0.16.0 → 0.16.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/callback-data-builder.test.d.ts +1 -0
- package/{esm/context → context}/business-message.d.ts +17 -17
- package/{esm/context → context}/callback-query.d.ts +4 -4
- package/{cjs/context → context}/chat-join-request.d.ts +1 -1
- package/{esm/context → context}/chosen-inline-result.d.ts +1 -1
- package/{esm/context → context}/inline-query.d.ts +2 -2
- package/{cjs/context → context}/message.d.ts +3 -3
- package/context/parse.d.ts +13 -0
- package/{esm/context → context}/pre-checkout-query.d.ts +1 -1
- package/{cjs/dispatcher.d.ts → dispatcher.d.ts} +2 -2
- package/{cjs/filters → filters}/bots.d.ts +13 -23
- package/filters/bots.test.d.ts +1 -0
- package/{esm/filters → filters}/chat.d.ts +2 -2
- package/filters/logic.test.d.ts +1 -0
- package/filters/message.d.ts +214 -0
- package/{cjs/filters → filters}/state.d.ts +1 -1
- package/{esm/filters → filters}/text.d.ts +5 -5
- package/{esm/filters → filters}/user.d.ts +1 -1
- package/{esm/handler.d.ts → handler.d.ts} +1 -1
- package/index.cjs +2510 -0
- package/index.d.ts +8 -1
- package/index.js +2505 -1
- package/package.json +28 -24
- package/{esm/state → state}/repository.d.ts +6 -6
- package/{esm/state → state}/service.d.ts +3 -2
- package/{cjs/state → state}/update-state.d.ts +1 -1
- package/{esm/wizard.d.ts → wizard.d.ts} +4 -4
- package/cjs/callback-data-builder.js +0 -139
- package/cjs/callback-data-builder.js.map +0 -1
- package/cjs/context/base.js +0 -3
- package/cjs/context/base.js.map +0 -1
- package/cjs/context/business-message.d.ts +0 -60
- package/cjs/context/business-message.js +0 -146
- package/cjs/context/business-message.js.map +0 -1
- package/cjs/context/callback-query.d.ts +0 -62
- package/cjs/context/callback-query.js +0 -109
- package/cjs/context/callback-query.js.map +0 -1
- package/cjs/context/chat-join-request.js +0 -35
- package/cjs/context/chat-join-request.js.map +0 -1
- package/cjs/context/chosen-inline-result.d.ts +0 -22
- package/cjs/context/chosen-inline-result.js +0 -36
- package/cjs/context/chosen-inline-result.js.map +0 -1
- package/cjs/context/index.js +0 -25
- package/cjs/context/index.js.map +0 -1
- package/cjs/context/inline-query.d.ts +0 -15
- package/cjs/context/inline-query.js +0 -23
- package/cjs/context/inline-query.js.map +0 -1
- package/cjs/context/message.js +0 -171
- package/cjs/context/message.js.map +0 -1
- package/cjs/context/parse.d.ts +0 -13
- package/cjs/context/parse.js +0 -43
- package/cjs/context/parse.js.map +0 -1
- package/cjs/context/pre-checkout-query.d.ts +0 -17
- package/cjs/context/pre-checkout-query.js +0 -27
- package/cjs/context/pre-checkout-query.js.map +0 -1
- package/cjs/context/scene-transition.js +0 -52
- package/cjs/context/scene-transition.js.map +0 -1
- package/cjs/dispatcher.js +0 -905
- package/cjs/dispatcher.js.map +0 -1
- package/cjs/filters/bots.js +0 -133
- package/cjs/filters/bots.js.map +0 -1
- package/cjs/filters/bundle.js +0 -27
- package/cjs/filters/bundle.js.map +0 -1
- package/cjs/filters/chat.d.ts +0 -27
- package/cjs/filters/chat.js +0 -55
- package/cjs/filters/chat.js.map +0 -1
- package/cjs/filters/group.js +0 -72
- package/cjs/filters/group.js.map +0 -1
- package/cjs/filters/index.js +0 -29
- package/cjs/filters/index.js.map +0 -1
- package/cjs/filters/logic.js +0 -114
- package/cjs/filters/logic.js.map +0 -1
- package/cjs/filters/message.d.ts +0 -332
- package/cjs/filters/message.js +0 -179
- package/cjs/filters/message.js.map +0 -1
- package/cjs/filters/state.js +0 -32
- package/cjs/filters/state.js.map +0 -1
- package/cjs/filters/text.d.ts +0 -64
- package/cjs/filters/text.js +0 -135
- package/cjs/filters/text.js.map +0 -1
- package/cjs/filters/types.js +0 -6
- package/cjs/filters/types.js.map +0 -1
- package/cjs/filters/updates.js +0 -40
- package/cjs/filters/updates.js.map +0 -1
- package/cjs/filters/user.d.ts +0 -24
- package/cjs/filters/user.js +0 -78
- package/cjs/filters/user.js.map +0 -1
- package/cjs/handler.d.ts +0 -41
- package/cjs/handler.js +0 -4
- package/cjs/handler.js.map +0 -1
- package/cjs/index.js +0 -31
- package/cjs/index.js.map +0 -1
- package/cjs/package.json +0 -3
- package/cjs/propagation.js +0 -27
- package/cjs/propagation.js.map +0 -1
- package/cjs/state/index.js +0 -22
- package/cjs/state/index.js.map +0 -1
- package/cjs/state/key.js +0 -43
- package/cjs/state/key.js.map +0 -1
- package/cjs/state/provider.js +0 -3
- package/cjs/state/provider.js.map +0 -1
- package/cjs/state/providers/index.js +0 -19
- package/cjs/state/providers/index.js.map +0 -1
- package/cjs/state/providers/memory.js +0 -81
- package/cjs/state/providers/memory.js.map +0 -1
- package/cjs/state/providers/sqlite.js +0 -100
- package/cjs/state/providers/sqlite.js.map +0 -1
- package/cjs/state/repository.d.ts +0 -62
- package/cjs/state/repository.js +0 -3
- package/cjs/state/repository.js.map +0 -1
- package/cjs/state/service.d.ts +0 -19
- package/cjs/state/service.js +0 -70
- package/cjs/state/service.js.map +0 -1
- package/cjs/state/update-state.js +0 -220
- package/cjs/state/update-state.js.map +0 -1
- package/cjs/wizard.d.ts +0 -64
- package/cjs/wizard.js +0 -103
- package/cjs/wizard.js.map +0 -1
- package/esm/callback-data-builder.d.ts +0 -49
- package/esm/callback-data-builder.js +0 -135
- package/esm/callback-data-builder.js.map +0 -1
- package/esm/context/base.d.ts +0 -9
- package/esm/context/base.js +0 -2
- package/esm/context/base.js.map +0 -1
- package/esm/context/business-message.js +0 -142
- package/esm/context/business-message.js.map +0 -1
- package/esm/context/callback-query.js +0 -103
- package/esm/context/callback-query.js.map +0 -1
- package/esm/context/chat-join-request.d.ts +0 -17
- package/esm/context/chat-join-request.js +0 -31
- package/esm/context/chat-join-request.js.map +0 -1
- package/esm/context/chosen-inline-result.js +0 -32
- package/esm/context/chosen-inline-result.js.map +0 -1
- package/esm/context/index.d.ts +0 -9
- package/esm/context/index.js +0 -9
- package/esm/context/index.js.map +0 -1
- package/esm/context/inline-query.js +0 -19
- package/esm/context/inline-query.js.map +0 -1
- package/esm/context/message.d.ts +0 -76
- package/esm/context/message.js +0 -167
- package/esm/context/message.js.map +0 -1
- package/esm/context/parse.d.ts +0 -13
- package/esm/context/parse.js +0 -39
- package/esm/context/parse.js.map +0 -1
- package/esm/context/pre-checkout-query.js +0 -23
- package/esm/context/pre-checkout-query.js.map +0 -1
- package/esm/context/scene-transition.d.ts +0 -24
- package/esm/context/scene-transition.js +0 -48
- package/esm/context/scene-transition.js.map +0 -1
- package/esm/dispatcher.d.ts +0 -880
- package/esm/dispatcher.js +0 -901
- package/esm/dispatcher.js.map +0 -1
- package/esm/filters/bots.d.ts +0 -74
- package/esm/filters/bots.js +0 -129
- package/esm/filters/bots.js.map +0 -1
- package/esm/filters/bundle.d.ts +0 -10
- package/esm/filters/bundle.js +0 -11
- package/esm/filters/bundle.js.map +0 -1
- package/esm/filters/chat.js +0 -50
- package/esm/filters/chat.js.map +0 -1
- package/esm/filters/group.d.ts +0 -26
- package/esm/filters/group.js +0 -67
- package/esm/filters/group.js.map +0 -1
- package/esm/filters/index.d.ts +0 -4
- package/esm/filters/index.js +0 -3
- package/esm/filters/index.js.map +0 -1
- package/esm/filters/logic.d.ts +0 -29
- package/esm/filters/logic.js +0 -107
- package/esm/filters/logic.js.map +0 -1
- package/esm/filters/message.d.ts +0 -332
- package/esm/filters/message.js +0 -156
- package/esm/filters/message.js.map +0 -1
- package/esm/filters/state.d.ts +0 -15
- package/esm/filters/state.js +0 -27
- package/esm/filters/state.js.map +0 -1
- package/esm/filters/text.js +0 -127
- package/esm/filters/text.js.map +0 -1
- package/esm/filters/types.d.ts +0 -91
- package/esm/filters/types.js +0 -5
- package/esm/filters/types.js.map +0 -1
- package/esm/filters/updates.d.ts +0 -39
- package/esm/filters/updates.js +0 -34
- package/esm/filters/updates.js.map +0 -1
- package/esm/filters/user.js +0 -72
- package/esm/filters/user.js.map +0 -1
- package/esm/handler.js +0 -3
- package/esm/handler.js.map +0 -1
- package/esm/index.d.ts +0 -8
- package/esm/index.js +0 -9
- package/esm/index.js.map +0 -1
- package/esm/propagation.d.ts +0 -22
- package/esm/propagation.js +0 -24
- package/esm/propagation.js.map +0 -1
- package/esm/state/index.d.ts +0 -5
- package/esm/state/index.js +0 -6
- package/esm/state/index.js.map +0 -1
- package/esm/state/key.d.ts +0 -24
- package/esm/state/key.js +0 -39
- package/esm/state/key.js.map +0 -1
- package/esm/state/provider.d.ts +0 -5
- package/esm/state/provider.js +0 -2
- package/esm/state/provider.js.map +0 -1
- package/esm/state/providers/index.d.ts +0 -2
- package/esm/state/providers/index.js +0 -3
- package/esm/state/providers/index.js.map +0 -1
- package/esm/state/providers/memory.d.ts +0 -29
- package/esm/state/providers/memory.js +0 -77
- package/esm/state/providers/memory.js.map +0 -1
- package/esm/state/providers/sqlite.d.ts +0 -28
- package/esm/state/providers/sqlite.js +0 -96
- package/esm/state/providers/sqlite.js.map +0 -1
- package/esm/state/repository.js +0 -2
- package/esm/state/repository.js.map +0 -1
- package/esm/state/service.js +0 -66
- package/esm/state/service.js.map +0 -1
- package/esm/state/update-state.d.ts +0 -151
- package/esm/state/update-state.js +0 -215
- package/esm/state/update-state.js.map +0 -1
- package/esm/wizard.js +0 -99
- package/esm/wizard.js.map +0 -1
- /package/{cjs/callback-data-builder.d.ts → callback-data-builder.d.ts} +0 -0
- /package/{cjs/context → context}/base.d.ts +0 -0
- /package/{cjs/context → context}/index.d.ts +0 -0
- /package/{cjs/context → context}/scene-transition.d.ts +0 -0
- /package/{cjs/filters → filters}/bundle.d.ts +0 -0
- /package/{cjs/filters → filters}/group.d.ts +0 -0
- /package/{cjs/filters → filters}/index.d.ts +0 -0
- /package/{cjs/filters → filters}/logic.d.ts +0 -0
- /package/{cjs/filters → filters}/types.d.ts +0 -0
- /package/{cjs/filters → filters}/updates.d.ts +0 -0
- /package/{cjs/index.d.ts → index.d.cts} +0 -0
- /package/{cjs/propagation.d.ts → propagation.d.ts} +0 -0
- /package/{cjs/state → state}/index.d.ts +0 -0
- /package/{cjs/state → state}/key.d.ts +0 -0
- /package/{cjs/state → state}/provider.d.ts +0 -0
- /package/{cjs/state → state}/providers/index.d.ts +0 -0
- /package/{cjs/state → state}/providers/memory.d.ts +0 -0
- /package/{cjs/state → state}/providers/sqlite.d.ts +0 -0
package/esm/filters/message.js
DELETED
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
// ^^ will be looked into in MTQ-29
|
|
3
|
-
import { RawDocument, RawLocation, } from '@mtcute/core';
|
|
4
|
-
/**
|
|
5
|
-
* Filter incoming messages.
|
|
6
|
-
*
|
|
7
|
-
* Messages sent to yourself (i.e. Saved Messages) are also "incoming"
|
|
8
|
-
*/
|
|
9
|
-
export const incoming = (msg) => !msg.isOutgoing;
|
|
10
|
-
/**
|
|
11
|
-
* Filter outgoing messages.
|
|
12
|
-
*
|
|
13
|
-
* Messages sent to yourself (i.e. Saved Messages) are **not** "outgoing"
|
|
14
|
-
*/
|
|
15
|
-
export const outgoing = (msg) => msg.isOutgoing;
|
|
16
|
-
/**
|
|
17
|
-
* Filter for scheduled messages
|
|
18
|
-
*/
|
|
19
|
-
export const scheduled = (msg) => msg.isScheduled;
|
|
20
|
-
/**
|
|
21
|
-
* Filter messages that are replies to some other message
|
|
22
|
-
*/
|
|
23
|
-
export const reply = (msg) => msg.replyToMessage !== null;
|
|
24
|
-
/**
|
|
25
|
-
* Filter messages that are replies with the given origin type
|
|
26
|
-
*/
|
|
27
|
-
export const replyOrigin = (origin) => (msg) => msg.replyToMessage?.originIs(origin) ?? false; // originIs does additional checks
|
|
28
|
-
/**
|
|
29
|
-
* Filter messages containing some media
|
|
30
|
-
*/
|
|
31
|
-
export const media = (msg) => msg.media !== null;
|
|
32
|
-
/**
|
|
33
|
-
* Filter messages containing media of given type
|
|
34
|
-
*/
|
|
35
|
-
export const mediaOf = (type) => (msg) => msg.media?.type === type;
|
|
36
|
-
/** Filter messages containing a photo */
|
|
37
|
-
export const photo = mediaOf('photo');
|
|
38
|
-
/** Filter messages containing a dice */
|
|
39
|
-
export const dice = mediaOf('dice');
|
|
40
|
-
/** Filter messages containing a contact */
|
|
41
|
-
export const contact = mediaOf('contact');
|
|
42
|
-
/** Filter messages containing an audio file */
|
|
43
|
-
export const audio = mediaOf('audio');
|
|
44
|
-
/** Filter messages containing a voice message (audio-only) */
|
|
45
|
-
export const voice = mediaOf('voice');
|
|
46
|
-
/** Filter messages containing a sticker */
|
|
47
|
-
export const sticker = mediaOf('sticker');
|
|
48
|
-
/** Filter messages containing a document (a file) */
|
|
49
|
-
export const document = mediaOf('document');
|
|
50
|
-
/** Filter messages containing any video (videos, round messages and animations) */
|
|
51
|
-
export const anyVideo = mediaOf('video');
|
|
52
|
-
/** Filter messages containing a static location */
|
|
53
|
-
export const location = mediaOf('location');
|
|
54
|
-
/** Filter messages containing a live location */
|
|
55
|
-
export const liveLocation = mediaOf('live_location');
|
|
56
|
-
/** Filter messages containing a game */
|
|
57
|
-
export const game = mediaOf('game');
|
|
58
|
-
/** Filter messages containing a web page */
|
|
59
|
-
export const webpage = mediaOf('webpage');
|
|
60
|
-
/** Filter messages containing a venue */
|
|
61
|
-
export const venue = mediaOf('venue');
|
|
62
|
-
/** Filter messages containing a poll */
|
|
63
|
-
export const poll = mediaOf('poll');
|
|
64
|
-
/** Filter messages containing an invoice */
|
|
65
|
-
export const invoice = mediaOf('invoice');
|
|
66
|
-
/**
|
|
67
|
-
* Filter messages containing any location (live or static).
|
|
68
|
-
*/
|
|
69
|
-
export const anyLocation = (msg) => msg.media instanceof RawLocation;
|
|
70
|
-
/**
|
|
71
|
-
* Filter messages containing a document
|
|
72
|
-
*
|
|
73
|
-
* This will also match media like audio, video, voice
|
|
74
|
-
* that also use Documents
|
|
75
|
-
*/
|
|
76
|
-
export const anyDocument = (msg) => msg.media instanceof RawDocument;
|
|
77
|
-
/**
|
|
78
|
-
* Filter messages containing a simple video.
|
|
79
|
-
*
|
|
80
|
-
* This does not include round messages and animations
|
|
81
|
-
*/
|
|
82
|
-
export const video = (msg) => msg.media?.type === 'video' && !msg.media.isAnimation && !msg.media.isRound;
|
|
83
|
-
/**
|
|
84
|
-
* Filter messages containing an animation.
|
|
85
|
-
*
|
|
86
|
-
* > **Note**: Legacy GIFs (i.e. documents with `image/gif` MIME)
|
|
87
|
-
* > are also considered animations.
|
|
88
|
-
*/
|
|
89
|
-
export const animation = (msg) => msg.media?.type === 'video' && msg.media.isAnimation && !msg.media.isRound;
|
|
90
|
-
/**
|
|
91
|
-
* Filter messages containing a round message (aka video note).
|
|
92
|
-
*/
|
|
93
|
-
export const roundMessage = (msg) => msg.media?.type === 'video' && !msg.media.isAnimation && msg.media.isRound;
|
|
94
|
-
/**
|
|
95
|
-
* Filter messages containing a sticker by its type
|
|
96
|
-
*/
|
|
97
|
-
export const stickerByType = (type) => (msg) => msg.media?.type === 'sticker' && msg.media.stickerType === type;
|
|
98
|
-
/**
|
|
99
|
-
* Filter messages containing a sticker by its source file type
|
|
100
|
-
*/
|
|
101
|
-
export const stickerBySourceType = (type) => (msg) => msg.media?.type === 'sticker' && msg.media.sourceType === type;
|
|
102
|
-
/**
|
|
103
|
-
* Filter text-only messages non-service messages
|
|
104
|
-
*/
|
|
105
|
-
export const text = (msg) => msg.media === null && !msg.isService;
|
|
106
|
-
/**
|
|
107
|
-
* Filter service messages
|
|
108
|
-
*/
|
|
109
|
-
export const service = (msg) => msg.isService;
|
|
110
|
-
/**
|
|
111
|
-
* Filter service messages by action type
|
|
112
|
-
*/
|
|
113
|
-
export const action = (type) => {
|
|
114
|
-
if (Array.isArray(type)) {
|
|
115
|
-
const index = {};
|
|
116
|
-
type.forEach((it) => (index[it] = true));
|
|
117
|
-
return (msg) => msg.action?.type in index;
|
|
118
|
-
}
|
|
119
|
-
return (msg) => msg.action?.type === type;
|
|
120
|
-
};
|
|
121
|
-
export const sender = (type) => (msg) => msg.sender.type === type;
|
|
122
|
-
/**
|
|
123
|
-
* Filter that matches messages that are replies to some other message that can be fetched
|
|
124
|
-
* (i.e. not `private` origin, and has not been deleted)
|
|
125
|
-
*
|
|
126
|
-
* Optionally, you can pass a filter that will be applied to the replied message.
|
|
127
|
-
*/
|
|
128
|
-
export const replyTo = (filter) => async (msg, state) => {
|
|
129
|
-
if (!msg.replyToMessage?.id)
|
|
130
|
-
return false;
|
|
131
|
-
const reply = msg._name === 'new_message' ? await msg.getReplyTo() : msg.replyTo;
|
|
132
|
-
if (!reply)
|
|
133
|
-
return false;
|
|
134
|
-
if (msg._name === 'new_message') {
|
|
135
|
-
msg.getReplyTo = () => Promise.resolve(reply);
|
|
136
|
-
}
|
|
137
|
-
if (!filter)
|
|
138
|
-
return true;
|
|
139
|
-
return filter(reply, state);
|
|
140
|
-
};
|
|
141
|
-
/**
|
|
142
|
-
* Middleware-like filter that will fetch the sender of the message
|
|
143
|
-
* and make it available to further filters, as well as the handler itself.
|
|
144
|
-
*/
|
|
145
|
-
export const withCompleteSender = (filter) => async (msg, state) => {
|
|
146
|
-
try {
|
|
147
|
-
await msg.getCompleteSender();
|
|
148
|
-
}
|
|
149
|
-
catch (e) {
|
|
150
|
-
return false;
|
|
151
|
-
}
|
|
152
|
-
if (!filter)
|
|
153
|
-
return true;
|
|
154
|
-
return filter(msg, state);
|
|
155
|
-
};
|
|
156
|
-
//# sourceMappingURL=message.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"message.js","sourceRoot":"","sources":["../../../src/filters/message.ts"],"names":[],"mappings":"AAAA,uDAAuD;AACvD,mCAAmC;AACnC,OAAO,EAOH,WAAW,EACX,WAAW,GAQd,MAAM,cAAc,CAAA;AAMrB;;;;GAIG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAiD,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,UAAU,CAAA;AAE9F;;;;GAIG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAgD,CAAC,GAAG,EAA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ACjB,IAAI,CAAC;QACD,MAAM,GAAG,CAAC,iBAAiB,EAAE,CAAA;IACjC,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACT,OAAO,KAAK,CAAA;IAChB,CAAC;IAED,IAAI,CAAC,MAAM;QAAE,OAAO,IAAI,CAAA;IAExB,OAAO,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,CAAA;AAC7B,CAAC,CAAA","sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\n// ^^ will be looked into in MTQ-29\nimport {\n _RepliedMessageAssertionsByOrigin,\n MaybeArray,\n Message,\n MessageAction,\n MessageMediaType,\n Peer,\n RawDocument,\n RawLocation,\n RepliedMessageInfo,\n RepliedMessageOrigin,\n Sticker,\n StickerSourceType,\n StickerType,\n User,\n Video,\n} from '@mtcute/core'\n\nimport { BusinessMessageContext } from '../context/business-message.js'\nimport { MessageContext } from '../index.js'\nimport { Modify, UpdateFilter } from './types.js'\n\n/**\n * Filter incoming messages.\n *\n * Messages sent to yourself (i.e. Saved Messages) are also \"incoming\"\n */\nexport const incoming: UpdateFilter<Message, { isOutgoing: false }> = (msg) => !msg.isOutgoing\n\n/**\n * Filter outgoing messages.\n *\n * Messages sent to yourself (i.e. Saved Messages) are **not** \"outgoing\"\n */\nexport const outgoing: UpdateFilter<Message, { isOutgoing: true }> = (msg) => msg.isOutgoing\n\n/**\n * Filter for scheduled messages\n */\nexport const scheduled: UpdateFilter<Message, { isScheduled: true }> = (msg) => msg.isScheduled\n\n/**\n * Filter messages that are replies to some other message\n */\nexport const reply: UpdateFilter<Message, { replyToMessage: RepliedMessageInfo }> = (msg) => msg.replyToMessage !== null\n\n/**\n * Filter messages that are replies with the given origin type\n */\nexport const replyOrigin =\n <T extends RepliedMessageOrigin>(\n origin: T,\n ): UpdateFilter<\n Message,\n {\n replyToMessage: Modify<RepliedMessageInfo, _RepliedMessageAssertionsByOrigin[T] & { origin: T }>\n }\n > =>\n (msg) =>\n msg.replyToMessage?.originIs(origin) ?? false // originIs does additional checks\n\n/**\n * Filter messages containing some media\n */\nexport const media: UpdateFilter<Message, { media: Exclude<Message['media'], null> }> = (msg) => msg.media !== null\n\n/**\n * Filter messages containing media of given type\n */\nexport const mediaOf =\n <T extends MessageMediaType>(type: T): UpdateFilter<Message, { media: Extract<Message['media'], { type: T }> }> =>\n (msg) =>\n msg.media?.type === type\n\n/** Filter messages containing a photo */\nexport const photo = mediaOf('photo')\n/** Filter messages containing a dice */\nexport const dice = mediaOf('dice')\n/** Filter messages containing a contact */\nexport const contact = mediaOf('contact')\n/** Filter messages containing an audio file */\nexport const audio = mediaOf('audio')\n/** Filter messages containing a voice message (audio-only) */\nexport const voice = mediaOf('voice')\n/** Filter messages containing a sticker */\nexport const sticker = mediaOf('sticker')\n/** Filter messages containing a document (a file) */\nexport const document = mediaOf('document')\n/** Filter messages containing any video (videos, round messages and animations) */\nexport const anyVideo = mediaOf('video')\n/** Filter messages containing a static location */\nexport const location = mediaOf('location')\n/** Filter messages containing a live location */\nexport const liveLocation = mediaOf('live_location')\n/** Filter messages containing a game */\nexport const game = mediaOf('game')\n/** Filter messages containing a web page */\nexport const webpage = mediaOf('webpage')\n/** Filter messages containing a venue */\nexport const venue = mediaOf('venue')\n/** Filter messages containing a poll */\nexport const poll = mediaOf('poll')\n/** Filter messages containing an invoice */\nexport const invoice = mediaOf('invoice')\n\n/**\n * Filter messages containing any location (live or static).\n */\nexport const anyLocation: UpdateFilter<Message, { media: Location }> = (msg) => msg.media instanceof RawLocation\n\n/**\n * Filter messages containing a document\n *\n * This will also match media like audio, video, voice\n * that also use Documents\n */\nexport const anyDocument: UpdateFilter<Message, { media: RawDocument }> = (msg) => msg.media instanceof RawDocument\n\n/**\n * Filter messages containing a simple video.\n *\n * This does not include round messages and animations\n */\nexport const video: UpdateFilter<\n Message,\n {\n media: Modify<\n Video,\n {\n isRound: false\n isAnimation: false\n }\n >\n }\n> = (msg) => msg.media?.type === 'video' && !msg.media.isAnimation && !msg.media.isRound\n\n/**\n * Filter messages containing an animation.\n *\n * > **Note**: Legacy GIFs (i.e. documents with `image/gif` MIME)\n * > are also considered animations.\n */\nexport const animation: UpdateFilter<\n Message,\n {\n media: Modify<\n Video,\n {\n isRound: false\n isAnimation: true\n }\n >\n }\n> = (msg) => msg.media?.type === 'video' && msg.media.isAnimation && !msg.media.isRound\n\n/**\n * Filter messages containing a round message (aka video note).\n */\nexport const roundMessage: UpdateFilter<\n Message,\n {\n media: Modify<\n Video,\n {\n isRound: true\n isAnimation: false\n }\n >\n }\n> = (msg) => msg.media?.type === 'video' && !msg.media.isAnimation && msg.media.isRound\n\n/**\n * Filter messages containing a sticker by its type\n */\nexport const stickerByType =\n (type: StickerType): UpdateFilter<Message, { media: Sticker }> =>\n (msg) =>\n msg.media?.type === 'sticker' && msg.media.stickerType === type\n\n/**\n * Filter messages containing a sticker by its source file type\n */\nexport const stickerBySourceType =\n (type: StickerSourceType): UpdateFilter<Message, { media: Sticker }> =>\n (msg) =>\n msg.media?.type === 'sticker' && msg.media.sourceType === type\n\n/**\n * Filter text-only messages non-service messages\n */\nexport const text: UpdateFilter<\n Message,\n {\n media: null\n isService: false\n }\n> = (msg) => msg.media === null && !msg.isService\n\n/**\n * Filter service messages\n */\nexport const service: UpdateFilter<Message, { isService: true }> = (msg) => msg.isService\n\n/**\n * Filter service messages by action type\n */\nexport const action = <T extends Exclude<MessageAction, null>['type']>(\n type: MaybeArray<T>,\n): UpdateFilter<\n Message,\n {\n action: Extract<MessageAction, { type: T }>\n sender: T extends 'user_joined_link' | 'user_removed' | 'history_cleared' | 'contact_joined' | 'bot_allowed'\n ? User\n : Peer\n }\n> => {\n if (Array.isArray(type)) {\n const index: Partial<Record<T, true>> = {}\n type.forEach((it) => (index[it] = true))\n\n return (msg) => (msg.action?.type as any) in index\n }\n\n return (msg) => msg.action?.type === type\n}\n\nexport const sender =\n <T extends Message['sender']['type']>(\n type: T,\n ): UpdateFilter<Message, { sender: Extract<Message['sender'], { type: T }> }> =>\n (msg) =>\n msg.sender.type === type\n\n/**\n * Filter that matches messages that are replies to some other message that can be fetched\n * (i.e. not `private` origin, and has not been deleted)\n *\n * Optionally, you can pass a filter that will be applied to the replied message.\n */\nexport const replyTo =\n <Mod, State extends object>(\n filter?: UpdateFilter<Message, Mod, State>,\n ): UpdateFilter<MessageContext | BusinessMessageContext, { getReplyTo: () => Promise<Message & Mod> }, State> =>\n async (msg, state) => {\n if (!msg.replyToMessage?.id) return false\n\n const reply = msg._name === 'new_message' ? await msg.getReplyTo() : msg.replyTo\n if (!reply) return false\n\n if (msg._name === 'new_message') {\n msg.getReplyTo = () => Promise.resolve(reply)\n }\n\n if (!filter) return true\n\n return filter(reply, state)\n }\n\n/**\n * Middleware-like filter that will fetch the sender of the message\n * and make it available to further filters, as well as the handler itself.\n */\nexport const withCompleteSender =\n <Mod, State extends object>(\n filter?: UpdateFilter<MessageContext, Mod, State>,\n ): UpdateFilter<MessageContext, Mod, State> =>\n async (msg, state) => {\n try {\n await msg.getCompleteSender()\n } catch (e) {\n return false\n }\n\n if (!filter) return true\n\n return filter(msg, state)\n }\n"]}
|
package/esm/filters/state.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { MaybePromise } from '@mtcute/core';
|
|
2
|
-
import { UpdateFilter } from './types.js';
|
|
3
|
-
/**
|
|
4
|
-
* Create a filter for the cases when the state is empty
|
|
5
|
-
*/
|
|
6
|
-
export declare const stateEmpty: UpdateFilter<any>;
|
|
7
|
-
/**
|
|
8
|
-
* Create a filter based on state predicate
|
|
9
|
-
*
|
|
10
|
-
* If state exists and matches `predicate`, update passes
|
|
11
|
-
* this filter, otherwise it doesn't
|
|
12
|
-
*
|
|
13
|
-
* @param predicate State predicate
|
|
14
|
-
*/
|
|
15
|
-
export declare const state: <T extends object>(predicate: (state: T) => MaybePromise<boolean>) => UpdateFilter<any, {}, T>;
|
package/esm/filters/state.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Create a filter for the cases when the state is empty
|
|
3
|
-
*/
|
|
4
|
-
export const stateEmpty = async (upd, state) => {
|
|
5
|
-
if (!state)
|
|
6
|
-
return false;
|
|
7
|
-
return !(await state.get());
|
|
8
|
-
};
|
|
9
|
-
/**
|
|
10
|
-
* Create a filter based on state predicate
|
|
11
|
-
*
|
|
12
|
-
* If state exists and matches `predicate`, update passes
|
|
13
|
-
* this filter, otherwise it doesn't
|
|
14
|
-
*
|
|
15
|
-
* @param predicate State predicate
|
|
16
|
-
*/
|
|
17
|
-
export const state = (predicate) => {
|
|
18
|
-
return async (upd, state) => {
|
|
19
|
-
if (!state)
|
|
20
|
-
return false;
|
|
21
|
-
const data = await state.get();
|
|
22
|
-
if (!data)
|
|
23
|
-
return false;
|
|
24
|
-
return predicate(data);
|
|
25
|
-
};
|
|
26
|
-
};
|
|
27
|
-
//# sourceMappingURL=state.js.map
|
package/esm/filters/state.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"state.js","sourceRoot":"","sources":["../../../src/filters/state.ts"],"names":[],"mappings":"AAKA;;GAEG;AACH,MAAM,CAAC,MAAM,UAAU,GAAsB,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,EAAE;IAC9D,IAAI,CAAC,KAAK;QAAE,OAAO,KAAK,CAAA;IAExB,OAAO,CAAC,CAAC,MAAM,KAAK,CAAC,GAAG,EAAE,CAAC,CAAA;AAC/B,CAAC,CAAA;AAED;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,KAAK,GAAG,CACjB,SAA8C,EAEtB,EAAE;IAC1B,OAAO,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,EAAE;QACxB,IAAI,CAAC,KAAK;YAAE,OAAO,KAAK,CAAA;QACxB,MAAM,IAAI,GAAG,MAAM,KAAK,CAAC,GAAG,EAAE,CAAA;QAC9B,IAAI,CAAC,IAAI;YAAE,OAAO,KAAK,CAAA;QAEvB,OAAO,SAAS,CAAC,IAAI,CAAC,CAAA;IAC1B,CAAC,CAAA;AACL,CAAC,CAAA","sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nimport { MaybePromise } from '@mtcute/core'\n\nimport { UpdateFilter } from './types.js'\n\n/**\n * Create a filter for the cases when the state is empty\n */\nexport const stateEmpty: UpdateFilter<any> = async (upd, state) => {\n if (!state) return false\n\n return !(await state.get())\n}\n\n/**\n * Create a filter based on state predicate\n *\n * If state exists and matches `predicate`, update passes\n * this filter, otherwise it doesn't\n *\n * @param predicate State predicate\n */\nexport const state = <T extends object>(\n predicate: (state: T) => MaybePromise<boolean>,\n // eslint-disable-next-line @typescript-eslint/ban-types\n): UpdateFilter<any, {}, T> => {\n return async (upd, state) => {\n if (!state) return false\n const data = await state.get()\n if (!data) return false\n\n return predicate(data)\n }\n}\n"]}
|
package/esm/filters/text.js
DELETED
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
function extractText(obj) {
|
|
2
|
-
switch (obj._name) {
|
|
3
|
-
case 'new_message':
|
|
4
|
-
case 'new_business_message':
|
|
5
|
-
return obj.text;
|
|
6
|
-
case 'inline_query':
|
|
7
|
-
return obj.query;
|
|
8
|
-
case 'chosen_inline_result':
|
|
9
|
-
return obj.id;
|
|
10
|
-
case 'callback_query':
|
|
11
|
-
case 'inline_callback_query':
|
|
12
|
-
case 'business_callback_query':
|
|
13
|
-
if (obj.raw.data)
|
|
14
|
-
return obj.dataStr;
|
|
15
|
-
}
|
|
16
|
-
return null;
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Filter objects that match a given regular expression
|
|
20
|
-
* - for `Message`, `Message.text` is used
|
|
21
|
-
* - for `InlineQuery`, `InlineQuery.query` is used
|
|
22
|
-
* - for {@link ChosenInlineResult}, {@link ChosenInlineResult#id} is used
|
|
23
|
-
* - for callback queries, `dataStr` is used
|
|
24
|
-
*
|
|
25
|
-
* When a regex matches, the match array is stored in a
|
|
26
|
-
* type-safe extension field `.match` of the object
|
|
27
|
-
*
|
|
28
|
-
* @param regex Regex to be matched
|
|
29
|
-
*/
|
|
30
|
-
export const regex = (regex) => (obj) => {
|
|
31
|
-
const txt = extractText(obj);
|
|
32
|
-
if (!txt)
|
|
33
|
-
return false;
|
|
34
|
-
const m = txt.match(regex);
|
|
35
|
-
if (m) {
|
|
36
|
-
obj.match = m;
|
|
37
|
-
return true;
|
|
38
|
-
}
|
|
39
|
-
return false;
|
|
40
|
-
};
|
|
41
|
-
/**
|
|
42
|
-
* Filter objects which contain the exact text given
|
|
43
|
-
* - for `Message`, `Message.text` is used
|
|
44
|
-
* - for `InlineQuery`, `InlineQuery.query` is used
|
|
45
|
-
* - for {@link ChosenInlineResult}, {@link ChosenInlineResult.id} is used
|
|
46
|
-
* - for callback queries, `dataStr` is used
|
|
47
|
-
*
|
|
48
|
-
* @param str String to be matched
|
|
49
|
-
* @param ignoreCase Whether string case should be ignored
|
|
50
|
-
*/
|
|
51
|
-
export const equals = (str, ignoreCase = false) => {
|
|
52
|
-
if (ignoreCase) {
|
|
53
|
-
str = str.toLowerCase();
|
|
54
|
-
return (obj) => extractText(obj)?.toLowerCase() === str;
|
|
55
|
-
}
|
|
56
|
-
return (obj) => extractText(obj) === str;
|
|
57
|
-
};
|
|
58
|
-
/**
|
|
59
|
-
* Filter objects which contain the text given (as a substring)
|
|
60
|
-
* - for `Message`, `Message.text` is used
|
|
61
|
-
* - for `InlineQuery`, `InlineQuery.query` is used
|
|
62
|
-
* - for {@link ChosenInlineResult}, {@link ChosenInlineResult.id} is used
|
|
63
|
-
* - for callback queries, `dataStr` is used
|
|
64
|
-
*
|
|
65
|
-
* @param str Substring to be matched
|
|
66
|
-
* @param ignoreCase Whether string case should be ignored
|
|
67
|
-
*/
|
|
68
|
-
export const contains = (str, ignoreCase = false) => {
|
|
69
|
-
if (ignoreCase) {
|
|
70
|
-
str = str.toLowerCase();
|
|
71
|
-
return (obj) => {
|
|
72
|
-
const txt = extractText(obj);
|
|
73
|
-
return txt != null && txt.toLowerCase().includes(str);
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
return (obj) => {
|
|
77
|
-
const txt = extractText(obj);
|
|
78
|
-
return txt != null && txt.includes(str);
|
|
79
|
-
};
|
|
80
|
-
};
|
|
81
|
-
/**
|
|
82
|
-
* Filter objects which contain the text starting with a given string
|
|
83
|
-
* - for `Message`, `Message.text` is used
|
|
84
|
-
* - for `InlineQuery`, `InlineQuery.query` is used
|
|
85
|
-
* - for {@link ChosenInlineResult}, {@link ChosenInlineResult.id} is used
|
|
86
|
-
* - for callback queries, `dataStr` is used
|
|
87
|
-
*
|
|
88
|
-
* @param str Substring to be matched
|
|
89
|
-
* @param ignoreCase Whether string case should be ignored
|
|
90
|
-
*/
|
|
91
|
-
export const startsWith = (str, ignoreCase = false) => {
|
|
92
|
-
if (ignoreCase) {
|
|
93
|
-
str = str.toLowerCase();
|
|
94
|
-
return (obj) => {
|
|
95
|
-
const txt = extractText(obj);
|
|
96
|
-
return txt != null && txt.toLowerCase().substring(0, str.length) === str;
|
|
97
|
-
};
|
|
98
|
-
}
|
|
99
|
-
return (obj) => {
|
|
100
|
-
const txt = extractText(obj);
|
|
101
|
-
return txt != null && txt.substring(0, str.length) === str;
|
|
102
|
-
};
|
|
103
|
-
};
|
|
104
|
-
/**
|
|
105
|
-
* Filter objects which contain the text ending with a given string
|
|
106
|
-
* - for `Message`, `Message.text` is used
|
|
107
|
-
* - for `InlineQuery`, `InlineQuery.query` is used
|
|
108
|
-
* - for {@link ChosenInlineResult}, {@link ChosenInlineResult.id} is used
|
|
109
|
-
* - for callback queries, `dataStr` is used
|
|
110
|
-
*
|
|
111
|
-
* @param str Substring to be matched
|
|
112
|
-
* @param ignoreCase Whether string case should be ignored
|
|
113
|
-
*/
|
|
114
|
-
export const endsWith = (str, ignoreCase = false) => {
|
|
115
|
-
if (ignoreCase) {
|
|
116
|
-
str = str.toLowerCase();
|
|
117
|
-
return (obj) => {
|
|
118
|
-
const txt = extractText(obj);
|
|
119
|
-
return txt != null && txt.toLowerCase().substring(0, str.length) === str;
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
return (obj) => {
|
|
123
|
-
const txt = extractText(obj);
|
|
124
|
-
return txt != null && txt.substring(0, str.length) === str;
|
|
125
|
-
};
|
|
126
|
-
};
|
|
127
|
-
//# sourceMappingURL=text.js.map
|
package/esm/filters/text.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"text.js","sourceRoot":"","sources":["../../../src/filters/text.ts"],"names":[],"mappings":"AAuBA,SAAS,WAAW,CAAC,GAAoB;IACrC,QAAQ,GAAG,CAAC,KAAK,EAAE,CAAC;QAChB,KAAK,aAAa,CAAC;QACnB,KAAK,sBAAsB;YACvB,OAAO,GAAG,CAAC,IAAI,CAAA;QACnB,KAAK,cAAc;YACf,OAAO,GAAG,CAAC,KAAK,CAAA;QACpB,KAAK,sBAAsB;YACvB,OAAO,GAAG,CAAC,EAAE,CAAA;QACjB,KAAK,gBAAgB,CAAC;QACtB,KAAK,uBAAuB,CAAC;QAC7B,KAAK,yBAAyB;YAC1B,IAAI,GAAG,CAAC,GAAG,CAAC,IAAI;gBAAE,OAAO,GAAG,CAAC,OAAO,CAAA;IAC5C,CAAC;IAED,OAAO,IAAI,CAAA;AACf,CAAC;AAED;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,MAAM,KAAK,GACd,CAAC,KAAa,EAA8D,EAAE,CAC1E,CAAC,GAAG,EAAE,EAAE;IACJ,MAAM,GAAG,GAAG,WAAW,CAAC,GAAG,CAAC,CAAA;IAC5B,IAAI,CAAC,GAAG;QAAE,OAAO,KAAK,CAAA;IAEtB,MAAM,CAAC,GAAG,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,CAAA;IAE1B,IAAI,CAAC,EAAE,CAAC;QACH,GAAgD,CAAC,KAAK,GAAG,CAAC,CAAA;QAE3D,OAAO,IAAI,CAAA;IACf,CAAC;IAED,OAAO,KAAK,CAAA;AAChB,CAAC,CAAA;AAET;;;;;;;;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AAW,CAAC,GAAG,CAAC,CAAA;YAE5B,OAAO,GAAG,IAAI,IAAI,IAAI,GAAG,CAAC,WAAW,EAAE,CAAC,SAAS,CAAC,CAAC,EAAE,GAAG,CAAC,MAAM,CAAC,KAAK,GAAG,CAAA;QAC5E,CAAC,CAAA;IACL,CAAC;IAED,OAAO,CAAC,GAAG,EAAE,EAAE;QACX,MAAM,GAAG,GAAG,WAAW,CAAC,GAAG,CAAC,CAAA;QAE5B,OAAO,GAAG,IAAI,IAAI,IAAI,GAAG,CAAC,SAAS,CAAC,CAAC,EAAE,GAAG,CAAC,MAAM,CAAC,KAAK,GAAG,CAAA;IAC9D,CAAC,CAAA;AACL,CAAC,CAAA;AAED;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAG,CAAC,GAAW,EAAE,UAAU,GAAG,KAAK,EAAiC,EAAE;IACvF,IAAI,UAAU,EAAE,CAAC;QACb,GAAG,GAAG,GAAG,CAAC,WAAW,EAAE,CAAA;QAEvB,OAAO,CAAC,GAAG,EAAE,EAAE;YACX,MAAM,GAAG,GAAG,WAAW,CAAC,GAAG,CAAC,CAAA;YAE5B,OAAO,GAAG,IAAI,IAAI,IAAI,GAAG,CAAC,WAAW,EAAE,CAAC,SAAS,CAAC,CAAC,EAAE,GAAG,CAAC,MAAM,CAAC,KAAK,GAAG,CAAA;QAC5E,CAAC,CAAA;IACL,CAAC;IAED,OAAO,CAAC,GAAG,EAAE,EAAE;QACX,MAAM,GAAG,GAAG,WAAW,CAAC,GAAG,CAAC,CAAA;QAE5B,OAAO,GAAG,IAAI,IAAI,IAAI,GAAG,CAAC,SAAS,CAAC,CAAC,EAAE,GAAG,CAAC,MAAM,CAAC,KAAK,GAAG,CAAA;IAC9D,CAAC,CAAA;AACL,CAAC,CAAA","sourcesContent":["import {\n BusinessCallbackQuery,\n BusinessMessage,\n CallbackQuery,\n ChosenInlineResult,\n InlineCallbackQuery,\n InlineQuery,\n Message,\n} from '@mtcute/core'\n\nimport { UpdateContextDistributed } from '../context/base.js'\nimport { UpdateFilter } from './types.js'\n\ntype UpdatesWithText = UpdateContextDistributed<\n | Message\n | BusinessMessage\n | InlineQuery\n | ChosenInlineResult\n | CallbackQuery\n | InlineCallbackQuery\n | BusinessCallbackQuery\n>\n\nfunction extractText(obj: UpdatesWithText): string | null {\n switch (obj._name) {\n case 'new_message':\n case 'new_business_message':\n return obj.text\n case 'inline_query':\n return obj.query\n case 'chosen_inline_result':\n return obj.id\n case 'callback_query':\n case 'inline_callback_query':\n case 'business_callback_query':\n if (obj.raw.data) return obj.dataStr\n }\n\n return null\n}\n\n/**\n * Filter objects that match a given regular expression\n * - for `Message`, `Message.text` is used\n * - for `InlineQuery`, `InlineQuery.query` is used\n * - for {@link ChosenInlineResult}, {@link ChosenInlineResult#id} is used\n * - for callback queries, `dataStr` is used\n *\n * When a regex matches, the match array is stored in a\n * type-safe extension field `.match` of the object\n *\n * @param regex Regex to be matched\n */\nexport const regex =\n (regex: RegExp): UpdateFilter<UpdatesWithText, { match: RegExpMatchArray }> =>\n (obj) => {\n const txt = extractText(obj)\n if (!txt) return false\n\n const m = txt.match(regex)\n\n if (m) {\n (obj as typeof obj & { match: RegExpMatchArray }).match = m\n\n return true\n }\n\n return false\n }\n\n/**\n * Filter objects which contain the exact text given\n * - for `Message`, `Message.text` is used\n * - for `InlineQuery`, `InlineQuery.query` is used\n * - for {@link ChosenInlineResult}, {@link ChosenInlineResult.id} is used\n * - for callback queries, `dataStr` is used\n *\n * @param str String to be matched\n * @param ignoreCase Whether string case should be ignored\n */\nexport const equals = (str: string, ignoreCase = false): UpdateFilter<UpdatesWithText> => {\n if (ignoreCase) {\n str = str.toLowerCase()\n\n return (obj) => extractText(obj)?.toLowerCase() === str\n }\n\n return (obj) => extractText(obj) === str\n}\n\n/**\n * Filter objects which contain the text given (as a substring)\n * - for `Message`, `Message.text` is used\n * - for `InlineQuery`, `InlineQuery.query` is used\n * - for {@link ChosenInlineResult}, {@link ChosenInlineResult.id} is used\n * - for callback queries, `dataStr` is used\n *\n * @param str Substring to be matched\n * @param ignoreCase Whether string case should be ignored\n */\nexport const contains = (str: string, ignoreCase = false): UpdateFilter<UpdatesWithText> => {\n if (ignoreCase) {\n str = str.toLowerCase()\n\n return (obj) => {\n const txt = extractText(obj)\n\n return txt != null && txt.toLowerCase().includes(str)\n }\n }\n\n return (obj) => {\n const txt = extractText(obj)\n\n return txt != null && txt.includes(str)\n }\n}\n\n/**\n * Filter objects which contain the text starting with a given string\n * - for `Message`, `Message.text` is used\n * - for `InlineQuery`, `InlineQuery.query` is used\n * - for {@link ChosenInlineResult}, {@link ChosenInlineResult.id} is used\n * - for callback queries, `dataStr` is used\n *\n * @param str Substring to be matched\n * @param ignoreCase Whether string case should be ignored\n */\nexport const startsWith = (str: string, ignoreCase = false): UpdateFilter<UpdatesWithText> => {\n if (ignoreCase) {\n str = str.toLowerCase()\n\n return (obj) => {\n const txt = extractText(obj)\n\n return txt != null && txt.toLowerCase().substring(0, str.length) === str\n }\n }\n\n return (obj) => {\n const txt = extractText(obj)\n\n return txt != null && txt.substring(0, str.length) === str\n }\n}\n\n/**\n * Filter objects which contain the text ending with a given string\n * - for `Message`, `Message.text` is used\n * - for `InlineQuery`, `InlineQuery.query` is used\n * - for {@link ChosenInlineResult}, {@link ChosenInlineResult.id} is used\n * - for callback queries, `dataStr` is used\n *\n * @param str Substring to be matched\n * @param ignoreCase Whether string case should be ignored\n */\nexport const endsWith = (str: string, ignoreCase = false): UpdateFilter<UpdatesWithText> => {\n if (ignoreCase) {\n str = str.toLowerCase()\n\n return (obj) => {\n const txt = extractText(obj)\n\n return txt != null && txt.toLowerCase().substring(0, str.length) === str\n }\n }\n\n return (obj) => {\n const txt = extractText(obj)\n\n return txt != null && txt.substring(0, str.length) === str\n }\n}\n"]}
|
package/esm/filters/types.d.ts
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import { MaybePromise } from '@mtcute/core';
|
|
2
|
-
import { UpdateState } from '../state/update-state.js';
|
|
3
|
-
/**
|
|
4
|
-
* Type describing a primitive filter, which is a function taking some `Base`
|
|
5
|
-
* and a {@link TelegramClient}, checking it against some condition
|
|
6
|
-
* and returning a boolean.
|
|
7
|
-
*
|
|
8
|
-
* If `true` is returned, the filter is considered
|
|
9
|
-
* to be matched, and the appropriate update handler function is called,
|
|
10
|
-
* otherwise next registered handler is checked.
|
|
11
|
-
*
|
|
12
|
-
* Additionally, filter might contain a type modification
|
|
13
|
-
* to `Base` for better code insights. If it is present,
|
|
14
|
-
* it is used to overwrite types (!) of some of the `Base` fields
|
|
15
|
-
* to given (note that this is entirely compile-time! object is not modified)
|
|
16
|
-
*
|
|
17
|
-
* For parametrized filters (like {@link filters.regex}),
|
|
18
|
-
* type modification can also be used to add additional fields
|
|
19
|
-
* (in case of `regex`, its match array is added to `.match`)
|
|
20
|
-
*
|
|
21
|
-
* Example without type mod:
|
|
22
|
-
* ```typescript
|
|
23
|
-
*
|
|
24
|
-
* const hasPhoto: UpdateFilter<Message> = msg => msg.media?.type === 'photo'
|
|
25
|
-
*
|
|
26
|
-
* // ..later..
|
|
27
|
-
* tg.onNewMessage(hasPhoto, async (msg) => {
|
|
28
|
-
* // `hasPhoto` filter matched, so we can safely assume
|
|
29
|
-
* // that `msg.media` is a Photo.
|
|
30
|
-
* //
|
|
31
|
-
* // but it is very redundant, verbose and error-rome,
|
|
32
|
-
* // wonder if we could make typescript do this automagically and safely...
|
|
33
|
-
* await (msg.media as Photo).downloadToFile(`${msg.id}.jpg`)
|
|
34
|
-
* })
|
|
35
|
-
* ```
|
|
36
|
-
*
|
|
37
|
-
* Example with type mod:
|
|
38
|
-
* ```typescript
|
|
39
|
-
*
|
|
40
|
-
* const hasPhoto: UpdateFilter<Message, { media: Photo }> = msg => msg.media?.type === 'photo'
|
|
41
|
-
*
|
|
42
|
-
* // ..later..
|
|
43
|
-
* tg.onNewMessage(hasPhoto, async (msg) => {
|
|
44
|
-
* // since `hasPhoto` filter matched,
|
|
45
|
-
* // we have applied the modification to `msg`,
|
|
46
|
-
* // and `msg.media` now has type `Photo`
|
|
47
|
-
* //
|
|
48
|
-
* // no more redundancy and type casts!
|
|
49
|
-
* await msg.media.downloadToFile(`${msg.id}.jpg`)
|
|
50
|
-
* })
|
|
51
|
-
* ```
|
|
52
|
-
*
|
|
53
|
-
* > **Note**: Type modification can contain anything, even totally unrelated types
|
|
54
|
-
* > and it is *your* task to keep track that everything is correct.
|
|
55
|
-
* >
|
|
56
|
-
* > Bad example:
|
|
57
|
-
* > ```typescript
|
|
58
|
-
* > // we check for `Photo`, but type contains `Audio`. this will be a problem!
|
|
59
|
-
* > const hasPhoto: UpdateFilter<Message, { media: Audio }> = msg => msg.media?.type === 'photo'
|
|
60
|
-
* >
|
|
61
|
-
* > // ..later..
|
|
62
|
-
* > tg.onNewMessage(hasPhoto, async (msg) => {
|
|
63
|
-
* > // oops! `msg.media` is `Audio` and does not have `.width`!
|
|
64
|
-
* > console.log(msg.media.width)
|
|
65
|
-
* > })
|
|
66
|
-
* > ```
|
|
67
|
-
*
|
|
68
|
-
* > **Warning!** Do not use the generics provided in functions
|
|
69
|
-
* > like `and`, `or`, etc. Those are meant to be inferred by the compiler!
|
|
70
|
-
*/
|
|
71
|
-
export type UpdateFilter<Base, Mod = {}, State extends object = never> = (update: Base, state?: UpdateState<State>) => MaybePromise<boolean>;
|
|
72
|
-
export type Modify<Base, Mod> = Omit<Base, keyof Mod> & Mod;
|
|
73
|
-
export type Invert<Base, Mod> = {
|
|
74
|
-
[P in keyof Mod & keyof Base]: Exclude<Base[P], Mod[P]>;
|
|
75
|
-
};
|
|
76
|
-
export type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
|
|
77
|
-
export type ExtractBase<Filter> = Filter extends UpdateFilter<infer I, any> ? I : never;
|
|
78
|
-
export type ExtractMod<Filter> = Filter extends UpdateFilter<any, infer I> ? I : never;
|
|
79
|
-
export type ExtractState<Filter> = Filter extends UpdateFilter<any, any, infer I> ? I : never;
|
|
80
|
-
export type TupleKeys<T extends any[]> = Exclude<keyof T, keyof []>;
|
|
81
|
-
export type WrapBase<T extends any[]> = {
|
|
82
|
-
[K in TupleKeys<T>]: {
|
|
83
|
-
base: ExtractBase<T[K]>;
|
|
84
|
-
};
|
|
85
|
-
};
|
|
86
|
-
export type Values<T> = T[keyof T];
|
|
87
|
-
export type UnwrapBase<T> = T extends {
|
|
88
|
-
base: any;
|
|
89
|
-
} ? T['base'] : never;
|
|
90
|
-
export type ExtractBaseMany<Filters extends any[]> = UnwrapBase<UnionToIntersection<Values<WrapBase<Filters>>>>;
|
|
91
|
-
export type EmptyObject = Record<never, never>;
|
package/esm/filters/types.js
DELETED
package/esm/filters/types.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/filters/types.ts"],"names":[],"mappings":"AAAA,iDAAiD;AACjD,uDAAuD;AACvD,mCAAmC","sourcesContent":["/* eslint-disable @typescript-eslint/ban-types */\n/* eslint-disable @typescript-eslint/no-explicit-any */\n// ^^ will be looked into in MTQ-29\n\nimport { MaybePromise } from '@mtcute/core'\n\nimport { UpdateState } from '../state/update-state.js'\n/**\n * Type describing a primitive filter, which is a function taking some `Base`\n * and a {@link TelegramClient}, checking it against some condition\n * and returning a boolean.\n *\n * If `true` is returned, the filter is considered\n * to be matched, and the appropriate update handler function is called,\n * otherwise next registered handler is checked.\n *\n * Additionally, filter might contain a type modification\n * to `Base` for better code insights. If it is present,\n * it is used to overwrite types (!) of some of the `Base` fields\n * to given (note that this is entirely compile-time! object is not modified)\n *\n * For parametrized filters (like {@link filters.regex}),\n * type modification can also be used to add additional fields\n * (in case of `regex`, its match array is added to `.match`)\n *\n * Example without type mod:\n * ```typescript\n *\n * const hasPhoto: UpdateFilter<Message> = msg => msg.media?.type === 'photo'\n *\n * // ..later..\n * tg.onNewMessage(hasPhoto, async (msg) => {\n * // `hasPhoto` filter matched, so we can safely assume\n * // that `msg.media` is a Photo.\n * //\n * // but it is very redundant, verbose and error-rome,\n * // wonder if we could make typescript do this automagically and safely...\n * await (msg.media as Photo).downloadToFile(`${msg.id}.jpg`)\n * })\n * ```\n *\n * Example with type mod:\n * ```typescript\n *\n * const hasPhoto: UpdateFilter<Message, { media: Photo }> = msg => msg.media?.type === 'photo'\n *\n * // ..later..\n * tg.onNewMessage(hasPhoto, async (msg) => {\n * // since `hasPhoto` filter matched,\n * // we have applied the modification to `msg`,\n * // and `msg.media` now has type `Photo`\n * //\n * // no more redundancy and type casts!\n * await msg.media.downloadToFile(`${msg.id}.jpg`)\n * })\n * ```\n *\n * > **Note**: Type modification can contain anything, even totally unrelated types\n * > and it is *your* task to keep track that everything is correct.\n * >\n * > Bad example:\n * > ```typescript\n * > // we check for `Photo`, but type contains `Audio`. this will be a problem!\n * > const hasPhoto: UpdateFilter<Message, { media: Audio }> = msg => msg.media?.type === 'photo'\n * >\n * > // ..later..\n * > tg.onNewMessage(hasPhoto, async (msg) => {\n * > // oops! `msg.media` is `Audio` and does not have `.width`!\n * > console.log(msg.media.width)\n * > })\n * > ```\n *\n * > **Warning!** Do not use the generics provided in functions\n * > like `and`, `or`, etc. Those are meant to be inferred by the compiler!\n */\n// we need the second parameter because it carries meta information\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\nexport type UpdateFilter<Base, Mod = {}, State extends object = never> = (\n update: Base,\n state?: UpdateState<State>,\n) => MaybePromise<boolean>\n\nexport type Modify<Base, Mod> = Omit<Base, keyof Mod> & Mod\nexport type Invert<Base, Mod> = {\n [P in keyof Mod & keyof Base]: Exclude<Base[P], Mod[P]>\n}\n\nexport type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never\n\nexport type ExtractBase<Filter> = Filter extends UpdateFilter<infer I, any> ? I : never\n\nexport type ExtractMod<Filter> = Filter extends UpdateFilter<any, infer I> ? I : never\n\nexport type ExtractState<Filter> = Filter extends UpdateFilter<any, any, infer I> ? I : never\n\nexport type TupleKeys<T extends any[]> = Exclude<keyof T, keyof []>\nexport type WrapBase<T extends any[]> = {\n [K in TupleKeys<T>]: { base: ExtractBase<T[K]> }\n}\nexport type Values<T> = T[keyof T]\nexport type UnwrapBase<T> = T extends { base: any } ? T['base'] : never\nexport type ExtractBaseMany<Filters extends any[]> = UnwrapBase<UnionToIntersection<Values<WrapBase<Filters>>>>\n\nexport type EmptyObject = Record<never, never>\n"]}
|
package/esm/filters/updates.d.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { ChatMemberUpdate, ChatMemberUpdateType, UserStatus, UserStatusUpdate } from '@mtcute/core';
|
|
2
|
-
import { UpdateFilter } from './types.js';
|
|
3
|
-
/**
|
|
4
|
-
* Create a filter for {@link ChatMemberUpdate} by update type
|
|
5
|
-
*
|
|
6
|
-
* @param types Update type(s)
|
|
7
|
-
* @link ChatMemberUpdate.Type
|
|
8
|
-
*/
|
|
9
|
-
export declare const chatMember: {
|
|
10
|
-
<T extends ChatMemberUpdateType>(type: T): UpdateFilter<ChatMemberUpdate, {
|
|
11
|
-
type: T;
|
|
12
|
-
}>;
|
|
13
|
-
<T extends ChatMemberUpdateType[]>(types: T): UpdateFilter<ChatMemberUpdate, {
|
|
14
|
-
type: T[number];
|
|
15
|
-
}>;
|
|
16
|
-
};
|
|
17
|
-
/**
|
|
18
|
-
* Create a filter for {@link UserStatusUpdate} by new user status
|
|
19
|
-
*
|
|
20
|
-
* @param statuses Update type(s)
|
|
21
|
-
* @link User.Status
|
|
22
|
-
*/
|
|
23
|
-
export declare const userStatus: {
|
|
24
|
-
<T extends UserStatus>(status: T): UpdateFilter<UserStatusUpdate, {
|
|
25
|
-
type: T;
|
|
26
|
-
lastOnline: T extends 'offline' ? Date : null;
|
|
27
|
-
nextOffline: T extends 'online' ? Date : null;
|
|
28
|
-
}>;
|
|
29
|
-
<T extends UserStatus[]>(statuses: T): UpdateFilter<UserStatusUpdate, {
|
|
30
|
-
type: T[number];
|
|
31
|
-
}>;
|
|
32
|
-
};
|
|
33
|
-
/**
|
|
34
|
-
* Create a filter for {@link ChatMemberUpdate} for updates
|
|
35
|
-
* regarding current user
|
|
36
|
-
*/
|
|
37
|
-
export declare const chatMemberSelf: UpdateFilter<ChatMemberUpdate, {
|
|
38
|
-
isSelf: true;
|
|
39
|
-
}>;
|
package/esm/filters/updates.js
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Create a filter for {@link ChatMemberUpdate} by update type
|
|
3
|
-
*
|
|
4
|
-
* @param types Update type(s)
|
|
5
|
-
* @link ChatMemberUpdate.Type
|
|
6
|
-
*/
|
|
7
|
-
export const chatMember = (types) => {
|
|
8
|
-
if (Array.isArray(types)) {
|
|
9
|
-
const index = {};
|
|
10
|
-
types.forEach((typ) => (index[typ] = true));
|
|
11
|
-
return (upd) => upd.type in index;
|
|
12
|
-
}
|
|
13
|
-
return (upd) => upd.type === types;
|
|
14
|
-
};
|
|
15
|
-
/**
|
|
16
|
-
* Create a filter for {@link UserStatusUpdate} by new user status
|
|
17
|
-
*
|
|
18
|
-
* @param statuses Update type(s)
|
|
19
|
-
* @link User.Status
|
|
20
|
-
*/
|
|
21
|
-
export const userStatus = (statuses) => {
|
|
22
|
-
if (Array.isArray(statuses)) {
|
|
23
|
-
const index = {};
|
|
24
|
-
statuses.forEach((typ) => (index[typ] = true));
|
|
25
|
-
return (upd) => upd.status in index;
|
|
26
|
-
}
|
|
27
|
-
return (upd) => upd.status === statuses;
|
|
28
|
-
};
|
|
29
|
-
/**
|
|
30
|
-
* Create a filter for {@link ChatMemberUpdate} for updates
|
|
31
|
-
* regarding current user
|
|
32
|
-
*/
|
|
33
|
-
export const chatMemberSelf = (upd) => upd.isSelf;
|
|
34
|
-
//# sourceMappingURL=updates.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"updates.js","sourceRoot":"","sources":["../../../src/filters/updates.ts"],"names":[],"mappings":"AAIA;;;;;GAKG;AACH,MAAM,CAAC,MAAM,UAAU,GAGnB,CAAC,KAAuC,EAAkC,EAAE;IAC5E,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;QACvB,MAAM,KAAK,GAAgD,EAAE,CAAA;QAC7D,KAAK,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,CAAC,CAAA;QAE3C,OAAO,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,IAAI,IAAI,KAAK,CAAA;IACrC,CAAC;IAED,OAAO,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,IAAI,KAAK,KAAK,CAAA;AACtC,CAAC,CAAA;AAED;;;;;GAKG;AACH,MAAM,CAAC,MAAM,UAAU,GAYnB,CAAC,QAAgC,EAAkC,EAAE;IACrE,IAAI,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC;QAC1B,MAAM,KAAK,GAAsC,EAAE,CAAA;QACnD,QAAQ,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,CAAC,CAAA;QAE9C,OAAO,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,MAAM,IAAI,KAAK,CAAA;IACvC,CAAC;IAED,OAAO,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,MAAM,KAAK,QAAQ,CAAA;AAC3C,CAAC,CAAA;AAED;;;GAGG;AACH,MAAM,CAAC,MAAM,cAAc,GAAqD,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,MAAM,CAAA","sourcesContent":["import { ChatMemberUpdate, ChatMemberUpdateType, MaybeArray, UserStatus, UserStatusUpdate } from '@mtcute/core'\n\nimport { UpdateFilter } from './types.js'\n\n/**\n * Create a filter for {@link ChatMemberUpdate} by update type\n *\n * @param types Update type(s)\n * @link ChatMemberUpdate.Type\n */\nexport const chatMember: {\n <T extends ChatMemberUpdateType>(type: T): UpdateFilter<ChatMemberUpdate, { type: T }>\n <T extends ChatMemberUpdateType[]>(types: T): UpdateFilter<ChatMemberUpdate, { type: T[number] }>\n} = (types: MaybeArray<ChatMemberUpdateType>): UpdateFilter<ChatMemberUpdate> => {\n if (Array.isArray(types)) {\n const index: Partial<Record<ChatMemberUpdateType, true>> = {}\n types.forEach((typ) => (index[typ] = true))\n\n return (upd) => upd.type in index\n }\n\n return (upd) => upd.type === types\n}\n\n/**\n * Create a filter for {@link UserStatusUpdate} by new user status\n *\n * @param statuses Update type(s)\n * @link User.Status\n */\nexport const userStatus: {\n <T extends UserStatus>(\n status: T,\n ): UpdateFilter<\n UserStatusUpdate,\n {\n type: T\n lastOnline: T extends 'offline' ? Date : null\n nextOffline: T extends 'online' ? Date : null\n }\n >\n <T extends UserStatus[]>(statuses: T): UpdateFilter<UserStatusUpdate, { type: T[number] }>\n} = (statuses: MaybeArray<UserStatus>): UpdateFilter<UserStatusUpdate> => {\n if (Array.isArray(statuses)) {\n const index: Partial<Record<UserStatus, true>> = {}\n statuses.forEach((typ) => (index[typ] = true))\n\n return (upd) => upd.status in index\n }\n\n return (upd) => upd.status === statuses\n}\n\n/**\n * Create a filter for {@link ChatMemberUpdate} for updates\n * regarding current user\n */\nexport const chatMemberSelf: UpdateFilter<ChatMemberUpdate, { isSelf: true }> = (upd) => upd.isSelf\n"]}
|
package/esm/filters/user.js
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import { User, } from '@mtcute/core';
|
|
2
|
-
/**
|
|
3
|
-
* Filter messages generated by yourself (including Saved Messages)
|
|
4
|
-
*/
|
|
5
|
-
export const me = (msg) => (msg.sender.type === 'user' && msg.sender.isSelf) || msg.isOutgoing;
|
|
6
|
-
/**
|
|
7
|
-
* Filter messages sent by bots
|
|
8
|
-
*/
|
|
9
|
-
export const bot = (msg) => msg.sender.constructor === User && msg.sender.isBot;
|
|
10
|
-
// prettier-ignore
|
|
11
|
-
/**
|
|
12
|
-
* Filter updates by user ID(s) or username(s)
|
|
13
|
-
*
|
|
14
|
-
* Note that only some updates support filtering by username.
|
|
15
|
-
*/
|
|
16
|
-
export const userId = (id) => {
|
|
17
|
-
const indexId = new Set();
|
|
18
|
-
const indexUsername = new Set();
|
|
19
|
-
let matchSelf = false;
|
|
20
|
-
if (!Array.isArray(id))
|
|
21
|
-
id = [id];
|
|
22
|
-
id.forEach((id) => {
|
|
23
|
-
if (id === 'me' || id === 'self') {
|
|
24
|
-
matchSelf = true;
|
|
25
|
-
}
|
|
26
|
-
else if (typeof id === 'string') {
|
|
27
|
-
indexUsername.add(id);
|
|
28
|
-
}
|
|
29
|
-
else {
|
|
30
|
-
indexId.add(id);
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
return (upd) => {
|
|
34
|
-
switch (upd._name) {
|
|
35
|
-
case 'new_message':
|
|
36
|
-
case 'edit_message':
|
|
37
|
-
case 'new_business_message':
|
|
38
|
-
case 'edit_business_message': {
|
|
39
|
-
const sender = upd.sender;
|
|
40
|
-
return (matchSelf && sender.isSelf) ||
|
|
41
|
-
indexId.has(sender.id) ||
|
|
42
|
-
indexUsername.has(sender.username);
|
|
43
|
-
}
|
|
44
|
-
case 'user_status':
|
|
45
|
-
case 'user_typing': {
|
|
46
|
-
const id = upd.userId;
|
|
47
|
-
return (matchSelf && id === upd.client.storage.self.getCached()?.userId) ||
|
|
48
|
-
indexId.has(id);
|
|
49
|
-
}
|
|
50
|
-
case 'poll_vote':
|
|
51
|
-
case 'story':
|
|
52
|
-
case 'delete_story': {
|
|
53
|
-
const peer = upd.peer;
|
|
54
|
-
if (peer.type !== 'user')
|
|
55
|
-
return false;
|
|
56
|
-
return (matchSelf && peer.isSelf) ||
|
|
57
|
-
indexId.has(peer.id) ||
|
|
58
|
-
Boolean(peer.usernames?.some((u) => indexUsername.has(u.username)));
|
|
59
|
-
}
|
|
60
|
-
case 'history_read': {
|
|
61
|
-
const id = upd.chatId;
|
|
62
|
-
return (matchSelf && id === upd.client.storage.self.getCached()?.userId) ||
|
|
63
|
-
indexId.has(id);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
const user = upd.user;
|
|
67
|
-
return ((matchSelf && user.isSelf) ||
|
|
68
|
-
indexId.has(user.id) ||
|
|
69
|
-
Boolean(user.usernames?.some((u) => indexUsername.has(u.username))));
|
|
70
|
-
};
|
|
71
|
-
};
|
|
72
|
-
//# sourceMappingURL=user.js.map
|