mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2026-12-05 08:23:35 +00:00
Admin Dashboard init
This commit is contained in:
1 parent
d1744b7d71
commit
c0dca78709
10 files changed
+1478
-7
No files matched your search
@@ -0,0 +1,237 @@
|
||||
import { useNostr } from '@nostrify/react';
|
||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import { useCurrentUser } from './useCurrentUser';
|
||||
import { useNostrPublish } from './useNostrPublish';
|
||||
import { nip19 } from 'nostr-tools';
|
||||
|
||||
// Custom kind for admin storage
|
||||
const ADMIN_STORAGE_KIND = 39767;
|
||||
|
||||
// Get admin npub from environment variable
|
||||
const ADMIN_NPUB = import.meta.env.VITE_ADMIN_NPUB;
|
||||
|
||||
export interface AdminStorageData {
|
||||
hiddenEvents: string[];
|
||||
allowList: string[];
|
||||
denyList: string[];
|
||||
blockedWords: string[];
|
||||
caseSensitive: boolean;
|
||||
wholeWordsOnly: boolean;
|
||||
enableModeration: boolean;
|
||||
autoHideBlocked: boolean;
|
||||
requireApproval: boolean;
|
||||
siteName: string;
|
||||
siteDescription: string;
|
||||
}
|
||||
|
||||
const defaultAdminData: AdminStorageData = {
|
||||
hiddenEvents: [],
|
||||
allowList: [],
|
||||
denyList: [],
|
||||
blockedWords: [],
|
||||
caseSensitive: false,
|
||||
wholeWordsOnly: true,
|
||||
enableModeration: true,
|
||||
autoHideBlocked: true,
|
||||
requireApproval: false,
|
||||
siteName: '',
|
||||
siteDescription: '',
|
||||
};
|
||||
|
||||
export function useAdminStorage() {
|
||||
const { nostr } = useNostr();
|
||||
const { user } = useCurrentUser();
|
||||
const { mutateAsync: publishEvent } = useNostrPublish();
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
// Get admin pubkey
|
||||
let adminPubkey: string = '';
|
||||
try {
|
||||
if (ADMIN_NPUB) {
|
||||
const decoded = nip19.decode(ADMIN_NPUB);
|
||||
// @ts-expect-error - TypeScript has issues with nostr-tools type inference
|
||||
if (decoded.type === 'npub') {
|
||||
adminPubkey = decoded.data as unknown as string;
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// Invalid admin npub
|
||||
}
|
||||
|
||||
// Check if current user is admin
|
||||
const isAdmin = user?.pubkey === adminPubkey && !!adminPubkey;
|
||||
|
||||
// Query for admin storage events
|
||||
const adminDataQuery = useQuery({
|
||||
queryKey: ['admin-storage', adminPubkey],
|
||||
queryFn: async ({ signal }) => {
|
||||
if (!adminPubkey) return defaultAdminData;
|
||||
|
||||
const timeoutSignal = AbortSignal.timeout(1500);
|
||||
const combinedSignal = AbortSignal.any([signal, timeoutSignal]);
|
||||
|
||||
// Query for admin storage events
|
||||
const events = await nostr.query([{
|
||||
kinds: [ADMIN_STORAGE_KIND],
|
||||
authors: [adminPubkey],
|
||||
'#d': ['admin-config'],
|
||||
limit: 1
|
||||
}], { signal: combinedSignal });
|
||||
|
||||
if (events.length === 0) {
|
||||
return defaultAdminData;
|
||||
}
|
||||
|
||||
// Get the most recent event
|
||||
const latestEvent = events.sort((a, b) => b.created_at - a.created_at)[0];
|
||||
|
||||
try {
|
||||
const data = JSON.parse(latestEvent.content) as AdminStorageData;
|
||||
// Merge with defaults to handle missing fields
|
||||
return { ...defaultAdminData, ...data };
|
||||
} catch {
|
||||
return defaultAdminData;
|
||||
}
|
||||
},
|
||||
enabled: !!adminPubkey,
|
||||
});
|
||||
|
||||
// Mutation to save admin data
|
||||
const saveAdminData = useMutation({
|
||||
mutationFn: async (data: Partial<AdminStorageData>) => {
|
||||
if (!isAdmin) {
|
||||
throw new Error('Only admin can save admin data');
|
||||
}
|
||||
|
||||
const currentData = adminDataQuery.data || defaultAdminData;
|
||||
const updatedData = { ...currentData, ...data };
|
||||
|
||||
await publishEvent({
|
||||
kind: ADMIN_STORAGE_KIND,
|
||||
content: JSON.stringify(updatedData),
|
||||
tags: [
|
||||
['d', 'admin-config'],
|
||||
['alt', 'Site administration configuration and policies'],
|
||||
],
|
||||
});
|
||||
|
||||
return updatedData;
|
||||
},
|
||||
onSuccess: (updatedData) => {
|
||||
// Update the cache with new data
|
||||
queryClient.setQueryData(['admin-storage', adminPubkey], updatedData);
|
||||
},
|
||||
});
|
||||
|
||||
// Helper functions for specific data updates
|
||||
const updateHiddenEvents = (hiddenEvents: string[]) =>
|
||||
saveAdminData.mutateAsync({ hiddenEvents });
|
||||
|
||||
const updateAllowList = (allowList: string[]) =>
|
||||
saveAdminData.mutateAsync({ allowList });
|
||||
|
||||
const updateDenyList = (denyList: string[]) =>
|
||||
saveAdminData.mutateAsync({ denyList });
|
||||
|
||||
const updateBlockedWords = (blockedWords: string[]) =>
|
||||
saveAdminData.mutateAsync({ blockedWords });
|
||||
|
||||
const updateModerationSettings = (settings: {
|
||||
caseSensitive?: boolean;
|
||||
wholeWordsOnly?: boolean;
|
||||
enableModeration?: boolean;
|
||||
autoHideBlocked?: boolean;
|
||||
requireApproval?: boolean;
|
||||
}) => saveAdminData.mutateAsync(settings);
|
||||
|
||||
const updateSiteSettings = (settings: {
|
||||
siteName?: string;
|
||||
siteDescription?: string;
|
||||
}) => saveAdminData.mutateAsync(settings);
|
||||
|
||||
return {
|
||||
// Data
|
||||
data: adminDataQuery.data || defaultAdminData,
|
||||
isLoading: adminDataQuery.isLoading,
|
||||
error: adminDataQuery.error,
|
||||
|
||||
// Auth
|
||||
isAdmin,
|
||||
adminPubkey,
|
||||
|
||||
// Mutations
|
||||
saveAdminData: saveAdminData.mutateAsync,
|
||||
isSaving: saveAdminData.isPending,
|
||||
|
||||
// Helper functions
|
||||
updateHiddenEvents,
|
||||
updateAllowList,
|
||||
updateDenyList,
|
||||
updateBlockedWords,
|
||||
updateModerationSettings,
|
||||
updateSiteSettings,
|
||||
};
|
||||
}
|
||||
|
||||
// Hook for checking if content should be hidden based on admin policies
|
||||
export function useContentFilter() {
|
||||
const { data: adminData } = useAdminStorage();
|
||||
|
||||
const isContentHidden = (eventId: string): boolean => {
|
||||
return adminData.hiddenEvents.includes(eventId);
|
||||
};
|
||||
|
||||
const isUserBlocked = (pubkey: string): boolean => {
|
||||
// If allow list exists and user is not on it, they're blocked
|
||||
if (adminData.allowList.length > 0 && !adminData.allowList.includes(pubkey)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// If user is on deny list, they're blocked
|
||||
return adminData.denyList.includes(pubkey);
|
||||
};
|
||||
|
||||
const isContentBlocked = (content: string): boolean => {
|
||||
if (!adminData.enableModeration || adminData.blockedWords.length === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const text = adminData.caseSensitive ? content : content.toLowerCase();
|
||||
|
||||
return adminData.blockedWords.some(word => {
|
||||
const searchWord = adminData.caseSensitive ? word : word.toLowerCase();
|
||||
|
||||
if (adminData.wholeWordsOnly) {
|
||||
// Match whole words only
|
||||
const regex = new RegExp(`\\b${searchWord.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\b`);
|
||||
return regex.test(text);
|
||||
} else {
|
||||
// Match partial words
|
||||
return text.includes(searchWord);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const shouldHideContent = (eventId: string, pubkey: string, content: string): boolean => {
|
||||
if (!adminData.enableModeration) return false;
|
||||
|
||||
// Check if manually hidden
|
||||
if (isContentHidden(eventId)) return true;
|
||||
|
||||
// Check if user is blocked
|
||||
if (isUserBlocked(pubkey)) return true;
|
||||
|
||||
// Check if content contains blocked words and auto-hide is enabled
|
||||
if (adminData.autoHideBlocked && isContentBlocked(content)) return true;
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
return {
|
||||
isContentHidden,
|
||||
isUserBlocked,
|
||||
isContentBlocked,
|
||||
shouldHideContent,
|
||||
adminData,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user