Repository: pedroslopez/whatsapp-web.js
File: src/Client.js (JavaScript)

Code before:
    }

    /**
     * Get user device count by ID
     * Each WaWeb Connection counts as one device, and the phone (if exists) counts as one
     * So for a non-enterprise user with one WaWeb connection it should return "2"
     * @param {string} contactId
     * @returns {number}
     */
    async getContactDeviceCount(contactId) {
        let devices = await window.Store.DeviceList.getDeviceIds([window.Store.WidFactory.createWid(contactId)]);
        if(devices && devices.length  && devices[0] != null && typeof devices[0].devices == 'object'){
            return devices[0].devices.length;
        }
        return 0;
    }

    /**
     * Sync history conversation
     * @param {string} chatId
     * @return {boolean} true/false
     */
    async syncHistory(chatId) {
        return await this.pupPage.evaluate(async (chatId) => {
            const chat = await window.WWebJS.getChat(chatId);
            if (chat.endOfHistoryTransferType == window.Store.HistoryStates.COMPLETE_BUT_MORE_MESSAGES_REMAIN_ON_PRIMARY) {
                window.Store.HistorySync.sendPeerDataOperationRequest(window.require("WAWebProtobufsE2E.pb").Message$PeerDataOperationRequestType.HISTORY_SYNC_ON_DEMAND, {
                    chatId: chat.id
                });
                return true;
            }
            return false;
        }, chatId);
    }
}

module.exports = Client;

Review comment (refactor):
We can simplify this, since we know that the value of `COMPLETE_BUT_MORE_MESSAGES_REMAIN_ON_PRIMARY` is 0:

```suggestion
            if (chat.endOfHistoryTransferType === 0) {
```
<br/>

Properties of the `window.require("WAWebChatConstants").ConversationEndOfHistoryTransferModelPropType` object are:
```js
{
  COMPLETE_BUT_MORE_MESSAGES_REMAIN_ON_PRIMARY: 0,
  COMPLETE_AND_NO_MORE_MESSAGE_REMAIN_ON_PRIMARY: 1,
  INCOMPLETE: 2,
  NOT_INCLUDED_IN_HIST_SYNC: 3,
  COMPLETE_ON_DEMAND_SYNC_BUT_MORE_MSG_REMAIN_ON_PRIMARY: 4
}
```

Code after:
    }

    /**
     * Get user device count by ID
     * Each WaWeb Connection counts as one device, and the phone (if exists) counts as one
     * So for a non-enterprise user with one WaWeb connection it should return "2"
     * @param {string} contactId
     * @returns {number}
     */
    async getContactDeviceCount(contactId) {
        let devices = await window.Store.DeviceList.getDeviceIds([window.Store.WidFactory.createWid(contactId)]);
        if(devices && devices.length  && devices[0] != null && typeof devices[0].devices == 'object'){
            return devices[0].devices.length;
        }
        return 0;
    }

    /**
     * Sync chat history conversation
     * @param {string} chatId
     * @return {Promise<boolean>} True if operation completed successfully, false otherwise.
     */
    async syncHistory(chatId) {
        return await this.pupPage.evaluate(async (chatId) => {
            const chat = await window.WWebJS.getChat(chatId);
            if (chat.endOfHistoryTransferType === 0) {
                await window.Store.HistorySync.sendPeerDataOperationRequest(3, {
                    chatId: chat.id
                });
                return true;
            }
            return false;
        }, chatId);
    }
}

module.exports = Client;