''' manage remote users '''
import requests
from urllib.parse import urlparse

from fedireads import models


def get_or_create_remote_user(actor):
    ''' look up a remote user or add them '''
    try:
        return models.User.objects.get(actor=actor)
    except models.User.DoesNotExist:
        pass

    # TODO: also bring in the user's prevous reviews and books

    # load the user's info from the actor url
    response = requests.get(
        actor,
        headers={'Accept': 'application/activity+json'}
    )
    if not response.ok:
        response.raise_for_status()
    data = response.json()

    # the webfinger format for the username.
    # TODO: get the user's domain in a better way
    actor_parts = urlparse(actor)
    username = '%s@%s' % (actor_parts.path.split('/')[-1], actor_parts.netloc)
    shared_inbox = data.get('endpoints').get('sharedInbox') if \
        data.get('endpoints') else None

    try:
        user = models.User.objects.create_user(
            username,
            '', '', # email and passwords are left blank
            actor=actor,
            name=data.get('name'),
            summary=data.get('summary'),
            inbox=data['inbox'], #fail if there's no inbox
            outbox=data['outbox'], # fail if there's no outbox
            shared_inbox=shared_inbox,
            # TODO: probably shouldn't bother to store this for remote users
            public_key=data.get('publicKey').get('publicKeyPem'),
            local=False
        )
    except KeyError:
        return False
    return user



954d88efb9b1290074da00f8d060d955706eabb1
<mask token>

<mask token>


def get_or_create_remote_user(actor):
    """ look up a remote user or add them """
    try:
        return models.User.objects.get(actor=actor)
    except models.User.DoesNotExist:
        pass
    response = requests.get(actor, headers={'Accept':
        'application/activity+json'})
    if not response.ok:
        response.raise_for_status()
    data = response.json()
    actor_parts = urlparse(actor)
    username = '%s@%s' % (actor_parts.path.split('/')[-1], actor_parts.netloc)
    shared_inbox = data.get('endpoints').get('sharedInbox') if data.get(
        'endpoints') else None
    try:
        user = models.User.objects.create_user(username, '', '', actor=
            actor, name=data.get('name'), summary=data.get('summary'),
            inbox=data['inbox'], outbox=data['outbox'], shared_inbox=
            shared_inbox, public_key=data.get('publicKey').get(
            'publicKeyPem'), local=False)
    except KeyError:
        return False
    return user

<mask token>
import requests
from urllib.parse import urlparse
from fedireads import models


def get_or_create_remote_user(actor):
    """ look up a remote user or add them """
    try:
        return models.User.objects.get(actor=actor)
    except models.User.DoesNotExist:
        pass
    response = requests.get(actor, headers={'Accept':
        'application/activity+json'})
    if not response.ok:
        response.raise_for_status()
    data = response.json()
    actor_parts = urlparse(actor)
    username = '%s@%s' % (actor_parts.path.split('/')[-1], actor_parts.netloc)
    shared_inbox = data.get('endpoints').get('sharedInbox') if data.get(
        'endpoints') else None
    try:
        user = models.User.objects.create_user(username, '', '', actor=
            actor, name=data.get('name'), summary=data.get('summary'),
            inbox=data['inbox'], outbox=data['outbox'], shared_inbox=
            shared_inbox, public_key=data.get('publicKey').get(
            'publicKeyPem'), local=False)
    except KeyError:
        return False
    return user

''' manage remote users '''
import requests
from urllib.parse import urlparse

from fedireads import models


def get_or_create_remote_user(actor):
    ''' look up a remote user or add them '''
    try:
        return models.User.objects.get(actor=actor)
    except models.User.DoesNotExist:
        pass

    # TODO: also bring in the user's prevous reviews and books

    # load the user's info from the actor url
    response = requests.get(
        actor,
        headers={'Accept': 'application/activity+json'}
    )
    if not response.ok:
        response.raise_for_status()
    data = response.json()

    # the webfinger format for the username.
    # TODO: get the user's domain in a better way
    actor_parts = urlparse(actor)
    username = '%s@%s' % (actor_parts.path.split('/')[-1], actor_parts.netloc)
    shared_inbox = data.get('endpoints').get('sharedInbox') if \
        data.get('endpoints') else None

    try:
        user = models.User.objects.create_user(
            username,
            '', '', # email and passwords are left blank
            actor=actor,
            name=data.get('name'),
            summary=data.get('summary'),
            inbox=data['inbox'], #fail if there's no inbox
            outbox=data['outbox'], # fail if there's no outbox
            shared_inbox=shared_inbox,
            # TODO: probably shouldn't bother to store this for remote users
            public_key=data.get('publicKey').get('publicKeyPem'),
            local=False
        )
    except KeyError:
        return False
    return user


