File size: 2,445 Bytes
4c34106
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
84
/*
 * This file is part of WPPConnect.
 *
 * WPPConnect is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * WPPConnect is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with WPPConnect.  If not, see <https://www.gnu.org/licenses/>.
 */

export function scope(id, erro, status, text = null) {
  let e = {
    me: WPP.whatsapp.Conn.attributes,
    to: id,
    erro: erro,
    text: text,
    status: status,
  };
  return e;
}
export async function getchatId(chatId) {
  var to = await WAPI.getChatById(chatId),
    objTo = to.lastReceivedKey || {},
    extend = {
      formattedName: to.contact.formattedName,
      isBusiness: to.contact.isBusiness,
      isMyContact: to.contact.isMyContact,
      verifiedName: to.contact.verifiedName,
      pushname: to.contact.pushname,
    };
  Object.assign(objTo, extend);
  return objTo;
}

export async function sendExist(chatId, returnChat = true, Send = true) {
  if (!chatId) {
    return scope(chatId, true, 500, 'Chat ID is empty');
  }

  // Check chat exists (group is always a chat)
  let chat = await window.WAPI.getChat(chatId);

  if (!chat && chatId === 'status@broadcast') {
    chat = new WPP.whatsapp.ChatStore.modelClass({
      id: WPP.whatsapp.WidFactory.createWid('status@broadcast'),
    });
    WPP.whatsapp.ChatStore.add(chat);
    chat = await window.WAPI.getChat(chatId); // Fix some methods
  }

  // Check if contact number exists
  if (!chat && !chatId.includes('@g')) {
    let ck = await window.WAPI.checkNumberStatus(chatId);

    if (!ck.numberExists) {
      return scope(chatId, true, ck.status, 'The number does not exist');
    }

    // Load chat ID for non contact
    await WPP.chat.find(ck.id);

    chatId = ck.id._serialized;
    chat = await window.WAPI.getChat(chatId);
  }

  if (!chat) {
    return scope(chatId, true, 404);
  }
  if (Send) {
    await WPP.chat.markIsRead(chat.id).catch(() => null);
  }
  if (returnChat) {
    return chat;
  }
  return scope(chatId, false, 200);
}