Skip to content

Commit

Permalink
fix(ui): Unicode handeling in Event.Attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume Louvigny committed Nov 27, 2018
1 parent 21e732b commit e88b6d9
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 10 deletions.
6 changes: 2 additions & 4 deletions client/react-native/common/components/Screens/Chats/Detail.js
Expand Up @@ -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')
Expand Down Expand Up @@ -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}
</Text>
<Text
left={!isMyself}
Expand Down
@@ -1,6 +1,7 @@
import { btoa } from 'b64-lite'

import EventStream from './EventStream'
import { parseEmbedded } from '../../helpers/json'

export default context => ({
...EventStream(context),
Expand All @@ -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)
}
Expand Down
@@ -1,6 +1,7 @@
import { btoa } from 'b64-lite'

import EventStream from './EventStream'
import { parseEmbedded } from '../../helpers/json'

export default context => ({
...EventStream(context),
Expand All @@ -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
)
Expand Down
11 changes: 11 additions & 0 deletions 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)))
1 change: 1 addition & 0 deletions client/react-native/package.json
Expand Up @@ -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": {
Expand Down
4 changes: 4 additions & 0 deletions client/react-native/yarn.lock
Expand Up @@ -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"
Expand Down

0 comments on commit e88b6d9

Please sign in to comment.