<commit_before>import {UserModel, UserDocument} from '../model/user';
import request = require('request-promise-native');
import config = require('../config/config');

function fcm_create_noti_key(key_name:string, registration_ids:[string]): Promise<string> {
  return request({
      method: 'POST',
      uri: 'https://android.googleapis.com/gcm/notification',
      headers: {
        "Content-Type":"application/json",
        "Authorization":"key="+config.fcm_api_key,
        "project_id":config.fcm_project_id
      },
      body: {
            "operation": "create",
            "notification_key_name": key_name,
            "registration_ids": registration_ids
      },
      json: true
    }).then(function(body){
      return Promise.resolve(body.notification_key);
    }).catch(function(err){
      return Promise.reject(err.response.body.error);
    });
}

function fcm_get_noti_key(key_name:string): Promise<string> {
  return request({
      method: 'GET',
      uri: 'https://android.googleapis.com/gcm/notification',
      headers: {
        "Content-Type":"application/json",
        "Authorization":"key="+config.fcm_api_key,
        "project_id":config.fcm_project_id
      },
      qs: {
        "notification_key_name": key_name
      },
      json: true
    }).then(function (body) {
      return Promise.resolve(body.notification_key);
    }).catch(function (err) {
      return Promise.reject(err.response.body.error);
    });
}

function fcm_add_device(key_name:string, key:string, registration_ids:[string]): Promise<string> {
  return request({
      method: 'POST',
      uri: 'https://android.googleapis.com/gcm/notification',
      headers: {
        "Content-Type":"application/json",
        "Authorization":"key="+config.fcm_api_key,
        "project_id":config.fcm_project_id
      },
      body: {
            "operation": "add",
            "notification_key_name": key_name,
            "notification_key": key,
            "registration_ids": registration_ids
      },
      json: true
    }).then(function(body){
      return Promise.resolve(body.notification_key);
    }).catch(function(err){
      return Promise.reject(err.response.body.error);
    });
}

function fcm_remove_device(key_name:string, key:string, registration_ids:[string]): Promise<string> {
  return request({
      method: 'POST',
      uri: 'https://android.googleapis.com/gcm/notification',
      headers: {
        "Content-Type":"application/json",
        "Authorization":"key="+config.fcm_api_key,
        "project_id":config.fcm_project_id
      },
      body: {
            "operation": "remove",
            "notification_key_name": key_name,
            "notification_key": key,
            "registration_ids": registration_ids
      },
      json: true
    }).then(function(body){
      return Promise.resolve(body.notification_key);
    }).catch(function(err){
      return Promise.reject(err.response.body.error);
    });
}

function fcm_add_topic(registration_id:string): Promise<any> {
  return request({
      method: 'POST',
      uri: 'https://iid.googleapis.com/iid/v1/'+registration_id+'/rel/topics/global',
      headers: {
        "Content-Type":"application/json",
        "Authorization":"key="+config.fcm_api_key
        // no need for project_id
      }
    }).catch(function(err){
      return Promise.reject(err.response);
    });
}

function fcm_remove_topic_batch(registration_tokens:[string]): Promise<any> {
  return request({
      method: 'POST',
      uri: 'https://iid.googleapis.com/iid/v1:batchRemove',
      headers: {
        "Content-Type":"application/json",
        "Authorization":"key="+config.fcm_api_key
        // no need for project_id
      },
      body: {
        "to": "/topics/global",
        "registration_tokens": registration_tokens
      },
      json: true
    }).catch(function(err){
      return Promise.reject(err.response);
    });
}

function fcm_send_msg(to:string, title:string, body:string) {
  return request({
      method: 'POST',
      uri: 'https://fcm.googleapis.com/fcm/send',
      headers: {
        "Content-Type":"application/json",
        "Authorization":"key="+config.fcm_api_key
      },
      body: {
            "to": to,
            "notification" : {
              "body" : body,
              "title" : title
            },
            "priority" : "high",
            "content_available" : true
      },
      json:true,
    }).catch(function(err){
      return Promise.reject(err.response);
    });
}

function get_or_create_key(user:UserDocument, registration_id:string): Promise<string> {
  var key_name = "user-"+user._id;
  var promise:Promise<any> = fcm_create_noti_key(key_name, [registration_id]);
  promise = promise.catch(function (err) {
    if (err == "notification_key already exists") {
      return fcm_get_noti_key(key_name)
    } else {
      return Promise.reject(err);
    }
  });
  promise = promise.then(function(fcm_key){
    user.fcm_key = fcm_key;
    return user.save();
  });
  promise = promise.then(function(user) {
    return Promise.resolve(user.fcm_key);
  });
  return promise;
}

