@imessaging/core 0.2.0 → 0.3.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@imessaging/core",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Core messaging transport contracts and Telegram peer storage",
5
5
  "keywords": [
6
6
  "messaging",
package/src/index.ts CHANGED
@@ -1,10 +1,11 @@
1
1
  export { MemoryPeerStore } from "./memory-peer-store";
2
2
  export { createPeerKey } from "./peer-key";
3
- export { canEdit } from "./types";
3
+ export { canEdit, isTelegramRecipient } from "./types";
4
4
  export type {
5
5
  EditableMessage,
6
6
  EditableMessageTransport,
7
7
  EditResult,
8
+ EmailRecipient,
8
9
  MessageRecipient,
9
10
  MessageTransport,
10
11
  OutboundButton,
@@ -13,5 +14,6 @@ export type {
13
14
  ResolvedPeer,
14
15
  SendResult,
15
16
  TelegramPeerStore,
17
+ TelegramRecipient,
16
18
  TransportStatus,
17
19
  } from "./types";
package/src/peer-key.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { MessageRecipient } from "./types";
1
+ import { isTelegramRecipient, type MessageRecipient } from "./types";
2
2
 
3
3
  function normalizeUsername(username: string): string {
4
4
  return username.trim().replace(/^@/, "").toLowerCase();
@@ -10,6 +10,12 @@ export function createPeerKey(accountId: string, recipient: MessageRecipient): s
10
10
  throw new Error("accountId must not be empty");
11
11
  }
12
12
 
13
+ // Кеш пиров — телеграмный. Адрес почты сюда не кладут: ключ вида `telegram-peer:…:email:…`
14
+ // выглядел бы настоящим и молча жил бы в хранилище, ничего не адресуя.
15
+ if (!isTelegramRecipient(recipient)) {
16
+ throw new Error("peer key is Telegram-only and cannot be built for an email recipient");
17
+ }
18
+
13
19
  if (recipient.type === "username") {
14
20
  const username = normalizeUsername(recipient.username);
15
21
  if (!username) {
package/src/types.ts CHANGED
@@ -9,7 +9,36 @@ export type TelegramIdRecipient = {
9
9
  username?: string;
10
10
  };
11
11
 
12
- export type MessageRecipient = UsernameRecipient | TelegramIdRecipient;
12
+ /**
13
+ * Получатель письма.
14
+ *
15
+ * Адрес — самостоятельный вид получателя, а не telegram-имя с собачкой. Соблазн подставить почту
16
+ * в `UsernameRecipient` велик (поле тоже строковое), но тогда `createPeerKey` завёл бы для письма
17
+ * ключ telegram-пира, а mtproto-транспорт попытался бы разрешить адрес как @username и упал бы с
18
+ * сообщением про Telegram. Отдельный вариант заставляет каждый транспорт сказать вслух, работает
19
+ * он с этим получателем или нет.
20
+ */
21
+ export type EmailRecipient = {
22
+ type: "email";
23
+ address: string;
24
+ /** Отображаемое имя: `Имя <адрес>`. Необязательно — без него уходит голый адрес. */
25
+ name?: string;
26
+ };
27
+
28
+ export type MessageRecipient = UsernameRecipient | TelegramIdRecipient | EmailRecipient;
29
+
30
+ /** Получатель, адресуемый в Telegram. Сужение для транспортов, которые кроме него ничего не умеют. */
31
+ export type TelegramRecipient = UsernameRecipient | TelegramIdRecipient;
32
+
33
+ /**
34
+ * Телеграмный ли это получатель.
35
+ *
36
+ * Нужен транспортам как ЕДИНСТВЕННАЯ точка отказа от чужого канала: без него проверка расползётся
37
+ * по местам использования `recipient.id` и в каждом будет своей.
38
+ */
39
+ export function isTelegramRecipient(recipient: MessageRecipient): recipient is TelegramRecipient {
40
+ return recipient.type !== "email";
41
+ }
13
42
 
14
43
  export type OutboundDocument = {
15
44
  data: Uint8Array;