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(video): add rules module to sendNotification() method & push video #881

Merged
merged 6 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions packages/examples/sdk-backend-node/chat/chat.lowlevel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { config } from '../config';
import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts';
import { createWalletClient, http } from 'viem';
import { sepolia } from 'viem/chains';
import { VIDEO_NOTIFICATION_ACCESS_TYPE } from '@pushprotocol/restapi/src/lib/payloads/constants';

// CONFIGS
const { env, showAPIResponse } = config;
Expand Down Expand Up @@ -632,12 +633,17 @@ async function PushAPI_chat_video_call_notification(
encryptedPGPPrivateKey: user.encryptedPrivateKey,
signer: signer,
});
// get PGP KEy

const apiResponse = await PushAPI.payloads.sendNotification({
senderType: 1,
signer: signer,
signer,
pgpPrivateKey: pgpDecrpyptedPvtKey,
chatId: chatId,
rules:{
access:{
type: VIDEO_NOTIFICATION_ACCESS_TYPE.PUSH_CHAT,
data: chatId
}
},
type: 3, // target
identityType: 2, // direct payload
notification: {
Expand All @@ -651,7 +657,7 @@ async function PushAPI_chat_video_call_notification(
img: '',
additionalMeta: {
type: '1+1',
data: 'Random DATA',
data: "DATA REQUIRED FOR VIDEO CALL",
domain: 'push.org',
},
},
Expand Down
32 changes: 26 additions & 6 deletions packages/examples/sdk-frontend/pages/video.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import styled from 'styled-components';
import { usePushSocket } from '../hooks/usePushSocket';
import { useEffect, useRef, useState } from 'react';
import VideoPlayer from '../components/VideoPlayer';
import { ADDITIONAL_META_TYPE } from '@pushprotocol/restapi/src/lib/payloads/constants';
import { ADDITIONAL_META_TYPE, VIDEO_NOTIFICATION_ACCESS_TYPE } from '@pushprotocol/restapi/src/lib/payloads/constants';

interface VideoCallMetaDataType {
recipientAddress: string;
Expand All @@ -20,7 +20,7 @@ interface VideoCallMetaDataType {
}

// env which will be used for the video call
const env = ENV.STAGING;
const env = ENV.DEV;

const Home: NextPage = () => {
const { address, isConnected } = useAccount();
Expand Down Expand Up @@ -110,7 +110,12 @@ const Home: NextPage = () => {
signalData: data.meta.initiator.signal,
senderAddress: data.local.address,
recipientAddress: data.incoming[0].address,
chatId: data.meta.chatId,
rules: {
access: {
type: VIDEO_NOTIFICATION_ACCESS_TYPE.PUSH_CHAT,
data: data.meta.chatId
}
}
});
};

Expand Down Expand Up @@ -165,7 +170,12 @@ const Home: NextPage = () => {
await videoObjectRef.current?.request({
senderAddress: data.local.address,
recipientAddress: data.incoming[0].address,
chatId: data.meta.chatId,
rules: {
access: {
type: VIDEO_NOTIFICATION_ACCESS_TYPE.PUSH_CHAT,
data: data.meta.chatId
}
}
});
}
})();
Expand Down Expand Up @@ -218,7 +228,12 @@ const Home: NextPage = () => {
videoObjectRef.current?.request({
senderAddress: data.local.address,
recipientAddress: data.incoming[0].address,
chatId: data.meta.chatId,
rules: {
access: {
type: VIDEO_NOTIFICATION_ACCESS_TYPE.PUSH_CHAT,
data: data.meta.chatId
}
},
retry: true,
});
} else if (
Expand All @@ -229,7 +244,12 @@ const Home: NextPage = () => {
signalData: videoCallMetaData.signalingData,
senderAddress: data.local.address,
recipientAddress: data.incoming[0].address,
chatId: data.meta.chatId,
rules: {
access: {
type: VIDEO_NOTIFICATION_ACCESS_TYPE.PUSH_CHAT,
data: data.meta.chatId
}
},
retry: true,
});
}
Expand Down
4 changes: 4 additions & 0 deletions packages/restapi/src/lib/payloads/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,7 @@ export enum SPACE_ROLES {
}

export const DEFAULT_DOMAIN = 'push.org';

