feat(web): route DM creation to home instance with federated identity
All DM creation and add-member call sites now use the home api client and pass homeUserId/homeInstance instead of routing to the remote instance. Also updates addMember in the API client to accept AddDmMemberRequest.
This commit is contained in:
@@ -53,7 +53,7 @@ export function FriendsPage({ mobile }: FriendsPageProps) {
|
||||
const pendingIncoming = requests.filter(r => r.status === 'pending' && r.user?.id === r.fromId);
|
||||
const pendingOutgoing = requests.filter(r => r.status === 'pending' && r.user?.id === r.toId);
|
||||
|
||||
const handleOpenDm = async (friendId: string, instanceOrigin: string, homeUserId?: string) => {
|
||||
const handleOpenDm = async (friendId: string, homeUserId?: string, homeInstance?: string | null) => {
|
||||
try {
|
||||
// Check if a DM already exists with this user (on any instance)
|
||||
const existing = useSpaceStore.getState().findExistingDmForUser({ id: friendId, homeUserId: homeUserId ?? undefined });
|
||||
@@ -62,13 +62,12 @@ export function FriendsPage({ mobile }: FriendsPageProps) {
|
||||
navigate(`/channels/@me/${existing.dm.id}`);
|
||||
return;
|
||||
}
|
||||
let client = api;
|
||||
if (instanceOrigin) {
|
||||
const instance = useInstanceStore.getState().instances.find(i => i.origin === instanceOrigin);
|
||||
if (instance?.api) client = instance.api;
|
||||
}
|
||||
const dmChannel = await client.dm.create({ userId: friendId });
|
||||
addDmChannel(dmChannel, instanceOrigin);
|
||||
const dmChannel = await api.dm.create({
|
||||
userId: homeInstance ? undefined : friendId,
|
||||
homeUserId: homeUserId ?? undefined,
|
||||
homeInstance: homeInstance ?? undefined,
|
||||
});
|
||||
addDmChannel(dmChannel);
|
||||
navigate(`/channels/@me/${dmChannel.id}`);
|
||||
} catch (err) {
|
||||
console.error('Failed to open DM:', err);
|
||||
@@ -98,7 +97,7 @@ export function FriendsPage({ mobile }: FriendsPageProps) {
|
||||
</div>
|
||||
) : (
|
||||
onlineFriends.map(friend => (
|
||||
<FriendItem key={`${friend.id}:${friend._instanceOrigin}`} friend={friend} onRemove={() => removeFriend(friend.id)} onDm={() => handleOpenDm(friend.id, friend._instanceOrigin, friend.homeUserId ?? undefined)} />
|
||||
<FriendItem key={`${friend.id}:${friend._instanceOrigin}`} friend={friend} onRemove={() => removeFriend(friend.id)} onDm={() => handleOpenDm(friend.id, friend.homeUserId ?? undefined, friend.homeInstance)} />
|
||||
))
|
||||
)}
|
||||
</div>
|
||||
@@ -116,7 +115,7 @@ export function FriendsPage({ mobile }: FriendsPageProps) {
|
||||
</div>
|
||||
) : (
|
||||
friends.map(friend => (
|
||||
<FriendItem key={`${friend.id}:${friend._instanceOrigin}`} friend={friend} onRemove={() => removeFriend(friend.id)} onDm={() => handleOpenDm(friend.id, friend._instanceOrigin, friend.homeUserId ?? undefined)} />
|
||||
<FriendItem key={`${friend.id}:${friend._instanceOrigin}`} friend={friend} onRemove={() => removeFriend(friend.id)} onDm={() => handleOpenDm(friend.id, friend.homeUserId ?? undefined, friend.homeInstance)} />
|
||||
))
|
||||
)}
|
||||
</div>
|
||||
@@ -198,7 +197,7 @@ export function FriendsPage({ mobile }: FriendsPageProps) {
|
||||
if (mobile) {
|
||||
pushMobileScreen('user-profile', { userId: friend.id });
|
||||
} else {
|
||||
handleOpenDm(friend.id, friend._instanceOrigin ?? '', friend.homeUserId ?? undefined);
|
||||
handleOpenDm(friend.id, friend.homeUserId ?? undefined, friend.homeInstance);
|
||||
}
|
||||
}}
|
||||
className={rowClass}
|
||||
@@ -357,7 +356,7 @@ export function FriendsPage({ mobile }: FriendsPageProps) {
|
||||
function AddFriendTab({
|
||||
onOpenDm,
|
||||
}: {
|
||||
onOpenDm: (userId: string, origin: string, homeUserId?: string) => void;
|
||||
onOpenDm: (userId: string, homeUserId?: string, homeInstance?: string | null) => void;
|
||||
}) {
|
||||
const searchUsers = useSocialStore((s) => s.searchUsers);
|
||||
const sendFriendRequest = useSocialStore((s) => s.sendFriendRequest);
|
||||
@@ -610,7 +609,7 @@ function UserDiscoverCard({
|
||||
onRelationshipChange,
|
||||
}: {
|
||||
user: TaggedDiscoverUser;
|
||||
onOpenDm: (userId: string, origin: string, homeUserId?: string) => void;
|
||||
onOpenDm: (userId: string, homeUserId?: string, homeInstance?: string | null) => void;
|
||||
onRelationshipChange: (userId: string, origin: string, relationship: TaggedDiscoverUser['relationship'], requestId?: string) => void;
|
||||
}) {
|
||||
const sendFriendRequest = useSocialStore((s) => s.sendFriendRequest);
|
||||
@@ -729,7 +728,7 @@ function UserDiscoverCard({
|
||||
};
|
||||
|
||||
const handleMessage = () => {
|
||||
onOpenDm(user.id, user._instanceOrigin, user.homeUserId ?? undefined);
|
||||
onOpenDm(user.id, user.homeUserId ?? undefined, user.homeInstance);
|
||||
};
|
||||
|
||||
return (
|
||||
|
||||
@@ -3,7 +3,7 @@ import { createPortal } from 'react-dom';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import type { User, DmChannel } from '@backspace/shared';
|
||||
import { Avatar } from '../ui/Avatar';
|
||||
import { useSpaceStore, getApiForOrigin, resolveUserOrigin } from '../../stores/spaceStore';
|
||||
import { useSpaceStore } from '../../stores/spaceStore';
|
||||
import { useAuthStore } from '../../stores/authStore';
|
||||
import { useUIStore } from '../../stores/uiStore';
|
||||
import { api } from '../../api/client';
|
||||
@@ -182,10 +182,12 @@ export function DmSearchBar() {
|
||||
navigate(`/channels/@me/${existing.dm.id}`);
|
||||
return;
|
||||
}
|
||||
const origin = resolveUserOrigin(item.user);
|
||||
const dmApi = getApiForOrigin(origin);
|
||||
const channel = await dmApi.dm.create({ userId: item.user.id });
|
||||
addDmChannel(channel, origin);
|
||||
const channel = await api.dm.create({
|
||||
userId: item.user.homeInstance ? undefined : item.user.id,
|
||||
homeUserId: item.user.homeUserId ?? undefined,
|
||||
homeInstance: item.user.homeInstance ?? undefined,
|
||||
});
|
||||
addDmChannel(channel);
|
||||
close();
|
||||
useUIStore.getState().setShowDms(true);
|
||||
navigate(`/channels/@me/${channel.id}`);
|
||||
|
||||
@@ -114,7 +114,11 @@ export function AddDmMemberModal() {
|
||||
} else {
|
||||
// Existing group DM → add each friend sequentially
|
||||
for (const friend of selectedFriends) {
|
||||
await api.dm.addMember(dmChannelId, { userId: friend.id });
|
||||
await api.dm.addMember(dmChannelId, {
|
||||
userId: friend.homeInstance ? undefined : friend.id,
|
||||
homeUserId: friend.homeUserId ?? undefined,
|
||||
homeInstance: friend.homeInstance ?? undefined,
|
||||
});
|
||||
}
|
||||
closeModal();
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ import { useNavigate } from 'react-router-dom';
|
||||
import { Modal } from '../ui/Modal';
|
||||
import { Avatar } from '../ui/Avatar';
|
||||
import { useUIStore } from '../../stores/uiStore';
|
||||
import { useSpaceStore, getApiForOrigin, resolveUserOrigin } from '../../stores/spaceStore';
|
||||
import { useSpaceStore } from '../../stores/spaceStore';
|
||||
import { api } from '../../api/client';
|
||||
import type { User } from '@backspace/shared';
|
||||
import { parseFederatedUsername } from '../../utils/identity';
|
||||
@@ -67,10 +67,12 @@ export function NewDmModal() {
|
||||
navigate(`/channels/@me/${existing.dm.id}`);
|
||||
return;
|
||||
}
|
||||
const origin = resolveUserOrigin(user);
|
||||
const dmApi = getApiForOrigin(origin);
|
||||
const channel = await dmApi.dm.create({ userId: user.id });
|
||||
addDmChannel(channel, origin);
|
||||
const channel = await api.dm.create({
|
||||
userId: user.homeInstance ? undefined : user.id,
|
||||
homeUserId: user.homeUserId ?? undefined,
|
||||
homeInstance: user.homeInstance ?? undefined,
|
||||
});
|
||||
addDmChannel(channel);
|
||||
closeModal();
|
||||
useUIStore.getState().setShowDms(true);
|
||||
navigate(`/channels/@me/${channel.id}`);
|
||||
|
||||
@@ -6,6 +6,7 @@ import { Avatar } from '../ui/Avatar';
|
||||
import { Username } from '../ui/Username';
|
||||
import { useUIStore } from '../../stores/uiStore';
|
||||
import { useSpaceStore, getApiForOrigin, resolveUserOrigin } from '../../stores/spaceStore';
|
||||
import { api } from '../../api/client';
|
||||
import { useSocialStore, type TaggedFriend, type TaggedFriendRequest, InstanceNotConnectedError, InstanceDisconnectedError } from '../../stores/socialStore';
|
||||
import { ConnectInstanceModal } from './ConnectInstanceModal';
|
||||
import { useAuthStore } from '../../stores/authStore';
|
||||
@@ -170,9 +171,12 @@ export function UserProfileModal() {
|
||||
navigate(`/channels/@me/${existing.dm.id}`);
|
||||
return;
|
||||
}
|
||||
const dmApi = getApiForOrigin(userOrigin);
|
||||
const channel = await dmApi.dm.create({ userId: user.id });
|
||||
addDmChannel(channel, userOrigin);
|
||||
const channel = await api.dm.create({
|
||||
userId: user.homeInstance ? undefined : user.id,
|
||||
homeUserId: user.homeUserId ?? undefined,
|
||||
homeInstance: user.homeInstance ?? undefined,
|
||||
});
|
||||
addDmChannel(channel);
|
||||
useUIStore.getState().setShowDms(true);
|
||||
closeModal();
|
||||
navigate(`/channels/@me/${channel.id}`);
|
||||
|
||||
@@ -5,6 +5,7 @@ import type { User } from '@backspace/shared';
|
||||
import { Avatar } from '../ui/Avatar';
|
||||
import { Username } from '../ui/Username';
|
||||
import { useSpaceStore, getApiForOrigin, resolveUserOrigin } from '../../stores/spaceStore';
|
||||
import { api } from '../../api/client';
|
||||
import { useUIStore } from '../../stores/uiStore';
|
||||
import { getAvatarGradient, adjustColor, mutedGradient } from '../../utils/gradients';
|
||||
import { parseFederatedUsername } from '../../utils/identity';
|
||||
@@ -50,9 +51,12 @@ export function UserProfilePopout({ user, onClose, position }: UserProfilePopout
|
||||
navigate(`/channels/@me/${existing.dm.id}`);
|
||||
return;
|
||||
}
|
||||
const dmApi = getApiForOrigin(origin);
|
||||
const channel = await dmApi.dm.create({ userId: user.id });
|
||||
addDmChannel(channel, origin);
|
||||
const channel = await api.dm.create({
|
||||
userId: user.homeInstance ? undefined : user.id,
|
||||
homeUserId: user.homeUserId ?? undefined,
|
||||
homeInstance: user.homeInstance ?? undefined,
|
||||
});
|
||||
addDmChannel(channel);
|
||||
useUIStore.getState().setShowDms(true);
|
||||
onClose();
|
||||
navigate(`/channels/@me/${channel.id}`);
|
||||
|
||||
Reference in New Issue
Block a user