Skip to content

Commit

Permalink
fixed delete msg bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Bhupesh-mfsi authored and hai-ko committed Nov 8, 2023
1 parent 6a4dd39 commit 83e7ee3
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 60 deletions.
185 changes: 127 additions & 58 deletions packages/messenger-widget/src/components/Chat/bl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export const handleMessages = async (
updateShowShimEffect: Function,
) => {
if (!isMessageListInitialized && state.accounts.selectedContact) {
await fetchAndStoreMessages(
const items = await fetchAndStoreMessages(
state.connection,
state.auth.currentSession?.token!,
state.accounts.selectedContact.account.ensName,
Expand All @@ -234,74 +234,143 @@ export const handleMessages = async (
? state.accounts.contacts.map((contact) => contact.account)
: [],
);
}

const checkedContainers = containers.filter((container) => {
if (!state.accounts.selectedContact) {
throw Error('No selected contact');
}
const checkedContainers = items.filter((container) => {
if (!state.accounts.selectedContact) {
throw Error('No selected contact');
}

const account = isSameEnsName(
container.envelop.message.metadata.from,
state.accounts.selectedContact.account.ensName,
const account = isSameEnsName(
container.envelop.message.metadata.from,
state.accounts.selectedContact.account.ensName,
alias,
)
? state.accounts.selectedContact.account
: state.connection.account!;

return account.profile?.publicSigningKey
? checkSignature(
container.envelop.message,
account.profile?.publicSigningKey,
account.ensName,
container.envelop.message.signature,
)
: true;
});

const newMessages = checkedContainers
.filter((container) => container.messageState === MessageState.Send)
.map((container) => ({
...container,
messageState: MessageState.Read,
}));

const oldMessages = checkedContainers.filter(
(container) =>
container.messageState === MessageState.Read ||
container.messageState === MessageState.Created,
);

handleMessageContainer(
state,
oldMessages,
alias,
)
? state.accounts.selectedContact.account
: state.connection.account!;
setListOfMessages,
dispatch,
);

return account.profile?.publicSigningKey
? checkSignature(
container.envelop.message,
account.profile?.publicSigningKey,
account.ensName,
container.envelop.message.signature,
)
: true;
});
if (!state.userDb) {
throw Error(
`[handleMessages] Couldn't handle new messages. User db not created.`,
);
}

const newMessages = checkedContainers
.filter((container) => container.messageState === MessageState.Send)
.map((container) => ({
...container,
messageState: MessageState.Read,
}));
if (newMessages.length > 0) {
newMessages.forEach((message) =>
dispatch({
type: UserDbType.addMessage,
payload: {
container: message,
connection: state.connection,
},
}),
);
}

const oldMessages = checkedContainers.filter(
(container) =>
container.messageState === MessageState.Read ||
container.messageState === MessageState.Created,
);
if (!isMessageListInitialized) {
scrollToBottomOfChat();
updateIsMessageListInitialized(true);
}

handleMessageContainer(
state,
oldMessages,
alias,
setListOfMessages,
dispatch,
);
updateShowShimEffect(false);
} else {
const checkedContainers = containers.filter((container) => {
if (!state.accounts.selectedContact) {
throw Error('No selected contact');
}

if (!state.userDb) {
throw Error(
`[handleMessages] Couldn't handle new messages. User db not created.`,
const account = isSameEnsName(
container.envelop.message.metadata.from,
state.accounts.selectedContact.account.ensName,
alias,
)
? state.accounts.selectedContact.account
: state.connection.account!;

return account.profile?.publicSigningKey
? checkSignature(
container.envelop.message,
account.profile?.publicSigningKey,
account.ensName,
container.envelop.message.signature,
)
: true;
});

const newMessages = checkedContainers
.filter((container) => container.messageState === MessageState.Send)
.map((container) => ({
...container,
messageState: MessageState.Read,
}));

const oldMessages = checkedContainers.filter(
(container) =>
container.messageState === MessageState.Read ||
container.messageState === MessageState.Created,
);
}

if (newMessages.length > 0) {
newMessages.forEach((message) =>
dispatch({
type: UserDbType.addMessage,
payload: {
container: message,
connection: state.connection,
},
}),
handleMessageContainer(
state,
oldMessages,
alias,
setListOfMessages,
dispatch,
);
}

if (!isMessageListInitialized) {
scrollToBottomOfChat();
updateIsMessageListInitialized(true);
}
if (!state.userDb) {
throw Error(
`[handleMessages] Couldn't handle new messages. User db not created.`,
);
}

if (newMessages.length > 0) {
newMessages.forEach((message) =>
dispatch({
type: UserDbType.addMessage,
payload: {
container: message,
connection: state.connection,
},
}),
);
}

updateShowShimEffect(false);
if (!isMessageListInitialized) {
scrollToBottomOfChat();
updateIsMessageListInitialized(true);
}

updateShowShimEffect(false);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ export function Contacts(props: DashboardProps) {
</div>

{state.cache.contacts &&
index !== contactSelected &&
state.cache.contacts[index] &&
state.cache.contacts[index]
.unreadMsgCount > 0 && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export default function DeleteMessage() {

dispatch({
type: ModalStateType.LastMessageAction,
payload: MessageActionType.EDIT,
payload: MessageActionType.NONE,
});
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ import {
UiViewStateType,
} from '../../utils/enum-type-utils';
import { ConnectButton } from '@rainbow-me/rainbowkit';
import { globalConfig } from 'dm3-lib-shared';
import { hasUserProfile } from 'dm3-lib-profile';

export function RightHeader() {
// fetches context storage
const { state, dispatch } = useContext(GlobalContext);

// state to store profile pic of signed in user
const [profilePic, setProfilePic] = useState<string>('');
const [displayName, setDisplayName] = useState<string>('');

// method to fetch profile pic
const fetchAndSetProfilePic = async () => {
Expand Down Expand Up @@ -46,11 +49,57 @@ export function RightHeader() {
});
};

const fetchDisplayName = async () => {
try {
if (
state.connection.provider &&
state.connection.ethAddress &&
state.connection.account
) {
const isAddrEnsName =
state.connection.account?.ensName?.endsWith(
globalConfig.ADDR_ENS_SUBDOMAIN(),
);
const name = await state.connection.provider.lookupAddress(
state.connection.ethAddress,
);
if (name && !isAddrEnsName) {
const hasProfile = await hasUserProfile(
state.connection.provider,
name,
);
setDisplayName(
hasProfile ? name : state.connection.account?.ensName,
);
} else {
return setDisplayName(state.connection.account.ensName);
}
} else {
return setDisplayName(
state.connection.account
? state.connection.account.ensName
: '',
);
}
} catch (error) {
return setDisplayName(
state.connection.account
? state.connection.account.ensName
: '',
);
}
};

// loads the profile pic on page render
useEffect(() => {
fetchAndSetProfilePic();
fetchDisplayName();
}, []);

useEffect(() => {
fetchDisplayName();
}, [state.connection.account?.ensName]);

return (
<div
className={'col-12 d-flex align-items-center justify-content-end pr-0 profile-name-container'.concat(
Expand All @@ -67,7 +116,7 @@ export function RightHeader() {
onClick={() => updateView()}
className="profile-name font-weight-500 pointer-cursor text-secondary-color"
>
{state.connection.account?.ensName}
{displayName}
</span>
<img
src={profilePic ? profilePic : humanIcon}
Expand Down

0 comments on commit 83e7ee3

Please sign in to comment.