Skip to content

Commit

Permalink
added 4 methods to change channel metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
alechkos committed Nov 3, 2023
1 parent e45503b commit 1757130
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
16 changes: 15 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1341,7 +1341,21 @@ declare namespace WAWebJS {
/** Last message in the channel */
lastMessage: Message | undefined;


/** Updates the channel subject */
setSubject(newSubject: string): Promise<boolean>;
/** Updates the channel description */
setDescription(newDescription: string): Promise<boolean>;
/** Updates the channel profile picture */
setProfilePicture(newProfilePicture: MessageMedia): Promise<boolean>;
/**
* Updates available reactions to use in the channel
*
* Valid values for passing to the method are:
* 0 for ALL reactions to be available
* 1 for BASIC reactions to be available: 👍, ❤️, 😂, 😮, 😢, 🙏
* 3 for NONE reactions to be avaliable
*/
setReactionSetting(reactionCode: number): Promise<boolean>;
}

export interface MessageSearchOptions {
Expand Down
81 changes: 81 additions & 0 deletions src/structures/Channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,87 @@ class Channel extends Base {

return super._patch(data);
}

/**
* Updates the channel subject
* @param {string} newSubject
* @returns {Promise<boolean>} Returns true if the subject was properly updated. This can return false if the user does not have the necessary permissions.
*/
async setSubject(newSubject) {
const success = await this._setChannelMetadata({ name: newSubject }, { editName: true });
success && (this.name = newSubject);
return success;
}

/**
* Updates the channel description
* @param {string} newDescription
* @returns {Promise<boolean>} Returns true if the operation completed successfully, false otherwise
*/
async setDescription(newDescription) {
const success = await this._setChannelMetadata({ description: newDescription }, { editDescription: true });
success && (this.description = newDescription);
return success;
}

/**
* Updates the channel profile picture
* @param {MessageMedia} newProfilePicture
* @returns {Promise<boolean>} Returns true if the operation completed successfully, false otherwise
*/
async setProfilePicture(newProfilePicture) {
return await this._setChannelMetadata({ picture: newProfilePicture }, { editPicture: true });
}

/**
* Updates available reactions to use in the channel
*
* Valid values for passing to the method are:
* 0 for ALL reactions to be available
* 1 for BASIC reactions to be available: 👍, ❤️, 😂, 😮, 😢, 🙏
* 3 for NONE reactions to be avaliable
* @param {number} reactionCode
* @returns {Promise<boolean>} Returns true if the operation completed successfully, false otherwise
*/
async setReactionSetting(reactionCode) {
if (reactionCode === 2) return false;
const success = await this._setChannelMetadata(
{ reactionCodesSetting: reactionCode },
{ editReactionCodesSetting: true }
);
success && (this.channelMetadata.reactionCodesSetting = reactionCode);
return success;
}

/**
* Internal method to change the channel metadata
* @param {string|number|MessageMedia} value The new value to set
* @param {string} property The property of a channel metadata to change
* @returns {Promise<boolean>} Returns true if the operation completed successfully, false otherwise
*/
async _setChannelMetadata(value, property) {
return await this.client.pupPage.evaluate(async (channelId, value, property) => {
const channelWid = window.Store.WidFactory.createWid(channelId);
const channel = await window.Store.NewsletterCollection.find(channelWid);
if (!channel) return false;
if (property.editPicture) {
value.picture = value.picture
? await window.WWebJS.cropAndResizeImage(value.picture, {
asDataUrl: true,
mimetype: 'image/jpeg',
size: 640,
quality: 1
})
: null;
}
try {
await window.Store.ChannelUtils.editNewsletterMetadataAction(channel, property, value);
return true;
} catch (err) {
return false;
}
}, this.id._serialized, value, property);
}
}

module.exports = Channel;
1 change: 1 addition & 0 deletions src/util/Injected.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ exports.ExposeStore = (moduleRaidStr) => {
};
window.Store.ChannelUtils = {
...window.mR.findModule('createNewsletterQuery')[0],
...window.mR.findModule('editNewsletterMetadataAction')[0],
};

if (!window.Store.Chat._find) {
Expand Down

0 comments on commit 1757130

Please sign in to comment.