Skip to content

Commit

Permalink
Merge pull request #791 from ephemeraHQ/noe/fix-profiles-0x
Browse files Browse the repository at this point in the history
Noe/fix profiles 0x
  • Loading branch information
saulmc authored Sep 20, 2024
2 parents 1c846af + ad7e510 commit 5433522
Show file tree
Hide file tree
Showing 58 changed files with 293 additions and 376 deletions.
9 changes: 7 additions & 2 deletions android/app/src/main/java/com/converse/dev/Profile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,30 @@ import com.android.volley.toolbox.JsonObjectRequest
import com.android.volley.toolbox.Volley
import com.beust.klaxon.Klaxon
import kotlinx.coroutines.suspendCancellableCoroutine
import org.web3j.crypto.Keys
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException


suspend fun getProfile(appContext: Context, account: String, address: String): Profile? {

var profileState = getProfilesStore(appContext, account)?.state
var lowercasedAddress = address.lowercase()
var formattedAddress = Keys.toChecksumAddress(address)
profileState?.profiles?.get(address)?.let { return it }
profileState?.profiles?.get(formattedAddress)?.let { return it }
profileState?.profiles?.get(lowercasedAddress)?.let { return it }

// If profile is nil, let's refresh it
try {
refreshProfileFromBackend(appContext, account, address)
refreshProfileFromBackend(appContext, account, formattedAddress)
} catch (e: Exception) {
// Handle exception if needed
}

profileState = getProfilesStore(appContext, account)?.state

return profileState?.profiles?.get(address)
return profileState?.profiles?.get(formattedAddress)
}

