Created
May 23, 2016 18:56
-
-
Save alexkirsz/011ade925d7cd9a82c5e49b2a53bbc3c to your computer and use it in GitHub Desktop.
Core hook of the Facebook Sixth Sense Chrome extension
This file contains 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
function getUserId(fbid) { | |
return fbid.split(':')[1]; | |
} | |
requireLazy( | |
['MercuryTypingReceiver', 'MercuryThreads', 'ShortProfiles'], | |
(MercuryTypingReceiver, MercuryThreads, ShortProfiles) => { | |
MercuryTypingReceiver | |
.get() | |
.addRetroactiveListener('state-changed', onStateChanged); | |
// Called every time a user starts or stops typing in a thread | |
function onStateChanged(state) { | |
// State is a dictionary that maps thread ids to the list of the | |
// currently typing users ids' | |
const threadIds = Object.keys(state); | |
// Walk through all threads in order to retrieve a list of all | |
// user ids | |
const userIds = threadIds.reduce( | |
(res, threadId) => res.concat(state[threadId].map(getUserId)), | |
[] | |
); | |
MercuryThreads.get().getMultiThreadMeta(threadIds, threads => { | |
ShortProfiles.getMulti(userIds, users => { | |
// Now that we've retrieved all the information we need | |
// about the threads and the users, we send it to the | |
// Chrome application to process and display it to the user. | |
window.postMessage({ | |
type: 'update', | |
threads, | |
users, | |
state, | |
}, '*'); | |
}); | |
}); | |
} | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment