@@ -30,56 +30,95 @@ class MyClass: NSObject {
3030 }
3131
3232 @objc
33- func getMessage( messageID: String , content: String , type: String , userjid: String , completion: @escaping ( [ String : Any ] ? ) -> Void ) {
34- var dict = [ String: Any] ( )
35- removeInvalidMessage ( forIDs: [ messageID] , onCompletion: {
36- notification in
37-
38- switch ( type) {
39- case " text " :
40- let val = convertToDictionary ( text: self . run ( messageID: messageID, content: content) )
41- let nickName : String = val ? [ " nickName " ] as? String ?? " "
42- if let number = userjid. components ( separatedBy: " @ " ) . first, !number. isEmpty {
43- dict [ " nickName " ] = FlyEncryption . encryptDecryptData ( key: number, data: nickName, encrypt: false , isForProfileName: true )
44- } else {
45- dict [ " nickName " ] = nickName // Fallback in case `number` is nil or empty
46- }
47- dict [ " message " ] = val ? [ " message " ]
48- break ;
49- case " image " :
50- dict [ " message " ] = " 📷 Image "
51- break ;
52- case " video " :
53- dict [ " message " ] = " 📽️ Video "
54- break ;
55- case " audio " :
56- dict [ " message " ] = " 🎵 Audio "
57- break ;
58- case " file " :
59- dict [ " message " ] = " 📄 File "
60- break ;
61- case " location " :
62- dict [ " message " ] = " 📌 Location "
63- break ;
64- case " contact " :
65- dict [ " message " ] = " 👤 Contact "
66- break ;
67- case " recall " :
68- // Need to called for recall message
69- // removeNotification(messageId: messageID, n: notification)
70- dict [ " messageID " ] = messageID
71- dict [ " message " ] = " This message was deleted "
72- break ;
73- default :
74- dict [ " message " ] = " Unknown message format "
75- break
76- }
33+ func getMessage( userInfo: [ String : Any ] , completion: @escaping ( [ String : Any ] ? ) -> Void ) {
34+ var dict = [ String: Any] ( )
35+
36+ // Use empty strings if the keys are not present
37+ let messageID = userInfo [ " message_id " ] as? String ?? userInfo [ " notify_id " ] as? String ?? " "
38+ let content = userInfo [ " message_content " ] as? String ?? " "
39+ let chat_type = userInfo [ " chat_type " ] as? String ?? " "
40+ let group_name = userInfo [ " group_name " ] as? String ?? " "
41+ let type = userInfo [ " type " ] as? String ?? " "
42+ let userjid = userInfo [ " from_user " ] as? String ?? " "
43+
7744
78- DispatchQueue . main. asyncAfter ( deadline: DispatchTime . now ( ) + Double( 1.0 * Double( NSEC_PER_SEC) ) / Double( NSEC_PER_SEC) , execute: { [ self ] in
79- completion ( dict)
45+ if [ " added " , " removed " ] . contains ( where: { content. contains ( $0) } ) {
46+ if let aps = userInfo [ " aps " ] as? [ String : Any ] ,
47+ let alert = aps [ " alert " ] as? [ String : Any ] ,
48+ let body = alert [ " body " ] as? String ,
49+ let title = alert [ " title " ] as? String ,
50+ let groupName = userInfo [ " group_name " ] as? String {
51+
52+ let trimmedGroupName = groupName. trimmingCharacters ( in: . whitespacesAndNewlines)
53+ var cleanBody = body. trimmingCharacters ( in: . whitespacesAndNewlines)
54+
55+ // Build a regex to match any variation like "group_name:", "group_name :", "group_name : "
56+ let regexPattern = " ^ " + NSRegularExpression. escapedPattern ( for: trimmedGroupName) + " \\ s*: \\ s* "
57+
58+ if let regex = try ? NSRegularExpression ( pattern: regexPattern, options: [ . caseInsensitive] ) {
59+ let range = NSRange ( location: 0 , length: cleanBody. utf16. count)
60+ cleanBody = regex. stringByReplacingMatches ( in: cleanBody, options: [ ] , range: range, withTemplate: " " )
61+ . trimmingCharacters ( in: . whitespacesAndNewlines)
62+ }
63+
64+ dict [ " nickName " ] = title
65+ dict [ " message " ] = cleanBody
66+
67+ DispatchQueue . main. async {
68+ completion ( dict)
69+ }
70+ return
71+ }
72+ }
73+
74+ removeInvalidMessage ( forIDs: [ messageID] , onCompletion: { notification in
75+ switch type {
76+ case " text " :
77+ let val = convertToDictionary ( text: self . run ( messageID: messageID, content: content) )
78+ let rawNickName = val ? [ " nickName " ] as? String ?? " "
79+ let number = userjid. components ( separatedBy: " @ " ) . first ?? " "
80+
81+ var decryptedNickName = rawNickName
82+ if !number. isEmpty {
83+ decryptedNickName = FlyEncryption . encryptDecryptData ( key: number, data: rawNickName, encrypt: false , isForProfileName: true )
84+ }
85+
86+ // Optionally prepend group name if it's a group chat
87+ if chat_type == " normal " , let groupName = userInfo [ " group_name " ] as? String {
88+ dict [ " nickName " ] = " \( groupName) @ \( decryptedNickName) "
89+ } else {
90+ dict [ " nickName " ] = decryptedNickName
91+ }
92+
93+ dict [ " message " ] = val ? [ " message " ]
94+ case " image " :
95+ dict [ " message " ] = " 📷 Image "
96+ case " video " :
97+ dict [ " message " ] = " 📽️ Video "
98+ case " audio " :
99+ dict [ " message " ] = " 🎵 Audio "
100+ case " file " :
101+ dict [ " message " ] = " 📄 File "
102+ case " location " :
103+ dict [ " message " ] = " 📌 Location "
104+ case " contact " :
105+ dict [ " message " ] = " 👤 Contact "
106+ case " recall " :
107+ dict [ " messageID " ] = messageID
108+ dict [ " message " ] = " This message was deleted "
109+ default :
110+ dict [ " message " ] = " Unknown message format "
111+ }
112+
113+ if ( dict [ " nickName " ] == nil || ( dict [ " nickName " ] as? String ) ? . isEmpty == true ) ,
114+ let groupName = userInfo [ " group_name " ] as? String {
115+ dict [ " nickName " ] = groupName
116+ }
117+ // Call completion with the final dict
118+ DispatchQueue . main. asyncAfter ( deadline: DispatchTime . now ( ) + Double( 1.0 * Double( NSEC_PER_SEC) ) / Double( NSEC_PER_SEC) , execute: { [ self ] in
119+ completion ( dict)
120+ } )
80121 } )
81- // completion(dict)
82- } )
83122 }
84123}
85124
0 commit comments