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

Adding / Deleting Profile Picture #652

Closed
wants to merge 19 commits into from
Closed

Adding / Deleting Profile Picture #652

wants to merge 19 commits into from

Conversation

PurpShell
Copy link
Collaborator

@PurpShell PurpShell commented Apr 27, 2021

Profile Picture functionality

INFO

This pull request is adding the way to set and delete profile pictures.

Currently, this only works with chats in cache, (ProfilePicThumb),
I will see if I can change that though..

It automatically resizes any unfitting dimensions, picking the left of an image.. Recommended dimensions are 640*640.

YOU CAN ONLY SET / DELETE PFPs TO GROUPS and YOURSELF
(you must be able to do it on your own by default)
(you must have group admin on any chat that has restricting editing info only to admins)

EDIT (May 4, 2021): THIS HAS BEEN TESTED FOR 7 DAYS.. All seems working with no bans and no potential risk of being banned.

VIDEO DEMO:

output.mp4

USAGE

This pull request adds two methods.

client.setProfilePicture(id, picture):
- Parameters:
- id : String (Chat ID to set)
- picture: MessageMedia instance of the image you want to be set.
- Returns:
Image URL / False
client.deleteProfilePicture(id):
- Parameters:
- id : String (Chat ID to delete from)
- Returns:
True / False
Also 2 sub-methods for a GroupChat instance:

chat.setProfilePicture(picture):
- Parameters:
- picture: MessageMedia instance of the image you want to be set.
- Returns:
Image URL / False

chat.deleteProfilePicture():
- Parameters:
none
- Returns:
True / False

@PurpShell PurpShell changed the title Adding Profile Picture functionality Adding / Deleting Profile Picture Apr 29, 2021
@PurpShell PurpShell mentioned this pull request May 1, 2021
@guicuton
Copy link
Contributor

When these methods will be merged? They would be very usefull :-)

@bernardoely
Copy link

Looking forward to getting this functionality!! Really appreciate if you can merge this. Thanks.

Copy link
Owner

@pedroslopez pedroslopez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great! Thanks for adding this.

I think to avoid confusion we should leave client.setProfilePicture and client.removeProfilePicture to deal with the current user's profile and let groupChat.setProfilePicture / groupChat.removeProfilePicture deal with the group's picture. You can then move common functionality to Injected.js

