<commit_before># -*- coding: utf-8 -*-

import httplib
import logging
import uuid
import datetime

from flask import Blueprint, request, g, abort, url_for
from flask_restful import Api, Resource, reqparse

from drift.urlregistry import register_endpoints
from drift.auth.jwtchecker import current_user
from drift.core.extensions.schemachecker import simple_schema_request

from driftbase.db.models import Friendship, FriendInvite, CorePlayer


log = logging.getLogger(__name__)
bp = Blueprint("friendships", __name__)
api = Api(bp)


def get_player(player_id):
    player = g.db.query(CorePlayer).get(player_id)
    return player


class FriendshipsAPI(Resource):

    def get(self, player_id):
        """
        List my friends
        """
        if player_id != current_user["player_id"]:
            abort(httplib.FORBIDDEN, description="That is not your player!")

        left = g.db.query(Friendship.id, Friendship.player1_id, Friendship.player2_id).filter_by(player1_id=player_id, status="active")
        right = g.db.query(Friendship.id, Friendship.player2_id, Friendship.player1_id).filter_by(player2_id=player_id, status="active")
        friend_rows = left.union_all(right)
        friends = []
        for row in friend_rows:
            friendship_id = row[0]
            friend_id = row[2]
            friend = {
                "friend_id": friend_id,
                "player_url": url_for("players.player", player_id=friend_id, _external=True),
                "friendship_url": url_for("friendships.friendship", friendship_id=friendship_id, _external=True)
            }
            friends.append(friend)

        ret = friends
        return ret

    @simple_schema_request({
        "token": {"type": "string", },
    }, required=["token"])
    def post(self, player_id):
        """
        New friend
        """
        if player_id != current_user["player_id"]:
            abort(httplib.FORBIDDEN, description="That is not your player!")

        args = request.json
        invite_token=args.get("token")

        invite = g.db.query(FriendInvite).filter_by(token=invite_token).first()
        if invite is None:
            abort(httplib.NOT_FOUND, description="The invite was not found!")

        if invite.expiry_date < datetime.datetime.utcnow():
            abort(httplib.FORBIDDEN, description="The invite has expired!")

        if invite.deleted:
            abort(httplib.FORBIDDEN, description="The invite has been deleted!")

        friend_id = invite.issued_by_player_id
        left_id = player_id
        right_id = friend_id

        if left_id == right_id:
            abort(httplib.FORBIDDEN, description="You cannot befriend yourself!")

        if left_id > right_id:
            left_id, right_id = right_id, left_id

        existing_friendship = g.db.query(Friendship).filter(
            Friendship.player1_id == left_id,
            Friendship.player2_id == right_id
        ).first()
        if existing_friendship is not None:
            return {}, httplib.OK

        friendship = Friendship(player1_id=left_id, player2_id=right_id)
        g.db.add(friendship)
        g.db.commit()

        ret = {
            "friend_id": friend_id,
            "is_online": False,
            "url": url_for("friendships.friendship", friendship_id=friendship.id, _external=True),
            "messagequeue_url": url_for("messages.exchange", exchange="players", exchange_id=friend_id,
                                        _external=True) + "/{queue}",
        }

        return ret, httplib.CREATED


class FriendshipAPI(Resource):

    def delete(self, friendship_id):
        """
        Remove a friend
        """
        player_id = current_user["player_id"]

        friendship = g.db.query(Friendship).filter_by(id=friendship_id).first()
        if friendship is None:
            abort(httplib.NOT_FOUND)
        elif friendship.player1_id != player_id and friendship.player2_id != player_id:
            abort(httplib.FORBIDDEN)
        elif friendship.status == "deleted":
            return {}, httplib.GONE

        if friendship:
            friendship.status = "deleted"
            g.db.commit()

        return {}, httplib.NO_CONTENT
<commit_msg>Drop is_online data for friends<commit_after># -*- coding: utf-8 -*-

import httplib
import logging
import uuid
import datetime

