fix(blog): event fetch filter, editing as non-author, add errors
All checks were successful
Release to Staging / build_and_release (push) Successful in 55s
All checks were successful
Release to Staging / build_and_release (push) Successful in 55s
This commit is contained in:
parent
77b6aa0d75
commit
352179f1d9
@ -1,8 +1,7 @@
|
|||||||
import { filterForEventsTaggingId, NDKFilter } from '@nostr-dev-kit/ndk'
|
import { NDKFilter } from '@nostr-dev-kit/ndk'
|
||||||
import { NDKContextType } from 'contexts/NDKContext'
|
import { NDKContextType } from 'contexts/NDKContext'
|
||||||
import { kinds, nip19 } from 'nostr-tools'
|
import { kinds, nip19 } from 'nostr-tools'
|
||||||
import { LoaderFunctionArgs, redirect } from 'react-router-dom'
|
import { LoaderFunctionArgs, redirect } from 'react-router-dom'
|
||||||
import { toast } from 'react-toastify'
|
|
||||||
import { appRoutes } from 'routes'
|
import { appRoutes } from 'routes'
|
||||||
import { store } from 'store'
|
import { store } from 'store'
|
||||||
import { BlogPageLoaderResult, FilterOptions, NSFWFilter } from 'types'
|
import { BlogPageLoaderResult, FilterOptions, NSFWFilter } from 'types'
|
||||||
@ -16,28 +15,43 @@ import { extractBlogCardDetails, extractBlogDetails } from 'utils/blog'
|
|||||||
|
|
||||||
export const blogRouteLoader =
|
export const blogRouteLoader =
|
||||||
(ndkContext: NDKContextType) =>
|
(ndkContext: NDKContextType) =>
|
||||||
async ({ params }: LoaderFunctionArgs) => {
|
async ({ params, request }: LoaderFunctionArgs) => {
|
||||||
const { naddr } = params
|
const { naddr } = params
|
||||||
if (!naddr) {
|
if (!naddr) {
|
||||||
log(true, LogType.Error, 'Required naddr.')
|
log(true, LogType.Error, 'Required naddr.')
|
||||||
return redirect(appRoutes.blogs)
|
return redirect(appRoutes.blogs)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decode author from naddr
|
// Decode author and identifier from naddr
|
||||||
const decoded = nip19.decode<'naddr'>(naddr as `naddr1${string}`)
|
let pubkey: string | undefined
|
||||||
const { pubkey } = decoded.data
|
let identifier: string | undefined
|
||||||
|
try {
|
||||||
|
const decoded = nip19.decode<'naddr'>(naddr as `naddr1${string}`)
|
||||||
|
pubkey = decoded.data.pubkey
|
||||||
|
identifier = decoded.data.identifier
|
||||||
|
} catch (error) {
|
||||||
|
log(true, LogType.Error, `Failed to decode naddr: ${naddr}`, error)
|
||||||
|
throw new Error('Failed to fetch the blog. The address might be wrong')
|
||||||
|
}
|
||||||
|
|
||||||
|
const userState = store.getState().user
|
||||||
|
const loggedInUserPubkey = userState?.user?.pubkey as string | undefined
|
||||||
|
|
||||||
|
// Check if editing and the user is the original author
|
||||||
|
// Redirect if NOT
|
||||||
|
const url = new URL(request.url)
|
||||||
|
const isEditMode = url.pathname.endsWith('/edit')
|
||||||
|
if (isEditMode && loggedInUserPubkey !== pubkey) {
|
||||||
|
return redirect(appRoutes.blogs)
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Get the filter with #a from naddr for the main blog content
|
// Set the filter for the main blog content
|
||||||
const filter = filterForEventsTaggingId(naddr)
|
const filter = {
|
||||||
if (!filter) {
|
kinds: [kinds.LongFormArticle],
|
||||||
log(true, LogType.Error, 'Unable to create filter from blog naddr.')
|
authors: [pubkey],
|
||||||
return redirect(appRoutes.blogs)
|
'#d': [identifier]
|
||||||
}
|
}
|
||||||
// Update kinds to make sure we fetch correct event kind
|
|
||||||
filter.kinds = [kinds.LongFormArticle]
|
|
||||||
|
|
||||||
const userState = store.getState().user
|
|
||||||
|
|
||||||
// Get the blog filter options for latest blogs
|
// Get the blog filter options for latest blogs
|
||||||
const filterOptions = JSON.parse(
|
const filterOptions = JSON.parse(
|
||||||
@ -68,7 +82,7 @@ export const blogRouteLoader =
|
|||||||
const settled = await Promise.allSettled([
|
const settled = await Promise.allSettled([
|
||||||
ndkContext.fetchEvent(filter),
|
ndkContext.fetchEvent(filter),
|
||||||
ndkContext.fetchEvents(latestModsFilter),
|
ndkContext.fetchEvents(latestModsFilter),
|
||||||
ndkContext.getMuteLists(userState?.user?.pubkey as string),
|
ndkContext.getMuteLists(loggedInUserPubkey), // Pass pubkey for logged-in users
|
||||||
ndkContext.getNSFWList()
|
ndkContext.getNSFWList()
|
||||||
])
|
])
|
||||||
|
|
||||||
@ -93,6 +107,12 @@ export const blogRouteLoader =
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Throw an error if we are missing the main blog result
|
||||||
|
// Handle it with the react-router's errorComponent
|
||||||
|
if (!result.blog) {
|
||||||
|
throw new Error('We are unable to find the blog on the relays')
|
||||||
|
}
|
||||||
|
|
||||||
// Check the lateast blog events
|
// Check the lateast blog events
|
||||||
const fetchEventsResult = settled[1]
|
const fetchEventsResult = settled[1]
|
||||||
if (fetchEventsResult.status === 'fulfilled' && fetchEventsResult.value) {
|
if (fetchEventsResult.status === 'fulfilled' && fetchEventsResult.value) {
|
||||||
@ -165,13 +185,11 @@ export const blogRouteLoader =
|
|||||||
|
|
||||||
return result
|
return result
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log(
|
let message = 'An error occurred in fetching blog details from relays'
|
||||||
true,
|
log(true, LogType.Error, message, error)
|
||||||
LogType.Error,
|
if (error instanceof Error) {
|
||||||
'An error occurred in fetching blog details from relays',
|
message = error.message
|
||||||
error
|
throw new Error(message)
|
||||||
)
|
}
|
||||||
toast.error('An error occurred in fetching blog details from relays')
|
|
||||||
return redirect(appRoutes.blogs)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -103,13 +103,15 @@ export const routerWithNdkContext = (context: NDKContextType) =>
|
|||||||
path: appRoutes.blog,
|
path: appRoutes.blog,
|
||||||
element: <BlogPage />,
|
element: <BlogPage />,
|
||||||
loader: blogRouteLoader(context),
|
loader: blogRouteLoader(context),
|
||||||
action: blogRouteAction(context)
|
action: blogRouteAction(context),
|
||||||
|
errorElement: <NotFoundPage title={'Something went wrong.'} />
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: appRoutes.blogEdit,
|
path: appRoutes.blogEdit,
|
||||||
element: <WritePage key='edit' />,
|
element: <WritePage key='edit' />,
|
||||||
loader: blogRouteLoader(context),
|
loader: blogRouteLoader(context),
|
||||||
action: writeRouteAction(context)
|
action: writeRouteAction(context),
|
||||||
|
errorElement: <NotFoundPage title={'Something went wrong.'} />
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: appRoutes.blogReport_actionOnly,
|
path: appRoutes.blogReport_actionOnly,
|
||||||
|
Loading…
Reference in New Issue
Block a user