@ -1,10 +1,13 @@
import { AbortSignal } from 'abort-controller' ;
import { AbortSignal } from 'abort-controller' ;
import { Data } from '../../../../data/data' ;
import { Data } from '../../../../data/data' ;
import { ConversationModel } from '../../../../models/conversation' ;
import { Action , OpenGroupReactionResponse , Reaction } from '../../../../types/Reaction' ;
import { Action , OpenGroupReactionResponse , Reaction } from '../../../../types/Reaction' ;
import { getEmojiDataFromNative } from '../../../../util/emoji' ;
import { getEmojiDataFromNative } from '../../../../util/emoji' ;
import { h itRateLimit } from '../../../../util/reactions' ;
import { h andleMessageReaction, h itRateLimit } from '../../../../util/reactions' ;
import { OnionSending } from '../../../onions/onionSend' ;
import { OnionSending } from '../../../onions/onionSend' ;
import { UserUtils } from '../../../utils' ;
import { OpenGroupPollingUtils } from '../opengroupV2/OpenGroupPollingUtils' ;
import { OpenGroupPollingUtils } from '../opengroupV2/OpenGroupPollingUtils' ;
import { getUsBlindedInThatServer } from './knownBlindedkeys' ;
import { batchGlobalIsSuccess , parseBatchGlobalStatusCode } from './sogsV3BatchPoll' ;
import { batchGlobalIsSuccess , parseBatchGlobalStatusCode } from './sogsV3BatchPoll' ;
import {
import {
addToMutationCache ,
addToMutationCache ,
@ -13,25 +16,27 @@ import {
updateMutationCache ,
updateMutationCache ,
} from './sogsV3MutationCache' ;
} from './sogsV3MutationCache' ;
export const hasReactionSupport = async ( serverId : number ) : Promise < boolean > = > {
export const hasReactionSupport = async (
serverId : number
) : Promise < { supported : boolean ; conversation : ConversationModel | null } > = > {
const found = await Data . getMessageByServerId ( serverId ) ;
const found = await Data . getMessageByServerId ( serverId ) ;
if ( ! found ) {
if ( ! found ) {
window . log . warn ( ` Open Group Message ${ serverId } not found in db ` ) ;
window . log . warn ( ` Open Group Message ${ serverId } not found in db ` ) ;
return false ;
return { supported : false , conversation : null } ;
}
}
const conversationModel = found ? . getConversation ( ) ;
const conversationModel = found ? . getConversation ( ) ;
if ( ! conversationModel ) {
if ( ! conversationModel ) {
window . log . warn ( ` Conversation for ${ serverId } not found in db ` ) ;
window . log . warn ( ` Conversation for ${ serverId } not found in db ` ) ;
return false ;
return { supported : false , conversation : null } ;
}
}
if ( ! conversationModel . hasReactions ( ) ) {
if ( ! conversationModel . hasReactions ( ) ) {
window . log . warn ( "This open group doesn't have reaction support. Server Message ID" , serverId ) ;
window . log . warn ( "This open group doesn't have reaction support. Server Message ID" , serverId ) ;
return false ;
return { supported : false , conversation : null } ;
}
}
return true ;
return { supported : true , conversation : conversationModel } ;
} ;
} ;
export const sendSogsReactionOnionV4 = async (
export const sendSogsReactionOnionV4 = async (
@ -47,8 +52,8 @@ export const sendSogsReactionOnionV4 = async (
throw new Error ( ` Could not find sogs pubkey of url: ${ serverUrl } ` ) ;
throw new Error ( ` Could not find sogs pubkey of url: ${ serverUrl } ` ) ;
}
}
const canReact = await hasReactionSupport ( reaction . id ) ;
const { supported , conversation } = await hasReactionSupport ( reaction . id ) ;
if ( ! canReact ) {
if ( ! supported ) {
return false ;
return false ;
}
}
@ -56,6 +61,11 @@ export const sendSogsReactionOnionV4 = async (
return false ;
return false ;
}
}
if ( ! conversation ) {
window . log . warn ( ` Conversation for ${ reaction . id } not found in db ` ) ;
return false ;
}
// The SOGS endpoint supports any text input so we need to make sure we are sending a valid unicode emoji
// The SOGS endpoint supports any text input so we need to make sure we are sending a valid unicode emoji
// for an invalid input we use https://emojipedia.org/frame-with-an-x/ as a replacement since it cannot rendered as an emoji but is valid unicode
// for an invalid input we use https://emojipedia.org/frame-with-an-x/ as a replacement since it cannot rendered as an emoji but is valid unicode
const emoji = getEmojiDataFromNative ( reaction . emoji ) ? reaction . emoji : '🖾' ;
const emoji = getEmojiDataFromNative ( reaction . emoji ) ? reaction . emoji : '🖾' ;
@ -77,6 +87,15 @@ export const sendSogsReactionOnionV4 = async (
addToMutationCache ( cacheEntry ) ;
addToMutationCache ( cacheEntry ) ;
// Since responses can take a long time we immediately update the sender's UI and if there is a problem it is overwritten by handleOpenGroupMessageReactions later.
const me = UserUtils . getOurPubKeyStrFromCache ( ) ;
await handleMessageReaction ( {
reaction ,
sender : blinded ? getUsBlindedInThatServer ( conversation ) || me : me ,
you : true ,
isOpenGroup : true ,
} ) ;
// reaction endpoint requires an empty dict {}
// reaction endpoint requires an empty dict {}
const stringifiedBody = null ;
const stringifiedBody = null ;
const result = await OnionSending . sendJsonViaOnionV4ToSogs ( {
const result = await OnionSending . sendJsonViaOnionV4ToSogs ( {