Exemplo n.º 1
0
def serialize_post(post, authenticated_user):
    if not post:
        return None

    ret = {
        'id': post.post_id,
        'creationTime': post.creation_time,
        'lastEditTime': post.last_edit_time,
        'safety': SAFETY_MAP[post.safety],
        'source': post.source,
        'type': TYPE_MAP[post.type],
        'checksum': post.checksum,
        'fileSize': post.file_size,
        'canvasWidth': post.canvas_width,
        'canvasHeight': post.canvas_height,
        'contentUrl': get_post_content_url(post),
        'thumbnailUrl': get_post_thumbnail_url(post),
        'flags': post.flags,
        'tags': [tag.first_name for tag in post.tags],
        'relations': [rel.post_id for rel in post.relations],
        'notes': sorted(serialize_note(note) for note in post.notes),
        'user': users.serialize_user(post.user, authenticated_user),
        'score': post.score,
        'featureCount': post.feature_count,
        'lastFeatureTime': post.last_feature_time,
        'favoritedBy': [users.serialize_user(rel.user, authenticated_user) \
            for rel in post.favorited_by],
        'hasCustomThumbnail': files.has(get_post_thumbnail_backup_path(post)),
    }

    if authenticated_user:
        ret['ownScore'] = scores.get_score(post, authenticated_user)

    return ret
Exemplo n.º 2
0
def serialize_post(post, auth_user, options=None):
    return util.serialize_entity(
        post,
        {
            'id': lambda: post.post_id,
            'version': lambda: post.version,
            'creationTime': lambda: post.creation_time,
            'lastEditTime': lambda: post.last_edit_time,
            'safety': lambda: SAFETY_MAP[post.safety],
            'source': lambda: post.source,
            'type': lambda: TYPE_MAP[post.type],
            'mimeType': lambda: post.mime_type,
            'checksum': lambda: post.checksum,
            'fileSize': lambda: post.file_size,
            'canvasWidth': lambda: post.canvas_width,
            'canvasHeight': lambda: post.canvas_height,
            'contentUrl': lambda: get_post_content_url(post),
            'thumbnailUrl': lambda: get_post_thumbnail_url(post),
            'flags': lambda: post.flags,
            'tags': lambda: [
                tag.names[0].name for tag in tags.sort_tags(post.tags)],
            'relations': lambda: sorted(
                {
                    post['id']:
                        post for post in [
                            serialize_micro_post(rel, auth_user)
                            for rel in post.relations]
                }.values(),
                key=lambda post: post['id']),
            'user': lambda: users.serialize_micro_user(post.user, auth_user),
            'score': lambda: post.score,
            'ownScore': lambda: scores.get_score(post, auth_user),
            'ownFavorite': lambda: len([
                user for user in post.favorited_by
                if user.user_id == auth_user.user_id]
            ) > 0,
            'tagCount': lambda: post.tag_count,
            'favoriteCount': lambda: post.favorite_count,
            'commentCount': lambda: post.comment_count,
            'noteCount': lambda: post.note_count,
            'relationCount': lambda: post.relation_count,
            'featureCount': lambda: post.feature_count,
            'lastFeatureTime': lambda: post.last_feature_time,
            'favoritedBy': lambda: [
                users.serialize_micro_user(rel.user, auth_user)
                for rel in post.favorited_by
            ],
            'hasCustomThumbnail':
                lambda: files.has(get_post_thumbnail_backup_path(post)),
            'notes': lambda: sorted(
                [serialize_note(note) for note in post.notes],
                key=lambda x: x['polygon']),
            'comments': lambda: [
                comments.serialize_comment(comment, auth_user)
                for comment in sorted(
                    post.comments,
                    key=lambda comment: comment.creation_time)],
        },
        options)
Exemplo n.º 3
0
def serialize_comment(comment, authenticated_user):
    ret = {
        'id': comment.comment_id,
        'user': users.serialize_user(comment.user, authenticated_user),
        'post': posts.serialize_post(comment.post, authenticated_user),
        'text': comment.text,
        'creationTime': comment.creation_time,
        'lastEditTime': comment.last_edit_time,
    }
    if authenticated_user:
        ret['ownScore'] = scores.get_score(comment, authenticated_user)
    return ret
Exemplo n.º 4
0
def serialize_comment(comment, auth_user, options=None):
    return util.serialize_entity(
        comment,
        {
            'id': lambda: comment.comment_id,
            'user':
                lambda: users.serialize_micro_user(comment.user, auth_user),
            'postId': lambda: comment.post.post_id,
            'version': lambda: comment.version,
            'text': lambda: comment.text,
            'creationTime': lambda: comment.creation_time,
            'lastEditTime': lambda: comment.last_edit_time,
            'score': lambda: comment.score,
            'ownScore': lambda: scores.get_score(comment, auth_user),
        },
        options)
Exemplo n.º 5
0
 def serialize_own_score(self) -> Any:
     return scores.get_score(self.post, self.auth_user)
Exemplo n.º 6
0
 def serialize_own_score(self) -> Any:
     return scores.get_score(self.comment, self.auth_user)