Skip to content

Commit

Permalink
contact: set the lastSeen time for all kind of event
Browse files Browse the repository at this point in the history
MichaelMure committed Apr 16, 2018
1 parent 63a0e9e commit 5626980
Showing 7 changed files with 96 additions and 35 deletions.
61 changes: 33 additions & 28 deletions app/actions/chat.js
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ import type { Store } from 'utils/types'
import { mainWindowVisible } from 'utils/constants'
import createProtocol from 'ipfs/createProtocol'
import { nextToken } from 'utils/tokenGenerator'
import * as contactActions from 'actions/contact'

/// #if isElectron
import { ipcRenderer } from 'electron'
@@ -101,6 +102,24 @@ export function unsubscribe() {
}
}

export function sendChat(contact: Contact, message: string) {
return async function (dispatch, getState) {
console.log('Sending \'' + message + '\' to ' + contact.identity)

const state: Store = getState()
const messageId = nextToken()

const data = protocol.chat(
messageId,
state.profile,
message
)

await dispatch(priv.chatSent(contact, messageId, message))
return dispatch(pubsub.send(contact.chatPubsubTopic, data))
}
}

function handleMessage(dispatch, getState, payload) {
const {id, from, message} = payload

@@ -116,6 +135,8 @@ function handleMessage(dispatch, getState, payload) {
dispatch(priv.chatReceived(contact, id, message))
dispatch(sendChatAck(contact, id))

dispatch(contactActions.onAliveWithContact(contact))

/// #if isElectron
if(!ipcRenderer.sendSync(mainWindowVisible)) {
new Notification(contact.identity, {
@@ -126,6 +147,17 @@ function handleMessage(dispatch, getState, payload) {
/// #endif
}

export function sendChatAck(contact: Contact, id: string) {
return async function (dispatch, getState) {
console.log('Sending message ACK ' + id + ' to ' + contact.identity)

const state: Store = getState()
const data = protocol.chatAck(id, state.profile)

await dispatch(pubsub.send(contact.chatPubsubTopic, data))
}
}

function handleAck(dispatch, getState, payload) {
const {id, from} = payload

@@ -139,33 +171,6 @@ function handleAck(dispatch, getState, payload) {

console.log('Received ACK with id ' + id + ' from ' + from)
dispatch(priv.chatAckReceived(contact, id))
}

export function sendChat(contact: Contact, message: string) {
return async function (dispatch, getState) {
console.log('Sending \'' + message + '\' to ' + contact.identity)

const state: Store = getState()
const messageId = nextToken()

const data = protocol.chat(
messageId,
state.profile,
message
)

await dispatch(priv.chatSent(contact, messageId, message))
return dispatch(pubsub.send(contact.chatPubsubTopic, data))
}
}

export function sendChatAck(contact: Contact, id: string) {
return async function (dispatch, getState) {
console.log('Sending message ACK ' + id + ' to ' + contact.identity)

const state: Store = getState()
const data = protocol.chatAck(id, state.profile)

await dispatch(pubsub.send(contact.chatPubsubTopic, data))
}
dispatch(contactActions.onAliveWithContact(contact))
}
42 changes: 42 additions & 0 deletions app/actions/contact.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
// @flow
import { createAction } from 'redux-actions'
import Contact from 'models/Contact'
import ContactList from 'models/ContactList'
import { IpfsConnector } from '@michaelmure/ipfs-connector'
import { waitForIpfsReady } from 'ipfs/index'
import removeIpfsPrefix from 'utils/removeIpfsPrefix'
import * as chatActions from 'actions/chat'
import * as shareListActions from 'actions/shareList'
import * as contactListActions from 'actions/contactList'
import * as ipfsActions from './ipfs'

export const priv = {
isAlive: createAction('CONTACT_ISALIVE',
(pubkey: string) => ({pubkey})
),
}

export const updateContact = createAction('CONTACTLIST_CONTACT_UPDATE',
(contact: Contact) => (contact)
)
@@ -25,6 +35,7 @@ export const addedAck = createAction('CONTACT_ADDED_ACK',
(pubkey: string) => ({pubkey})
)


export function fetchProfile(pubkey: string) {
return async function (dispatch) {
console.log('fetch contact profile: ' + pubkey)
@@ -72,6 +83,37 @@ export function fetchProfileAvatar(pubkey: string, avatarHash: ?string) {
}
}

export function onAliveWithPubkey(pubkey: string, wasPing: boolean = false) {
return async function(dispatch, getState) {
if(!wasPing) {
await dispatch(priv.isAlive(pubkey))
}

const contactList : ContactList = getState().contactList
const contact: ?Contact = contactList.findContactInPool(pubkey)

if(!contact) {
return
}

dispatch(shareListActions.onContactAlive(contact))
dispatch(chatActions.onContactAlive(contact))
dispatch(contactListActions.onContactAlive(contact))
}
}

export function onAliveWithContact(contact: Contact, wasPing: boolean = false) {
return async function(dispatch) {
if(!wasPing) {
await dispatch(priv.isAlive(contact.pubkey))
}

dispatch(shareListActions.onContactAlive(contact))
dispatch(chatActions.onContactAlive(contact))
dispatch(contactListActions.onContactAlive(contact))
}
}

/**
* @HACK temporary fix around the bad double NAT connectivity
* Dial a relay connection to the known peer ID of the contact
14 changes: 8 additions & 6 deletions app/actions/contactList.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// @flow
import { createAction } from 'redux-actions'
import * as contactActions from 'actions/contact'
import * as chatActions from 'actions/chat'
import * as shareListActions from 'actions/shareList'
import * as contactResolver from 'actions/contactResolver'
import type { Store } from 'utils/types'
import ContactList from 'models/ContactList'
@@ -169,7 +167,7 @@ export function garbageCollectPool() {
}

// On a contact pong, inform the contact we have added him if needed
function onContactAlive(contact: Contact) {
export function onContactAlive(contact: Contact) {
return async function (dispatch) {
if (!contact.addedAck) {
return dispatch(addedAsContact(contact))
@@ -287,6 +285,8 @@ function handleQueryContacts(dispatch, getState, payload) {

const data = protocol.contactsReply(profile, contactList.publicContacts(state.settings))
dispatch(pubsub.send(contact.contactsPubsubTopic, data))

dispatch(contactActions.onAliveWithContact(contact))
}

function handleContactsReply(dispatch, getState, payload) {
@@ -373,9 +373,7 @@ function handlePong(dispatch, getState, payload) {
dispatch(contactActions.pingResult(contact.pubkey, true))

// trigger actions to be done when we find that a contact is online
dispatch(shareListActions.onContactAlive(contact))
dispatch(chatActions.onContactAlive(contact))
dispatch(onContactAlive(contact))
dispatch(contactActions.onAliveWithContact(contact, true))
}

export function addedAsContact(contact: Contact) {
@@ -398,6 +396,8 @@ function handleAddedContactQuery(dispatch, getState, payload) {
dispatch(storeAddedAsContact(from))
dispatch(fetchContactIfMissing(from))
dispatch(pubsub.send(Contact.contactsPubsubTopic(from), protocol.addedContactAck(profile)))

dispatch(contactActions.onAliveWithPubkey(from))
}

function handleAddedContactAck(dispatch, getState, payload) {
@@ -414,4 +414,6 @@ function handleAddedContactAck(dispatch, getState, payload) {
console.log(contact.identity + ' is aware we have added him as a contact')

dispatch(contactActions.addedAck(from))

dispatch(contactActions.onAliveWithContact(contact))
}
2 changes: 1 addition & 1 deletion app/actions/contactResolver.js
Original file line number Diff line number Diff line change
@@ -95,7 +95,7 @@ async function handleLookup(dispatch, getState, payload) {
return
}

console.log('Got a contact lookup query from ' + from)
console.log('Got a contact lookup query from ' + from + ' for ' + pubkey)

const state: Store = getState()
const profile = state.profile
7 changes: 7 additions & 0 deletions app/actions/shareList.js
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@ import { updateLocality } from 'actions/share'

/// #if isElectron
import { ipcRenderer } from 'electron'
import * as contactActions from './contact'
/// #endif

export const setFilter = createAction('SHARELIST_FILTER_SET',
@@ -203,6 +204,8 @@ function handleQueryShares(dispatch, getState, payload) {

const data = protocol.sharesReply(profile, shares)
dispatch(pubsub.send(contact.sharesPubsubTopic, data))

dispatch(contactActions.onAliveWithContact(contact))
}

async function handleSharesReply(dispatch, getState, payload) {
@@ -226,6 +229,8 @@ async function handleSharesReply(dispatch, getState, payload) {
}))

dispatch(contactListActions.fetchAllMissingContacts())

dispatch(contactActions.onAliveWithContact(contact))
}

export function sendShare(share: Share, pubkey: string) {
@@ -276,6 +281,7 @@ async function handleSharePush(dispatch, getState, payload) {
await handleNewShare(dispatch, contact, hash)

dispatch(contactListActions.fetchAllMissingContacts())
dispatch(contactActions.onAliveWithContact(contact))
}

// Helper to factorize the handling code for a new incoming Share
@@ -335,4 +341,5 @@ function handleShareAck(dispatch, getState, payload) {
}

dispatch(shareActions.setRecipientNotified(share, contact.pubkey))
dispatch(contactActions.onAliveWithContact(contact))
}
4 changes: 4 additions & 0 deletions app/reducers/contact.js
Original file line number Diff line number Diff line change
@@ -39,6 +39,10 @@ export default handleActions({
: newState
},

[contact.priv.isAlive]: (state: Contact, action: Action) => (
state.set(writable.lastSeen, Date.now())
),

[contact.addedAck]: (state: Contact, action: Action) => {
return state.set(writable.addedAck, true)
},
1 change: 1 addition & 0 deletions app/reducers/contactList.js
Original file line number Diff line number Diff line change
@@ -83,6 +83,7 @@ export default handleActions({
contact.setPingToken,
contact.pingResult,
contact.addedAck,
contact.priv.isAlive,
)] : (state: ContactList, action) => contactByPubkey(state, action)

}, initialState )

0 comments on commit 5626980

Please sign in to comment.