Ejemplo n.º 1
0
def spread_status(status):
    r = redis.StrictRedis(host=os.environ.get('REDIS_HOST', 'localhost'))
    time = status.created_at.timestamp()

    #Populate all the followers timelines
    json_file = json.dumps(status.to_json(), default=str)
    for follower in status.user.followers():
        TimelineManager(follower).push_home(status)
        # Add it to notifications
        r.publish(f'timeline:{follower.id}', f'update {json_file}')

    #Add id to the own timeline
    TimelineManager(status.user).push_home(status)
    r.publish(f'timeline:{status.user.id}', f'update {json_file}')
Ejemplo n.º 2
0
    async def get(self, user):

        #r = await aioredis.create_connection(
        #    f"redis://{os.environ.get('REDIS_HOST', 'localhost')}",
        #    loop=IOLoop.current().asyncio_loop,
        #    encoding='utf-8'
        #)

        local = self.get_argument('media_ids', False)
        max_id = self.get_argument('max_id', None)
        since_id = self.get_argument('since_id', None)
        limit = self.get_argument('limit', 20)

        statuses = []
        errors = 0

        for post in TimelineManager(user).query(since_id=since_id,
                                                max_id=max_id,
                                                local=True,
                                                limit=limit):
            status = Status.get_or_none(id=int(post))
            if status:
                json_data = status.to_json()
                count = await self.application.objects.count(
                    Like.select().join(UserProfile).switch(Like).join(
                        Status).switch(Like).where(Like.user.id == user.id,
                                                   Like.status.id == status))
                if count:
                    json_data["favourited"] = True

                statuses.append(json_data)

        self.write(json.dumps(statuses, default=str))
Ejemplo n.º 3
0
    def get(self, username):
        user = User.get_or_none(username=username)
        pagination = ap_pagination(self)
        if user:
            user = user.profile.get()

            # Retrive statuses
            objects = []
            for k in TimelineManager(user).query():
                try:
                    objects.append(
                        Status.get(
                            Status.identifier == status).to_activitystream())
                except:
                    pass

            print(objects)
            # create collection page
            collectionPage = activities.OrderedCollectionPage(
                map(activities.Note, objects), **pagination)

            # create collection
            collection = activities.OrderedCollection([collectionPage])
            self.write(json.dumps(collectionPage.to_json(context=True)))
            self.set_status(200)
Ejemplo n.º 4
0
def remove_status(status):
    r = redis.StrictRedis(host=os.environ.get('REDIS_HOST', 'localhost'))

    # Remove it from the own timeline
    TimelineManager(status.user).remove_from_home(status)
    # Remove it from the followers timeline
    for follower in status.user.followers():
        TimelineManager(follower).remove_from_home(status)

    #Update the user posts count

    UserProfile.update({
        UserProfile.statuses_count:
        UserProfile.statuses_count - 1
    }).where(UserProfile.id == status.user.id).execute()

    # Remove the status
    status.delete_instance(recursive=True)
Ejemplo n.º 5
0
    def on_get(self, req, resp, username):
        user = User.get_or_none(username=username)

        if user:
            user = user.profile.get()

            objects = [
                Status.get_by_id(int(status))
                for status in TimelineManager(user).query() if status
            ]
            collectionPage = activities.OrderedCollectionPage(
                map(activities.Note, objects))
            collection = activities.OrderedCollection([collectionPage])
            resp.body = json.dumps(collection.to_json(context=True))
            resp.status = falcon.HTTP_200
Ejemplo n.º 6
0
def like_status(status, user):

    """
        status - the status target of the like - Status
        user - the user liking the post - UserProfile
    """

    r = redis.StrictRedis(host=os.environ.get('REDIS_HOST', 'localhost'))
    
    #Add id to the like timeline
    TimelineManager(status.user).push_likes(status)
    
    notification = Notification.create(
        user = user,
        target = status.user,
        status = status,
        notification_type = notification_types['like']
    )

    json_file = json.dumps(status.json(), default=str)

    r.publish(f'timeline:{status.user.id}', f'notification {json_file}')
Ejemplo n.º 7
0
def spread_status(status: Status,
                  mentions: List[str],
                  ap_spread: bool = False) -> None:
    """
    Given a status we have to: 

    - Populate users timelines
    - Notifiy mentions
    - Send via AP TODO

    if we are spreading to local follers (for example from a external
    request) the ap_spread should be false but if the local user is the
    one creating the spread tag we should use ap_spread = true

    """
    r = redis.StrictRedis(host=os.environ.get('REDIS_HOST', 'localhost'))
    time = status.created_at.timestamp()

    #Populate all the followers timelines
    json_file = json.dumps(status.to_json(), default=str)
    for follower in status.user.followers():
        TimelineManager(follower).push_home(status)
        # Add it to notifications
        r.publish(f'timeline:{follower.id}', f'update {json_file}')

    #Add id to the own timeline
    TimelineManager(status.user).push_home(status)
    r.publish(f'timeline:{status.user.id}', f'update {json_file}')

    # Notify mentioned people

    remote = []

    # Notify mentions
    for user in mentions:
        # We have two formats: local user and remote user
        # if the user is local the user string is of the from
        # 'username' otherwise is '*****@*****.**'

        #target_user = User.get_or_none(User.username==user)
        target_user = ActivityPubId(user).get_or_create_remote_user()
        if target_user:
            # First we check that there is an user with this username
            # and then we get it's profile
            if not target_user.is_remote:
                NotificationManager(
                    status.user).create_mention_notification(target_user)
            else:
                remote.append(target_user)

    # Spread via AP
    if ap_spread:
        create = generate_create_note(status, remote)

        # Can this be the coolest var in the project?
        # it's just a set of addresses. Here I'm taking care
        # to minimize the number of requests to external actors
        # targeting shared inboxes (like a pro, look at me mom)

        targets_spotted = set()

        remote_followers = (UserProfile.select().join(
            FollowerRelation,
            on=FollowerRelation.user).where((UserProfile.is_remote == True) & (
                FollowerRelation.follows == status.user)))

        for follower in remote_followers:
            if follower.public_inbox:
                targets_spotted.add(follower.public_inbox)
            else:
                targets_spotted.add(follower.uris.inbox)

        # We're ready mike, drop cargo to the target
        print(targets_spotted)
        for inbox in targets_spotted:
            push_to_remote_actor(inbox, create.to_json())
Ejemplo n.º 8
0
def remove_from_timeline(user, target):
    r = redis.StrictRedis(host=os.environ.get('REDIS_HOST', 'localhost'))
    for status in target.statuses:
        TimelineManager(user).remove_from_home(status)
Ejemplo n.º 9
0
    def _notify_user(self,notification: Notification) -> None:
        """
        Notify the correct user after creating the notification object
        """

        TimelineManager(notification.target).push_notification(notification)