Skip to content

Commit

Permalink
fix: participant high level functions (#896)
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammeds1992 authored Nov 30, 2023
1 parent 6df8a6f commit 645d564
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 24 deletions.
23 changes: 18 additions & 5 deletions packages/restapi/src/lib/chat/getGroupMembers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from 'axios';
import { getAPIBaseUrls } from '../helpers';
import Constants, { ENV } from '../constants';
import {ChatMemberProfile } from '../types';
import { ChatMemberProfile } from '../types';

/**
* GET /v1/chat/:chatId/members
Expand All @@ -11,22 +11,36 @@ export interface FetchChatGroupInfoType {
chatId: string;
page?: number;
limit?: number;
pending?: boolean;
role?: string;
env?: ENV;
}

export const getGroupMembers = async (
options: FetchChatGroupInfoType
): Promise<ChatMemberProfile[]> => {
const { chatId, page = 1, limit = 20, env = Constants.ENV.PROD } = options;
const {
chatId,
page = 1,
limit = 20,
env = Constants.ENV.PROD,
pending,
role,
} = options;

try {
if (!chatId) {
throw new Error('Chat ID is required.');
}

const API_BASE_URL = getAPIBaseUrls(env);
const requestUrl = `${API_BASE_URL}/v1/chat/groups/${chatId}/members?pageNumber=${page}&pageSize=${limit}`;

let requestUrl = `${API_BASE_URL}/v1/chat/groups/${chatId}/members?pageNumber=${page}&pageSize=${limit}`;
if (pending !== undefined) {
requestUrl += `&pending=${pending}`;
}
if (role) {
requestUrl += `&role=${encodeURIComponent(role)}`;
}
const response = await axios.get(requestUrl);
return response.data.members;
} catch (error) {
Expand All @@ -39,4 +53,3 @@ export const getGroupMembers = async (
);
}
};

65 changes: 48 additions & 17 deletions packages/restapi/src/lib/pushapi/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
GroupInfoDTO,
ChatMemberProfile,
ChatMemberCounts,
GroupParticipantCounts,
} from '../types';
import {
GroupUpdateOptions,
Expand All @@ -21,6 +22,7 @@ import {
ManageGroupOptions,
RemoveFromGroupOptions,
GetGroupParticipantsOptions,
ParticipantStatus,
} from './pushAPITypes';
import * as PUSH_USER from '../user';
import * as PUSH_CHAT from '../chat';
Expand All @@ -33,6 +35,8 @@ import {
import { User } from './user';
import { updateGroupConfig } from '../chat/updateGroupConfig';
import { PushAPI } from './PushAPI';
import { ParticipantsType } from './participant';

export class Chat {
private userInstance: User;
private scalabilityV2Feature: boolean;
Expand Down Expand Up @@ -317,23 +321,50 @@ export class Chat {
}
},

participants: async (
chatId: string,
options?: GetGroupParticipantsOptions
): Promise<{ count: ChatMemberCounts; members: ChatMemberProfile[] }> => {
const { page = 1, limit = 20 } = options ?? {};
const getGroupMembersOptions: PUSH_CHAT.FetchChatGroupInfoType = {
chatId,
page,
limit,
env: this.env,
};
const count = await PUSH_CHAT.getGroupMemberCount({
chatId,
env: this.env,
});
const members = await PUSH_CHAT.getGroupMembers(getGroupMembersOptions);
return { count, members };
participants: {
list: async (
chatId: string,
options?: GetGroupParticipantsOptions
): Promise<{ members: ChatMemberProfile[] }> => {
const { page = 1, limit = 20 } = options ?? {};
const getGroupMembersOptions: PUSH_CHAT.FetchChatGroupInfoType = {
chatId,
page,
limit,
env: this.env,
};

const members = await PUSH_CHAT.getGroupMembers(getGroupMembersOptions);
return { members };
},

count: async (chatId: string): Promise<GroupParticipantCounts> => {
const count = await PUSH_CHAT.getGroupMemberCount({
chatId,
env: this.env,
});
return {
participants: count.overallCount - count.pendingCount,
pending: count.pendingCount,
};
},

status: async (
chatId: string,
accountId: string
): Promise<ParticipantStatus> => {
const status = await PUSH_CHAT.getGroupMemberStatus({
chatId: chatId,
did: accountId,
env: this.env,
});

return {
pending: status.isPending,
role: status.isAdmin ? 'ADMIN' : 'MEMBER',
participant: status.isMember,
};
},
},

permissions: async (chatId: string): Promise<GroupAccess> => {
Expand Down
12 changes: 11 additions & 1 deletion packages/restapi/src/lib/pushapi/pushAPITypes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Constants, { ENV } from '../constants';
import { ChatStatus, ProgressHookType, Rules } from '../types';
import { ChatMemberCounts, ChatMemberProfile, ChatStatus, ProgressHookType, Rules } from '../types';

export enum ChatListType {
CHATS = 'CHATS',
Expand Down Expand Up @@ -40,6 +40,10 @@ export interface RemoveFromGroupOptions {
export interface GetGroupParticipantsOptions {
page?: number;
limit?: number;
filter?: {
pending?: boolean;
role?: string;
};
}

export interface GroupUpdateOptions {
Expand All @@ -57,3 +61,9 @@ export interface InfoOptions {
overrideAccount?: string;
}


export interface ParticipantStatus {
pending: boolean;
role: 'ADMIN' | 'MEMBER';
participant: boolean;
}
14 changes: 14 additions & 0 deletions packages/restapi/src/lib/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,12 +394,26 @@ export interface SpaceAccess {
rules?: SpaceRules;
}

export interface RoleCounts {
total: number;
pending: number;
}

export interface ChatMemberCounts {
overallCount: number;
adminsCount: number;
membersCount: number;
pendingCount: number;
approvedCount: number;
roles: {
ADMIN: RoleCounts;
MEMBER: RoleCounts;
};
}

export interface GroupParticipantCounts {
participants: number;
pending: number;
}

export interface ChatMemberProfile {
Expand Down
2 changes: 1 addition & 1 deletion packages/restapi/tests/lib/notification/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ enum ENV {
*/
LOCAL = 'local',
}
describe.only('test', () => {
describe('test', () => {
// const signer = createWalletClient({
// account: privateKeyToAccount(`0x${process.env['WALLET_PRIVATE_KEY']}`),
// chain: goerli,
Expand Down

0 comments on commit 645d564

Please sign in to comment.