Skip to content

Commit

Permalink
fix(rn): conversation read
Browse files Browse the repository at this point in the history
Signed-off-by: Godefroy Ponsinet <godefroy.ponsinet@outlook.com>
  • Loading branch information
90dy committed Dec 11, 2018
1 parent 5ad485c commit d1bf266
Show file tree
Hide file tree
Showing 31 changed files with 831 additions and 125 deletions.
1 change: 0 additions & 1 deletion client/react-native/common/components/Screens/Chats/Add.js
Expand Up @@ -156,7 +156,6 @@ export default class ListScreen extends Component {
<Item
{...props}
onPress={() => {
console.log(props)
const index = contactsID.lastIndexOf(props.data.id)
index < 0
? contactsID.push(props.data.id)
Expand Down
47 changes: 21 additions & 26 deletions client/react-native/common/components/Screens/Chats/Detail.js
Expand Up @@ -13,31 +13,10 @@ import { parseEmbedded } from '../../../helpers/json'
class Message extends React.PureComponent {
static contextType = RelayContext

async componentDidMount () {
const conversation = this.props.navigation.getParam('conversation')
const contactId = this.props.data.senderId
const isMyself =
conversation.members.find(m => m.contactId === contactId).contact
.status === 42

if (isMyself || this.props.data.seenAt !== null) {
return
}

messageSeen = async () => {
await this.props.screenProps.context.mutations.eventSeen({
id: this.props.data.id,
})
await this.props.screenProps.context.mutations.conversationRead({
id: conversation.id,
})
}

async componentDidUpdate (prevProps) {
if (prevProps.data.id === this.props.data.id) {
return
}

await this.componentDidMount()
}

render () {
Expand All @@ -49,6 +28,10 @@ class Message extends React.PureComponent {

const { data } = this.props

// TODO: implement message seen
// if (new Date(this.props.data.seenAt).getTime() <= 0) {
// this.messageSeen()
// }
return (
<Flex.Rows
align={isMyself ? 'end' : 'start'}
Expand Down Expand Up @@ -85,9 +68,15 @@ class Message extends React.PureComponent {
>
{new Date(data.createdAt).toTimeString()}{' '}
{isMyself ? (
<Icon name={data.ackedAt ? 'check-circle' : 'circle'} />
<Icon
name={
new Date(data.ackedAt).getTime() > 0 ? 'check-circle' : 'circle'
}
/>
) : null}{' '}
<Icon name={data.seenAt ? 'eye' : 'eye-off'} />{' '}
<Icon
name={new Date(data.seenAt).getTime() > 0 ? 'eye' : 'eye-off'}
/>{' '}
{/* TODO: used for debugging, remove me */}
</Text>
</Flex.Rows>
Expand All @@ -112,12 +101,18 @@ class Input extends PureComponent {
})
}

async componentWillUnmount () {
const conversation = this.props.navigation.getParam('conversation')
await this.props.screenProps.context.mutations.conversationRead({
id: conversation.id,
})
}

onSubmit = () => {
const { input } = this.state
this.setState({ input: '' }, async () => {
try {
const conversation = this.props.navigation.getParam('conversation')
console.log('conversation', conversation)
await this.props.screenProps.context.mutations.conversationAddMessage({
conversation: {
id: conversation.id,
Expand Down Expand Up @@ -231,7 +226,7 @@ export default class Detail extends PureComponent {
},
},
])}
subscriptions={[subscriptions.conversationNewMessage]}
subscriptions={[subscriptions.newMessage]}
fragment={fragments.EventList}
alias='EventList'
renderItem={props => (
Expand Down
16 changes: 10 additions & 6 deletions client/react-native/common/components/Screens/Chats/List.js
Expand Up @@ -11,7 +11,7 @@ import { conversation as utils } from '../../../utils'
const Item = fragments.Conversation(({ data, navigation }) => {
const { id, updatedAt, readAt } = data
const isRead = new Date(readAt).getTime() > 0
const isInvite = new Date(updatedAt).getTime() > 0 && !isRead
const isInvite = !isRead && new Date(updatedAt).getTime() <= 0
return (
<Flex.Cols
align='center'
Expand All @@ -31,10 +31,10 @@ const Item = fragments.Conversation(({ data, navigation }) => {
{utils.getTitle(data)}
</Text>
<Text color={colors.subtleGrey} tiny middle left bold={!isRead}>
{isInvite
? 'New conversation'
: isRead
? 'No new message'
{isRead
? 'No new message'
: isInvite
? 'New conversation'
: 'You have a new message'}
</Text>
</Flex.Rows>
Expand Down Expand Up @@ -81,7 +81,11 @@ export default class ListScreen extends PureComponent {
variables={queries.ConversationList.defaultVariables}
fragment={fragments.ConversationList}
alias='ConversationList'
subscriptions={[subscriptions.conversationInvite]}
subscriptions={[
subscriptions.conversationInvite,
subscriptions.conversationNewMessage,
subscriptions.conversationRead,
]}
renderItem={props => <Item {...props} navigation={navigation} />}
/>
</Screen>
Expand Down
Expand Up @@ -16,7 +16,6 @@ export default class DetailPublicKey extends PureComponent {
})

render () {
console.log(this.props)
const id = this.props.navigation.getParam('id')
const displayName = this.props.navigation.getParam('displayName')

Expand Down
Expand Up @@ -76,7 +76,6 @@ export default class ContactList extends PureComponent {
context: { queries, subscriptions },
},
} = this.props
console.log(this.context)
return (
<Screen style={{ backgroundColor: colors.white }}>
<Pagination
Expand Down
Expand Up @@ -35,7 +35,6 @@ export default class DeviceInfos extends PureComponent {
},
} = this.props
this.setState({ refreshing: true }, async () => {
console.log(queries)
const data = await queries.DeviceInfos.fetch()
this.setState({ infos: data.infos, refreshing: false })
})
Expand Down
4 changes: 4 additions & 0 deletions client/react-native/common/graphql/enums.gen.js
Expand Up @@ -205,12 +205,14 @@ export const BertyP2pKindInputKind = {
Sent: 101,
Ack: 102,
Ping: 103,
Seen: 104,
ContactRequest: 201,
ContactRequestAccepted: 202,
ContactShareMe: 203,
ContactShare: 204,
ConversationInvite: 301,
ConversationNewMessage: 302,
ConversationRead: 303,
DevtoolsMapset: 401,
SenderAliasUpdate: 501,
Node: 99,
Expand All @@ -221,12 +223,14 @@ export const ValueBertyP2pKindInputKind = {
101: 'Sent',
102: 'Ack',
103: 'Ping',
104: 'Seen',
201: 'ContactRequest',
202: 'ContactRequestAccepted',
203: 'ContactShareMe',
204: 'ContactShare',
301: 'ConversationInvite',
302: 'ConversationNewMessage',
303: 'ConversationRead',
401: 'DevtoolsMapset',
501: 'SenderAliasUpdate',
99: 'Node',
Expand Down
Expand Up @@ -55,7 +55,6 @@ export default context => (input, configs) =>
input,
{
updater: (store, data) =>
console.log('ConversationCreate', data) ||
context.updaters.conversationList.forEach(updater =>
updater(store, data.ConversationCreate)
),
Expand Down
2 changes: 1 addition & 1 deletion client/react-native/common/graphql/queries/Conversation.js
Expand Up @@ -71,5 +71,5 @@ export default context => ({
context.environment,
query,
merge([defaultVariables, variables])
)).GetConversation,
)).Conversation,
})
Expand Up @@ -12,7 +12,6 @@ export default context => ({
(async (store, data) => {
if (data.EventStream.kind === 201) {
const attributes = parseEmbedded(data.EventStream.attributes)
console.log(attributes)
const id = btoa('contact:' + attributes.me.id)
updater(store, { id })
await context.queries.Contact.fetch({
Expand Down
Expand Up @@ -8,7 +8,6 @@ export default context => ({
updater &&
(async (store, data) => {
if (data.EventStream.kind === 302) {
updater(store, data.EventStream)
await context.queries.Conversation.fetch({
id: data.EventStream.conversationId,
})
Expand Down
@@ -0,0 +1,23 @@
import EventStream from './EventStream'

export default context => ({
...EventStream(context),
subscribe: ({ updater }) =>
EventStream(context).subscribe({
updater:
updater &&
(async (store, data) => {
if (data.EventStream.kind === 303) {
await context.queries.Conversation.fetch({
id: data.EventStream.conversationId,
})
await context.queries.EventList.fetch({
filter: {
conversationId: data.EventStream.conversationId,
kind: 302,
},
})
}
}),
}),
})
39 changes: 39 additions & 0 deletions client/react-native/common/graphql/subscriptions/EventSeen.js
@@ -0,0 +1,39 @@
import { parseEmbedded } from '../../helpers/json'
import EventStream from './EventStream'

export default context => ({
...EventStream(context),
subscribe: ({ updater }) =>
EventStream(context).subscribe({
updater:
updater &&
(async (store, data) => {
if (data.EventStream.kind === 104) {
const attributes = parseEmbedded(data.EventStream.attributes)

// update all messages
const events = []
attributes.ids &&
attributes.ids.forEach(async id => {
try {
events.push(await context.queries.Event.fetch({ id }))
} catch (err) {
console.warn(err)
}
})

// update all conversations
events.reduce((conversations, event) => {
if (conversations[event.conversationId]) {
return conversations
}
const promise = context.queries.Conversation.fetch({
id: data.EventStream.conversationId,
})
conversations[event.conversationId] = promise
return conversations
}, {})
}
}),
}),
})
Expand Up @@ -25,11 +25,10 @@ const EventStream = graphql`
}
`

let _context = null
let _subscriber = null

export default context => {
if (subscriber === null || context !== _context) {
if (_subscriber === null) {
_subscriber = subscriber({
environment: context.environment,
subscription: EventStream,
Expand Down
15 changes: 15 additions & 0 deletions client/react-native/common/graphql/subscriptions/NewMessage.js
@@ -0,0 +1,15 @@
import EventStream from './EventStream'

export default context => ({
...EventStream(context),
subscribe: ({ updater }) =>
EventStream(context).subscribe({
updater:
updater &&
(async (store, data) => {
if (data.EventStream.kind === 302) {
updater(store, data.EventStream)
}
}),
}),
})
3 changes: 3 additions & 0 deletions client/react-native/common/graphql/subscriptions/index.js
@@ -1,7 +1,10 @@
export eventStream from './EventStream'
export eventSeen from './EventSeen'
export contactRequest from './ContactRequest'
export contactRequestAccepted from './ContactRequestAccepted'
export conversationInvite from './ConversationInvite'
export conversationNewMessage from './ConversationNewMessage'
export conversationRead from './ConversationRead'
export newMessage from './NewMessage'
export monitorPeers from './MonitorPeers'
export logStream from './LogStream'
1 change: 0 additions & 1 deletion client/react-native/common/relay/RelayContext.js
Expand Up @@ -31,5 +31,4 @@ export const contextValue = ({
}

const RelayContext = React.createContext()
console.log(RelayContext)
export default RelayContext
2 changes: 0 additions & 2 deletions client/react-native/common/relay/environment.js
Expand Up @@ -111,8 +111,6 @@ const setupMiddlewares = async ({ getIp, getPort }) => [
]

export const setup = async ({ getIp, getPort }) => {
const store = new Store(new RecordSource())
console.log(store, store.getSource())
return new Environment({
network: new RelayNetworkLayer(await setupMiddlewares({ getIp, getPort }), {
subscribeFn: setupSubscription({
Expand Down
7 changes: 1 addition & 6 deletions client/react-native/common/relay/genericUpdater.js
Expand Up @@ -33,7 +33,6 @@ export default (fragment, alias, args) => {
return (store, data, deletion) => {
const helper = new FragmentHelper(fragment)
const connectionHelper = helper.getConnection(alias)
console.log(alias, data, args)

const root = store.getRoot()

Expand Down Expand Up @@ -76,11 +75,7 @@ export default (fragment, alias, args) => {

if (
edges.length > 0 &&
edges.some(
e =>
console.log(e.getLinkedRecord('node')) ||
e.getLinkedRecord('node').getValue('id') === data.id
)
edges.some(e => e.getLinkedRecord('node').getValue('id') === data.id)
) {
// update
return
Expand Down
6 changes: 6 additions & 0 deletions client/react-native/common/schema.graphql

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d1bf266

Please sign in to comment.