import { DeltaBackend , sendMessageParams } from '../../delta-remote' ; import ChatStore from '../../stores/chat' ; import { ScreenContext , unwrapContext } from '../../contexts' ; import { ChatListItemType , FullChat } from '../../../shared/shared-types' ; import { MuteDuration } from '../../../shared/constants' ; import { C } from 'deltachat-node/dist/constants' ; import { runtime } from '../../runtime' ; import { getLogger } from '../../../shared/logger' ; import AlertDialog from '../dialogs/AlertDialog' ; const log = getLogger ( 'renderer/message' ) ; type Chat = ChatListItemType | FullChat ; export const selectChat = ( chatId ) => { ChatStore . effect . selectChat ( chatId ) ; } ; export const unselectChat = ( ) => { ChatStore . reducer . unselectChat ( ) ; } ; export async function setChatVisibility ( chatId , visibility , shouldUnselectChat = false ) { await DeltaBackend . call ( 'chat.setVisibility' , chatId , visibility ) ; if ( shouldUnselectChat ) unselectChat ( ) ; } export function openLeaveChatDialog ( screenContext , chatId ) { const tx = window . static_translate ; screenContext . openDialog ( 'ConfirmationDialog' , { message : tx ( 'ask_leave_group' ) , confirmLabel : tx ( 'menu_leave_group' ) , isConfirmDanger : true , noMargin : true , cb : ( yes ) => yes && DeltaBackend . call ( 'chat.leaveGroup' , chatId ) , } ) ; } export function openDeleteChatDialog ( screenContext , chat , selectedChatId ) { const tx = window . static_translate ; screenContext . openDialog ( 'ConfirmationDialog' , { message : tx ( 'ask_delete_named_chat' , chat . name ) , confirmLabel : tx ( 'delete' ) , isConfirmDanger : true , cb : ( yes ) => yes && chat . id && DeltaBackend . call ( 'chat.delete' , chat . id ) . then ( ( ) => { if ( selectedChatId === chat . id ) { unselectChat ( ) ; } } ) , } ) ; } /**
 * used to block contacts based on a DM chat/chatlistentry with that contact
 */ export function openBlockFirstContactOfChatDialog ( screenContext , selectedChat ) { const tx = window . static_translate ; const contactIds = selectedChat ?. contactIds ; if ( contactIds ?. length ) { screenContext . openDialog ( 'ConfirmationDialog' , { message : tx ( 'ask_block_contact' ) , confirmLabel : tx ( 'menu_block_contact' ) , isConfirmDanger : true , cb : ( yes ) => yes && contactIds !== null && DeltaBackend . call ( 'contacts.blockContact' , contactIds [ 0 ] ) . then ( unselectChat ) , } ) ; } } export function openEncryptionInfoDialog ( screenContext , chatListItem ) { screenContext . openDialog ( 'EncryptionInfo' , { chatListItem } ) ; } export function openViewGroupDialog ( screenContext , selectedChat ) { screenContext . openDialog ( 'ViewGroup' , { chat : selectedChat } ) ; } export async function openViewProfileDialog ( screenContext , contact_id ) { screenContext . openDialog ( 'ViewProfile' , { contact : await DeltaBackend . call ( 'contacts.getContact' , contact_id ) , } ) ; } export async function openMuteChatDialog ( screenContext , chatId ) { screenContext . openDialog ( 'MuteChat' , { chatId } ) ; } export async function unMuteChat ( chatId ) { await DeltaBackend . call ( 'chat.setMuteDuration' , chatId , MuteDuration . OFF ) ; } export async function sendCallInvitation ( screenContext , chatId ) { try { const messageId = await DeltaBackend . call ( 'chat.sendVideoChatInvitation' , chatId ) ; await joinCall ( screenContext , messageId ) ; } catch ( error ) { log . error ( 'failed send call invitation' , error ) ; screenContext . openDialog ( AlertDialog , { message : error . toString ( ) } ) ; } } export async function joinCall ( screenContext , messageId ) { try { const message = await DeltaBackend . call ( 'messageList.getMessage' , messageId ) ; if ( ! message ) { throw new Error ( 'Message not found' ) ; } if ( message . viewType !== C . DC_MSG_VIDEOCHAT_INVITATION ) { throw new Error ( 'Message is not a video chat invitation' ) ; } return runtime . openLink ( message . videochatUrl ) ; } catch ( error ) { log . error ( 'failed to join call' , error ) ; screenContext . openDialog ( AlertDialog , { message : error . toString ( ) } ) ; } } export async function createChatByContactIdAndSelectIt ( contactId ) { const chatId = await DeltaBackend . call ( 'contacts.createChatByContactId' , contactId ) ; if ( ! chatId ) { throw new Error ( window . static_translate ( 'create_chat_error_desktop' ) ) ; } const chat = await DeltaBackend . call ( 'chatList.getFullChatById' , chatId ) ; if ( chat && chat . archived ) { log . debug ( 'chat was archived, unarchiving it' ) ; await DeltaBackend . call ( 'chat.setVisibility' , chatId , C . DC_CHAT_VISIBILITY_NORMAL ) ; } selectChat ( chatId ) ; } export function sendMessage ( chatId , message ) { ChatStore . effect . sendMessage ( { chatId , message } ) ; } export const deleteMessage = ( messageId ) => { ChatStore . effect . uiDeleteMessage ( messageId ) ; } ;