Just making a mess mostly I think
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
'use server';
|
||||
import { createServerClient } from '@/utils/supabase';
|
||||
import type { Profile, Result } from '@/utils/supabase';
|
||||
import { getUser, getProfile } from '@/lib/hooks';
|
||||
import { getUser, getProfile, getSignedUrl } from '@/lib/actions';
|
||||
|
||||
export type UserWithStatus = {
|
||||
id?: string;
|
||||
@@ -30,14 +30,12 @@ export const getRecentUsersWithStatuses = async (): Promise<
|
||||
|
||||
const { data, error } = (await supabase
|
||||
.from('statuses')
|
||||
.select(
|
||||
`
|
||||
.select(`
|
||||
user:profiles!user_id(*),
|
||||
status,
|
||||
created_at,
|
||||
updated_by:profiles!updated_by_id(*)
|
||||
`,
|
||||
)
|
||||
`)
|
||||
.gte('created_at', oneDayAgo.toISOString())
|
||||
.order('created_at', { ascending: false })) as {
|
||||
data: UserWithStatus[];
|
||||
@@ -47,7 +45,6 @@ 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;
|
||||
@@ -55,7 +52,34 @@ export const getRecentUsersWithStatuses = async (): Promise<
|
||||
return true;
|
||||
});
|
||||
|
||||
return { success: true, data: filtered };
|
||||
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;
|
||||
}
|
||||
filteredWithAvatars.push(userWithStatus);
|
||||
}
|
||||
return { success: true, data: filteredWithAvatars };
|
||||
} catch (error) {
|
||||
return { success: false, error: `Error: ${error as Error}` };
|
||||
}
|
||||
@@ -96,17 +120,14 @@ export const updateStatuses = async (
|
||||
): Promise<Result<void>> => {
|
||||
try {
|
||||
const supabase = await createServerClient();
|
||||
const userResponse = await getUser();
|
||||
if (!userResponse.success) throw new Error('Not authenticated!');
|
||||
const profileResponse = await getProfile();
|
||||
if (!profileResponse.success) throw new Error(profileResponse.error);
|
||||
const user = userResponse.data;
|
||||
if (!profileResponse.success) throw new Error('Not authenticated!');
|
||||
const userProfile = profileResponse.data;
|
||||
|
||||
const inserts = userIds.map((usersId) => ({
|
||||
user_id: usersId,
|
||||
const inserts = userIds.map((userId) => ({
|
||||
user_id: userId,
|
||||
status,
|
||||
updated_by_id: user.id,
|
||||
updated_by_id: userProfile.id,
|
||||
}));
|
||||
|
||||
const { data: insertedStatuses, error: insertedStatusesError } =
|
||||
@@ -116,13 +137,9 @@ export const updateStatuses = async (
|
||||
if (insertedStatuses) {
|
||||
const broadcastArray = new Array<UserWithStatus>(insertedStatuses.length);
|
||||
for (const insertedStatus of insertedStatuses) {
|
||||
const { data: profile, error: profileError } = await supabase
|
||||
.from('profiles')
|
||||
.select('*')
|
||||
.eq('id', insertedStatus.user_id)
|
||||
.single();
|
||||
if (profileError) throw profileError as Error;
|
||||
|
||||
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,
|
||||
@@ -148,21 +165,17 @@ export const updateUserStatus = async (
|
||||
): Promise<Result<void>> => {
|
||||
try {
|
||||
const supabase = await createServerClient();
|
||||
const userResponse = await getUser();
|
||||
if (!userResponse.success)
|
||||
throw new Error(`Not authenticated! ${userResponse.error}`);
|
||||
const profileResponse = await getProfile();
|
||||
if (!profileResponse.success)
|
||||
throw new Error(`Could not get profile! ${profileResponse.error}`);
|
||||
const user = userResponse.data;
|
||||
throw new Error(`Not authenticated! ${profileResponse.error}`);
|
||||
const userProfile = profileResponse.data;
|
||||
|
||||
const { data: insertedStatus, error: insertedStatusError } = await supabase
|
||||
.from('statuses')
|
||||
.insert({
|
||||
user_id: user.id,
|
||||
user_id: userProfile.id,
|
||||
status,
|
||||
updated_by_id: user.id,
|
||||
updated_by_id: userProfile.id,
|
||||
})
|
||||
.select()
|
||||
.single();
|
||||
@@ -172,6 +185,7 @@ export const updateUserStatus = async (
|
||||
user: userProfile,
|
||||
status: insertedStatus.status,
|
||||
created_at: insertedStatus.created_at,
|
||||
updated_by: userProfile,
|
||||
};
|
||||
|
||||
await broadcastStatusUpdates([userStatus]);
|
||||
@@ -220,14 +234,6 @@ export const getUserHistory = async (
|
||||
};
|
||||
if (statusesError) throw statusesError as Error;
|
||||
|
||||
const { data: profile, error: profileError } = (await supabase
|
||||
.from('profiles')
|
||||
.select('*')
|
||||
.eq('id', userId)
|
||||
.single()) as { data: Profile; error: unknown };
|
||||
if (profileError) throw profileError as Error;
|
||||
if (!profile) throw new Error('User profile not found!');
|
||||
|
||||
const totalCount = count ?? 0;
|
||||
const totalPages = Math.ceil(totalCount / perPage);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user