<commit_before>package notification

import (
	"errors"
	"github.com/jinzhu/gorm"
	// "github.com/koding/bongo"
	"github.com/koding/logging"
	"math"
	"net/http"
	"net/url"
	"socialapi/config"
	"socialapi/models"
	"socialapi/workers/api/modules/helpers"
	"socialapi/workers/cache"
	"socialapi/workers/helper"
	"strconv"
)

var (
	NOTIFICATION_LIMIT = 8
	ACTOR_LIMIT        = 3
	cacheEnabled       = false
	log                logging.Logger
)

const (
	NOTIFICATION_TYPE_SUBSCRIBE   = "subscribe"
	NOTIFICATION_TYPE_UNSUBSCRIBE = "unsubscribe"
)

func init() {
	log = helper.CreateLogger("NotificationAPI", false)
	helpers.Log = log
}

func List(u *url.URL, h http.Header, _ interface{}) (int, http.Header, interface{}, error) {
	q := helpers.GetQuery(u)
	if err := validateNotificationRequest(q); err != nil {
		return helpers.NewBadRequestResponse(err)
	}

	conf := config.Get()
	cacheEnabled = conf.Cache.Notification

	urlQuery := u.Query()
	cache, err := strconv.ParseBool(urlQuery.Get("cache"))
	if err == nil {
		cacheEnabled = cache
	}

	list, err := fetchNotifications(q)
	if err != nil {
		if err == gorm.RecordNotFound {
			return helpers.NewNotFoundResponse()
		}

		return helpers.NewBadRequestResponse(err)
	}

	return helpers.NewOKResponse(list)
}

func Glance(u *url.URL, h http.Header, req *models.Notification) (int, http.Header, interface{}, error) {
	q := models.NewQuery()
	q.AccountId = req.AccountId
	if err := validateNotificationRequest(q); err != nil {
		return helpers.NewBadRequestResponse(err)
	}

	if err := req.Glance(); err != nil {
		if err == gorm.RecordNotFound {
			return helpers.NewNotFoundResponse()
		}

		return helpers.NewBadRequestResponse(err)
	}

	go func() {
		if cacheEnabled {
			cacheInstance := cache.NewNotificationCache()
			cacheInstance.Glance(req)
		}
	}()

	req.Glanced = true

	return helpers.NewDefaultOKResponse()
}

func Follow(u *url.URL, h http.Header, req *models.NotificationActivity) (int, http.Header, interface{}, error) {

	// 	n := models.NewNotification()
	// 	if err := n.Follow(req); err != nil {
	// 		return helpers.NewBadRequestResponse(err)
	// 	}

	return helpers.NewDefaultOKResponse()
}

type GroupRequest struct {
	Name         string  `json:"name"`
	TypeConstant string  `json:"typeConstant"`
	ActorId      int64   `json:"actorId"`
	Admins       []int64 `json:"admins"`
}

func InteractGroup(u *url.URL, h http.Header, req *GroupRequest) (int, http.Header, interface{}, error) {

	// 	// first fetch channel id as target id
	// 	c := models.NewChannel()
	// 	selector := map[string]interface{}{
	// 		"type_constant": models.Channel_TYPE_GROUP,
	// 		"group_name":    req.Name,
	// 		"name":          req.Name,
	// 	}

	// 	if err := c.One(bongo.NewQS(selector)); err != nil {
	// 		return helpers.NewBadRequestResponse(err)
	// 	}

	// 	a := models.NewNotificationActivity()
	// 	a.TargetId = c.Id
	// 	a.ActorId = req.ActorId
	// 	a.TypeConstant = req.TypeConstant

	// 	var err error

	// 	n := models.NewNotification()
	// 	switch req.TypeConstant {
	// 	case models.NotificationContent_TYPE_JOIN:
	// 		err = n.JoinGroup(a, req.Admins)
	// 	case models.NotificationContent_TYPE_LEAVE:
	// 		err = n.LeaveGroup(a, req.Admins)
	// 	default:
	// 		err = errors.New("group interaction type not found")
	// 	}
	// 	if err != nil {
	// 		return helpers.NewBadRequestResponse(err)
	// 	}

	return helpers.NewDefaultOKResponse()
}

