Skip to content

Commit

Permalink
feat(video): add rules module to sendNotification() method & push vid…
Browse files Browse the repository at this point in the history
…eo (#881)

* feat(video): add rules module to sendNotification() method & push video

* fix(sendNotification): remove rules object for non-chat notifications
  • Loading branch information
madhur-push authored Dec 18, 2023
1 parent e021be9 commit df0917b
Show file tree
Hide file tree
Showing 10 changed files with 321 additions and 39 deletions.
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
2 changes: 1 addition & 1 deletion packages/examples/sdk-frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"lint": "next lint"
},
"dependencies": {
"@pushprotocol/restapi": "^1.4.39",
"@pushprotocol/restapi": "^1.4.46",
"@pushprotocol/socket": "^0.5.1",
"@pushprotocol/uiweb": "^1.1.8",
"@rainbow-me/rainbowkit": "0.12.14",
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
22 changes: 19 additions & 3 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 @@ -328,7 +341,6 @@ export enum GROUP_RULES_SUB_CATEGORY {
GET = 'GET',
}


export enum GROUP_RULES_PERMISSION {
ENTRY = 'Entry',
CHAT = 'Chat',
Expand Down Expand Up @@ -795,7 +807,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 @@ -808,7 +822,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
11 changes: 9 additions & 2 deletions packages/restapi/src/lib/video/Video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ export class Video {
senderAddress,
recipientAddress,
chatId,
rules,
onReceiveMessage,
retry = false,
details,
Expand All @@ -180,7 +181,7 @@ export class Video {
this.setData((oldData) => {
return produce(oldData, (draft) => {
draft.local.address = senderAddress;
draft.meta.chatId = chatId;
draft.meta.chatId = chatId ?? rules!.access.data;
draft.meta.initiator.address = senderAddress;

const incomingIndex = getIncomingIndexFromAddress(
Expand Down Expand Up @@ -241,6 +242,7 @@ export class Video {
? VideoCallStatus.RETRY_INITIALIZED
: VideoCallStatus.INITIALIZED,
chatId,
rules,
signalData: data,
env: this.env,
callType: this.callType,
Expand Down Expand Up @@ -298,6 +300,7 @@ export class Video {
senderAddress,
recipientAddress: connectToAddresses,
chatId,
rules,
details: {
type: SPACE_REQUEST_TYPE.ESTABLISH_MESH,
data: {},
Expand Down Expand Up @@ -415,6 +418,7 @@ export class Video {
senderAddress,
recipientAddress,
chatId,
rules,
onReceiveMessage,
retry = false,
details,
Expand Down Expand Up @@ -444,7 +448,7 @@ export class Video {
this.setData((oldData) => {
return produce(oldData, (draft) => {
draft.local.address = senderAddress;
draft.meta.chatId = chatId;
draft.meta.chatId = chatId ?? rules!.access.data;
draft.meta.initiator.address = senderAddress;

const incomingIndex = getIncomingIndexFromAddress(
Expand Down Expand Up @@ -491,6 +495,7 @@ export class Video {
recipientAddress,
status: VideoCallStatus.RETRY_INITIALIZED,
chatId,
rules,
signalData: null,
callType: this.callType,
env: this.env,
Expand Down Expand Up @@ -520,6 +525,7 @@ export class Video {
? VideoCallStatus.RETRY_RECEIVED
: VideoCallStatus.RECEIVED,
chatId,
rules,
signalData: data,
env: this.env,
callType: this.callType,
Expand Down Expand Up @@ -586,6 +592,7 @@ export class Video {
senderAddress,
recipientAddress: connectToAddresses,
chatId,
rules,
details: {
type: SPACE_REQUEST_TYPE.ESTABLISH_MESH,
data: {},
Expand Down
Loading

0 comments on commit df0917b

Please sign in to comment.