<?php
function rate_custom_rest(){

    register_rest_field(array('news','tutorials','forum'), '_rating_array', array(
            'get_callback' => function (){
	            global $post;
                $user = wp_get_current_user();
	            $rating = drk_get_post_votes($user->ID, $post->ID);
                wp_reset_postdata();
                return $rating;
            }
    ));
    register_rest_field(array('news','tutorials','forum'), '_rate', array(
        'get_callback' => function (){
            global $post;
            $user = wp_get_current_user();
            $rating = drk_get_post_votes($user->ID, $post->ID);
            wp_reset_postdata();
            return $rating["rate"];
        }
    ));

    register_rest_field(array('comment'), '_rating_array', array(
        'get_callback' => function ($comment_arr){
            $user = wp_get_current_user();
            $rating = drk_get_comments_votes($user->ID, $comment_arr["id"]);
            wp_reset_postdata();
            return $rating;
        }
    ));
    register_rest_field(array('comment'), '_rate', array(
        'get_callback' => function ($comment_arr){
            $user = wp_get_current_user();
            $comment_id = $comment_arr["id"];
            $rating = drk_get_comments_votes($user->ID, $comment_id);
            $_rating =  $rating["_rate"];
            return  $_rating;
        }
    ));
}

add_action('rest_api_init', 'rate_custom_rest');