Last active
November 3, 2021 15:22
-
-
Save hieptl/9e6ae5266f0398c47a2313528abee63e to your computer and use it in GitHub Desktop.
index.js - CometChatConversationListItem - Encrypted Chat App
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
async componentDidMount() { | |
const message = await this.getLastMessage(); | |
... | |
} | |
async componentDidUpdate(prevProps) { | |
... | |
if (previousItem !== currentItem) { | |
const message = await this.getLastMessage(); | |
... | |
} | |
... | |
} | |
getVirgilGroupInstance = async guid => { | |
try { | |
if (guid) { | |
const eThree = this.context.eThree; | |
let group = await eThree.getGroup(guid); | |
if (group) { | |
return group; | |
} | |
const conversationWith = this.props.conversation.conversationWith; | |
const ownerCard = await eThree.findUsers(conversationWith.owner); | |
group = await eThree.loadGroup(guid, ownerCard); | |
return group; | |
} | |
return null | |
} catch (error) { | |
return null; | |
} | |
} | |
decryptGroupMessage = async (message) => { | |
try { | |
if (message && message.data.text) { | |
const eThree = this.context.eThree; | |
const group = await this.getVirgilGroupInstance(message.receiverId); | |
const messageSender = await eThree.findUsers(message.sender.uid); | |
const decryptedMessage = await group.decrypt(message.data.text, messageSender); | |
return decryptedMessage; | |
} | |
return message.data.text; | |
} catch (error) { | |
return message.data.text; | |
} | |
} | |
decryptUserMessage = async (message) => { | |
try { | |
if (message && message.data.text) { | |
const eThree = this.context.eThree; | |
const identities = [message.sender.uid, message.receiverId]; | |
const findUserResults = await eThree.findUsers(identities); | |
const decryptedMessage = await eThree.authDecrypt(message.data.text, findUserResults[message.sender.uid]); | |
return decryptedMessage; | |
} | |
return message.data.text; | |
} catch (error) { | |
return message.data.text; | |
} | |
}; | |
decryptMessage = async (message) => { | |
try { | |
const receiverType = message.receiverType; | |
if (receiverType === 'group') { | |
return await this.decryptGroupMessage(message); | |
} else if (receiverType === 'user') { | |
return await this.decryptUserMessage(message); | |
} | |
return message.data.text; | |
} catch (error) { | |
return message.data.text; | |
} | |
}; | |
getLastMessage = async () => { | |
... | |
if (lastMessage && lastMessage.data.text) { | |
const decryptedMessage = await this.decryptMessage(lastMessage); | |
lastMessage.text = decryptedMessage; | |
lastMessage.data.text = decryptedMessage; | |
} | |
... | |
}; | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment