Wire DmPollStore sync and scenario fixtures into platform.

Sync poll rows on create/vote/edit/close/delete, expose export/import APIs and dmPollEngine in view.

Co-authored-by: Cursor <[email protected]>
This commit is contained in:
Raven Scott
2026-06-02 17:17:55 -04:00
co-authored by Cursor
parent a62b63b918
commit 2a48f91877
+76 -3
View File
@@ -27,6 +27,7 @@ const { PearcordInvite } = require('pearcord-invite')
const {
PearcordDM,
DmChannelMetaStore,
DmPollStore,
DM_GUILD_ID,
dmChannelId,
listDmConversations
@@ -2715,6 +2716,47 @@ class PearcordPlatform extends EventEmitter {
return this._dmMetaStore
}
async _ensureDmPollStore () {
if (this._dmPollStore) return this._dmPollStore
this._dmPollStore = new DmPollStore({
db: this.db,
storagePath: this.storagePath
})
await this._dmPollStore.ready()
return this._dmPollStore
}
async _syncDmPollStoreFromMessage (msg) {
if (!msg?.id || !msg.channelId) return null
const poll = parseDmPollMessageContent(this._messagePlaintext(msg) || msg.content)
if (!poll) return null
const store = await this._ensureDmPollStore()
return store.upsert({
messageId: msg.id,
channelId: msg.channelId,
guildId: msg.guildId || DM_GUILD_ID,
question: poll.question,
options: poll.options.map((o) => o.text),
multiSelect: !!poll.multiSelect,
anonymous: !!poll.anonymous,
expiresAt: poll.expiresAt || null,
closedAt: poll.closedAt || null,
createdAt: msg.createdAt || Date.now()
})
}
async exportDmPollScenarioFixtures ({ channelId = null } = {}) {
const store = await this._ensureDmPollStore()
return store.exportFixtures({
channelId: channelId || (this.mode === 'dm' ? this.activeChannelId : null)
})
}
async importDmPollScenarioFixtures (fixture = {}, opts = {}) {
const store = await this._ensureDmPollStore()
return store.importFixtures(fixture, opts)
}
_applyGlobalPresenceMeshFromPrefs (prefs) {
if (!this.contacts?.setGlobalPresenceMeshEnabled) return
this.contacts.setGlobalPresenceMeshEnabled(prefs?.globalPresenceMeshEnabled !== false)
@@ -19760,7 +19802,9 @@ class PearcordPlatform extends EventEmitter {
anonymous: !!anonymous,
expiresAt: Date.now() + ttl
})
return this.sendMessage(body)
const msg = await this.sendMessage(body)
await this._syncDmPollStoreFromMessage(msg).catch(() => {})
return msg
}
async voteDmPoll (messageId, optionIndex, voted = null) {
@@ -19798,6 +19842,21 @@ class PearcordPlatform extends EventEmitter {
}
}
const out = await this.toggleReaction(messageId, voteEmoji)
const resolvedOptionIndex = poll.options.findIndex((o) => o.emoji === voteEmoji)
const store = await this._ensureDmPollStore().catch(() => null)
if (store && resolvedOptionIndex >= 0) {
if (!poll.multiSelect && desired) await store.removeUserVotes(messageId, userId)
if (desired) {
await store.upsertVote({
channelId,
messageId,
userId,
optionIndex: resolvedOptionIndex
})
} else {
await store.removeVote({ channelId, messageId, userId, optionIndex: resolvedOptionIndex })
}
}
return {
...out,
unreadImpact: 'none',
@@ -19828,7 +19887,9 @@ class PearcordPlatform extends EventEmitter {
expiresAt: poll.expiresAt,
closedAt: poll.closedAt
})
return this.editMessage(messageId, body)
const edited = await this.editMessage(messageId, body)
await this._syncDmPollStoreFromMessage(edited).catch(() => {})
return edited
}
async closeDmPoll (messageId) {
@@ -19849,7 +19910,9 @@ class PearcordPlatform extends EventEmitter {
expiresAt: poll.expiresAt,
closedAt: Date.now()
})
return this.editMessage(messageId, body)
const closed = await this.editMessage(messageId, body)
await this._syncDmPollStoreFromMessage(closed).catch(() => {})
return closed
}
async deleteDmPoll (messageId) {
@@ -19862,6 +19925,8 @@ class PearcordPlatform extends EventEmitter {
const poll = parseDmPollMessageContent(this._messagePlaintext(msg) || msg.content)
if (!poll) throw new Error('message is not a dm poll')
await this.deleteMessage(messageId)
const store = await this._ensureDmPollStore().catch(() => null)
if (store) await store.delete(messageId).catch(() => {})
return { id: messageId, deleted: true }
}
@@ -22800,6 +22865,7 @@ class PearcordPlatform extends EventEmitter {
peers: []
}
let dmMetaEngine = 'json'
let dmPollEngine = 'json'
let userPrefsEngine = 'json'
try {
const metaStore = await this._ensureDmMetaStore()
@@ -22807,6 +22873,12 @@ class PearcordPlatform extends EventEmitter {
} catch {
dmMetaEngine = 'json'
}
try {
const pollStore = await this._ensureDmPollStore()
dmPollEngine = pollStore.engine || 'json'
} catch {
dmPollEngine = 'json'
}
try {
const prefsMirror = await this._ensurePrefsMirrorStore()
userPrefsEngine = prefsMirror.engine || 'json'
@@ -22968,6 +23040,7 @@ class PearcordPlatform extends EventEmitter {
permissionMeta: PERMISSION_META,
dmConversations,
dmMetaEngine,
dmPollEngine,
userPrefsEngine,
dmPinsEngine: this.dm?.pinsEngine || null,
globalPresence,