suspend fun refreshProfileFromBackend(appContext: Context, account: String, address: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ import java.util.*
import kotlin.reflect.full.declaredMemberProperties
import kotlin.reflect.jvm.isAccessible
import kotlin.reflect.jvm.javaField
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ProcessLifecycleOwner

class PushNotificationsService : FirebaseMessagingService() {
companion object {
Expand Down Expand Up @@ -111,6 +113,12 @@ class PushNotificationsService : FirebaseMessagingService() {
return
}

val appIsInForeground = ProcessLifecycleOwner.get().lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED)
val currentAccount = getCurrentAccount(this)
if (appIsInForeground && currentAccount !== null && currentAccount.lowercase() == notificationData.account.lowercase()) {
Log.d(TAG, "Preventing notification for ${notificationData.account} because user is on it")
return
}
Log.d(TAG, "INSTANTIATED XMTP CLIENT FOR ${notificationData.contentTopic}")

val encryptedMessageData = Base64.decode(notificationData.message, Base64.NO_WRAP)
Expand Down
12 changes: 0 additions & 12 deletions android/app/src/main/java/com/converse/dev/xmtp/Conversations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,6 @@ fun persistNewConversation(appContext:Context, account: String, conversation: Co
}
}

fun getSavedConversationTitle(appContext: Context, topic: String): String {
try {
Log.d("PushNotificationsService", "Getting data conversation-$topic")
val mmkv = getMmkv(appContext)
val savedConversationDict = mmkv?.decodeString("conversation-$topic") ?: return ""
val parsedConversationDict = Klaxon().parse<ConversationDictData>(savedConversationDict)
return parsedConversationDict?.title ?: parsedConversationDict?.shortAddress ?: ""
} catch (e: Exception) {
return ""
}
}

suspend fun getNewGroup(xmtpClient: Client, contentTopic: String): Group? {
return try {
if (isGroupWelcomeTopic(contentTopic)) {
Expand Down
9 changes: 6 additions & 3 deletions android/app/src/main/java/com/converse/dev/xmtp/Messages.kt
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,18 @@ suspend fun handleOngoingConversationMessage(
}

val message = conversation.decode(envelope)
val contentTopic = envelope.contentTopic
var conversationTitle = getSavedConversationTitle(appContext, contentTopic)
var conversationTitle = ""

val decodedMessageResult = handleMessageByContentType(
appContext,
message,
xmtpClient,
)

decodedMessageResult.senderAddress?.let {
conversationTitle = shortAddress(it)
}

val senderProfile =
decodedMessageResult.senderAddress?.let { getProfile(appContext, xmtpClient.address, it) }
var senderAvatar: String? = null
Expand All @@ -210,7 +213,7 @@ suspend fun handleOngoingConversationMessage(
}

val shouldShowNotification = if (decodedMessageResult.senderAddress != xmtpClient.address && !decodedMessageResult.forceIgnore && decodedMessageResult.content != null) {
if (conversationTitle.isEmpty() && decodedMessageResult.senderAddress != null) {
if ((conversationTitle == null || conversationTitle!!.isEmpty()) && decodedMessageResult.senderAddress != null) {
conversationTitle = shortAddress(decodedMessageResult.senderAddress)
}
true
Expand Down
7 changes: 4 additions & 3 deletions components/Chat/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
itemSeparatorColor,
tertiaryBackgroundColor,
} from "@styles/colors";
import { getCleanAddress } from "@utils/eth";
import differenceInCalendarDays from "date-fns/differenceInCalendarDays";
import React, { useCallback, useEffect, useMemo, useRef } from "react";
import {
Expand Down Expand Up @@ -47,7 +48,7 @@ import { useKeyboardAnimation } from "../../utils/animations/keyboardAnimation";
import { isAttachmentMessage } from "../../utils/attachment/helpers";
import { useConversationContext } from "../../utils/conversation";
import { converseEventEmitter } from "../../utils/events";
import { getProfileData } from "../../utils/profile";
import { getProfile, getProfileData } from "../../utils/profile";
import { UUID_REGEX } from "../../utils/regex";
import { isContentType } from "../../utils/xmtpRN/contentTypes";
import { Recommendation } from "../Recommendations/Recommendation";
Expand Down Expand Up @@ -183,7 +184,7 @@ export default function Chat() {
const peerSocials = useProfilesStore(
useShallow((s) =>
conversation?.peerAddress
? s.profiles[conversation.peerAddress]?.socials
? getProfile(conversation.peerAddress, s.profiles)?.socials
: undefined
)
);
Expand Down Expand Up @@ -228,7 +229,7 @@ export default function Chat() {
conversation &&
!isBlockedPeer &&
(!conversation.isGroup ||
conversation.groupMembers.includes(xmtpAddress.toLowerCase()))
conversation.groupMembers.includes(getCleanAddress(xmtpAddress)))
);

const textInputStyle = useAnimatedStyle(
Expand Down
8 changes: 4 additions & 4 deletions components/Chat/ChatGroupUpdatedMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
useInboxIdStore,
useProfilesStore,
} from "../../data/store/accountsStore";
import { getPreferredName } from "../../utils/profile";
import { getPreferredName, getProfile } from "../../utils/profile";

export default function ChatGroupUpdatedMessage({
message,
Expand All @@ -27,7 +27,7 @@ export default function ChatGroupUpdatedMessage({
// TODO: Feat: handle multiple members
const initiatedByAddress = byInboxId[parsedContent.initiatedByInboxId]?.[0];
const initiatedByReadableName = getPreferredName(
profiles[initiatedByAddress]?.socials,
getProfile(initiatedByAddress, profiles)?.socials,
initiatedByAddress
);
const membersActions: string[] = [];
Expand All @@ -37,7 +37,7 @@ export default function ChatGroupUpdatedMessage({
// We haven't synced yet the members
if (!firstAddress) return;
const readableName = getPreferredName(
profiles[firstAddress]?.socials,
getProfile(firstAddress, profiles)?.socials,
firstAddress
);
membersActions.push(`${readableName} joined the conversation`);
Expand All @@ -48,7 +48,7 @@ export default function ChatGroupUpdatedMessage({
// We haven't synced yet the members
if (!firstAddress) return;
const readableName = getPreferredName(
profiles[firstAddress]?.socials,
getProfile(firstAddress, profiles)?.socials,
firstAddress
);
membersActions.push(`${readableName} left the conversation`);
Expand Down
5 changes: 4 additions & 1 deletion components/Chat/ChatNullState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
getPreferredUsername,
getPreferredName,
getPreferredAvatar,
getProfile,
} from "../../utils/profile";
import NewConversationButton from "../ConversationList/NewConversationButton";

Expand All @@ -48,7 +49,9 @@ const ChatNullState: React.FC<ChatNullStateProps> = ({
const colorScheme = useColorScheme();
const styles = useStyles();

const socials = useProfilesStore((s) => s.profiles[currentAccount]?.socials);
const socials = useProfilesStore(
(s) => getProfile(currentAccount, s.profiles)?.socials
);
const username = getPreferredUsername(socials);
const displayName = getPreferredName(socials, currentAccount);
const profileUrl = `https://${config.websiteDomain}/dm/${
Expand Down
4 changes: 2 additions & 2 deletions components/Chat/ChatPlaceholder/ChatPlaceholder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
} from "../../../data/store/accountsStore";
import { useConversationContext } from "../../../utils/conversation";
import { sendMessage } from "../../../utils/message";
import { getProfileData } from "../../../utils/profile";
import { getProfile, getProfileData } from "../../../utils/profile";
import { conversationName } from "../../../utils/str";
import { consentToPeersOnProtocol } from "../../../utils/xmtpRN/conversations";
import ActivityIndicator from "../../ActivityIndicator/ActivityIndicator";
Expand All @@ -42,7 +42,7 @@ export default function ChatPlaceholder({ messagesCount }: Props) {
);
const peerSocials = useProfilesStore((s) =>
conversation?.peerAddress
? s.profiles[conversation.peerAddress]?.socials
? getProfile(conversation.peerAddress, s.profiles)?.socials
: undefined
);
const profileData = getProfileData(recommendationData, peerSocials);
Expand Down
14 changes: 11 additions & 3 deletions components/Chat/Message/Message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ import {
} from "../../../utils/messageContent";
import { navigate } from "../../../utils/navigation";
import { LimitedMap } from "../../../utils/objects";
import { getPreferredAvatar, getPreferredName } from "../../../utils/profile";
import {
getPreferredAvatar,
getPreferredName,
getProfile,
} from "../../../utils/profile";
import { getMessageReactions } from "../../../utils/reactions";
import { getReadableProfile } from "../../../utils/str";
import { isTransactionMessage } from "../../../utils/transaction";
Expand Down Expand Up @@ -101,7 +105,9 @@ const MessageSender = ({ message }: { message: MessageToDisplay }) => {
const address = useInboxIdStore(
(s) => s.byInboxId[message.senderAddress]?.[0] ?? message.senderAddress
);
const senderSocials = useProfilesStore((s) => s.profiles[address]?.socials);
const senderSocials = useProfilesStore(
(s) => getProfile(address, s.profiles)?.socials
);
const styles = useStyles();
return (
<View style={styles.groupSenderWrapper}>
Expand All @@ -116,7 +122,9 @@ const MessageSenderAvatar = ({ message }: { message: MessageToDisplay }) => {
const address = useInboxIdStore(
(s) => s.byInboxId[message.senderAddress]?.[0] ?? message.senderAddress
);
const senderSocials = useProfilesStore((s) => s.profiles[address]?.socials);
const senderSocials = useProfilesStore(
(s) => getProfile(address, s.profiles)?.socials
);
const styles = useStyles();
const openProfile = useCallback(() => {
navigate("Profile", { address: message.senderAddress });
Expand Down
12 changes: 9 additions & 3 deletions components/Chat/Message/MessageReactions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ import {
useCurrentAccount,
useProfilesStore,
} from "../../../data/store/accountsStore";
import { getPreferredAvatar, getPreferredName } from "../../../utils/profile";
import {
getPreferredAvatar,
getPreferredName,
getProfile,
} from "../../../utils/profile";
import {
MessageReaction,
addReactionToMessage,
Expand Down Expand Up @@ -145,9 +149,11 @@ function ChatMessageReactions({ message, reactions }: Props) {
.map((reactor, index) => (
<Avatar
key={reactor}
uri={getPreferredAvatar(profiles[reactor]?.socials)}
uri={getPreferredAvatar(
getProfile(reactor, profiles)?.socials
)}
name={getPreferredName(
profiles[reactor]?.socials,
getProfile(reactor, profiles)?.socials,
reactor
)}
size={AvatarSizes.messageReactor}
Expand Down
8 changes: 6 additions & 2 deletions components/Chat/Message/MessageReactionsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import {
} from "@styles/colors";
import { AvatarSizes, BorderRadius, Paddings, Margins } from "@styles/sizes";
import { favoritedEmojis } from "@utils/emojis/favoritedEmojis";
import { getPreferredAvatar, getPreferredName } from "@utils/profile";
import {
getPreferredAvatar,
getPreferredName,
getProfile,
} from "@utils/profile";
import {
addReactionToMessage,
getReactionContent,
Expand Down Expand Up @@ -69,7 +73,7 @@ const Item: FC<MessageReactionsItemProps> = ({ content, addresses, index }) => {
const animatedValue = useSharedValue(0);
const membersSocials = useProfilesStore((s) =>
addresses.map((address) => {
const socials = s.profiles[address]?.socials;
const socials = getProfile(address, s.profiles)?.socials;
return {
address,
uri: getPreferredAvatar(socials),
Expand Down
8 changes: 4 additions & 4 deletions components/Conversation/ConversationTitle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { XmtpConversation } from "../../data/store/chatStore";
import { useGroupName } from "../../hooks/useGroupName";
import { useGroupPhoto } from "../../hooks/useGroupPhoto";
import { NavigationParamList } from "../../screens/Navigation/Navigation";
import { getPreferredAvatar } from "../../utils/profile";
import { getPreferredAvatar, getProfile } from "../../utils/profile";
import { conversationName, getTitleFontScale } from "../../utils/str";
import Avatar from "../Avatar";
import { useDebugEnabled } from "../DebugButton";
Expand Down Expand Up @@ -59,7 +59,7 @@ export default function ConversationTitle({
? groupPhoto
: getPreferredAvatar(
conversation?.peerAddress
? profiles[conversation.peerAddress]?.socials
? getProfile(conversation.peerAddress, profiles)?.socials
: undefined
)
);
Expand All @@ -77,7 +77,7 @@ export default function ConversationTitle({
conversation.peerAddress !== previousConversation.peerAddress ||
conversation.context?.conversationId !==
previousConversation.context?.conversationId ||
conversation.conversationTitle ||
conversationName(conversation) ||
(previousConversation.isGroup &&
conversation.isGroup &&
previousConversation.groupName !== conversation.groupName)
Expand All @@ -87,7 +87,7 @@ export default function ConversationTitle({
setTitle(conversationName(conversation));
}
if (!conversation.peerAddress) return;
const socials = profiles[conversation.peerAddress]?.socials;
const socials = getProfile(conversation.peerAddress, profiles)?.socials;
setAvatar(getPreferredAvatar(socials));
}
conversationRef.current = conversation;
Expand Down
4 changes: 2 additions & 2 deletions components/ConversationFlashList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
ConversationFlatListItem,
ConversationWithLastMessagePreview,
} from "../utils/conversation";
import { getPreferredAvatar } from "../utils/profile";
import { getPreferredAvatar, getProfile } from "../utils/profile";
import { conversationName } from "../utils/str";

type Props = {
Expand Down Expand Up @@ -105,7 +105,7 @@ export default function ConversationFlashList({
const conversation = item as ConversationWithLastMessagePreview;
const lastMessagePreview = conversation.lastMessagePreview;
const socials = conversation.peerAddress
? profiles[conversation.peerAddress]?.socials
? getProfile(conversation.peerAddress, profiles)?.socials
: undefined;
if (conversation.isGroup) {
return <GroupConversationItem conversation={conversation} />;
Expand Down
8 changes: 6 additions & 2 deletions components/ConversationList/ProfileSettingsButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import {
} from "../../data/store/accountsStore";
import { evmHelpers } from "../../utils/evm/helpers";
import { navigate } from "../../utils/navigation";
import { getPreferredAvatar, getPreferredName } from "../../utils/profile";
import {
getPreferredAvatar,
getPreferredName,
getProfile,
} from "../../utils/profile";
import Avatar from "../Avatar";
import Button from "../Button/Button";

Expand All @@ -22,7 +26,7 @@ export default function ProfileSettingsButton() {
const [stringSize, setStringSize] = useState(0);
const account = currentAccount();
const profiles = useProfilesStore((state) => state.profiles);
const socials = profiles[account]?.socials;
const socials = getProfile(account, profiles)?.socials;

const openProfile = useCallback(() => {
navigate("Profile", { address: currentAccount() });
Expand Down
8 changes: 6 additions & 2 deletions components/GroupAvatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ import {
useCurrentAccount,
} from "../data/store/accountsStore";
import { useGroupMembers } from "../hooks/useGroupMembers";
import { getPreferredAvatar, getPreferredName } from "../utils/profile";
import {
getPreferredAvatar,
getPreferredName,
getProfile,
} from "../utils/profile";

const MAIN_CIRCLE_RADIUS = 50;
const MAX_VISIBLE_MEMBERS = 4;
Expand Down Expand Up @@ -156,7 +160,7 @@ const GroupAvatar: React.FC<GroupAvatarProps> = ({
(acc: { address: string; uri?: string; name?: string }[], id) => {
const member = groupMembers.byId[id];
const address = member.addresses[0].toLowerCase();
const senderSocials = profiles[address]?.socials;
const senderSocials = getProfile(address, profiles)?.socials;
const shouldExclude =
excludeSelf && account && address === account.toLowerCase();
if (shouldExclude) return acc;
Expand Down
Loading

0 comments on commit 5433522

Please sign in to comment.