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:
@@ -27,6 +27,7 @@ const { PearcordInvite } = require('pearcord-invite')
|
|||||||
const {
|
const {
|
||||||
PearcordDM,
|
PearcordDM,
|
||||||
DmChannelMetaStore,
|
DmChannelMetaStore,
|
||||||
|
DmPollStore,
|
||||||
DM_GUILD_ID,
|
DM_GUILD_ID,
|
||||||
dmChannelId,
|
dmChannelId,
|
||||||
listDmConversations
|
listDmConversations
|
||||||
@@ -2715,6 +2716,47 @@ class PearcordPlatform extends EventEmitter {
|
|||||||
return this._dmMetaStore
|
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) {
|
_applyGlobalPresenceMeshFromPrefs (prefs) {
|
||||||
if (!this.contacts?.setGlobalPresenceMeshEnabled) return
|
if (!this.contacts?.setGlobalPresenceMeshEnabled) return
|
||||||
this.contacts.setGlobalPresenceMeshEnabled(prefs?.globalPresenceMeshEnabled !== false)
|
this.contacts.setGlobalPresenceMeshEnabled(prefs?.globalPresenceMeshEnabled !== false)
|
||||||
@@ -19760,7 +19802,9 @@ class PearcordPlatform extends EventEmitter {
|
|||||||
anonymous: !!anonymous,
|
anonymous: !!anonymous,
|
||||||
expiresAt: Date.now() + ttl
|
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) {
|
async voteDmPoll (messageId, optionIndex, voted = null) {
|
||||||
@@ -19798,6 +19842,21 @@ class PearcordPlatform extends EventEmitter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
const out = await this.toggleReaction(messageId, voteEmoji)
|
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 {
|
return {
|
||||||
...out,
|
...out,
|
||||||
unreadImpact: 'none',
|
unreadImpact: 'none',
|
||||||
@@ -19828,7 +19887,9 @@ class PearcordPlatform extends EventEmitter {
|
|||||||
expiresAt: poll.expiresAt,
|
expiresAt: poll.expiresAt,
|
||||||
closedAt: poll.closedAt
|
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) {
|
async closeDmPoll (messageId) {
|
||||||
@@ -19849,7 +19910,9 @@ class PearcordPlatform extends EventEmitter {
|
|||||||
expiresAt: poll.expiresAt,
|
expiresAt: poll.expiresAt,
|
||||||
closedAt: Date.now()
|
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) {
|
async deleteDmPoll (messageId) {
|
||||||
@@ -19862,6 +19925,8 @@ class PearcordPlatform extends EventEmitter {
|
|||||||
const poll = parseDmPollMessageContent(this._messagePlaintext(msg) || msg.content)
|
const poll = parseDmPollMessageContent(this._messagePlaintext(msg) || msg.content)
|
||||||
if (!poll) throw new Error('message is not a dm poll')
|
if (!poll) throw new Error('message is not a dm poll')
|
||||||
await this.deleteMessage(messageId)
|
await this.deleteMessage(messageId)
|
||||||
|
const store = await this._ensureDmPollStore().catch(() => null)
|
||||||
|
if (store) await store.delete(messageId).catch(() => {})
|
||||||
return { id: messageId, deleted: true }
|
return { id: messageId, deleted: true }
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -22800,6 +22865,7 @@ class PearcordPlatform extends EventEmitter {
|
|||||||
peers: []
|
peers: []
|
||||||
}
|
}
|
||||||
let dmMetaEngine = 'json'
|
let dmMetaEngine = 'json'
|
||||||
|
let dmPollEngine = 'json'
|
||||||
let userPrefsEngine = 'json'
|
let userPrefsEngine = 'json'
|
||||||
try {
|
try {
|
||||||
const metaStore = await this._ensureDmMetaStore()
|
const metaStore = await this._ensureDmMetaStore()
|
||||||
@@ -22807,6 +22873,12 @@ class PearcordPlatform extends EventEmitter {
|
|||||||
} catch {
|
} catch {
|
||||||
dmMetaEngine = 'json'
|
dmMetaEngine = 'json'
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
const pollStore = await this._ensureDmPollStore()
|
||||||
|
dmPollEngine = pollStore.engine || 'json'
|
||||||
|
} catch {
|
||||||
|
dmPollEngine = 'json'
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
const prefsMirror = await this._ensurePrefsMirrorStore()
|
const prefsMirror = await this._ensurePrefsMirrorStore()
|
||||||
userPrefsEngine = prefsMirror.engine || 'json'
|
userPrefsEngine = prefsMirror.engine || 'json'
|
||||||
@@ -22968,6 +23040,7 @@ class PearcordPlatform extends EventEmitter {
|
|||||||
permissionMeta: PERMISSION_META,
|
permissionMeta: PERMISSION_META,
|
||||||
dmConversations,
|
dmConversations,
|
||||||
dmMetaEngine,
|
dmMetaEngine,
|
||||||
|
dmPollEngine,
|
||||||
userPrefsEngine,
|
userPrefsEngine,
|
||||||
dmPinsEngine: this.dm?.pinsEngine || null,
|
dmPinsEngine: this.dm?.pinsEngine || null,
|
||||||
globalPresence,
|
globalPresence,
|
||||||
|
|||||||
Reference in New Issue
Block a user