[core] GOWS Channels

This commit is contained in:
devlikepro committed 2025-01-21 11:57:26 +07:00
1 parent ed0b406027
commit 851c7b75f9
1 file changed
+127 -4
+127 -4
View File
@@ -1,4 +1,5 @@
import {
getUrlFromDirectPath,
isJidGroup,
isJidStatusBroadcast,
jidNormalizedUser,
@@ -6,7 +7,11 @@ import {
import * as grpc from '@grpc/grpc-js';
import { connectivityState } from '@grpc/grpc-js';
import { UnprocessableEntityException } from '@nestjs/common';
import { WhatsappSession } from '@waha/core/abc/session.abc';
import {
getChannelInviteLink,
WhatsappSession,
} from '@waha/core/abc/session.abc';
import { Jid } from '@waha/core/engines/const';
import { EventsFromObservable } from '@waha/core/engines/gows/EventsFromObservable';
import { GowsEventStreamObservable } from '@waha/core/engines/gows/GowsEventStreamObservable';
import { messages } from '@waha/core/engines/gows/grpc/gows';
@@ -16,9 +21,18 @@ import {
toCusFormat,
toJID,
} from '@waha/core/engines/noweb/session.noweb.core';
import { AvailableInPlusVersion } from '@waha/core/exceptions';
import {
AvailableInPlusVersion,
NotImplementedByEngineError,
} from '@waha/core/exceptions';
import { IMediaEngineProcessor } from '@waha/core/media/IMediaEngineProcessor';
import { QR } from '@waha/core/QR';
import {
Channel,
ChannelRole,
CreateChannelRequest,
ListChannelsQuery,
} from '@waha/structures/channels.dto';
import {
ChatRequest,
CheckNumberStatusQuery,
@@ -46,6 +60,7 @@ import {
} from '@waha/structures/presence.dto';
import { WAMessage, WAMessageReaction } from '@waha/structures/responses.dto';
import { MeInfo, ProxyConfig } from '@waha/structures/sessions.dto';
import { StatusRequest, TextStatus } from '@waha/structures/status.dto';
import { EnginePayload, WAMessageAckBody } from '@waha/structures/webhooks.dto';
import { sleep, waitUntil } from '@waha/utils/promiseTimeout';
import { onlyEvent } from '@waha/utils/reactive/ops/onlyEvent';
@@ -66,8 +81,6 @@ import { promisify } from 'util';
import * as gows from './types';
import MessageServiceClient = messages.MessageServiceClient;
import { Jid } from '@waha/core/engines/const';
import { StatusRequest, TextStatus } from '@waha/structures/status.dto';
enum WhatsMeowEvent {
CONNECTED = 'gows.ConnectedEventData',
@@ -652,6 +665,116 @@ export class WhatsappSessionGoWSCore extends WhatsappSession {
};
}
/**
* Channels methods
*/
protected toChannel(newsletter: messages.Newsletter): Channel {
const role: ChannelRole =
(newsletter.role?.toUpperCase() as ChannelRole) || ChannelRole.GUEST;
let picture = newsletter.picture;
if (picture.startsWith('/')) {
picture = getUrlFromDirectPath(picture);
}
let preview = newsletter.preview;
if (preview.startsWith('/')) {
preview = getUrlFromDirectPath(preview);
}
return {
id: newsletter.id,
name: newsletter.name,
description: newsletter.description,
invite: getChannelInviteLink(newsletter.invite),
picture: picture,
preview: preview,
verified: newsletter.verified,
role: role,
};
}
public async channelsList(query: ListChannelsQuery): Promise<Channel[]> {
const request = new messages.NewsletterListRequest({
session: this.session,
});
const response = await promisify(this.client.GetSubscribedNewsletters)(
request,
);
const data = response.toObject();
const newsletters = data.newsletters;
let channels: Channel[] = newsletters.map(this.toChannel.bind(this));
if (query.role) {
// @ts-ignore
channels = channels.filter((channel) => channel.role === query.role);
}
return channels;
}
public async channelsCreateChannel(
request: CreateChannelRequest,
): Promise<Channel> {
const req = new messages.CreateNewsletterRequest({
session: this.session,
name: request.name,
description: request.description,
});
const response = await promisify(this.client.CreateNewsletter)(req);
const newsletter = response.toObject() as messages.Newsletter;
return this.toChannel(newsletter);
}
public async channelsGetChannel(id: string): Promise<Channel> {
return await this.channelsGetChannelByInviteCode(id);
}
public async channelsGetChannelByInviteCode(
inviteCode: string,
): Promise<Channel> {
const request = new messages.NewsletterInfoRequest({
session: this.session,
id: inviteCode,
});
const response = await promisify(this.client.GetNewsletterInfo)(request);
const newsletter = response.toObject() as messages.Newsletter;
return this.toChannel(newsletter);
}
public channelsFollowChannel(id: string): Promise<any> {
return this.channelsToggleFollow(id, true);
}
public channelsUnfollowChannel(id: string): Promise<any> {
return this.channelsToggleFollow(id, false);
}
protected async channelsToggleFollow(id: string, follow: boolean) {
const request = new messages.NewsletterToggleFollowRequest({
session: this.session,
jid: id,
follow: follow,
});
const response = await promisify(this.client.NewsletterToggleFollow)(
request,
);
return response.toObject();
}
public channelsMuteChannel(id: string): Promise<void> {
return this.channelsToggleMute(id, true);
}
public channelsUnmuteChannel(id: string): Promise<void> {
return this.channelsToggleMute(id, false);
}
protected async channelsToggleMute(id: string, mute: boolean): Promise<any> {
const request = new messages.NewsletterToggleMuteRequest({
session: this.session,
jid: id,
mute: mute,
});
const response = await promisify(this.client.NewsletterToggleMute)(request);
return response.toObject();
}
//
// END - Methods for API
//