fix(p2p): DM attachment sync, download mesh, and user profile refresh
Gossip attachment meta on DM send, wire DM attach-mesh provider, fetch bytes from DM peers when not in a guild, expose usersById in view, and emit user-profile after ingest for live member names. Co-authored-by: Cursor <[email protected]>
This commit is contained in:
@@ -858,7 +858,23 @@ class PearcordPlatform extends EventEmitter {
|
||||
this.dm = null
|
||||
}
|
||||
|
||||
_dmAttachmentProvider () {
|
||||
return async (attachmentId) => {
|
||||
const row = await this.attachments?.get(attachmentId)
|
||||
if (!row) throw new Error('attachment not found')
|
||||
if (!this.attachments.hasLocalBytes(row)) {
|
||||
throw new Error('attachment file missing locally')
|
||||
}
|
||||
return this.attachments.readBytes(row)
|
||||
}
|
||||
}
|
||||
|
||||
_wireDM (dmInstance) {
|
||||
dmInstance.setAttachmentProvider(this._dmAttachmentProvider())
|
||||
dmInstance.on('attachment', (p) => {
|
||||
this.attachments?.ingestGossip(p).catch(() => {})
|
||||
this.emit('attachment', p)
|
||||
})
|
||||
dmInstance.setMessageHandler(async (msg) => {
|
||||
const existing = await this.db.get(COLLECTIONS.MESSAGES, {
|
||||
channelId: msg.channelId,
|
||||
@@ -3102,8 +3118,9 @@ class PearcordPlatform extends EventEmitter {
|
||||
if (!bundle) return 0
|
||||
const msgCount = bundle.messages?.length || 0
|
||||
const memberCount = bundle.members?.length || 0
|
||||
const userCount = bundle.users?.length || 0
|
||||
const hasIcon = !!bundle.guild?.iconHash
|
||||
if (!msgCount && !memberCount && !hasIcon) return 0
|
||||
if (!msgCount && !memberCount && !hasIcon && !userCount) return 0
|
||||
await this.guild.gossipGuildSync(bundle)
|
||||
return msgCount || memberCount
|
||||
}
|
||||
@@ -3386,7 +3403,7 @@ class PearcordPlatform extends EventEmitter {
|
||||
this.emit('presence', payload)
|
||||
})
|
||||
guildInstance.on('user', (u) => {
|
||||
this._ingestUserProfile(u).catch(() => {})
|
||||
void this._ingestUserProfile(u).catch(() => {})
|
||||
})
|
||||
guildInstance.on('profile-cosmetic', (payload) => {
|
||||
this._ingestPeerCosmetics(payload)
|
||||
@@ -3827,6 +3844,7 @@ class PearcordPlatform extends EventEmitter {
|
||||
const prev = await this.db.get(COLLECTIONS.USERS, { id: user.id })
|
||||
const row = { ...prev, ...user, id: user.id }
|
||||
await this.db.insert(COLLECTIONS.USERS, row)
|
||||
this.emit('user-profile', row)
|
||||
return row
|
||||
}
|
||||
|
||||
@@ -8779,12 +8797,26 @@ class PearcordPlatform extends EventEmitter {
|
||||
}
|
||||
|
||||
async _fetchAttachMeshWithRetry (attachmentId, timeoutMs = 14000) {
|
||||
if (!this.guild) return null
|
||||
let buf = await this.guild.fetchAttachmentFromPeers(attachmentId, timeoutMs).catch(() => null)
|
||||
if (buf?.length) return buf
|
||||
if (!(this.guild._attachChannels?.size > 0)) return null
|
||||
await new Promise((r) => setTimeout(r, 600))
|
||||
return this.guild.fetchAttachmentFromPeers(attachmentId, timeoutMs).catch(() => null)
|
||||
if (this.guild) {
|
||||
let buf = await this.guild.fetchAttachmentFromPeers(attachmentId, timeoutMs).catch(() => null)
|
||||
if (buf?.length) return buf
|
||||
if (this.guild._attachChannels?.size > 0) {
|
||||
await new Promise((r) => setTimeout(r, 600))
|
||||
buf = await this.guild.fetchAttachmentFromPeers(attachmentId, timeoutMs).catch(() => null)
|
||||
if (buf?.length) return buf
|
||||
}
|
||||
}
|
||||
const dm = this.dm || this._dmBg
|
||||
if (dm?.fetchAttachmentFromPeers) {
|
||||
let buf = await dm.fetchAttachmentFromPeers(attachmentId, timeoutMs).catch(() => null)
|
||||
if (buf?.length) return buf
|
||||
if (dm._attachChannels?.size > 0) {
|
||||
await new Promise((r) => setTimeout(r, 600))
|
||||
buf = await dm.fetchAttachmentFromPeers(attachmentId, timeoutMs).catch(() => null)
|
||||
if (buf?.length) return buf
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
async readAttachmentBytesWithSource (attachmentId) {
|
||||
@@ -8804,13 +8836,16 @@ class PearcordPlatform extends EventEmitter {
|
||||
await this.attachments.persistFetched(row, buf)
|
||||
return { buf, source: 'attach-mesh' }
|
||||
}
|
||||
if (isHexDriveKey(row.driveKey) && (this.guild?.peers?.size || 0) > 0) {
|
||||
const meshSwarm = this.guild?.swarm || this.dm?.swarm || this._dmBg?.swarm
|
||||
const meshPeers =
|
||||
(this.guild?.peers?.size || 0) + (this.dm?.peers?.size || 0) + (this._dmBg?.peers?.size || 0)
|
||||
if (isHexDriveKey(row.driveKey) && meshSwarm && meshPeers > 0) {
|
||||
buf = await drive.fetchRemote({
|
||||
driveKey: row.driveKey,
|
||||
drivePath: row.drivePath,
|
||||
attachmentId: row.id,
|
||||
filename: row.filename,
|
||||
swarm: this.guild?.swarm,
|
||||
swarm: meshSwarm,
|
||||
timeoutMs: 8000
|
||||
})
|
||||
if (buf?.length) {
|
||||
@@ -11843,7 +11878,7 @@ class PearcordPlatform extends EventEmitter {
|
||||
const row = await this.attachments.linkMessage(aid, msg.id)
|
||||
linked.push(row)
|
||||
if (this.guild) this.guild.gossipAttachmentMeta(row)
|
||||
else if (this.dm) this.dm.gossipAttachmentMeta?.(row)
|
||||
else if (this.dm) this.dm.gossipAttachmentMeta(row)
|
||||
}
|
||||
}
|
||||
await this._recordSlowmodeSend()
|
||||
@@ -13705,6 +13740,8 @@ class PearcordPlatform extends EventEmitter {
|
||||
if (rows.length) attachmentsByMessage[m.id] = rows
|
||||
}
|
||||
}
|
||||
const usersById =
|
||||
guild && members.length ? await this._usersById(members) : {}
|
||||
const memberTimeouts = {}
|
||||
let communicationDisabled = false
|
||||
if (guild && this.moderation) {
|
||||
@@ -14336,6 +14373,7 @@ class PearcordPlatform extends EventEmitter {
|
||||
guild,
|
||||
channels,
|
||||
members,
|
||||
usersById,
|
||||
guildCustomRoles,
|
||||
guildBuiltinRoles,
|
||||
permissionMeta: PERMISSION_META,
|
||||
|
||||
Reference in New Issue
Block a user