remove sender/recipient state from convo agent

This commit is contained in:
Samuel Newman
2026-04-24 11:03:42 +03:00
parent 34cffc1c71
commit f513946bf5
3 changed files with 35 additions and 95 deletions
@@ -5,7 +5,6 @@ import {useLingui} from '@lingui/react'
import {type ActiveConvoStates} from '#/state/messages/convo' import {type ActiveConvoStates} from '#/state/messages/convo'
import {useModerationOpts} from '#/state/preferences/moderation-opts' import {useModerationOpts} from '#/state/preferences/moderation-opts'
import {useSession} from '#/state/session'
import {atoms as a, useTheme} from '#/alf' import {atoms as a, useTheme} from '#/alf'
import {LeaveConvoPrompt} from '#/components/dms/LeaveConvoPrompt' import {LeaveConvoPrompt} from '#/components/dms/LeaveConvoPrompt'
import {KnownFollowers} from '#/components/KnownFollowers' import {KnownFollowers} from '#/components/KnownFollowers'
@@ -16,16 +15,15 @@ export function ChatStatusInfo({convoState}: {convoState: ActiveConvoStates}) {
const t = useTheme() const t = useTheme()
const {_} = useLingui() const {_} = useLingui()
const moderationOpts = useModerationOpts() const moderationOpts = useModerationOpts()
const {currentAccount} = useSession()
const leaveConvoControl = usePromptControl() const leaveConvoControl = usePromptControl()
const onAcceptChat = useCallback(() => { const onAcceptChat = useCallback(() => {
convoState.markConvoAccepted() convoState.markConvoAccepted()
}, [convoState]) }, [convoState])
const otherUser = convoState.recipients.find( // either the other person, or the chat owner
user => user.did !== currentAccount?.did, // if we ever allow someone other than the owner to invite people, this will need to change
) const otherUser = convoState.convo.primaryMember
if (!moderationOpts) { if (!moderationOpts) {
return null return null
+32 -76
View File
@@ -177,6 +177,12 @@ export class Convo {
} }
private generateSnapshot(): ConvoState { private generateSnapshot(): ConvoState {
const shared = {
isFetchingHistory: this.isFetchingHistory,
// Explicit null check since the value is initially undefined.
hasAllHistory: this.oldestRev === null,
}
const methods = { const methods = {
deleteMessage: this.deleteMessage, deleteMessage: this.deleteMessage,
sendMessage: this.sendMessage, sendMessage: this.sendMessage,
@@ -186,6 +192,15 @@ export class Convo {
removeReaction: this.removeReaction, removeReaction: this.removeReaction,
} }
const emptyMethods = {
deleteMessage: undefined,
sendMessage: undefined,
fetchMessageHistory: undefined,
markConvoAccepted: undefined,
addReaction: undefined,
removeReaction: undefined,
}
switch (this.status) { switch (this.status) {
case ConvoStatus.Initializing: { case ConvoStatus.Initializing: {
return { return {
@@ -193,17 +208,8 @@ export class Convo {
items: [], items: [],
convo: this.convo, convo: this.convo,
error: undefined, error: undefined,
sender: this.sender, ...shared,
recipients: this.recipients, ...emptyMethods,
isFetchingHistory: this.isFetchingHistory,
// Explicit null check since the value is initially undefined.
hasAllHistory: this.oldestRev === null,
deleteMessage: undefined,
sendMessage: undefined,
fetchMessageHistory: undefined,
markConvoAccepted: undefined,
addReaction: undefined,
removeReaction: undefined,
} }
} }
case ConvoStatus.Disabled: { case ConvoStatus.Disabled: {
@@ -212,11 +218,7 @@ export class Convo {
items: this.getItems(), items: this.getItems(),
convo: this.convo!, convo: this.convo!,
error: undefined, error: undefined,
sender: this.sender!, ...shared,
recipients: this.recipients!,
isFetchingHistory: this.isFetchingHistory,
// Explicit null check since the value is initially undefined.
hasAllHistory: this.oldestRev === null,
...methods, ...methods,
} }
} }
@@ -226,11 +228,7 @@ export class Convo {
items: this.getItems(), items: this.getItems(),
convo: this.convo!, convo: this.convo!,
error: undefined, error: undefined,
sender: this.sender!, ...shared,
recipients: this.recipients!,
isFetchingHistory: this.isFetchingHistory,
// Explicit null check since the value is initially undefined.
hasAllHistory: this.oldestRev === null,
...methods, ...methods,
} }
} }
@@ -240,11 +238,7 @@ export class Convo {
items: this.getItems(), items: this.getItems(),
convo: this.convo!, convo: this.convo!,
error: undefined, error: undefined,
sender: this.sender!, ...shared,
recipients: this.recipients!,
isFetchingHistory: this.isFetchingHistory,
// Explicit null check since the value is initially undefined.
hasAllHistory: this.oldestRev === null,
...methods, ...methods,
} }
} }
@@ -254,11 +248,7 @@ export class Convo {
items: this.getItems(), items: this.getItems(),
convo: this.convo!, convo: this.convo!,
error: undefined, error: undefined,
sender: this.sender!, ...shared,
recipients: this.recipients!,
isFetchingHistory: this.isFetchingHistory,
// Explicit null check since the value is initially undefined.
hasAllHistory: this.oldestRev === null,
...methods, ...methods,
} }
} }
@@ -268,16 +258,9 @@ export class Convo {
items: [], items: [],
convo: undefined, convo: undefined,
error: this.error!, error: this.error!,
sender: undefined,
recipients: undefined,
isFetchingHistory: false, isFetchingHistory: false,
hasAllHistory: false, hasAllHistory: false,
deleteMessage: undefined, ...emptyMethods,
sendMessage: undefined,
fetchMessageHistory: undefined,
markConvoAccepted: undefined,
addReaction: undefined,
removeReaction: undefined,
} }
} }
default: { default: {
@@ -286,17 +269,10 @@ export class Convo {
items: [], items: [],
convo: this.convo, convo: this.convo,
error: undefined, error: undefined,
sender: this.sender,
recipients: this.recipients,
isFetchingHistory: false, isFetchingHistory: false,
// Explicit null check since the value is initially undefined. // Explicit null check since the value is initially undefined.
hasAllHistory: this.oldestRev === null, hasAllHistory: this.oldestRev === null,
deleteMessage: undefined, ...emptyMethods,
sendMessage: undefined,
fetchMessageHistory: undefined,
markConvoAccepted: undefined,
addReaction: undefined,
removeReaction: undefined,
} }
} }
} }
@@ -496,8 +472,6 @@ export class Convo {
private reset() { private reset() {
this.convo = undefined this.convo = undefined
this.sender = undefined
this.recipients = undefined
this.snapshot = undefined this.snapshot = undefined
this.status = ConvoStatus.Uninitialized this.status = ConvoStatus.Uninitialized
@@ -554,19 +528,13 @@ export class Convo {
data: NonNullable<ConvoParams['placeholderData']>, data: NonNullable<ConvoParams['placeholderData']>,
) { ) {
this.setConvo(data.convo) this.setConvo(data.convo)
this.sender = data.convo.members.find(m => m.did === this.senderUserDid)
this.recipients = data.convo.members.filter(
m => m.did !== this.senderUserDid,
)
} }
private async setup() { private async setup() {
try { try {
const {convo, sender, recipients} = await this.fetchConvo() const {convo} = await this.fetchConvo()
this.setConvo(convo) this.setConvo(convo)
this.sender = sender
this.recipients = recipients
/* /*
* Some validation prior to `Ready` status * Some validation prior to `Ready` status
@@ -574,14 +542,14 @@ export class Convo {
if (!this.convo) { if (!this.convo) {
throw new Error('could not find convo') throw new Error('could not find convo')
} }
if (!this.sender) {
throw new Error('could not find sender in convo') const self = this.convo.members.find(m => m.did === this.senderUserDid)
}
if (!this.recipients) { if (!self) {
throw new Error('could not find recipients in convo') throw new Error('could not find self in convo')
} }
const userIsDisabled = Boolean(this.sender.chatDisabled) const userIsDisabled = Boolean(self.chatDisabled)
if (userIsDisabled) { if (userIsDisabled) {
this.dispatch({event: ConvoDispatchEvent.Disable}) this.dispatch({event: ConvoDispatchEvent.Disable})
@@ -650,11 +618,7 @@ export class Convo {
} }
private pendingFetchConvo: private pendingFetchConvo:
| Promise<{ | Promise<{convo: ChatBskyConvoDefs.ConvoView}>
convo: ChatBskyConvoDefs.ConvoView
sender: ChatBskyActorDefs.ProfileViewBasic | undefined
recipients: ChatBskyActorDefs.ProfileViewBasic[]
}>
| undefined | undefined
async fetchConvo() { async fetchConvo() {
if (this.pendingFetchConvo) return this.pendingFetchConvo if (this.pendingFetchConvo) return this.pendingFetchConvo
@@ -674,8 +638,6 @@ export class Convo {
return { return {
convo, convo,
sender: convo.members.find(m => m.did === this.senderUserDid),
recipients: convo.members.filter(m => m.did !== this.senderUserDid),
} }
} finally { } finally {
this.pendingFetchConvo = undefined this.pendingFetchConvo = undefined
@@ -687,11 +649,9 @@ export class Convo {
async refreshConvo() { async refreshConvo() {
try { try {
const {convo, sender, recipients} = await this.fetchConvo() const {convo} = await this.fetchConvo()
// throw new Error('UNCOMMENT TO TEST REFRESH FAILURE') // throw new Error('UNCOMMENT TO TEST REFRESH FAILURE')
this.setConvo(convo) this.setConvo(convo)
this.sender = sender || this.sender
this.recipients = recipients || this.recipients
} catch (err) { } catch (err) {
const e = err as Error const e = err as Error
if (!isNetworkError(e) && !isErrorMaybeAppPasswordPermissions(e)) { if (!isNetworkError(e) && !isErrorMaybeAppPasswordPermissions(e)) {
@@ -1290,10 +1250,6 @@ export class Convo {
id: nanoid(), id: nanoid(),
rev: '__fake__', rev: '__fake__',
sentAt: new Date().toISOString(), sentAt: new Date().toISOString(),
/*
* `getItems` is only run in "active" status states, where
* `this.sender` is defined
*/
sender: { sender: {
$type: 'chat.bsky.convo.defs#messageViewSender', $type: 'chat.bsky.convo.defs#messageViewSender',
did: this.senderUserDid, did: this.senderUserDid,
-14
View File
@@ -157,8 +157,6 @@ export type ConvoStateUninitialized = {
items: [] items: []
convo: ConvoWithDetails | undefined convo: ConvoWithDetails | undefined
error: undefined error: undefined
sender: ChatBskyActorDefs.ProfileViewBasic | undefined
recipients: ChatBskyActorDefs.ProfileViewBasic[] | undefined
isFetchingHistory: false isFetchingHistory: false
hasAllHistory: boolean hasAllHistory: boolean
deleteMessage: undefined deleteMessage: undefined
@@ -173,8 +171,6 @@ export type ConvoStateInitializing = {
items: [] items: []
convo: ConvoWithDetails | undefined convo: ConvoWithDetails | undefined
error: undefined error: undefined
sender: ChatBskyActorDefs.ProfileViewBasic | undefined
recipients: ChatBskyActorDefs.ProfileViewBasic[] | undefined
isFetchingHistory: boolean isFetchingHistory: boolean
hasAllHistory: boolean hasAllHistory: boolean
deleteMessage: undefined deleteMessage: undefined
@@ -189,8 +185,6 @@ export type ConvoStateReady = {
items: ConvoItem[] items: ConvoItem[]
convo: ConvoWithDetails convo: ConvoWithDetails
error: undefined error: undefined
sender: ChatBskyActorDefs.ProfileViewBasic
recipients: ChatBskyActorDefs.ProfileViewBasic[]
isFetchingHistory: boolean isFetchingHistory: boolean
hasAllHistory: boolean hasAllHistory: boolean
deleteMessage: DeleteMessage deleteMessage: DeleteMessage
@@ -205,8 +199,6 @@ export type ConvoStateBackgrounded = {
items: ConvoItem[] items: ConvoItem[]
convo: ConvoWithDetails convo: ConvoWithDetails
error: undefined error: undefined
sender: ChatBskyActorDefs.ProfileViewBasic
recipients: ChatBskyActorDefs.ProfileViewBasic[]
isFetchingHistory: boolean isFetchingHistory: boolean
hasAllHistory: boolean hasAllHistory: boolean
deleteMessage: DeleteMessage deleteMessage: DeleteMessage
@@ -221,8 +213,6 @@ export type ConvoStateSuspended = {
items: ConvoItem[] items: ConvoItem[]
convo: ConvoWithDetails convo: ConvoWithDetails
error: undefined error: undefined
sender: ChatBskyActorDefs.ProfileViewBasic
recipients: ChatBskyActorDefs.ProfileViewBasic[]
isFetchingHistory: boolean isFetchingHistory: boolean
hasAllHistory: boolean hasAllHistory: boolean
deleteMessage: DeleteMessage deleteMessage: DeleteMessage
@@ -237,8 +227,6 @@ export type ConvoStateError = {
items: [] items: []
convo: undefined convo: undefined
error: ConvoError error: ConvoError
sender: undefined
recipients: undefined
isFetchingHistory: false isFetchingHistory: false
hasAllHistory: false hasAllHistory: false
deleteMessage: undefined deleteMessage: undefined
@@ -253,8 +241,6 @@ export type ConvoStateDisabled = {
items: ConvoItem[] items: ConvoItem[]
convo: ConvoWithDetails convo: ConvoWithDetails
error: undefined error: undefined
sender: ChatBskyActorDefs.ProfileViewBasic
recipients: ChatBskyActorDefs.ProfileViewBasic[]
isFetchingHistory: boolean isFetchingHistory: boolean
hasAllHistory: boolean hasAllHistory: boolean
deleteMessage: DeleteMessage deleteMessage: DeleteMessage