func SubscribeMessage(u *url.URL, h http.Header, req *models.NotificationRequest) (int, http.Header, interface{}, error) {
	if err := subscription(req, NOTIFICATION_TYPE_SUBSCRIBE); err != nil {
		return helpers.NewBadRequestResponse(err)
	}

	return helpers.NewDefaultOKResponse()
}<commit_msg>Social: Follow api handler is added<commit_after>package notification

import (
	"errors"
	"github.com/jinzhu/gorm"
	// "github.com/koding/bongo"
	"github.com/koding/logging"
	"math"
	"net/http"
	"net/url"
	"socialapi/config"
	"socialapi/models"
	"socialapi/workers/api/modules/helpers"
	"socialapi/workers/cache"
	"socialapi/workers/helper"
	"strconv"
)

var (
	NOTIFICATION_LIMIT = 8
	ACTOR_LIMIT        = 3
	cacheEnabled       = false
	log                logging.Logger
)

const (
	NOTIFICATION_TYPE_SUBSCRIBE   = "subscribe"
	NOTIFICATION_TYPE_UNSUBSCRIBE = "unsubscribe"
)

func init() {
	log = helper.CreateLogger("NotificationAPI", false)
	helpers.Log = log
}

func List(u *url.URL, h http.Header, _ interface{}) (int, http.Header, interface{}, error) {
	q := helpers.GetQuery(u)
	if err := validateNotificationRequest(q); err != nil {
		return helpers.NewBadRequestResponse(err)
	}

	conf := config.Get()
	cacheEnabled = conf.Cache.Notification

	urlQuery := u.Query()
	cache, err := strconv.ParseBool(urlQuery.Get("cache"))
	if err == nil {
		cacheEnabled = cache
	}

	list, err := fetchNotifications(q)
	if err != nil {
		if err == gorm.RecordNotFound {
			return helpers.NewNotFoundResponse()
		}

		return helpers.NewBadRequestResponse(err)
	}

	return helpers.NewOKResponse(list)
}

func Glance(u *url.URL, h http.Header, req *models.Notification) (int, http.Header, interface{}, error) {
	q := models.NewQuery()
	q.AccountId = req.AccountId
	if err := validateNotificationRequest(q); err != nil {
		return helpers.NewBadRequestResponse(err)
	}

	if err := req.Glance(); err != nil {
		if err == gorm.RecordNotFound {
			return helpers.NewNotFoundResponse()
		}

		return helpers.NewBadRequestResponse(err)
	}

	go func() {
		if cacheEnabled {
			cacheInstance := cache.NewNotificationCache()
			cacheInstance.Glance(req)
		}
	}()

	req.Glanced = true

	return helpers.NewDefaultOKResponse()
}

func Follow(u *url.URL, h http.Header, req *models.NotificationRequest) (int, http.Header, interface{}, error) {
	c := models.NewChannel()
	c.TypeConstant = models.Channel_TYPE_FOLLOWERS
	c.CreatorId = req.TargetId
	if err := c.Create(); err != nil {
		return helpers.NewBadRequestResponse(err)
	}

	_, err := c.AddParticipant(req.AccountId)
	if err != nil {
		return helpers.NewBadRequestResponse(err)
	}

	return helpers.NewDefaultOKResponse()
}

func SubscribeMessage(u *url.URL, h http.Header, req *models.NotificationRequest) (int, http.Header, interface{}, error) {
	if err := subscription(req, NOTIFICATION_TYPE_SUBSCRIBE); err != nil {
		return helpers.NewBadRequestResponse(err)
	}

	return helpers.NewDefaultOKResponse()
}
{'fraction_non_alphanumeric': 0.1381049976351884, 'fraction_numerical': 0.0012612328551158758, 'mean_word_length': 3.876249039200615, 'pattern_counts': {'":': 3, '<': 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': '2769031', 'n_tokens_mistral': 2315, 'n_tokens_neox': 2224, 'n_words': 451}