/*
 * create_device
 * Add this registration_id for the user<commit_msg>libfcm add more logging<commit_after>import {UserModel, UserDocument} from '../model/user';
import request = require('request-promise-native');
import config = require('../config/config');

function fcm_create_noti_key(key_name:string, registration_ids:[string]): Promise<string> {
  return request({
      method: 'POST',
      uri: 'https://android.googleapis.com/gcm/notification',
      headers: {
        "Content-Type":"application/json",
        "Authorization":"key="+config.fcm_api_key,
        "project_id":config.fcm_project_id
      },
      body: {
            "operation": "create",
            "notification_key_name": key_name,
            "registration_ids": registration_ids
      },
      json: true
    }).then(function(body){
      return Promise.resolve(body.notification_key);
    }).catch(function(err){
      console.log("fcm_create_noti_key: " + err.response.body.error);
      return Promise.reject(err.response.body.error);
    });
}

function fcm_get_noti_key(key_name:string): Promise<string> {
  return request({
      method: 'GET',
      uri: 'https://android.googleapis.com/gcm/notification',
      headers: {
        "Content-Type":"application/json",
        "Authorization":"key="+config.fcm_api_key,
        "project_id":config.fcm_project_id
      },
      qs: {
        "notification_key_name": key_name
      },
      json: true
    }).then(function (body) {
      return Promise.resolve(body.notification_key);
    }).catch(function (err) {
      console.log("fcm_get_noti_key: " + err.response.body.error);
      return Promise.reject(err.response.body.error);
    });
}

function fcm_add_device(key_name:string, key:string, registration_ids:[string]): Promise<string> {
  return request({
      method: 'POST',
      uri: 'https://android.googleapis.com/gcm/notification',
      headers: {
        "Content-Type":"application/json",
        "Authorization":"key="+config.fcm_api_key,
        "project_id":config.fcm_project_id
      },
      body: {
            "operation": "add",
            "notification_key_name": key_name,
            "notification_key": key,
            "registration_ids": registration_ids
      },
      json: true
    }).then(function(body){
      return Promise.resolve(body.notification_key);
    }).catch(function(err){
      console.log("fcm_add_device: " + err.response.body.error);
      return Promise.reject(err.response.body.error);
    });
}

function fcm_remove_device(key_name:string, key:string, registration_ids:[string]): Promise<string> {
  return request({
      method: 'POST',
      uri: 'https://android.googleapis.com/gcm/notification',
      headers: {
        "Content-Type":"application/json",
        "Authorization":"key="+config.fcm_api_key,
        "project_id":config.fcm_project_id
      },
      body: {
            "operation": "remove",
            "notification_key_name": key_name,
            "notification_key": key,
            "registration_ids": registration_ids
      },
      json: true
    }).then(function(body){
      return Promise.resolve(body.notification_key);
    }).catch(function(err){
      console.log("fcm_remove_device: " + err.response.body.error);
      return Promise.reject(err.response.body.error);
    });
}

function fcm_add_topic(registration_id:string): Promise<any> {
  return request({
      method: 'POST',
      uri: 'https://iid.googleapis.com/iid/v1/'+registration_id+'/rel/topics/global',
      headers: {
        "Content-Type":"application/json",
        "Authorization":"key="+config.fcm_api_key
        // no need for project_id
      }
    }).catch(function(err){
      console.log("fcm_add_topic: " + err.response);
      return Promise.reject(err.response);
    });
}

function fcm_remove_topic_batch(registration_tokens:[string]): Promise<any> {
  return request({
      method: 'POST',
      uri: 'https://iid.googleapis.com/iid/v1:batchRemove',
      headers: {
        "Content-Type":"application/json",
        "Authorization":"key="+config.fcm_api_key
        // no need for project_id
      },
      body: {
        "to": "/topics/global",
        "registration_tokens": registration_tokens
      },
      json: true
    }).catch(function(err){
      console.log("fcm_remove_topic_batch: " + err.response);
      return Promise.reject(err.response);
    });
}

function fcm_send_msg(to:string, title:string, body:string) {
  return request({
      method: 'POST',
      uri: 'https://fcm.googleapis.com/fcm/send',
      headers: {
        "Content-Type":"application/json",
        "Authorization":"key="+config.fcm_api_key
      },
      body: {
            "to": to,
            "notification" : {
              "body" : body,
              "title" : title
            },
            "priority" : "high",
            "content_available" : true
      },
      json:true,
    }).catch(function(err){
      console.log("fcm_send_msg: " + err.response);
      return Promise.reject(err.response);
    });
}

function get_or_create_key(user:UserDocument, registration_id:string): Promise<string> {
  var key_name = "user-"+user._id;
  var promise:Promise<any> = fcm_create_noti_key(key_name, [registration_id]);
  promise = promise.catch(function (err) {
    if (err == "notification_key already exists") {
      return fcm_get_noti_key(key_name)
    } else {
      return Promise.reject(err);
    }
  });
  promise = promise.then(function(fcm_key){
    user.fcm_key = fcm_key;
    return user.save();
  });
  promise = promise.then(function(user) {
    return Promise.resolve(user.fcm_key);
  });
  return promise;
}

/*
 * create_device
 * Add this registration_id for the user
{'fraction_non_alphanumeric': 0.14761068490618356, 'fraction_numerical': 0.00036971993714761067, 'mean_word_length': 2.834160170092133, 'pattern_counts': {'":': 66, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 14, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1109607', 'n_tokens_mistral': 3337, 'n_tokens_neox': 3322, 'n_words': 510}