export enum VIDEO_NOTIFICATION_ACCESS_TYPE {
PUSH_CHAT = 'PUSH_CHAT',
}
3 changes: 3 additions & 0 deletions packages/restapi/src/lib/payloads/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
ISendNotificationInputOptions,
INotificationPayload,
walletType,
VideNotificationRules,
} from '../types';
import {
IDENTITY_TYPE,
Expand Down Expand Up @@ -194,6 +195,7 @@ export async function getVerificationProof({
wallet,
pgpPrivateKey,
env,
rules
}: {
senderType: 0 | 1;
signer: any;
Expand All @@ -210,6 +212,7 @@ export async function getVerificationProof({
wallet?: walletType;
pgpPrivateKey?: string;
env?: ENV;
rules?:VideNotificationRules;
}) {
let message = null;
let verificationProof = null;
Expand Down
44 changes: 42 additions & 2 deletions packages/restapi/src/lib/payloads/sendNotifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ import {
DEFAULT_DOMAIN,
NOTIFICATION_TYPE,
SOURCE_TYPES,
VIDEO_CALL_TYPE,
VIDEO_NOTIFICATION_ACCESS_TYPE,
} from './constants';
import { ENV } from '../constants';
import { getChannel } from '../channels/getChannel';
/**
* Validate options for some scenarios
*/
function validateOptions(options: any) {
function validateOptions(options: ISendNotificationInputOptions) {
if (!options?.channel) {
throw '[Push SDK] - Error - sendNotification() - "channel" is mandatory!';
}
Expand Down Expand Up @@ -56,6 +58,26 @@ function validateOptions(options: any) {
throw '[Push SDK] - Error - sendNotification() - "payload" mandatory for Identity Type: Direct Payload, Minimal!';
}
}

const isAdditionalMetaPayload = options.payload?.additionalMeta;

const isVideoOrSpaceType =
typeof options.payload?.additionalMeta === 'object' &&
(options.payload.additionalMeta.type ===
`${VIDEO_CALL_TYPE.PUSH_VIDEO}+1` ||
options.payload.additionalMeta.type ===
`${VIDEO_CALL_TYPE.PUSH_SPACE}+1`);

if (
isAdditionalMetaPayload &&
isVideoOrSpaceType &&
!options.chatId &&
!options.rules
) {
throw new Error(
'[Push SDK] - Error - sendNotification() - Either chatId or rules object is required to send a additional meta notification for video or spaces'
);
}
}

/**
Expand Down Expand Up @@ -111,6 +133,7 @@ export async function sendNotification(options: ISendNotificationInputOptions) {
ipfsHash,
env = ENV.PROD,
chatId,
rules,
pgpPrivateKey,
} = options || {};

Expand Down Expand Up @@ -161,7 +184,9 @@ export async function sendNotification(options: ISendNotificationInputOptions) {
ipfsHash,
uuid,
// for the pgpv2 verfication proof
chatId,
chatId:
rules?.access.data ?? // for backwards compatibilty with 'chatId' param
chatId,
pgpPrivateKey,
});

Expand Down Expand Up @@ -197,6 +222,21 @@ export async function sendNotification(options: ISendNotificationInputOptions) {
recipients: recipients || '',
channel: _channelAddress,
}),
/*
- If 'rules' is not provided, check if 'chatId' is available.
- If 'chatId' is available, create a new 'rules' object for backwards compatibility.
- If neither 'rules' nor 'chatId' is available, do not include 'rules' in the payload.
*/
...(rules || chatId
? {
rules: rules ?? {
access: {
data: chatId,
type: VIDEO_NOTIFICATION_ACCESS_TYPE.PUSH_CHAT,
},
},
}
: {}),
};

const requestURL = `${API_BASE_URL}/v1/payloads/`;
Expand Down
25 changes: 19 additions & 6 deletions packages/restapi/src/lib/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
SPACE_DISCONNECT_TYPE,
SPACE_INVITE_ROLES,
SPACE_REQUEST_TYPE,
VIDEO_NOTIFICATION_ACCESS_TYPE,
} from '../../lib/payloads/constants';
import { ENV, MessageType } from '../constants';
import { EthEncryptedData } from '@metamask/eth-sig-util';
Expand Down Expand Up @@ -82,6 +83,16 @@ export type ParsedResponseType = {
};
};

export interface VideNotificationRules {
access: {
type: VIDEO_NOTIFICATION_ACCESS_TYPE;
data: string;
};
}

// SendNotificationRules can be extended in the future for other use cases
export type SendNotificationRules = VideNotificationRules;

export interface ISendNotificationInputOptions {
senderType?: 0 | 1;
signer: any;
Expand Down Expand Up @@ -136,7 +147,9 @@ export interface ISendNotificationInputOptions {
};
ipfsHash?: string;
env?: ENV;
/** @deprecated - Use `rules` object instead */
chatId?: string;
rules?: SendNotificationRules;
pgpPrivateKey?: string;
}

Expand Down Expand Up @@ -327,7 +340,6 @@ export enum GROUP_RULES_SUB_CATEGORY {
GET = 'GET',
}


export enum GROUP_RULES_PERMISSION {
ENTRY = 'Entry',
CHAT = 'Chat',
Expand All @@ -354,7 +366,6 @@ export type ConditionBase = {
subcategory?: string;
data?: Data;
access?: boolean;

};

export type Condition = ConditionBase & {
Expand All @@ -371,7 +382,6 @@ export interface Rules {
};
}


export interface SpaceRules {
entry?: {
conditions: Array<Condition | ConditionBase> | (Condition | ConditionBase);
Expand Down Expand Up @@ -600,7 +610,6 @@ export type viemSignerType = {
account: { [key: string]: any };
privateKey?: string;
provider?: providers.Provider;

};

export type SignerType = ethersV5SignerType | viemSignerType;
Expand Down Expand Up @@ -723,7 +732,9 @@ export type VideoCreateInputOptions = {
export type VideoRequestInputOptions = {
senderAddress: string;
recipientAddress: string | string[];
chatId: string;
/** @deprecated - Use `rules` object instead */
chatId?: string;
rules?: VideNotificationRules;
onReceiveMessage?: (message: string) => void;
retry?: boolean;
details?: {
Expand All @@ -736,7 +747,9 @@ export type VideoAcceptRequestInputOptions = {
signalData: any;
senderAddress: string;
recipientAddress: string;
chatId: string;
/** @deprecated - Use `rules` object instead */
chatId?: string;
rules?: VideNotificationRules;
onReceiveMessage?: (message: string) => void;
retry?: boolean;
details?: {
Expand Down
Loading