* @returns {Promise<Boolean>} true if it succeeded / false if it didn't delete the profile picture.
*/
async deleteProfilePicture() {
return await this.client.setProfilePicture(this.id._serialized);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be calling deleteProfilePicture instead of setProfilePicture?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops

I usually make PRs locally and test then re-type it out in github's editor..

I am going to use git functions from now on, overall less mistakes

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for putting an eye on this, sorry for the confusion

@PurpShell PurpShell marked this pull request as draft June 5, 2021 21:14
@PurpShell PurpShell mentioned this pull request Jun 11, 2021
@PurpShell PurpShell linked an issue Jun 11, 2021 that may be closed by this pull request
@Murilandrade
Copy link

Murilandrade commented Jun 12, 2021

when I try to do that I get an erro on the window.mR.findModule('setProfilePic')[0]; is there a solution for this?

@PurpShell
Copy link
Collaborator Author

when I try to do that I get an erro on the window.mR.findModule('setProfilePic')[0]; is there a solution for this?

I will check it out. i still have to re-factor the code anyways so I will have to find the module by collection and not the method name itself

Best regards,

PurpShell

@PurpShell
Copy link
Collaborator Author

As you might have seen, the PR has been put on draft mode and the eslint failed

@bragarpaulo
Copy link

Very nice this!!!! I hope to use it soon.

Congrats @PurpShell & @pedroslopez

eslint fix for your Pull request
@eowoosh
Copy link

eowoosh commented Jul 11, 2021

@PurpShell Hello! It's possible you share function Util.formatImageToProfilePic() ?

TypeError: Util.formatImageToProfilePic is not a function

@dbsdenis
Copy link

@PurpShell Hello! É possível compartilhar a função Util.formatImageToProfilePic ()?

TypeError: Util.formatImageToProfilePic is not a function

Exemplo de uso:

if (msg.body === '!addpicture') {
    // Add picture the group
    let chat = await msg.getChat();
    let picture = MessageMedia.fromFilePath("image.png");
    if (chat.isGroup) {
        await client.setProfilePicture(chat.id._serialized, picture);
    } else {
        msg.reply('This command can only be used in a group!');
    }
}

if (msg.body === '!removepicture') {
    // Remove picture the group
    let chat = await msg.getChat();
    if (chat.isGroup) {
        await client.deleteProfilePicture(chat.id._serialized);
    } else {
        msg.reply('This command can only be used in a group!');
    }
}

src/util/Util.js

/**
 * Formats a image to profile picture
 * @param {MessageMedia} media
 * 
 * @returns {Promise<MessageMedia>} media in picture format
 */
static async formatImageToProfilePic(media) {

    if (!media.mimetype.includes('image'))
        throw new Error('media is not a image');

    const buff = Buffer.from(media.data, 'base64');

    let preview = sharp(buff).resize(96, 96);
    let img = sharp(buff).resize(640, 640);
  
    preview = `data:${media.mimetype};base64,${(await preview.toBuffer()).toString('base64')}`;
    img = `data:${media.mimetype};base64,${(await img.toBuffer()).toString('base64')}`;

    return {             
        img,
        preview
    };
}

@PurpShell
Copy link
Collaborator Author

@PurpShell Hello! É possível compartilhar a função Util.formatImageToProfilePic ()?
TypeError: Util.formatImageToProfilePic is not a function

Exemplo de uso:

if (msg.body === '!addpicture') {
    // Add picture the group
    let chat = await msg.getChat();
    let picture = MessageMedia.fromFilePath("image.png");
    if (chat.isGroup) {
        await client.setProfilePicture(chat.id._serialized, picture);
    } else {
        msg.reply('This command can only be used in a group!');
    }
}

if (msg.body === '!removepicture') {
    // Remove picture the group
    let chat = await msg.getChat();
    if (chat.isGroup) {
        await client.deleteProfilePicture(chat.id._serialized);
    } else {
        msg.reply('This command can only be used in a group!');
    }
}

src/util/Util.js

/**
 * Formats a image to profile picture
 * @param {MessageMedia} media
 * 
 * @returns {Promise<MessageMedia>} media in picture format
 */
static async formatImageToProfilePic(media) {

    if (!media.mimetype.includes('image'))
        throw new Error('media is not a image');

    const buff = Buffer.from(media.data, 'base64');

    let preview = sharp(buff).resize(96, 96);
    let img = sharp(buff).resize(640, 640);
  
    preview = `data:${media.mimetype};base64,${(await preview.toBuffer()).toString('base64')}`;
    img = `data:${media.mimetype};base64,${(await img.toBuffer()).toString('base64')}`;

    return {             
        img,
        preview
    };
}

this is different from my way, though i haven't been able to push since i have been getting issues with sharp.

also the way you do this is very bad, resizing an image of any dimension / aspect ratio, and also not providing the format in JPEG.

@dbsdenis
Copy link

@PurpShell Hello! É possível compartilhar uma função Util.formatImageToProfilePic ()?
TypeError: Util.formatImageToProfilePic is not a function

Exemplo de uso:

if (msg.body === '!addpicture') {
    // Add picture the group
    let chat = await msg.getChat();
    let picture = MessageMedia.fromFilePath("image.png");
    if (chat.isGroup) {
        await client.setProfilePicture(chat.id._serialized, picture);
    } else {
        msg.reply('This command can only be used in a group!');
    }
}

if (msg.body === '!removepicture') {
    // Remove picture the group
    let chat = await msg.getChat();
    if (chat.isGroup) {
        await client.deleteProfilePicture(chat.id._serialized);
    } else {
        msg.reply('This command can only be used in a group!');
    }
}

src/util/Util.js

/**
 * Formats a image to profile picture
 * @param {MessageMedia} media
 * 
 * @returns {Promise<MessageMedia>} media in picture format
 */
static async formatImageToProfilePic(media) {

    if (!media.mimetype.includes('image'))
        throw new Error('media is not a image');

    const buff = Buffer.from(media.data, 'base64');

    let preview = sharp(buff).resize(96, 96);
    let img = sharp(buff).resize(640, 640);
  
    preview = `data:${media.mimetype};base64,${(await preview.toBuffer()).toString('base64')}`;
    img = `data:${media.mimetype};base64,${(await img.toBuffer()).toString('base64')}`;

    return {             
        img,
        preview
    };
}

isso é diferente do meu jeito, embora eu não tenha conseguido empurrar desde que comecei a ter problemas com sharp.

além disso, a maneira como você faz isso é muito ruim, redimensionando uma imagem de qualquer dimensão / relação de aspecto e também não fornecendo o formato em JPEG.

It's temporary, but it works... 😁

@PurpShell
Copy link
Collaborator Author

I am working on this pull request again! Sorry for the long wait! I'll work more on this on my windows machine, since no sharp issues there! So far I've tried to imitate the original way the function worked, but it seems like something has either changed or i'm getting it wrong. Excuse me for not providing the util function! I reset my node_modules a bunch of times since April, which caused me to lose the function.

@PurpShell
Copy link
Collaborator Author

PurpShell commented Aug 27, 2021

Update: even after fixing the util function, some change has occurred in whatsapp web that doesn't allow me to set a profile picture

I'll look into it in the following days

@stale
Copy link

stale bot commented Oct 11, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Oct 11, 2021
@PurpShell
Copy link
Collaborator Author

my issues with sharp aren't bad.. I think I can try to extract the tools wa uses to crop and get the photo

@PurpShell PurpShell closed this Nov 11, 2021
@PurpShell
Copy link
Collaborator Author

Since I am getting rid of sharp, which this PR uses, I will also find the functions whatsapp uses to resize the image, and finally settle this issue

@DangerD1024
Copy link

Hi, when you will merge that? =)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Set Group Thumbnail