From e88b6d97eee0f7bbfb97cc1d8d5bdc1787d64a25 Mon Sep 17 00:00:00 2001 From: Guillaume Louvigny Date: Wed, 28 Nov 2018 00:42:55 +0100 Subject: [PATCH] fix(ui): Unicode handeling in Event.Attributes --- .../common/components/Screens/Chats/Detail.js | 6 ++---- .../common/graphql/subscriptions/ContactRequest.js | 5 ++--- .../graphql/subscriptions/ConversationInvite.js | 5 ++--- client/react-native/common/helpers/json.js | 11 +++++++++++ client/react-native/package.json | 1 + client/react-native/yarn.lock | 4 ++++ 6 files changed, 22 insertions(+), 10 deletions(-) create mode 100644 client/react-native/common/helpers/json.js diff --git a/client/react-native/common/components/Screens/Chats/Detail.js b/client/react-native/common/components/Screens/Chats/Detail.js index b58b09b405..ffda853c36 100644 --- a/client/react-native/common/components/Screens/Chats/Detail.js +++ b/client/react-native/common/components/Screens/Chats/Detail.js @@ -8,6 +8,7 @@ import { fragments } from '../../../graphql' import { merge } from '../../../helpers' import { shadow } from '../../../styles' import { conversation as utils } from '../../../utils' +import { parseEmbedded } from '../../../helpers/json' const Message = fragments.Event(props => { const conversation = props.navigation.getParam('conversation') @@ -36,10 +37,7 @@ const Message = fragments.Event(props => { [isMyself ? 'left' : 'right']: 42, }} > - { - JSON.parse(String.fromCharCode.apply(null, props.data.attributes)) - .message.text - } + {parseEmbedded(props.data.attributes).message.text} ({ ...EventStream(context), @@ -10,9 +11,7 @@ export default context => ({ updater && ((store, data) => { if (data.EventStream.kind === 201) { - const attributes = JSON.parse( - String.fromCharCode.apply(null, data.EventStream.attributes) - ) + const attributes = parseEmbedded(data.EventStream.attributes) attributes.me.id = btoa('contact:' + attributes.me.id) return updater && updater(store, attributes.me) } diff --git a/client/react-native/common/graphql/subscriptions/ConversationInvite.js b/client/react-native/common/graphql/subscriptions/ConversationInvite.js index e42123d040..d1ad175ebe 100644 --- a/client/react-native/common/graphql/subscriptions/ConversationInvite.js +++ b/client/react-native/common/graphql/subscriptions/ConversationInvite.js @@ -1,6 +1,7 @@ import { btoa } from 'b64-lite' import EventStream from './EventStream' +import { parseEmbedded } from '../../helpers/json' export default context => ({ ...EventStream(context), @@ -10,9 +11,7 @@ export default context => ({ updater && ((store, data) => { if (data.EventStream.kind === 301) { - const attributes = JSON.parse( - String.fromCharCode.apply(null, data.EventStream.attributes) - ) + const attributes = parseEmbedded(data.EventStream.attributes) attributes.conversation.id = btoa( 'conversation:' + attributes.conversation.id ) diff --git a/client/react-native/common/helpers/json.js b/client/react-native/common/helpers/json.js new file mode 100644 index 0000000000..86176e4c8f --- /dev/null +++ b/client/react-native/common/helpers/json.js @@ -0,0 +1,11 @@ +// eslint-disable-next-line +let TextDecoder = TextDecoder || undefined + +if (TextDecoder === undefined) { + const encoding = require('text-encoding') + TextDecoder = encoding.TextDecoder +} + +const decoder = new TextDecoder('utf-8') + +export const parseEmbedded = serialized => JSON.parse(decoder.decode(new Uint8Array(serialized))) diff --git a/client/react-native/package.json b/client/react-native/package.json index 71e121f3ce..f6b53c6ccf 100644 --- a/client/react-native/package.json +++ b/client/react-native/package.json @@ -38,6 +38,7 @@ "relay-compiler": "^1.6.2", "relay-devtools": "^1.4.0", "subscriptions-transport-ws": "^0.9.15", + "text-encoding": "^0.7.0", "whatwg-url": "^7.0.0" }, "devDependencies": { diff --git a/client/react-native/yarn.lock b/client/react-native/yarn.lock index a6f3fb8eee..41a4af784e 100644 --- a/client/react-native/yarn.lock +++ b/client/react-native/yarn.lock @@ -6056,6 +6056,10 @@ term-size@^1.2.0: dependencies: execa "^0.7.0" +text-encoding@^0.7.0: + version "0.7.0" + resolved "https://registry.yarnpkg.com/text-encoding/-/text-encoding-0.7.0.tgz#f895e836e45990624086601798ea98e8f36ee643" + text-table@^0.2.0, text-table@~0.2.0: version "0.2.0" resolved "https://registry.yarnpkg.com/text-table/-/text-table-0.2.0.tgz#7f5ee823ae805207c00af2df4a84ec3fcfa570b4"