softwarebyze / DFIA

0 stars 0 forks source link

Connecting user with angel #6

Open softwarebyze opened 2 months ago

softwarebyze commented 2 months ago
    • [ ] set up user roles. the roles we need for now are 'user' and 'angel'

link

    • [ ] user initiates a call with unique call id
    • [ ] notify available angels
    • [ ] create a channel for angels
// Create a channel for angels if it doesn't exist
const channel = client.channel('messaging', 'angels', {
  name: 'Angels Channel',
  members: ['angel1', 'angel2', 'angel3'], // IDs of angel users
});

await channel.create();
    • [ ] Send a message to the Angels Channel
// Send a message with call details
await channel.sendMessage({
  text: `${user.name} is requesting help.`,
  attachments: [
    {
      type: 'call',
      callId: callId,
    },
  ],
});
    • [ ] Angel receives notification
      
      // In the angel's app
      const channel = client.channel('messaging', 'angels');

await channel.watch();

// Listen for new messages channel.on('message.new', async (event) => { const message = event.message; const callAttachment = message.attachments.find((att) => att.type === 'call');

if (callAttachment) { // Display a prompt to accept the call showIncomingCallPrompt(callAttachment.callId, message.user); } });


`showIncomingCallPrompt`
```ts
const showIncomingCallPrompt = (callId, user) => {
  Alert.alert(
    'Incoming Call',
    `${user.name} is requesting help.`,
    [
      {
        text: 'Ignore',
        style: 'cancel',
      },
      {
        text: 'Accept',
        onPress: () => acceptCall(callId),
      },
    ],
    { cancelable: false }
  );
};
    • [ ] Angel Accepts and Joins call
      
      const acceptCall = async (callId) => {
      const client = useStreamVideoClient();

    const call = client.call('default', callId);

    // Join the call await call.join();

    // Navigate to the call screen navigation.navigate('CallScreen', { callId }); };

softwarebyze commented 2 months ago

Example Code Adjustments

CallButton.tsx:

export const CallButton = (props: { onPress: (callId: string) => void }) => {
  const onPress = async () => {
    const isSubscribed = await presentProPaywall();
    if (isSubscribed || __DEV__) {
      // Create the call and notify angels
      const callId = await createCallAndNotifyAngels();
      props.onPress(callId);
      analytics.track('call_an_angel', {
        call_id: callId,
      });
    }
  };
  // ...rest of the component
};

const createCallAndNotifyAngels = async () => {
  const client = useStreamVideoClient();
  const callId = `help-${Date.now()}`;
  const call = client.call('default', callId);

  await call.create();
  await call.join();

  // Notify angels
  const channel = client.channel('messaging', 'angels');
  await channel.create();
  await channel.sendMessage({
    text: `${user.name} is requesting help.`,
    attachments: [
      {
        type: 'call',
        callId: callId,
      },
    ],
  });

  return callId;
};

AngelsScreen.tsx:


import React, { useEffect } from 'react';
import { Alert } from 'react-native';
import { useStreamChatClient } from 'stream-chat-react-native';

export const AngelsScreen = () => {
  const client = useStreamChatClient();

  useEffect(() => {
    const setupChannel = async () => {
      const channel = client.channel('messaging', 'angels');
      await channel.watch();

      channel.on('message.new', (event) => {
        const message = event.message;
        const callAttachment = message.attachments.find((att) => att.type === 'call');

        if (callAttachment) {
          showIncomingCallPrompt(callAttachment.callId, message.user);
        }
      });
    };

    setupChannel();
  }, []);

  const showIncomingCallPrompt = (callId, user) => {
    Alert.alert(
      'Incoming Call',
      `${user.name} is requesting help.`,
      [
        {
          text: 'Ignore',
          style: 'cancel',
        },
        {
          text: 'Accept',
          onPress: () => acceptCall(callId),
        },
      ],
      { cancelable: false }
    );
  };

  const acceptCall = async (callId) => {
    const client = useStreamVideoClient();
    const call = client.call('default', callId);
    await call.join();

    // Navigate to CallScreen
    navigation.navigate('CallScreen', { callId });
  };

  return null; // Or a UI indicating that the angel is available
};