# Path: cfg.py
# API_VERSION = 1
# SKIER_VERSION = 1.5
# 
# Path: skier/pgp.py
# def add_pgp_key(armored: str) -> tuple:
# def get_pgp_armor_key(keyid: str) -> str:
# def get_pgp_keyinfo(keyid: str) -> keyinfo.KeyInfo:
# def search_through_keys(search_str: str, page: int=1, count: int=10) -> Pagination:
# 
# Path: skier/pgpactions.py
# def import_key(keyserver: str, keyid: str):
# def synch_keys():
# def _broadcast_add(key: db.Key):
# def broadcast_add(key: db.Key):


import json
import datetime
import db
from flask import Blueprint, request
from cfg import cfg
from skier import pgp, pgpactions
def importers():
    return json.dumps(cfg.config.skier_imports + cfg.config.sks_imports)

if cfg.config.pool_enabled.syncactions:
    @pgpapi.route("/sync/since")
    def check_keys_made_since():
        time = request.args.get("ts", datetime.datetime.utcnow().timestamp())
        try:
            dt = datetime.datetime.utcfromtimestamp(time)
        except TypeError: # or whatever
            dt = datetime.datetime.utcnow()
        # Query the database, check how many keys have been added since.
        keys_since = db.Key.query.filter(db.Key.added_time < dt)
        # Return a list of keys, and the count.
        # Assuming the other server has a correct API, it will take this result, and compare it with the other servers results.
        # Whichever has the highest number will be used.
        # This makes sure they are all synced every <x> hours.
        result = {"number": keys_since.count(), "keys": [key.key_fp_id for key in keys_since.all()]}
        return json.dumps(result), 200, {"Content-Type": "application/json", "X-Query-Timestamp": dt.timestamp()}

    @pgpapi.route("/sync/new", methods=["POST"])
    def handle_key_broadcast():
        # Recieve a new key broadcast.
        if 'key' in request.form:
            key = request.form["key"]
        else:
            # Prevent crashes from recieving an empty post.
            return
        res = pgp.add_pgp_key(key)
        if res[0]:

# Path: cfg.py
# API_VERSION = 1
# SKIER_VERSION = 1.5
# 
# Path: skier/pgp.py
# def add_pgp_key(armored: str) -> tuple:
# def get_pgp_armor_key(keyid: str) -> str:
# def get_pgp_keyinfo(keyid: str) -> keyinfo.KeyInfo:
# def search_through_keys(search_str: str, page: int=1, count: int=10) -> Pagination:
# 
# Path: skier/pgpactions.py
# def import_key(keyserver: str, keyid: str):
# def synch_keys():
# def _broadcast_add(key: db.Key):
# def broadcast_add(key: db.Key):


# Path: skier/pgpapi.py
import json
import datetime
import db
from flask import Blueprint, request
from cfg import cfg
from skier import pgp, pgpactions

def importers():
    return json.dumps(cfg.config.skier_imports + cfg.config.sks_imports)

if cfg.config.pool_enabled.syncactions:
    @pgpapi.route("/sync/since")
    def check_keys_made_since():
        time = request.args.get("ts", datetime.datetime.utcnow().timestamp())
        try:
            dt = datetime.datetime.utcfromtimestamp(time)
        except TypeError: # or whatever
            dt = datetime.datetime.utcnow()
        # Query the database, check how many keys have been added since.
        keys_since = db.Key.query.filter(db.Key.added_time < dt)
        # Return a list of keys, and the count.
        # Assuming the other server has a correct API, it will take this result, and compare it with the other servers results.
        # Whichever has the highest number will be used.
        # This makes sure they are all synced every <x> hours.
        result = {"number": keys_since.count(), "keys": [key.key_fp_id for key in keys_since.all()]}
        return json.dumps(result), 200, {"Content-Type": "application/json", "X-Query-Timestamp": dt.timestamp()}

    @pgpapi.route("/sync/new", methods=["POST"])
    def handle_key_broadcast():
        # Recieve a new key broadcast.
        if 'key' in request.form:
            key = request.form["key"]
        else:
            # Prevent crashes from recieving an empty post.
            return
        res = pgp.add_pgp_key(key)
        if res[0]:

            return "", 200