Guard fetchConvo
(cherry picked from commit 8385579d31500bb4bfb60afeecdc1eb3ddd7e747)
This commit is contained in:
+54
-18
@@ -236,6 +236,13 @@ export class Convo {
|
|||||||
private isProcessingPendingMessages = false
|
private isProcessingPendingMessages = false
|
||||||
private pendingPoll: Promise<void> | undefined
|
private pendingPoll: Promise<void> | undefined
|
||||||
private nextPoll: NodeJS.Timeout | undefined
|
private nextPoll: NodeJS.Timeout | undefined
|
||||||
|
private pendingConvoFetch:
|
||||||
|
| Promise<{
|
||||||
|
convo: ChatBskyConvoDefs.ConvoView
|
||||||
|
sender: AppBskyActorDefs.ProfileViewBasic | undefined
|
||||||
|
recipients: AppBskyActorDefs.ProfileViewBasic[]
|
||||||
|
}>
|
||||||
|
| undefined
|
||||||
|
|
||||||
convoId: string
|
convoId: string
|
||||||
convo: ChatBskyConvoDefs.ConvoView | undefined
|
convo: ChatBskyConvoDefs.ConvoView | undefined
|
||||||
@@ -524,11 +531,18 @@ export class Convo {
|
|||||||
|
|
||||||
private async setup() {
|
private async setup() {
|
||||||
try {
|
try {
|
||||||
await this.fetchConvo()
|
const {convo, sender, recipients} = await this.fetchConvo()
|
||||||
|
|
||||||
|
this.convo = convo
|
||||||
|
this.sender = sender
|
||||||
|
this.recipients = recipients
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Some validation prior to `Ready` status
|
* Some validation prior to `Ready` status
|
||||||
*/
|
*/
|
||||||
|
if (!this.convo) {
|
||||||
|
throw new Error('Convo: could not find convo')
|
||||||
|
}
|
||||||
if (!this.sender) {
|
if (!this.sender) {
|
||||||
throw new Error('Convo: could not find sender in convo')
|
throw new Error('Convo: could not find sender in convo')
|
||||||
}
|
}
|
||||||
@@ -573,29 +587,51 @@ export class Convo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fetchConvo() {
|
async fetchConvo() {
|
||||||
const response = await this.agent.api.chat.bsky.convo.getConvo(
|
if (this.pendingConvoFetch) return this.pendingConvoFetch
|
||||||
{
|
|
||||||
convoId: this.convoId,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
headers: {
|
|
||||||
Authorization: this.__tempFromUserDid,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
)
|
|
||||||
this.convo = response.data.convo
|
|
||||||
this.sender = this.convo.members.find(m => m.did === this.__tempFromUserDid)
|
|
||||||
this.recipients = this.convo.members.filter(
|
|
||||||
m => m.did !== this.__tempFromUserDid,
|
|
||||||
)
|
|
||||||
|
|
||||||
this.commit()
|
this.pendingConvoFetch = new Promise<{
|
||||||
|
convo: ChatBskyConvoDefs.ConvoView
|
||||||
|
sender: AppBskyActorDefs.ProfileViewBasic | undefined
|
||||||
|
recipients: AppBskyActorDefs.ProfileViewBasic[]
|
||||||
|
}>(async (resolve, reject) => {
|
||||||
|
try {
|
||||||
|
const response = await this.agent.api.chat.bsky.convo.getConvo(
|
||||||
|
{
|
||||||
|
convoId: this.convoId,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
Authorization: this.__tempFromUserDid,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
const convo = response.data.convo
|
||||||
|
|
||||||
|
resolve({
|
||||||
|
convo,
|
||||||
|
sender: convo.members.find(m => m.did === this.__tempFromUserDid),
|
||||||
|
recipients: convo.members.filter(
|
||||||
|
m => m.did !== this.__tempFromUserDid,
|
||||||
|
),
|
||||||
|
})
|
||||||
|
} catch (e) {
|
||||||
|
reject(e)
|
||||||
|
} finally {
|
||||||
|
this.pendingConvoFetch = undefined
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return this.pendingConvoFetch
|
||||||
}
|
}
|
||||||
|
|
||||||
async refreshConvo() {
|
async refreshConvo() {
|
||||||
try {
|
try {
|
||||||
await this.fetchConvo()
|
const {convo, sender, recipients} = await this.fetchConvo()
|
||||||
// throw new Error('UNCOMMENT TO TEST REFRESH FAILURE')
|
// throw new Error('UNCOMMENT TO TEST REFRESH FAILURE')
|
||||||
|
this.convo = convo || this.convo
|
||||||
|
this.sender = sender || this.sender
|
||||||
|
this.recipients = recipients || this.recipients
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
logger.error(`Convo: failed to refresh convo`)
|
logger.error(`Convo: failed to refresh convo`)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user