Last commit before I burn it all to the ground with ReactQuery
This commit is contained in:
@ -24,6 +24,16 @@ type PaginatedHistory = {
|
||||
export const getRecentUsersWithStatuses = async (): Promise<
|
||||
Result<UserWithStatus[]>
|
||||
> => {
|
||||
const getAvatarUrl = async (url: string | null | undefined) => {
|
||||
if (!url) return null;
|
||||
const avatarUrl = await getSignedUrl({
|
||||
bucket: 'avatars',
|
||||
url,
|
||||
transform: { width: 128, height: 128 },
|
||||
});
|
||||
if (avatarUrl.success) return avatarUrl.data;
|
||||
else return null;
|
||||
};
|
||||
try {
|
||||
const supabase = await createServerClient();
|
||||
const oneDayAgo = new Date(Date.now() - 1000 * 60 * 60 * 24);
|
||||
@ -45,6 +55,7 @@ export const getRecentUsersWithStatuses = async (): Promise<
|
||||
if (error) throw error as Error;
|
||||
if (!data?.length) return { success: true, data: [] };
|
||||
|
||||
// 3️⃣ client-side dedupe: keep the first status you see per user
|
||||
const seen = new Set<string>();
|
||||
const filtered = data.filter((row) => {
|
||||
if (seen.has(row.user.id)) return false;
|
||||
@ -53,32 +64,16 @@ export const getRecentUsersWithStatuses = async (): Promise<
|
||||
});
|
||||
|
||||
const filteredWithAvatars = new Array<UserWithStatus>();
|
||||
|
||||
for (const userWithStatus of filtered) {
|
||||
|
||||
if (userWithStatus.user.avatar_url) {
|
||||
const avatarResponse = await getSignedUrl({
|
||||
bucket: 'avatars',
|
||||
url: userWithStatus.user.avatar_url,
|
||||
});
|
||||
if (avatarResponse.success) {
|
||||
userWithStatus.user.avatar_url = avatarResponse.data;
|
||||
} else userWithStatus.user.avatar_url = null;
|
||||
} else userWithStatus.user.avatar_url = null;
|
||||
|
||||
if (userWithStatus.updated_by?.avatar_url) {
|
||||
const updatedByAvatarResponse = await getSignedUrl({
|
||||
bucket: 'avatars',
|
||||
url: userWithStatus.updated_by.avatar_url ?? '',
|
||||
});
|
||||
if (updatedByAvatarResponse.success) {
|
||||
userWithStatus.updated_by.avatar_url = updatedByAvatarResponse.data;
|
||||
} else userWithStatus.updated_by.avatar_url = null;
|
||||
} else {
|
||||
if (userWithStatus.updated_by) userWithStatus.updated_by.avatar_url = null;
|
||||
}
|
||||
if (userWithStatus.user.avatar_url)
|
||||
userWithStatus.user.avatar_url =
|
||||
await getAvatarUrl(userWithStatus.updated_by?.avatar_url);
|
||||
if (userWithStatus.updated_by?.avatar_url)
|
||||
userWithStatus.user.avatar_url =
|
||||
await getAvatarUrl(userWithStatus.updated_by?.avatar_url);
|
||||
filteredWithAvatars.push(userWithStatus);
|
||||
}
|
||||
|
||||
return { success: true, data: filteredWithAvatars };
|
||||
} catch (error) {
|
||||
return { success: false, error: `Error: ${error as Error}` };
|
||||
@ -115,41 +110,36 @@ export const broadcastStatusUpdates = async (
|
||||
};
|
||||
|
||||
export const updateStatuses = async (
|
||||
userIds: string[],
|
||||
usersWithStatuses: UserWithStatus[],
|
||||
status: string,
|
||||
): Promise<Result<void>> => {
|
||||
try {
|
||||
const supabase = await createServerClient();
|
||||
const profileResponse = await getProfile();
|
||||
if (!profileResponse.success) throw new Error('Not authenticated!');
|
||||
const userProfile = profileResponse.data;
|
||||
const user = profileResponse.data;
|
||||
|
||||
const inserts = userIds.map((userId) => ({
|
||||
user_id: userId,
|
||||
status,
|
||||
updated_by_id: userProfile.id,
|
||||
}));
|
||||
const {
|
||||
data: insertedStatuses,
|
||||
error: insertedStatusesError
|
||||
} = await supabase
|
||||
.from('statuses')
|
||||
.insert(usersWithStatuses.map((userWithStatus) => ({
|
||||
user_id: userWithStatus.user.id,
|
||||
status,
|
||||
updated_by_id: user.id,
|
||||
})))
|
||||
.select();
|
||||
|
||||
const { data: insertedStatuses, error: insertedStatusesError } =
|
||||
await supabase.from('statuses').insert(inserts).select();
|
||||
if (insertedStatusesError) throw insertedStatusesError as Error;
|
||||
|
||||
if (insertedStatuses) {
|
||||
const broadcastArray = new Array<UserWithStatus>(insertedStatuses.length);
|
||||
for (const insertedStatus of insertedStatuses) {
|
||||
const profileResponse = await getProfile(insertedStatus.user_id)
|
||||
if (!profileResponse.success) throw new Error(profileResponse.error);
|
||||
const profile = profileResponse.data;
|
||||
if (profile) {
|
||||
broadcastArray.push({
|
||||
user: profile,
|
||||
status: insertedStatus.status,
|
||||
created_at: insertedStatus.created_at,
|
||||
updated_by: userProfile,
|
||||
});
|
||||
}
|
||||
}
|
||||
await broadcastStatusUpdates(broadcastArray);
|
||||
if (insertedStatusesError) throw new Error('Couldn\'t insert statuses!');
|
||||
else if (insertedStatuses) {
|
||||
const createdAtFallback = new Date(Date.now()).toISOString();
|
||||
await broadcastStatusUpdates(usersWithStatuses.map((s, i) => { return {
|
||||
user: s.user,
|
||||
status: status,
|
||||
created_at: insertedStatuses[i]?.created_at ?? createdAtFallback,
|
||||
updated_by: user,
|
||||
}}));
|
||||
}
|
||||
return { success: true, data: undefined };
|
||||
} catch (error) {
|
||||
@ -181,14 +171,13 @@ export const updateUserStatus = async (
|
||||
.single();
|
||||
if (insertedStatusError) throw insertedStatusError as Error;
|
||||
|
||||
const userStatus: UserWithStatus = {
|
||||
await broadcastStatusUpdates([{
|
||||
user: userProfile,
|
||||
status: insertedStatus.status,
|
||||
created_at: insertedStatus.created_at,
|
||||
updated_by: userProfile,
|
||||
};
|
||||
}]);
|
||||
|
||||
await broadcastStatusUpdates([userStatus]);
|
||||
return { success: true, data: undefined };
|
||||
} catch (error) {
|
||||
return {
|
||||
|
Reference in New Issue
Block a user