Created
November 17, 2019 04:39
-
-
Save jgaskins/f864d31093566a468bde41e9a208cc8b to your computer and use it in GitHub Desktop.
Fetching a remote user
This file contains hidden or 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
// The $url is passed in as the only parameter to this query. The query | |
// handles everything else. | |
CALL apoc.load.jsonParams($url, {Accept: 'application/json'}, null, null, {}) YIELD value AS data | |
// Upsert the person into the DB, using the :Person label in case they aren't | |
// a fully-fledged account yet | |
MERGE (person:Person { id: data.id }) | |
ON CREATE SET | |
person.created_at = datetime() | |
// Go ahead and update even if this person exists in our DB since | |
// we're getting their latest attributes | |
SET | |
person:Account, | |
person:RemoteAccount, | |
person.handle = data.preferredUsername, | |
person.summary = data.summary, | |
person.icon = data.icon.url, | |
person.outbox_url = data.outbox, | |
person.followers_url = data.followers, | |
person.inbox_url = data.inbox, | |
person.shared_inbox = data.endpoints.sharedInbox, | |
person.display_name = data.name, | |
person.discoverable = coalesce(data.discoverable, false), | |
person.manually_approves_followers = data.manuallyApprovesFollowers, | |
person.updated_at = datetime() | |
// Upsert the various streams: | |
// - followers | |
// - inbox | |
// - shared inbox (when you post a message, you don't send a copy to all followers individually, you can instead post to a shared inbox at the server's top level and the server will distribute it to its local accounts) | |
// - outbox | |
MERGE (followers:Stream { id: data.followers }) | |
MERGE (person)-[:HAS_FOLLOWERS_STREAM]->(followers) | |
MERGE (inbox:Stream { id: data.inbox }) | |
MERGE (person)-[:HAS_INBOX_STREAM]->(inbox) | |
MERGE (shared_inbox:Stream { id: data.endpoints.sharedInbox }) | |
MERGE (person)-[:HAS_SHARED_INBOX_STREAM]->(shared_inbox) | |
MERGE (outbox:Stream { id: data.outbox }) | |
MERGE (person)-[:HAS_OUTBOX_STREAM]->(outbox) | |
// And then we return the :Person:Account:RemoteAccount node we've just upserted with fresh data from the remote server | |
RETURN person |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment