chore: tidy up

This commit is contained in:
Ricardo Arturo Cabral Mejía 2022-11-15 21:06:17 -05:00
parent faa32c3f31
commit b77239a7c3
3 changed files with 24 additions and 33 deletions

View File

@ -63,16 +63,12 @@ export class EventMessageHandler implements IMessageHandler {
protected canAcceptEvent(event: Event): string | undefined {
const now = Math.floor(Date.now()/1000)
const limits = this.settings().limits.event
if (limits.createdAt.maxPositiveDelta > 0) {
if (event.created_at > now + limits.createdAt.maxPositiveDelta) {
return `rejected: created_at is more than ${limits.createdAt.maxPositiveDelta} seconds in the future`
}
if (limits.createdAt.maxPositiveDelta > 0 && event.created_at > now + limits.createdAt.maxPositiveDelta) {
return `rejected: created_at is more than ${limits.createdAt.maxPositiveDelta} seconds in the future`
}
if (limits.createdAt.maxNegativeDelta > 0) {
if (event.created_at < now - limits.createdAt.maxNegativeDelta) {
return `rejected: created_at is more than ${limits.createdAt.maxNegativeDelta} seconds in the past`
}
if (limits.createdAt.maxNegativeDelta > 0 && event.created_at < now - limits.createdAt.maxNegativeDelta) {
return `rejected: created_at is more than ${limits.createdAt.maxNegativeDelta} seconds in the past`
}
if (limits.eventId.minLeadingZeroBits > 0) {
@ -89,16 +85,18 @@ export class EventMessageHandler implements IMessageHandler {
}
}
if (limits.pubkey.whitelist.length > 0) {
if (!limits.pubkey.whitelist.some((prefix) => event.pubkey.startsWith(prefix))) {
return 'blocked: pubkey not allowed'
}
if (
limits.pubkey.whitelist.length > 0
&& !limits.pubkey.whitelist.some((prefix) => event.pubkey.startsWith(prefix))
) {
return 'blocked: pubkey not allowed'
}
if (limits.pubkey.blacklist.length > 0) {
if (limits.pubkey.blacklist.some((prefix) => event.pubkey.startsWith(prefix))) {
return 'blocked: pubkey not allowed'
}
if (
limits.pubkey.blacklist.length > 0
&& limits.pubkey.blacklist.some((prefix) => event.pubkey.startsWith(prefix))
) {
return 'blocked: pubkey not allowed'
}
const isEventKindMatch = (item: EventKinds | EventKindsRange) =>
@ -106,16 +104,12 @@ export class EventMessageHandler implements IMessageHandler {
? item === event.kind
: event.kind >= item[0] && event.kind <= item[1]
if (limits.kind.whitelist.length > 0) {
if (!limits.kind.whitelist.some(isEventKindMatch)) {
return `blocked: event kind ${event.kind} not allowed`
}
if (limits.kind.whitelist.length > 0 && !limits.kind.whitelist.some(isEventKindMatch)) {
return `blocked: event kind ${event.kind} not allowed`
}
if (limits.kind.blacklist.length > 0) {
if (limits.kind.blacklist.some(isEventKindMatch)) {
return `blocked: event kind ${event.kind} not allowed`
}
if (limits.kind.blacklist.length > 0 && limits.kind.blacklist.some(isEventKindMatch)) {
return `blocked: event kind ${event.kind} not allowed`
}
}
@ -152,13 +146,10 @@ export class EventMessageHandler implements IMessageHandler {
)
}
const hits = await Promise.all(
rateLimits
.map(async (rateLimit) => ({ ...rateLimit, active: await hit(rateLimit) }))
)
const hits = await Promise.all(rateLimits.map(hit))
debug('rate limit check %s: %o', event.pubkey, hits)
return hits.some(({ active }) => active)
return hits.some((active) => active)
}
}

View File

@ -33,7 +33,7 @@ export class SubscribeMessageHandler implements IMessageHandler, IAbortable {
public async handleMessage(message: SubscribeMessage): Promise<void> {
debug('received message: %o', message)
const subscriptionId = message[1] as SubscriptionId
const subscriptionId = message[1]
const filters = uniqWith(equals, message.slice(2)) as SubscriptionFilter[]
const reason = this.canSubscribe(subscriptionId, filters)

View File

@ -73,11 +73,11 @@ export class EventRepository implements IEventRepository {
evolve({
exact: (pubkeys: string[]) =>
tableFields.forEach((tableField) =>
void bd.orWhereIn(tableField, pubkeys.map(toBuffer))
bd.orWhereIn(tableField, pubkeys.map(toBuffer))
),
even: forEach((prefix: string) =>
tableFields.forEach((tableField) =>
void bd.orWhereRaw(
bd.orWhereRaw(
`substring("${tableField}" from 1 for ?) = ?`,
[prefix.length >> 1, toBuffer(prefix)]
)
@ -85,7 +85,7 @@ export class EventRepository implements IEventRepository {
),
odd: forEach((prefix: string) =>
tableFields.forEach((tableField) =>
void bd.orWhereRaw(
bd.orWhereRaw(
`substring("${tableField}" from 1 for ?) BETWEEN ? AND ?`,
[
(prefix.length >> 1) + 1,