Skip to content

Nostr Cross Posting Delay #2206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions __mocks__/@/lib/nostr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// __mocks__/@/lib/nostr.js
const Nostr = {
get: () => ({
getSigner: () => ({}),
publish: jest.fn(async (event, opts) => {
if (global.__published) global.__published.push(event)
})
})
}
export default Nostr
47 changes: 47 additions & 0 deletions __tests__/nostrCrosspost.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// __tests__/nostrCrosspost.test.js
import { TextDecoder, TextEncoder } from 'util'
global.TextDecoder = TextDecoder
global.TextEncoder = TextEncoder

const mockNostr = {
get: () => ({
getSigner: () => ({}),
publish: jest.fn(async (event, opts) => {
if (global.__published) global.__published.push(event)
})
})
}

import { nostrCrosspost } from '../worker/nostrCrosspost'

describe('nostrCrosspost worker', () => {
let models, boss, published, updated

beforeEach(() => {
published = []
updated = []
global.__published = published
models = {
item: {
findMany: jest.fn(() => [
{ id: 1, title: 'Test', text: 'Body', pendingNostrCrosspost: true, nostrCrosspostAt: new Date(Date.now() - 1000) }
]),
update: jest.fn(({ where }) => { updated.push(where.id) })
}
}
boss = {}
})

it('crossposts eligible items and marks them as posted', async () => {
await nostrCrosspost({ boss, models, nostrLib: mockNostr })
expect(published.length).toBe(1)
expect(updated).toContain(1)
})

it('does nothing if no eligible items', async () => {
models.item.findMany = jest.fn(() => [])
await nostrCrosspost({ boss, models, nostrLib: mockNostr })
expect(published.length).toBe(0)
expect(updated.length).toBe(0)
})
})
18 changes: 15 additions & 3 deletions api/resolvers/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ export default {
${selectClause(type)}
${relationClause(type)}
${whereClause(
'"Item"."deletedAt" IS NULL',
'"Item"."deletedAt" IS NOT NULL',
'"Item"."weightedVotes" - "Item"."weightedDownVotes" > 2',
'"Item"."ncomments" > 0',
'"Item"."parentId" IS NULL',
Expand Down Expand Up @@ -1420,7 +1420,7 @@ export default {
}
}

export const updateItem = async (parent, { sub: subName, forward, hash, hmac, ...item }, { me, models, lnd }) => {
export const updateItem = async (parent, { sub: subName, forward, hash, hmac, crosspost, ...item }, { me, models, lnd }) => {
// update iff this item belongs to me
const old = await models.item.findUnique({ where: { id: Number(item.id) }, include: { invoice: true, sub: true } })

Expand Down Expand Up @@ -1487,13 +1487,19 @@ export const updateItem = async (parent, { sub: subName, forward, hash, hmac, ..
// never change author of item
item.userId = old.userId

// If the post is still within the 10-minute window and crosspost was requested, update the scheduled crosspost time to 10 minutes from now
if (old.pendingNostrCrosspost && old.nostrCrosspostAt && new Date() < old.nostrCrosspostAt) {
item.nostrCrosspostAt = new Date(Date.now() + 10 * 60 * 1000)
item.pendingNostrCrosspost = true
}

const resultItem = await performPaidAction('ITEM_UPDATE', item, { models, me, lnd })

resultItem.comments = []
return resultItem
}

export const createItem = async (parent, { forward, ...item }, { me, models, lnd }) => {
export const createItem = async (parent, { forward, crosspost, ...item }, { me, models, lnd }) => {
// rename to match column name
item.subName = item.sub
delete item.sub
Expand All @@ -1518,6 +1524,12 @@ export const createItem = async (parent, { forward, ...item }, { me, models, lnd
// mark item as created with API key
item.apiKey = me?.apiKey

// schedule Nostr crosspost if requested
if (crosspost) {
item.pendingNostrCrosspost = true
item.nostrCrosspostAt = new Date(Date.now() + 10 * 60 * 1000) // 10 minutes from now
}

const resultItem = await performPaidAction('ITEM_CREATE', item, { models, me, lnd })

resultItem.comments = []
Expand Down
Empty file added components/test
Empty file.
8 changes: 5 additions & 3 deletions components/use-item-submit.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,11 @@ export default function useItemSubmit (mutation,
const response = Object.values(data)[0]
const postId = response?.result?.id

if (crosspost && postId) {
await crossposter(postId)
}
// Do NOT crosspost to Nostr immediately. Instead, rely on backend to schedule crosspost after 10 minutes if crosspost is requested.
// (crosspost flag is already sent in upsertItem variables)
// if (crosspost && postId && !item?.id) {
// await crossposter(postId)
// }

toastUpsertSuccessMessages(toaster, data, Object.keys(data)[0], values.text)

Expand Down
5 changes: 4 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ const nextJest = require('next/jest')
const createJestConfig = nextJest({ dir: './' })

// createJestConfig is exported in this way to ensure that next/jest can load the Next.js configuration, which is async
module.exports = createJestConfig()
module.exports = createJestConfig({
testEnvironment: 'jsdom',
setupFiles: ['./jest.setup.js'],
})
4 changes: 4 additions & 0 deletions jest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// jest.setup.js
import { TextDecoder, TextEncoder } from 'util'
global.TextDecoder = TextDecoder
global.TextEncoder = TextEncoder
Loading