from flask import Blueprint, request, g, abort, url_for
from flask_restful import Api, Resource, reqparse

from drift.urlregistry import register_endpoints
from drift.auth.jwtchecker import current_user
from drift.core.extensions.schemachecker import simple_schema_request

from driftbase.db.models import Friendship, FriendInvite, CorePlayer


log = logging.getLogger(__name__)
bp = Blueprint("friendships", __name__)
api = Api(bp)


def get_player(player_id):
    player = g.db.query(CorePlayer).get(player_id)
    return player


class FriendshipsAPI(Resource):

    def get(self, player_id):
        """
        List my friends
        """
        if player_id != current_user["player_id"]:
            abort(httplib.FORBIDDEN, description="That is not your player!")

        left = g.db.query(Friendship.id, Friendship.player1_id, Friendship.player2_id).filter_by(player1_id=player_id, status="active")
        right = g.db.query(Friendship.id, Friendship.player2_id, Friendship.player1_id).filter_by(player2_id=player_id, status="active")
        friend_rows = left.union_all(right)
        friends = []
        for row in friend_rows:
            friendship_id = row[0]
            friend_id = row[2]
            friend = {
                "friend_id": friend_id,
                "player_url": url_for("players.player", player_id=friend_id, _external=True),
                "friendship_url": url_for("friendships.friendship", friendship_id=friendship_id, _external=True)
            }
            friends.append(friend)

        ret = friends
        return ret

    @simple_schema_request({
        "token": {"type": "string", },
    }, required=["token"])
    def post(self, player_id):
        """
        New friend
        """
        if player_id != current_user["player_id"]:
            abort(httplib.FORBIDDEN, description="That is not your player!")

        args = request.json
        invite_token=args.get("token")

        invite = g.db.query(FriendInvite).filter_by(token=invite_token).first()
        if invite is None:
            abort(httplib.NOT_FOUND, description="The invite was not found!")

        if invite.expiry_date < datetime.datetime.utcnow():
            abort(httplib.FORBIDDEN, description="The invite has expired!")

        if invite.deleted:
            abort(httplib.FORBIDDEN, description="The invite has been deleted!")

        friend_id = invite.issued_by_player_id
        left_id = player_id
        right_id = friend_id

        if left_id == right_id:
            abort(httplib.FORBIDDEN, description="You cannot befriend yourself!")

        if left_id > right_id:
            left_id, right_id = right_id, left_id

        existing_friendship = g.db.query(Friendship).filter(
            Friendship.player1_id == left_id,
            Friendship.player2_id == right_id
        ).first()
        if existing_friendship is not None:
            return {}, httplib.OK

        friendship = Friendship(player1_id=left_id, player2_id=right_id)
        g.db.add(friendship)
        g.db.commit()

        ret = {
            "friend_id": friend_id,
            "url": url_for("friendships.friendship", friendship_id=friendship.id, _external=True),
            "messagequeue_url": url_for("messages.exchange", exchange="players", exchange_id=friend_id,
                                        _external=True) + "/{queue}",
        }

        return ret, httplib.CREATED


class FriendshipAPI(Resource):

    def delete(self, friendship_id):
        """
        Remove a friend
        """
        player_id = current_user["player_id"]

        friendship = g.db.query(Friendship).filter_by(id=friendship_id).first()
        if friendship is None:
            abort(httplib.NOT_FOUND)
        elif friendship.player1_id != player_id and friendship.player2_id != player_id:
            abort(httplib.FORBIDDEN)
        elif friendship.status == "deleted":
            return {}, httplib.GONE

        if friendship:
            friendship.status = "deleted"
            g.db.commit()

        return {}, httplib.NO_CONTENT

{'fraction_non_alphanumeric': 0.10463733650416171, 'fraction_numerical': 0.0035671819262782403, 'mean_word_length': 2.5474483340362717, 'pattern_counts': {'":': 19, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5405833', 'n_tokens_mistral': 2532, 'n_tokens_neox': 2493, 'n_words': 512}