<commit_before>// Package team provides api functions for team worker
package team

import (
	"socialapi/config"
	"socialapi/models"

	"github.com/koding/logging"
	"github.com/streadway/amqp"
)

// Controller holds the required parameters for team async operations
type Controller struct {
	log    logging.Logger
	config *config.Config
}

// NewController creates a handler for consuming async operations of team
func NewController(log logging.Logger, config *config.Config) *Controller {
	return &Controller{
		log:    log,
		config: config,
	}
}

// DefaultErrHandler handles the errors, we dont need to ack a message,
// continue to the success
func (c *Controller) DefaultErrHandler(delivery amqp.Delivery, err error) bool {
	c.log.Error("an error occurred putting message back to queue", err)
	delivery.Nack(false, true)
	return false
}

// ParticipantCreated handles participant creation operations
func (c *Controller) ParticipantCreated(cp *models.ChannelParticipant) error {
	return nil
}

// ParticipantUpdate handles participant update operations
func (c *Controller) ParticipantUpdated(cp *models.ChannelParticipant) error {
	return nil
}<commit_msg>Socialapi: create/add/block users from defaults channels when the source channel is group channel<commit_after>// Package team provides api functions for team worker
package team

import (
	"socialapi/config"
	"socialapi/models"
	"strconv"

	"github.com/koding/logging"
	"github.com/streadway/amqp"
)

// Controller holds the required parameters for team async operations
type Controller struct {
	log    logging.Logger
	config *config.Config
}

// NewController creates a handler for consuming async operations of team
func NewController(log logging.Logger, config *config.Config) *Controller {
	return &Controller{
		log:    log,
		config: config,
	}
}

// DefaultErrHandler handles the errors, we dont need to ack a message,
// continue to the success
func (c *Controller) DefaultErrHandler(delivery amqp.Delivery, err error) bool {
	c.log.Error("an error occurred putting message back to queue", err)
	delivery.Nack(false, true)
	return false
}

// HandleParticipant handles participant operations, if a user joins to
func (c *Controller) HandleParticipant(cp *models.ChannelParticipant) error {
	channel := models.NewChannel()
	if err := channel.ById(cp.ChannelId); err != nil {
		return err
	}

	if channel.TypeConstant != models.Channel_TYPE_GROUP {
		return nil
	}

	group, err := modelhelper.GetGroup(channel.GroupName)
	if err != nil && mgo.ErrNotFound {
		return err
	}

	if err == mgo.ErrNotFound {
		c.log.Error("Group: %s is not found in mongo", channel.GroupName)
		return nil
	}

	for _, channelId := range group.DefaultChannels {
		ci, err := strconv.ParseInt(channelId, 10, 64)
		if err != nil {
			return err
		}
		cp := models.NewChannelParticipant()
		cp.ChannelId = ci
		cp.AccountId = cp.AccountId

		// i wrote all of them to have a referance for future, because we
		// are gonna need this logic while implementing invitations ~ CS
		switch p.StatusConstant {
		case models.ChannelParticipant_STATUS_ACTIVE:
			err = cp.Create()
		case models.ChannelParticipant_STATUS_BLOCKED:
			err = cp.Block()
		case p.StatusConstant == models.ChannelParticipant_STATUS_LEFT:
			err = cp.Delete()
		}

		// participant can be blocked before
		if err != nil && err != models.ErrParticipantBlocked {
			return err
		}
	}

	return nil
}
{'fraction_non_alphanumeric': 0.09008215962441314, 'fraction_numerical': 0.0011737089201877935, 'mean_word_length': 4.260802469135802, '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': '3613769', 'n_tokens_mistral': 1049, 'n_tokens_neox': 986, 'n_words': 342}