diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..a5e1d3d --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,32 @@ +name: release + +on: + workflow_dispatch: + inputs: + release_type: + description: "`alpha`, `alpha-minor`, `alpha-major` release?" + required: true + default: "alpha" + +jobs: + release: + runs-on: ubuntu-latest + if: ${{ github.repository == 'pedroslopez/whatsapp-web.js' }} + steps: + - uses: actions/checkout@v2 + with: + token: ${{ secrets.RELEASE_GITHUB_TOKEN }} + - run: git config --global user.email "hello@wwebjs.dev" + - run: git config --global user.name "WWebJS Bot" + - name: Bump version and publish to NPM + id: publish + run: ./tools/publish ${{ github.event.inputs.release_type }} + env: + NPM_TOKEN: ${{ secrets.RELEASE_NPM_TOKEN }} + - name: Create GitHub Release + id: create_release + uses: ncipollo/release-action@v1 + with: + prerelease: ${{ steps.publish.outputs.PRERELEASE }} + generateReleaseNotes: true + tag: v${{ steps.publish.outputs.NEW_VERSION }} diff --git a/index.d.ts b/index.d.ts index 63b8625..acc3bf6 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1062,6 +1062,10 @@ declare namespace WAWebJS { * Set this to Infinity to load all messages. */ limit?: number + /** + * Return only messages from the bot number or vise versa. To get all messages, leave the option undefined. + */ + fromMe?: boolean } /** diff --git a/package.json b/package.json index 13f41c0..46ce339 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "whatsapp-web.js", - "version": "1.17.1", + "version": "1.18.0-alpha.1", "description": "Library for interacting with the WhatsApp Web API ", "main": "./index.js", "typings": "./index.d.ts", diff --git a/src/Client.js b/src/Client.js index b36eb96..d3c9bb4 100644 --- a/src/Client.js +++ b/src/Client.js @@ -989,7 +989,13 @@ class Client extends EventEmitter { */ async getCommonGroups(contactId) { const commonGroups = await this.pupPage.evaluate(async (contactId) => { - const contact = window.Store.Contact.get(contactId); + let contact = window.Store.Contact.get(contactId); + if (!contact) { + const wid = window.Store.WidFactory.createUserWid(contactId); + const chatConstructor = window.Store.Contact.getModelsArray().find(c=>!c.isGroup).constructor; + contact = new chatConstructor({id: wid}); + } + if (contact.commonGroups) { return contact.commonGroups.serialize(); } diff --git a/src/authStrategies/RemoteAuth.js b/src/authStrategies/RemoteAuth.js index 5ae85cc..f84ba59 100644 --- a/src/authStrategies/RemoteAuth.js +++ b/src/authStrategies/RemoteAuth.js @@ -178,9 +178,9 @@ class RemoteAuth extends BaseAuthStrategy { await fs.promises.rm(dirElement, { recursive: true, force: true - }); + }).catch(() => {}); } else { - await fs.promises.unlink(dirElement); + await fs.promises.unlink(dirElement).catch(() => {}); } } } diff --git a/src/structures/Chat.js b/src/structures/Chat.js index 6f0f0e2..241d297 100644 --- a/src/structures/Chat.js +++ b/src/structures/Chat.js @@ -170,13 +170,22 @@ class Chat extends Base { /** * Loads chat messages, sorted from earliest to latest. - * @param {Object} searchOptions Options for searching messages. Right now only limit is supported. + * @param {Object} searchOptions Options for searching messages. Right now only limit and fromMe is supported. * @param {Number} [searchOptions.limit] The amount of messages to return. If no limit is specified, the available messages will be returned. Note that the actual number of returned messages may be smaller if there aren't enough messages in the conversation. Set this to Infinity to load all messages. + * @param {Boolean} [searchOptions.fromMe] Return only messages from the bot number or vise versa. To get all messages, leave the option undefined. * @returns {Promise>} */ async fetchMessages(searchOptions) { let messages = await this.client.pupPage.evaluate(async (chatId, searchOptions) => { - const msgFilter = m => !m.isNotification; // dont include notification messages + const msgFilter = (m) => { + if (m.isNotification) { + return false; // dont include notification messages + } + if (searchOptions && searchOptions.fromMe && m.id.fromMe !== searchOptions.fromMe) { + return false; + } + return true; + }; const chat = window.Store.Chat.get(chatId); let msgs = chat.msgs.getModelsArray().filter(msgFilter); diff --git a/src/structures/Message.js b/src/structures/Message.js index dc4acaf..e8260dd 100644 --- a/src/structures/Message.js +++ b/src/structures/Message.js @@ -430,14 +430,14 @@ class Message extends Base { /** * Deletes a message from the chat - * @param {?boolean} everyone If true and the message is sent by the current user, will delete it for everyone in the chat. + * @param {?boolean} everyone If true and the message is sent by the current user or the user is an admin, will delete it for everyone in the chat. */ async delete(everyone) { await this.client.pupPage.evaluate((msgId, everyone) => { let msg = window.Store.Msg.get(msgId); - if (everyone && msg.id.fromMe && msg._canRevoke()) { - return window.Store.Cmd.sendRevokeMsgs(msg.chat, [msg], {type: 'Sender'}); + if (everyone && msg._canRevoke()) { + return window.Store.Cmd.sendRevokeMsgs(msg.chat, [msg], { type: msg.id.fromMe ? 'Sender' : 'Admin' }); } return window.Store.Cmd.sendDeleteMsgs(msg.chat, [msg], true); diff --git a/tools/publish b/tools/publish new file mode 100755 index 0000000..c3a6a97 --- /dev/null +++ b/tools/publish @@ -0,0 +1,80 @@ +#!/bin/bash + +cd "$(dirname "$0")" +cd '..' + +BRANCH=`git rev-parse --abbrev-ref HEAD` +RELEASE_MODE=$1 + +echo "" +echo "-----> CHECK INPUTS" +echo "" + +if [[ "$RELEASE_MODE" == "alpha" ]] +then + PRERELEASE='true' + VERSION_ARGS="prerelease --preid alpha" + DIST_TAG="next" +elif [[ "$RELEASE_MODE" == "alpha-minor" ]] +then + PRERELEASE='true' + VERSION_ARGS="preminor --preid alpha" + DIST_TAG="next" +elif [[ "$RELEASE_MODE" == "alpha-major" ]] +then + PRERELEASE='true' + VERSION_ARGS="premajor --preid alpha" + DIST_TAG="next" +else + echo 'Release Mode required' + exit 1 +fi + +if [ -f ~/.npmrc ]; then + echo "Found existing .npmrc" +else + if [[ -z "$NPM_TOKEN" ]];then + echo "No NPM_TOKEN or ~/.npmrc, exiting.." + exit 1; + else + echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc + fi +fi +echo "Publishing as NPM user: `npm whoami`" + +if [[ $BRANCH != 'main' ]]; then + echo "Not on 'main' branch. Exiting" + exit 1 +fi + +if [[ -n $(git status -s) ]]; then + echo "There are uncommitted changes on this branch. Exiting..." + exit 1 +fi + +echo "" +echo "-----> BUMP VERSION" +echo "" + +npm version $VERSION_ARGS || exit 1 +git push && git push --tags || exit 1 + +NEW_VERSION=`cat package.json | jq -r .version` +echo "New Version: $NEW_VERSION" + +echo "" +echo "-----> PUSH TO NPM" +echo "" + +npm publish --tag $DIST_TAG + + +echo "" +echo "-----> Done!" +echo "Version $NEW_VERSION published to $DIST_TAG tag" +echo "" + +echo "::set-output name=NEW_VERSION::$NEW_VERSION" +echo "::set-output name=PRERELEASE::$PRERELEASE" + +exit 0