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

feat: Channel #2620

Open
wants to merge 73 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 69 commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
b92c530
window.injectToFunction clarifications
alechkos Nov 1, 2023
562d7b0
added createChannel method
alechkos Nov 2, 2023
197e67f
initial Channel class structure
alechkos Nov 3, 2023
cc78a0d
added getChannelById and getChannels methods
alechkos Nov 3, 2023
2baf22a
added 4 methods to change channel metadata
alechkos Nov 4, 2023
11c184e
added deleteChannel method
alechkos Nov 4, 2023
1ba8bec
added subscribe to/unsubscribe from channel methods
alechkos Nov 4, 2023
ea82240
ecmaVersion updated to 2022
alechkos Nov 5, 2023
0117c76
added searchChannels method
alechkos Nov 5, 2023
70f6428
refactoring of: getChat, getChannel, getChatModel, getChannelModel
alechkos Nov 7, 2023
0c61d59
removed Channel.canSend
alechkos Nov 7, 2023
7194588
sendMessage updated to support channels [p1]
alechkos Nov 8, 2023
09739b8
createChannel: returns string if an error occupied
alechkos Nov 13, 2023
ac2cd4e
added getSubscribers
alechkos Nov 14, 2023
321747a
added mute/unmute methods
alechkos Nov 14, 2023
c2c79e9
searchChannels: minor update
alechkos Nov 14, 2023
ab40edd
sendMessage updated to support channels [p2]
alechkos Nov 21, 2023
88c8d9c
[REFAC] refactored chat retrieving in getChatOrChannel
alechkos Nov 21, 2023
b74f418
[FEAT] added getChannelByInviteCode method
alechkos Nov 23, 2023
f2e7e55
[CHG] added check if channel creation enabled
alechkos Nov 23, 2023
92167ce
[FEAT] added sendChannelAdminInvitation method
alechkos Nov 24, 2023
6b18bd1
[WIP] preparations for sending polls [p1]
alechkos Nov 24, 2023
ece4618
[BUG] fixed some internal functions
alechkos Nov 24, 2023
3eb80a0
Merge branch 'main' into channels
alechkos Nov 24, 2023
8f2e4ce
[BUG] fixed poll creation
alechkos Nov 26, 2023
72d202d
Merge branch 'main' into channels
alechkos Nov 26, 2023
ac29f13
[COPY] renamed some internal functions
alechkos Nov 26, 2023
3d2e07e
Merge branch 'main' into channels
alechkos Dec 1, 2023
2b554e4
fix: typo
alechkos Dec 1, 2023
c8ff1bf
Merge branch 'main' into channels
alechkos Dec 1, 2023
451b30b
fix: send docs without caption
alechkos Dec 1, 2023
77b0bc0
fix: fixed some internal function
alechkos Dec 5, 2023
0c978c1
Merge branch 'main' into channels
alechkos Dec 5, 2023
7d24cc9
fix: channel retrieving
alechkos Dec 6, 2023
f846ac7
fix: fixed internal getChannelMetadata function
alechkos Dec 7, 2023
224b04f
fix: fixed channel admin can unsubscribe from channel
alechkos Dec 7, 2023
146d612
feat: channel admin functionality
alechkos Dec 7, 2023
35f312a
Merge branch 'main' into channels
alechkos Dec 7, 2023
d426b48
Merge branch 'main' into channels
alechkos Dec 12, 2023
21e17ca
Merge branch 'main' into channels
alechkos Dec 13, 2023
3a05ddb
Merge branch 'main' into channels
alechkos Dec 16, 2023
ad56da6
Merge branch 'main' into channels
alechkos Dec 17, 2023
9913ef8
feat: added fetchMessages method
alechkos Dec 20, 2023
cd37fe4
Merge branch 'main' into channels
alechkos Jan 2, 2024
b18ee26
Merge branch 'main' into channels
alechkos Jan 11, 2024
455813a
refactor: revert removing support for Buttons and Lists
alechkos Jan 11, 2024
dbaaef1
refactor: revert parameter name from 'chatOrChannel' to 'chat'
alechkos Jan 11, 2024
89e7a9a
Merge branch 'main' into channels
alechkos Jan 11, 2024
e485655
Merge branch 'main' into channels
alechkos Jan 14, 2024
f443778
fix: support Node v12
alechkos Jan 14, 2024
8ed8626
refactor: return 'undefined' in a case of falsy value
alechkos Jan 14, 2024
025cd66
refactor: use already created variable
alechkos Jan 14, 2024
1ccacbc
docs: fix 'countryCodes' property type
alechkos Jan 15, 2024
7e709be
fix: 'searchChannels' method fixed
alechkos Jan 15, 2024
91df885
fix: add check in 'setReactionSetting' method
alechkos Jan 15, 2024
bc250c7
Merge branch 'main' into channels
alechkos Jan 18, 2024
4fb1faa
Merge branch 'main' into channels
alechkos Mar 1, 2024
e9392ec
Merge branch 'main' into channels
alechkos Apr 3, 2024
fce9bdb
sendNewsletterMessageJob requires a proper Msg Object in 2.24xx (#10)
themazim Apr 19, 2024
5ea5018
Merge branch 'main' into channels
alechkos Apr 22, 2024
926198f
style: fix broken link in readme file
alechkos Apr 22, 2024
fee0deb
Merge branch 'main' into channels
alechkos Apr 27, 2024
0ea8853
Merge branch 'main' into channels
alechkos Apr 28, 2024
883507d
Merge branch 'main' into channels
alechkos Apr 30, 2024
51b1943
fix: only directPath determines whether hasMedia or not
alechkos Apr 30, 2024
e2d156b
Merge branch 'main' into channels
alechkos Apr 30, 2024
d2d74c9
Merge branch 'main' into channels
alechkos May 1, 2024
a3827cd
fix: get rid of unnecessary 'await'
alechkos May 1, 2024
96ad809
feat: add option to send polls in channels
alechkos May 1, 2024
3370e57
refactor: get channel instances with 'getChatById'
alechkos May 2, 2024
e414dea
chore: fix eslint
alechkos May 2, 2024
05e9eab
fix: fatal error
alechkos May 2, 2024
b68f83c
Merge branch 'main' into channels
alechkos Jun 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"SharedArrayBuffer": "readonly"
},
"parserOptions": {
"ecmaVersion": 2020
"ecmaVersion": 2022
},
"plugins": ["mocha"],
"ignorePatterns": ["docs"],
Expand Down
178 changes: 178 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ declare namespace WAWebJS {
/**Accepts an invitation to join a group */
acceptInvite(inviteCode: string): Promise<string>

/** Accepts a channel admin invitation and promotes the current user to a channel admin */
acceptChannelAdminInvite(channelId: string): Promise<boolean>

/** Revokes a channel admin invitation sent to a user by a channel owner */
revokeChannelAdminInvite(channelId: string, userId: string): Promise<boolean>

/** Demotes a channel admin to a regular subscriber (can be used also for self-demotion) */
demoteChannelAdmin(channelId: string, userId: string): Promise<boolean>

/** Accepts a private invitation to join a group (v4 invite) */
acceptGroupV4Invite: (inviteV4: InviteV4Data) => Promise<{status: number}>

Expand All @@ -38,6 +47,24 @@ declare namespace WAWebJS {
/** Creates a new group */
createGroup(title: string, participants?: string | Contact | Contact[] | string[], options?: CreateGroupOptions): Promise<CreateGroupResult|string>

/** Creates a new channel */
createChannel(title: string, options?: CreateChannelOptions): Promise<CreateChannelResult | string>

/** Subscribe to channel */
subscribeToChannel(channelId: string): Promise<boolean>

/** Unsubscribe from channel */
unsubscribeFromChannel(channelId: string, options?: UnsubscribeOptions): Promise<boolean>

/**
* Searches for channels based on search criteria, there are some notes:
* 1. The method finds only channels you are not subscribed to currently
* 2. If you have never been subscribed to a found channel
* or you have unsubscribed from it with {@link UnsubscribeOptions.deleteLocalModels} set to 'true',
* the lastMessage property of a found channel will be 'null'
*/
searchChannels(searchOptions: SearchChannelsOptions): Promise<Array<Channel> | []>

/** Closes the client */
destroy(): Promise<void>

Expand All @@ -53,6 +80,15 @@ declare namespace WAWebJS {
/** Get all current chat instances */
getChats(): Promise<Chat[]>

/** Gets a {@link Channel} object or a {@link ChannelMetadata} by its ID */
getChannelById(channelId: string, options?: { getMetadata?: boolean }): Promise<Channel | ChannelMetadata>

/** Gets a {@link Channel} object or a {@link ChannelMetadata} by invite code */
getChannelByInviteCode(inviteCode: string, options?: { getMetadata?: boolean }): Promise<Channel | ChannelMetadata>

/** Gets all cached {@link Channel} objects */
getChannels(): Promise<Channel[]>

/** Get contact instance by ID */
getContactById(contactId: string): Promise<Contact>

Expand Down Expand Up @@ -116,6 +152,9 @@ declare namespace WAWebJS {

/** Send a message to a specific chatId */
sendMessage(chatId: string, content: MessageContent, options?: MessageSendOptions): Promise<Message>

/** Sends a channel admin invitation to a user, allowing them to become an admin of the channel */
sendChannelAdminInvite(chatId: string, channelId: string, options?: { comment?: string }): Promise<boolean>

/** Searches for messages */
searchMessages(query: string, options?: { chatId?: string, page?: number, limit?: number }): Promise<Message[]>
Expand Down Expand Up @@ -614,6 +653,68 @@ declare namespace WAWebJS {
};
}

/** An object that handles options for channel creation */
export interface CreateChannelOptions {
/** The channel description */
description?: string,
/** The channel profile picture */
picture?: MessageMedia
}

/** An object that handles the result for createGroup method */
export interface CreateChannelResult {
/** A channel title */
title: string,
/** An object that handles the newly created channel ID */
nid: ChatId,
/** The channel invite link, starts with 'https://whatsapp.com/channel/' */
inviteLink: string,
/** The timestamp the channel was created at */
createdAtTs: number
}

/** Options for unsubscribe from a channel */
export interface UnsubscribeOptions {
/**
* If true, after an unsubscription, it will completely remove a channel from the channel collection
* making it seem like the current user have never interacted with it.
* Otherwise it will only remove a channel from the list of channels the current user is subscribed to
* and will set the membership type for that channel to GUEST
*/
deleteLocalModels?: boolean
}

/** Options for searching for channels */
export interface SearchChannelsOptions {
countryCodes?: string[];
sortOptions: {
field?: string;
order?: string;
};
searchText?: string;
view?: string;
limit?: number;
}

export interface ChannelMetadata {
id: string;
createdAtTs: number;
titleMetadata: {
title: string;
updatedAtTs: number;
};
descriptionMetadata: {
description: string;
updatedAtTs: string;
};
inviteLink: string;
membershipType: string;
stateType: string;
pictureUrl: string;
subscribersCount: number;
isVerified: boolean;
}

export interface GroupNotification {
/** ContactId for the user that produced the GroupNotification */
author: string,
Expand Down Expand Up @@ -1420,6 +1521,83 @@ declare namespace WAWebJS {
changeLabels: (labelIds: Array<string | number>) => Promise<void>
}

export interface Channel {
/** ID that represents the channel */
id: {
server: string;
user: string;
_serialized: string;
};
/** Title of the channel */
name: string;
/** The channel description */
description: string;
/** Indicates if it is a Channel */
isChannel: boolean;
/** Indicates if it is a Group */
isGroup: boolean;
/** Indicates if the channel is readonly */
isReadOnly: boolean;
/** Amount of messages unread */
unreadCount: number;
/** Unix timestamp for when the last activity occurred */
timestamp: number;
/** Indicates if the channel is muted or not */
isMuted: boolean;
/** Unix timestamp for when the mute expires */
muteExpiration: number;
/** Last message in the channel */
lastMessage: Message | undefined;

/** Gets the subscribers of the channel (only those who are in your contact list) */
getSubscribers(limit?: number): Promise<{contact: Contact, role: string}[]>;
/** 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: 👍, ❤️, 😂, 😮, 😢, 🙏
* 2 for NONE reactions to be avaliable
*/
setReactionSetting(reactionCode: number): Promise<boolean>;
/** Mutes the channel */
mute(): Promise<boolean>;
/** Unmutes the channel */
unmute(): Promise<boolean>;
/** Sends a message to this channel */
sendMessage(content: string|MessageMedia, options?: MessageSendChannelOptions): Promise<Message>;
/** Sends a channel admin invitation to a user, allowing them to become an admin of the channel */
sendChannelAdminInvite(chatId: string, options?: { comment?: string }): Promise<boolean>;
/** Accepts a channel admin invitation and promotes the current user to a channel admin */
acceptChannelAdminInvite(): Promise<boolean>;
/** Revokes a channel admin invitation sent to a user by a channel owner */
revokeChannelAdminInvite(userId: string): Promise<boolean>;
/** Demotes a channel admin to a regular subscriber (can be used also for self-demotion) */
demoteChannelAdmin(userId: string): Promise<boolean>;
/** Loads channel messages, sorted from earliest to latest */
fetchMessages: (searchOptions: MessageSearchOptions) => Promise<Message[]>;
/** Deletes the channel you created */
deleteChannel(): Promise<boolean>;
}

/** Options for sending a message */
export interface MessageSendChannelOptions {
/** Image or videos caption */
caption?: string
/** User IDs of user that will be mentioned in the message */
mentions?: string[]
/** Image or video to be sent */
media?: MessageMedia
/** Extra options */
extra?: any
}

export interface MessageSearchOptions {
/**
* The amount of messages to return. If no limit is specified, the available messages will be returned.
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = {
Chat: require('./src/structures/Chat'),
PrivateChat: require('./src/structures/PrivateChat'),
GroupChat: require('./src/structures/GroupChat'),
Channel: require('./src/structures/Channel'),
Message: require('./src/structures/Message'),
MessageMedia: require('./src/structures/MessageMedia'),
Contact: require('./src/structures/Contact'),
Expand Down