Skip to content

Commit

Permalink
Added compatibility for new transfer history while keeping current st…
Browse files Browse the repository at this point in the history
…ate support;
  • Loading branch information
stef-coenen committed Jan 22, 2025
1 parent daf1bf9 commit 724a77b
Show file tree
Hide file tree
Showing 17 changed files with 305 additions and 146 deletions.
6 changes: 2 additions & 4 deletions packages/common/src/hooks/auth/useDotYouClientContext.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { ApiType, DotYouClient } from '@homebase-id/js-lib/core';
import { DotYouClient } from '@homebase-id/js-lib/core';
import { useContext, createContext } from 'react';

export const DotYouClientContext = createContext<DotYouClient | null>(
new DotYouClient({ api: ApiType.Guest })
);
export const DotYouClientContext = createContext<DotYouClient | null>(null);

export const useDotYouClientContext = () => {
const dotYouClient = useContext(DotYouClientContext);
Expand Down
15 changes: 6 additions & 9 deletions packages/mobile/src/components/Chat/Chat-Delivery-Indicator.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HomebaseFile } from '@homebase-id/js-lib/core';
import { HomebaseFile, RecipientTransferHistory } from '@homebase-id/js-lib/core';
import { ChatDeliveryStatus, ChatMessage } from '../../provider/chat/ChatProvider';
import { t, useDotYouClientContext } from 'homebase-id-app-common';
import { Pressable, StyleProp, Text, TextStyle, View, ViewStyle } from 'react-native';
Expand Down Expand Up @@ -32,17 +32,14 @@ export const ChatDeliveryIndicator = ({
};

export const FailedDeliveryDetails = ({
msg,
recipient,
transferHistory,
style,
}: {
msg: HomebaseFile<ChatMessage>;
recipient: string;
transferHistory: RecipientTransferHistory | undefined;
style?: StyleProp<TextStyle>;
}) => {
const deliveryDetails = msg.serverMetadata?.transferHistory?.recipients[recipient];
if (!deliveryDetails) return null;
if (deliveryDetails.latestSuccessfullyDeliveredVersionTag) return null;
if (!transferHistory) return null;
if (transferHistory.latestSuccessfullyDeliveredVersionTag) return null;

return (
<Text
Expand All @@ -53,7 +50,7 @@ export const FailedDeliveryDetails = ({
style,
]}
>
{t(deliveryDetails.latestTransferStatus)}
{t(transferHistory.latestTransferStatus)}
</Text>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,14 @@ export const ReportModal = forwardRef(
);
};
const identity = useAuth().getIdentity();
const host = new DotYouClient({
api: ApiType.Guest,
loggedInIdentity: identity || undefined,
hostIdentity: identity || undefined,
}).getRoot();
const host =
(identity &&
new DotYouClient({
api: ApiType.Guest,
loggedInIdentity: identity,
hostIdentity: identity,
}).getRoot()) ||
'';

const onBlock = () =>
openURL(`${host}/owner/connections/${message?.fileMetadata.originalAuthor}/block`);
Expand Down
28 changes: 25 additions & 3 deletions packages/mobile/src/components/Chat/Reactions/Modal/RetryModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import { Container } from '../../../ui/Container/Container';
import { StyleSheet } from 'react-native';
import { TouchableHighlight } from 'react-native-gesture-handler';
import { useChatMessage } from '../../../../hooks/chat/useChatMessage';
import { HomebaseFile } from '@homebase-id/js-lib/core';
import { HomebaseFile, RecipientTransferHistory } from '@homebase-id/js-lib/core';
import { ChatMessage } from '../../../../provider/chat/ChatProvider';
import { UnifiedConversation } from '../../../../provider/chat/ConversationProvider';
import { ChatDrive, UnifiedConversation } from '../../../../provider/chat/ConversationProvider';
import { ErrorNotification } from '../../../ui/Alert/ErrorNotification';
import { Backdrop } from '../../../ui/Modal/Backdrop';
import { useTransferHistory } from '../../../../hooks/file/useTransferHistory';

export const RetryModal = forwardRef(
(
{
Expand All @@ -32,7 +34,26 @@ export const RetryModal = forwardRef(
) => {
const { isDarkMode } = useDarkMode();
const { mutate, error } = useChatMessage().update;
const recipients = message?.serverMetadata?.transferHistory?.recipients;
const { data: transferHistory } = useTransferHistory(
message && {
fileId: message?.fileId,
targetDrive: ChatDrive,
}
).fetch;

const recipients =
(message?.serverMetadata?.transferHistory &&
'recipients' in message.serverMetadata.transferHistory &&
(message.serverMetadata.transferHistory as unknown as {
[key: string]: RecipientTransferHistory;
})) ||
transferHistory?.history.results.reduce(
(acc, curr) => {
acc[curr.recipient] = curr;
return acc;
},
{} as Record<string, RecipientTransferHistory>
);

const onRetry = useCallback(() => {
if (!message) return;
Expand Down Expand Up @@ -69,6 +90,7 @@ export const RetryModal = forwardRef(
return recipients[failedRecipient[0]].latestTransferStatus;
}
}, [failedRecipient, recipients]);

return (
<BottomSheetModal
ref={ref}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,11 @@ export const PostInteracts = memo(

const permalink = useMemo(
() =>
`${new DotYouClient({ hostIdentity: odinId || undefined, api: ApiType.Guest }).getRoot()}/posts/${postContent.channelId}/${
postContent.slug ?? postContent.id
}`,
odinId
? `${new DotYouClient({ hostIdentity: odinId, api: ApiType.Guest }).getRoot()}/posts/${postContent.channelId}/${
postContent.slug ?? postContent.id
}`
: '',
[odinId, postContent]
);

Expand Down
10 changes: 6 additions & 4 deletions packages/mobile/src/components/Feed/Meta/Actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ export const ExternalActions = memo(
getReportContentUrl,
} = useManageSocialFeed({ odinId });

const host = new DotYouClient({
api: ApiType.Guest,
loggedInIdentity: identity || undefined,
}).getRoot();
const host = identity
? new DotYouClient({
api: ApiType.Guest,
hostIdentity: identity,
}).getRoot()
: '';
const options: ActionGroupProps[] = [
{
icon: <Users />,
Expand Down
4 changes: 0 additions & 4 deletions packages/mobile/src/components/ui/Media/BoringFile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ export const BoringFile = memo(

const isPending = 'pendingFile' in file;

const progressPercentage = Math.round(
((file as NewPayloadDescriptor)?.uploadProgress?.progress || 0) * 100
);

const openDocument = useCallback(
async (payload?: OdinBlob) => {
if (!blob && !payload) {
Expand Down
1 change: 1 addition & 0 deletions packages/mobile/src/hooks/auth/useAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ export const useAuth = () => {
if (!sharedSecret || !identity || !authToken) {
return new DotYouClient({
api: ApiType.App,
hostIdentity: '',
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const getSynchronousDotYouClient = async () => {
if (!sharedSecret || !identity) {
return new DotYouClient({
api: ApiType.App,
hostIdentity: '',
});
}

Expand Down
21 changes: 13 additions & 8 deletions packages/mobile/src/hooks/chat/useLiveChatProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,14 @@ const useChatWebsocket = (isEnabled: boolean) => {
fileMetadata: {
...m.fileMetadata,
updated:
Object.values(m.serverMetadata?.transferHistory?.recipients || []).reduce((acc, cur) => {
return Math.max(acc, cur.lastUpdated || 0);
}, 0) ||
(m.serverMetadata?.transferHistory &&
'recipients' in m.serverMetadata.transferHistory &&
Object.values(m.serverMetadata?.transferHistory?.recipients || []).reduce(
(acc, cur) => {
return Math.max(acc, cur.lastUpdated || 0);
},
0
)) ||
m.fileMetadata.updated ||
0,
},
Expand Down Expand Up @@ -433,11 +438,11 @@ const processChatMessagesBatch = async (
uniqueMessagesPerConversation[conversationId].map(async (newMessage) =>
typeof newMessage.fileMetadata.appData.content === 'string'
? await dsrToMessage(
dotYouClient,
newMessage as HomebaseFile<string>,
ChatDrive,
true
)
dotYouClient,
newMessage as HomebaseFile<string>,
ChatDrive,
true
)
: (newMessage as HomebaseFile<ChatMessage>)
)
)
Expand Down
4 changes: 3 additions & 1 deletion packages/mobile/src/hooks/feed/useManageSocialFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ export const useManageSocialFeed = (props?: { odinId: string }) => {
};

const getContentReportUrl = () => {
const host = new DotYouClient({ hostIdentity: odinId, api: ApiType.Guest }).getRoot();
const host = odinId
? new DotYouClient({ hostIdentity: odinId, api: ApiType.Guest }).getRoot()
: '';

// Fetch the reporting url from the other identities config
return fetch(`${host}/config/reporting`)
Expand Down
29 changes: 29 additions & 0 deletions packages/mobile/src/hooks/file/useTransferHistory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { getTransferHistory, SystemFileType, TargetDrive } from '@homebase-id/js-lib/core';
import { useQuery } from '@tanstack/react-query';
import { useDotYouClientContext } from 'homebase-id-app-common';

export const useTransferHistory = (props?: {
fileId: string;
targetDrive: TargetDrive;
systemFileType?: SystemFileType;
}) => {
const { fileId, targetDrive, systemFileType } = props || {};

const dotYouClient = useDotYouClientContext();

const fetchTransferHistory = async () => {
if (!fileId || !targetDrive) return null;

return getTransferHistory(dotYouClient, targetDrive, fileId, {
systemFileType,
});
};

return {
fetch: useQuery({
queryFn: fetchTransferHistory,
queryKey: ['transferHistory', targetDrive?.alias, fileId],
enabled: !!props,
}),
};
};
45 changes: 34 additions & 11 deletions packages/mobile/src/pages/chat/chat-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
DotYouClient,
FailedTransferStatuses,
HomebaseFile,
RecipientTransferHistory,
RichText,
} from '@homebase-id/js-lib/core';
import { memo, useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';
Expand Down Expand Up @@ -281,14 +282,34 @@ const ChatPage = memo(({ route, navigation }: ChatProp) => {
(recipient) => recipient !== identity
);

const anyRecipientMissingConversation = filteredRecipients.some((recipient) => {
const latestTransferStatus =
conversation.serverMetadata?.transferHistory?.recipients[recipient]
?.latestTransferStatus;
const anyRecipientMissingConversation = (() => {
if (
conversation.serverMetadata?.transferHistory &&
'recipients' in conversation.serverMetadata.transferHistory
) {
return filteredRecipients.some((recipient) => {
const latestTransferStatus = (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
conversation.serverMetadata as any as {
transferHistory: {
recipients: {
[key: string]: RecipientTransferHistory;
};
};
}
).transferHistory.recipients[recipient]?.latestTransferStatus;

if (!latestTransferStatus) return true;
return FailedTransferStatuses.includes(latestTransferStatus);
});
}

return (
conversation.serverMetadata?.originalRecipientCount !==
conversation.serverMetadata?.transferHistory?.summary.totalDelivered
);
})();

if (!latestTransferStatus) return true;
return FailedTransferStatuses.includes(latestTransferStatus);
});
if (anyRecipientMissingConversation) {
console.log('invite recipient');
inviteRecipient({ conversation });
Expand Down Expand Up @@ -495,10 +516,12 @@ const ChatPage = memo(({ route, navigation }: ChatProp) => {
const [isOpen, setIsOpen] = useState(false);
const { isDarkMode } = useDarkMode();

const host = new DotYouClient({
api: ApiType.Guest,
loggedInIdentity: identity || undefined,
}).getRoot();
const host = identity
? new DotYouClient({
api: ApiType.Guest,
hostIdentity: identity,
}).getRoot()
: '';
const chatOptions: {
label: string;
onPress: () => void;
Expand Down
Loading

0 comments on commit 724a77b

Please sign in to comment.