File size: 2,785 Bytes
46252cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
 * Contacts resource — contact lookup and management.
 *
 * Backed by `src/modules/contact/contact.controller.ts`.
 * @packageDocumentation
 */

import { encodeSegment } from '../http.js';
import type { OpenWAClient } from '../client.js';
import type {
  CheckNumberResponse,
  ContactPhoneResponse,
  ContactRecord,
  ProfilePictureResponse,
  SuccessResult,
} from '../types.js';

export interface ListContactsQuery {
  limit?: number;
  offset?: number;
}

export class ContactsResource {
  constructor(private readonly client: OpenWAClient) {}

  /** List contacts known to the session. */
  list(sessionId: string, query?: ListContactsQuery): Promise<ContactRecord[]> {
    return this.client.request<ContactRecord[]>({
      method: 'GET',
      path: `/api/sessions/${encodeSegment(sessionId)}/contacts`,
      query,
    });
  }

  /** Get details for a single contact by id (JID). */
  get(sessionId: string, contactId: string): Promise<ContactRecord> {
    return this.client.request<ContactRecord>({
      method: 'GET',
      path: `/api/sessions/${encodeSegment(sessionId)}/contacts/${encodeSegment(contactId)}`,
    });
  }

  /** Check whether a phone number is registered on WhatsApp. */
  check(sessionId: string, number: string): Promise<CheckNumberResponse> {
    return this.client.request<CheckNumberResponse>({
      method: 'GET',
      path: `/api/sessions/${encodeSegment(sessionId)}/contacts/check/${encodeSegment(number)}`,
    });
  }

  /** Get the contact's profile picture URL (or null). */
  profilePicture(sessionId: string, contactId: string): Promise<ProfilePictureResponse> {
    return this.client.request<ProfilePictureResponse>({
      method: 'GET',
      path: `/api/sessions/${encodeSegment(sessionId)}/contacts/${encodeSegment(contactId)}/profile-picture`,
    });
  }

  /** Resolve a contact id (e.g. a `@lid`) to a phone number. */
  phone(sessionId: string, contactId: string): Promise<ContactPhoneResponse> {
    return this.client.request<ContactPhoneResponse>({
      method: 'GET',
      path: `/api/sessions/${encodeSegment(sessionId)}/contacts/${encodeSegment(contactId)}/phone`,
    });
  }

  /** Block a contact. Requires an OPERATOR-level key. */
  block(sessionId: string, contactId: string): Promise<SuccessResult> {
    return this.client.request<SuccessResult>({
      method: 'POST',
      path: `/api/sessions/${encodeSegment(sessionId)}/contacts/${encodeSegment(contactId)}/block`,
    });
  }

  /** Unblock a contact. Requires an OPERATOR-level key. */
  unblock(sessionId: string, contactId: string): Promise<SuccessResult> {
    return this.client.request<SuccessResult>({
      method: 'DELETE',
      path: `/api/sessions/${encodeSegment(sessionId)}/contacts/${encodeSegment(contactId)}/block`,
    });
  }
}