Skip to content

Commit

Permalink
fix: stream changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammeds1992 committed Sep 22, 2023
1 parent 2357082 commit 421307d
Show file tree
Hide file tree
Showing 11 changed files with 425 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ const ChatTest = () => {
<Link to="/getGroupAccess" className="nav-button">
CHAT.GETGROUPACCESS
</Link>
<Link to="/getGroupMemberStatus" className="nav-button">
CHAT.GETGROUPMEMBERSTATUS
</Link>
<Link to="/searchGroups" className="nav-button">
CHAT.SEARCHGROUPS
</Link>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { useState, useContext } from 'react';
import {
Section,
SectionItem,
CodeFormatter,
SectionButton,
} from '../components/StyledComponents';
import Loader from '../components/Loader';
import { EnvContext } from '../context';
import * as PushAPI from '@pushprotocol/restapi';

const GetGroupMemberStatusTest = () => {
const { env } = useContext<any>(EnvContext);
const [isLoading, setLoading] = useState(false);
const [chatId, setChatId] = useState<string>('');
const [did, setDid] = useState<string>('');
const [sendResponse, setSendResponse] = useState<any>('');

const updateChatId = (e: React.SyntheticEvent<HTMLElement>) => {
setChatId((e.target as HTMLInputElement).value);
};

const updateDid = (e: React.SyntheticEvent<HTMLElement>) => {
setDid((e.target as HTMLInputElement).value);
};

const testGetGroupMemberStatus = async () => {
try {
setLoading(true);

const response = await PushAPI.chat.getGroupMemberStatus({
chatId: chatId,
did: did,
env,
});
setSendResponse(response);
} catch (e) {
console.error(e);
} finally {
setLoading(false);
}
};

return (
<div>
<h2>Get Group Member Status Test Page</h2>

<Loader show={isLoading} />

<Section>
<SectionItem>
<SectionButton onClick={testGetGroupMemberStatus}>get group member status</SectionButton>
</SectionItem>
<SectionItem>
<label>chatId</label>
<input
type="text"
onChange={updateChatId}
value={chatId}
style={{ width: 400, height: 30 }}
/>
</SectionItem>
<SectionItem>
<label>did</label>
<input
type="text"
onChange={updateDid}
value={did}
style={{ width: 400, height: 30 }}
/>
</SectionItem>
<SectionItem>
<div>
{sendResponse ? (
<CodeFormatter>
{JSON.stringify(sendResponse, null, 4)}
</CodeFormatter>
) : null}
</div>
</SectionItem>
</Section>
</div>
);
};

export default GetGroupMemberStatusTest;
3 changes: 3 additions & 0 deletions packages/examples/sdk-frontend-react/src/app/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ import { lightChatTheme } from '@pushprotocol/uiweb';
import SearchSpaceTest from './SpaceTest/SearchSpaceTest';
import SearchGroupTest from './ChatTest/SearchGroupTest';
import RejectRequestTest from './ChatTest/RejectRequestTest';
import GetGroupMemberStatusTest from './ChatTest/GetGroupMemberStatusTest';

window.Buffer = window.Buffer || Buffer;

Expand Down Expand Up @@ -454,6 +455,8 @@ export function App() {
<Route path="/createGroup" element={<CreateGroupTest />} />
<Route path="/getGroup" element={<GetGroupTest />} />
<Route path="/getGroupAccess" element={<GetGroupAccessTest />} />
<Route path="/getGroupMemberStatus" element={<GetGroupMemberStatusTest />} />

<Route
path="/addMembersToGroup"
element={<AddMembersToGroupTest />}
Expand Down
56 changes: 56 additions & 0 deletions packages/restapi/src/lib/chat/getGroupMemberStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import axios from 'axios';
import { getAPIBaseUrls } from '../helpers';
import Constants, { ENV } from '../constants';
import { GroupAccess, GroupMemberStatus } from '../types';
import { getUserDID } from './helpers';

/**
* GET /v1/chat/groups/:chatId/access/:did
*/

export interface GetGroupMemberStatusType {
chatId: string;
did: string; // Decentralized Identifier
env?: ENV;
}

export const getGroupMemberStatus = async (
options: GetGroupMemberStatusType
): Promise<GroupMemberStatus> => {
// Replace "any" with the actual response type
const { chatId, did, env = Constants.ENV.PROD } = options || {};
try {
if (chatId == null || chatId.length === 0) {
throw new Error(`chatId cannot be null or empty`);
}

if (did == null || did.length === 0) {
throw new Error(`did cannot be null or empty`);
}

const user = await getUserDID(did, env);

const API_BASE_URL = getAPIBaseUrls(env);
const requestUrl = `${API_BASE_URL}/v1/chat/groups/${chatId}/members/${user}/status`;

console.log(requestUrl)

return axios
.get(requestUrl)
.then((response) => {
return response.data;
})
.catch((err) => {
if (err?.response?.data) throw new Error(err?.response?.data);
throw new Error(err);
});
} catch (err) {
console.error(
`[Push SDK] - API - Error - API ${getGroupMemberStatus.name} -: `,
err
);
throw Error(
`[Push SDK] - API - Error - API ${getGroupMemberStatus.name} -: ${err}`
);
}
};
1 change: 1 addition & 0 deletions packages/restapi/src/lib/chat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ export * from './removeAdmins';
export * from './getGroupAccess';
export * from './searchGroups';
export * from './rejectRequest';
export * from './getGroupMemberStatus';
32 changes: 25 additions & 7 deletions packages/restapi/src/lib/pushapi/PushAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,6 @@ export class PushAPI {
chatId: string,
options: GroupUpdateOptions
): Promise<GroupDTO> => {
// Fetch Group Details
const group = await PUSH_CHAT.getGroup({
chatId: chatId,
env: this.env,
Expand Down Expand Up @@ -505,15 +504,34 @@ export class PushAPI {
}
},

// TODO: If invite was sent, the inside accept should be called here.
join: async (target: string): Promise<GroupDTO> => {
return await PUSH_CHAT.addMembers({
const status = await PUSH_CHAT.getGroupMemberStatus({
chatId: target,
did: this.account,
});

if (status.isPending) {
await PUSH_CHAT.approve({
senderAddress: target,
env: this.env,
account: this.account,
signer: this.signer,
pgpPrivateKey: this.decryptedPgpPvtKey,
});
} else if (!status.isMember) {
return await PUSH_CHAT.addMembers({
chatId: target,
members: [this.account],
env: this.env,
account: this.account,
signer: this.signer,
pgpPrivateKey: this.decryptedPgpPvtKey,
});
}

return await PUSH_CHAT.getGroup({
chatId: target,
members: [this.account],
env: this.env,
account: this.account,
signer: this.signer,
pgpPrivateKey: this.decryptedPgpPvtKey,
});
},

Expand Down
Loading

0 comments on commit 421307d

Please sign in to comment.