Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support to block/unblock contacts and receive block/unblock events #460

Merged
merged 1 commit into from
Sep 9, 2023

Conversation

emacielxp
Copy link
Contributor

Block/Unblock support

Added support to block/unblock contacts and receive block/unblock events

Changes:

connectionevents.go

  • Sync at local cache the blocked list when successfully connect

mdtest/main.go

  • Included commands to test the block/unblock feature:
    -- listblocked: get the blocked list from whatsapp
    -- block: block contact
    -- unblock: unblock contact

  • Included contact blocked status changed event listener

send.go

  • Included validation at SendMessage function if the contact is blocked to not send message for a blocked contact

notification.go

  • Included function to handle the whatsapp block/unblock event changes

types/events/events.go

  • Created the contact blocked status changed event type definition

user.go

  • Created necessary functions to allow cache, block and unblock contacts

Notes:

we are using this version at production environment with more than 400 whatsapps connected and all works fine

this feature was already tested and works fine at:

  • iphone business
  • iphone normal
  • android business
  • android normal

@mateusfmello
Copy link

Excellent modifications, I tested it and everything seems to be perfect.

I only wonder about the project standards.

In mdtest the commands pin, mute, archive, presence etc. are a single command varying the arguments, for "pinned" or "unpinned", "muted" or "unmuted" etc., I believe that the "block" command should follow this same pattern, thus saving code, simplifying the project and maintaining the standard that has been implemented.

I suggest the command would be like this:

block <jid> <action>

Where "action" is a boolean.

The code would look like this:

  • Would delete the "unblock" command;

Block command:

// /mdtest/main.go
case "block":
	if len(args) < 2 {
		log.Errorf("Usage: block <jid> <action>")
		return
	}
	target, ok := parseJID(args[0])
	if !ok {
		return
	}
	action, err := strconv.ParseBool(args[1])
	if err != nil {
		log.Errorf("invalid second argument: %v", err)
		return
	}

	blockedList, err := cli.BuildBlockContact(target, action)
	if err != nil {
		log.Errorf("Error blocking contact: %v", err)
	}

	log.Infof("%+v", blockedList)
}

In "/user.go"

  • Delete the UnblockContact and BlockContact methods

Create the BuildBlockContact method, which would be like this:

// BuildBlockCSe vontact updates the contact blocked status to false or true
func (cli *Client) BuildBlockContact(user types.JID, block bool) ([]types.JID, error) {
	blockedList, err := cli.setContactBlockedStatus(user, block)
	if err != nil {
		return nil, err
	}

	return blockedList, nil
}

@emacielxp if you like this idea I can make a PR for you if you prefer it that way.

@tulir tulir merged commit 62aaec3 into tulir:main Sep 9, 2023
@emacielxp emacielxp deleted the block-unblock-contacts branch November 13, 2023 14:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants