@imessaging/telegram-mtproto 0.8.6 → 0.8.8

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/telegram-mtproto",
3
- "version": "0.8.6",
3
+ "version": "0.8.8",
4
4
  "description": "Telegram MTProto user-account transport for imessaging",
5
5
  "keywords": [
6
6
  "messaging",
@@ -31,7 +31,7 @@
31
31
  "test": "bun test"
32
32
  },
33
33
  "dependencies": {
34
- "@imessaging/core": "^0.8.6",
34
+ "@imessaging/core": "^0.8.8",
35
35
  "big-integer": "^1.6.52"
36
36
  },
37
37
  "peerDependencies": {
package/src/incoming.ts CHANGED
@@ -337,6 +337,7 @@ export function toIncomingMessage(
337
337
  attachments: collectAttachments(message, chatId),
338
338
  links: collectLinks(message),
339
339
  mentionsMe: typeof message.mentioned === "boolean" ? message.mentioned : undefined,
340
+ outgoing: typeof message.out === "boolean" ? message.out : undefined,
340
341
  replyToMessageId,
341
342
  threadId,
342
343
  forwardedFrom: toForwardedFrom(message.fwdFrom),
@@ -1,4 +1,6 @@
1
1
  import type {
2
+ ContactCard,
3
+ ContactTransport,
2
4
  PhoneLookupOptions,
3
5
  PhoneLookupTransport,
4
6
  PhoneMatch,
@@ -110,6 +112,7 @@ function inputPeerChatId(peer: Api.TypeInputPeer): string {
110
112
 
111
113
  export class TelegramMtprotoTransport
112
114
  implements
115
+ ContactTransport,
113
116
  DraftTransport,
114
117
  EditableMessageTransport,
115
118
  EditedMessageTransport,
@@ -285,6 +288,43 @@ export class TelegramMtprotoTransport
285
288
  }
286
289
  }
287
290
 
291
+ /**
292
+ * Контакт в адресной книге: имя и заметка (contacts.addContact.note — поле слоя Telegram, не фамилия).
293
+ * Свой номер человеку не открываем: addPhonePrivacyException не ставится. Пир — только из хранилища:
294
+ * без него запрос ушёл бы искать человека по сети, а это дело findByPhone и переписки.
295
+ */
296
+ async saveContact(contact: ContactCard): Promise<void> {
297
+ this.assertConnected();
298
+ if (!/^\d+$/.test(contact.userId))
299
+ throw new Error(`Telegram user id must be numeric: ${contact.userId}`);
300
+ if (!contact.firstName.trim()) throw new Error("Contact firstName must not be empty");
301
+ const stored = await this.peerStore.get(this.options.accountId, {
302
+ type: "user",
303
+ id: contact.userId,
304
+ });
305
+ if (!stored?.accessHash) {
306
+ throw new Error(
307
+ `Telegram user ${contact.userId} is unknown to this account: find it with findByPhone or receive a message first`,
308
+ );
309
+ }
310
+ const phone = contact.phone?.replace(/[\s()-]/g, "") ?? "";
311
+ await this.client.invoke(
312
+ new Api.contacts.AddContact({
313
+ id: new Api.InputUser({
314
+ userId: bigInt(contact.userId),
315
+ accessHash: bigInt(stored.accessHash),
316
+ }),
317
+ firstName: contact.firstName.trim(),
318
+ lastName: contact.lastName?.trim() ?? "",
319
+ phone,
320
+ ...(contact.note
321
+ ? { note: new Api.TextWithEntities({ text: contact.note, entities: [] }) }
322
+ : {}),
323
+ }),
324
+ );
325
+ this.lastActivityAt = new Date();
326
+ }
327
+
288
328
  private async rememberPhoneMatch(user: Api.User): Promise<PhoneMatch> {
289
329
  const userId = user.id.toString();
290
330
  if (user.accessHash) {