@@ -63,20 +63,6 @@ const verifyQuotedMessage = async (
6363 return quotedMsg ;
6464} ;
6565
66-
67- // generate random id string for file names, function got from: https://stackoverflow.com/a/1349426/1851801
68- function makeRandomId ( length : number ) {
69- let result = '' ;
70- const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' ;
71- const charactersLength = characters . length ;
72- let counter = 0 ;
73- while ( counter < length ) {
74- result += characters . charAt ( Math . floor ( Math . random ( ) * charactersLength ) ) ;
75- counter += 1 ;
76- }
77- return result ;
78- }
79-
8066const verifyMediaMessage = async (
8167 msg : WbotMessage ,
8268 ticket : Ticket ,
@@ -90,13 +76,9 @@ const verifyMediaMessage = async (
9076 throw new Error ( "ERR_WAPP_DOWNLOAD_MEDIA" ) ;
9177 }
9278
93- let randomId = makeRandomId ( 5 ) ;
94-
9579 if ( ! media . filename ) {
9680 const ext = media . mimetype . split ( "/" ) [ 1 ] . split ( ";" ) [ 0 ] ;
97- media . filename = `${ randomId } -${ new Date ( ) . getTime ( ) } .${ ext } ` ;
98- } else {
99- media . filename = media . filename . split ( '.' ) . slice ( 0 , - 1 ) . join ( '.' ) + '.' + randomId + '.' + media . filename . split ( '.' ) . slice ( - 1 ) ;
81+ media . filename = `${ new Date ( ) . getTime ( ) } .${ ext } ` ;
10082 }
10183
10284 try {
@@ -128,38 +110,44 @@ const verifyMediaMessage = async (
128110 return newMessage ;
129111} ;
130112
113+
131114const verifyMessage = async (
132115 msg : WbotMessage ,
133116 ticket : Ticket ,
134117 contact : Contact
135- ) => {
136-
137- if ( msg . type === 'location' )
138- msg = prepareLocation ( msg ) ;
118+ ) => {
119+ if ( msg . type === "location" ) msg = prepareLocation ( msg ) ;
120+
139121
140122 const quotedMsg = await verifyQuotedMessage ( msg ) ;
141123 const messageData = {
142- id : msg . id . id ,
143- ticketId : ticket . id ,
144- contactId : msg . fromMe ? undefined : contact . id ,
145- body : msg . body ,
146- fromMe : msg . fromMe ,
147- mediaType : msg . type ,
148- read : msg . fromMe ,
149- quotedMsgId : quotedMsg ?. id
124+ id : msg . id . id ,
125+ ticketId : ticket . id ,
126+ contactId : msg . fromMe ? undefined : contact . id ,
127+ body : msg . body ,
128+ fromMe : msg . fromMe ,
129+ mediaType : msg . type ,
130+ read : msg . fromMe ,
131+ quotedMsgId : quotedMsg ?. id
150132 } ;
151133
152- await ticket . update ( { lastMessage : msg . type === "location" ? msg . location . description ? "Localization - " + msg . location . description . split ( '\\n' ) [ 0 ] : "Localization" : msg . body } ) ;
134+ await ticket . update ( {
135+ lastMessage :
136+ msg . type === "location"
137+ ? msg . location . options || "Localization"
138+ : msg . body
139+ } ) ;
153140
154141 await CreateMessageService ( { messageData } ) ;
155142} ;
156143
157144const prepareLocation = ( msg : WbotMessage ) : WbotMessage => {
158- let gmapsUrl = "https://maps.google.com/maps?q=" + msg . location . latitude + "%2C" + msg . location . longitude + "&z=17&hl=pt-BR" ;
159145
160- msg . body = "data:image/png;base64," + msg . body + "|" + gmapsUrl ;
161146
162- msg . body += "|" + ( msg . location . description ? msg . location . description : ( msg . location . latitude + ", " + msg . location . longitude ) )
147+ const gmapsUrl = "https://maps.google.com/maps?q=${msg.location.latitude}%2C${msg.location.longitude}&z=17" ;
148+ msg . body = 'data:image/png;base64,${msg.body}|${gmapsUrl}' ;
149+
150+ msg . body += "|" + ( msg . location . options ? msg . location . options : ( msg . location . latitude + ", " + msg . location . longitude ) )
163151
164152 return msg ;
165153} ;
0 commit comments