<commit_before>package rest

import (
	"encoding/json"
	"fmt"
	"math/rand"
	"net/url"
	"socialapi/models"
	"socialapi/request"
	"strconv"
	"time"

	"github.com/google/go-querystring/query"
)

func GetHistory(channelId int64, q *request.Query, token string) (*models.HistoryResponse, error) {
	v, err := query.Values(q)
	if err != nil {
		return nil, err
	}

	url := fmt.Sprintf("/channel/%d/history?%s", channelId, v.Encode())
	res, err := sendRequestWithAuth("GET", url, nil, token)
	if err != nil {
		return nil, err
	}

	var history models.HistoryResponse
	err = json.Unmarshal(res, &history)
	if err != nil {
		return nil, err
	}

	return &history, nil
}

func CountHistory(channelId int64) (*models.CountResponse, error) {
	c := models.NewChannelMessageList()
	c.ChannelId = channelId

	url := fmt.Sprintf("/channel/%d/history/count", channelId)
	res, err := marshallAndSendRequest("GET", url, c)
	if err != nil {
		return nil, err
	}

	var count models.CountResponse
	err = json.Unmarshal(res, &count)
	if err != nil {
		return nil, err
	}

	return &count, nil
}

func FetchChannelsByQuery(accountId int64, q *request.Query) ([]*models.Channel, error) {
	v, err := query.Values(q)
	if err != nil {
		return nil, err
	}

	url := fmt.Sprintf("/account/%d/channels?%s", accountId, v.Encode())
	res, err := sendRequest("GET", url, nil)
	if err != nil {
		return nil, err
	}

	var ccs []*models.ChannelContainer
	err = json.Unmarshal(res, &ccs)
	if err != nil {
		return nil, err
	}

	channels := make([]*models.Channel, len(ccs))
	for i, cc := range ccs {
		channels[i] = cc.Channel
	}

	return channels, nil
}

func FetchChannelByName(accountId int64, name, groupName, typeConstant, token string) (*models.Channel, error) {
	ccs, err := FetchChannelContainerByName(accountId, name, groupName, typeConstant, token)
	if err != nil {
		return nil, err
	}

	return ccs.Channel, nil
}

func FetchChannelContainerByName(accountId int64, name, groupName, typeConstant, token string) (*models.ChannelContainer, error) {
	url := fmt.Sprintf("/channel/name/%s?groupName=%s&type=%s&accountId=%d", name, groupName, typeConstant, accountId)
	res, err := sendRequest("GET", url, nil)
	if err != nil {
		return nil, err
	}

	ccs := &models.ChannelContainer{}
	err = json.Unmarshal(res, ccs)
	if err != nil {
		return nil, err
	}

	return ccs, nil
}

func FetchChannelsByParticipants(accountIds []int64, typeConstant, token string) ([]models.ChannelContainer, error) {
	v := url.Values{}
	v.Add("type", typeConstant)
	for _, id := range accountIds {
		v.Add("id", fmt.Sprintf("%d", id))
	}

	url := fmt.Sprintf("/channel/by/participants?%s", v.Encode())
	res, err := sendRequestWithAuth("GET", url, nil, token)
	if err != nil {
		return nil, err
	}

	var ccs []models.ChannelContainer<commit_msg>socialapi: add token into channel rest function<commit_after>package rest

import (
	"encoding/json"
	"fmt"
	"math/rand"
	"net/url"
	"socialapi/models"
	"socialapi/request"
	"strconv"
	"time"

	"github.com/google/go-querystring/query"
)

func GetHistory(channelId int64, q *request.Query, token string) (*models.HistoryResponse, error) {
	v, err := query.Values(q)
	if err != nil {
		return nil, err
	}

	url := fmt.Sprintf("/channel/%d/history?%s", channelId, v.Encode())
	res, err := sendRequestWithAuth("GET", url, nil, token)
	if err != nil {
		return nil, err
	}

	var history models.HistoryResponse
	err = json.Unmarshal(res, &history)
	if err != nil {
		return nil, err
	}

	return &history, nil
}

func CountHistory(channelId int64) (*models.CountResponse, error) {
	c := models.NewChannelMessageList()
	c.ChannelId = channelId

	url := fmt.Sprintf("/channel/%d/history/count", channelId)
	res, err := marshallAndSendRequest("GET", url, c)
	if err != nil {
		return nil, err
	}

	var count models.CountResponse
	err = json.Unmarshal(res, &count)
	if err != nil {
		return nil, err
	}

	return &count, nil
}

func FetchChannelsByQuery(accountId int64, q *request.Query) ([]*models.Channel, error) {
	v, err := query.Values(q)
	if err != nil {
		return nil, err
	}

	url := fmt.Sprintf("/account/%d/channels?%s", accountId, v.Encode())
	res, err := sendRequest("GET", url, nil)
	if err != nil {
		return nil, err
	}

	var ccs []*models.ChannelContainer
	err = json.Unmarshal(res, &ccs)
	if err != nil {
		return nil, err
	}

	channels := make([]*models.Channel, len(ccs))
	for i, cc := range ccs {
		channels[i] = cc.Channel
	}

	return channels, nil
}

func FetchChannelByName(accountId int64, name, groupName, typeConstant, token string) (*models.Channel, error) {
	ccs, err := FetchChannelContainerByName(accountId, name, groupName, typeConstant, token)
	if err != nil {
		return nil, err
	}

	return ccs.Channel, nil
}

func FetchChannelContainerByName(accountId int64, name, groupName, typeConstant, token string) (*models.ChannelContainer, error) {
	url := fmt.Sprintf("/channel/name/%s?groupName=%s&type=%s&accountId=%d", name, groupName, typeConstant, accountId)
	// res, err := sendRequest("GET", url, nil)
	res, err := sendRequestWithAuth("GET", url, nil, token)
	if err != nil {
		return nil, err
	}

	ccs := &models.ChannelContainer{}
	err = json.Unmarshal(res, ccs)
	if err != nil {
		return nil, err
	}

	return ccs, nil
}

func FetchChannelsByParticipants(accountIds []int64, typeConstant, token string) ([]models.ChannelContainer, error) {
	v := url.Values{}
	v.Add("type", typeConstant)
	for _, id := range accountIds {
		v.Add("id", fmt.Sprintf("%d", id))
	}

	url := fmt.Sprintf("/channel/by/participants?%s", v.Encode())
	res, err := sendRequestWithAuth("GET", url, nil, token)
	if err != nil {
		return nil, err
	}

	var ccs []models.ChannelContainer
{'fraction_non_alphanumeric': 0.1531802120141343, 'fraction_numerical': 0.004240282685512367, 'mean_word_length': 4.360795454545454, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '4490356', 'n_tokens_mistral': 2065, 'n_tokens_neox': 1980, 'n_words': 529}