<commit_before>package models

import (
	"encoding/json"
	"koding/db/mongodb/modelhelper"
	"strconv"
	"time"

	"github.com/jinzhu/gorm"
)

const (
	PrivateMessageSystem_TYPE_INVITE = "invite"
	PrivateMessageSystem_TYPE_JOIN   = "join"
	PrivateMessageSystem_TYPE_LEAVE  = "leave"
	PrivateMessageSystem_TYPE_REJECT = "reject"
	PrivateMessageSystem_TYPE_KICK   = "kick"
	PrivateMessageSystem_TYPE_INIT   = "initiate"
)

type PrivateChannelRequest struct {
	Body            string      `json:"body"`
	Payload         gorm.Hstore `json:"payload,omitempty"`
	GroupName       string      `json:"groupName"`
	Recipients      []string
	AccountId       int64  `json:"accountId,string"`
	ChannelId       int64  `json:"channelId,string"`
	RequestData     string `json:"requestData"`
	ClientRequestId string `json:"ClientRequestId"`
	Purpose         string `json:"purpose"`
	TypeConstant    string `json:"type"`
}

func (p *PrivateChannelRequest) SetSystemMessageType(systemType string) {
	if p.Payload == nil {
		p.Payload = gorm.Hstore{}
	}

	if systemType != "" {
		p.Payload["systemType"] = &systemType
	}
}

func (p *PrivateChannelRequest) Create() (*ChannelContainer, error) {
	// validate the request
	if err := p.validate(); err != nil {
		return nil, err
	}

	// fetch participants
	participantIds, err := p.obtainParticipantIds()
	if err != nil {
		return nil, err
	}

	// create the channel
	c := NewPrivateChannel(p.AccountId, p.GroupName, p.TypeConstant)
	c.Purpose = p.Purpose
	if err := c.Create(); err != nil {
		return nil, err
	}

	// add participants to tha channel
	for _, participantId := range participantIds {
		cp, err := c.AddParticipant(participantId)
		if err != nil {
			continue
		}

		if participantId != p.AccountId {
			continue
		}

		// do not show unread count to the creator of the private message
		oneSecondLater := time.Now().UTC().Add(time.Second * 1)
		// do not show unread count as 1 to user<commit_msg>Socialapi: do not add users to PM channels<commit_after>package models

import (
	"encoding/json"
	"koding/db/mongodb/modelhelper"
	"strconv"
	"time"

	"github.com/jinzhu/gorm"
)

const (
	PrivateMessageSystem_TYPE_INVITE = "invite"
	PrivateMessageSystem_TYPE_JOIN   = "join"
	PrivateMessageSystem_TYPE_LEAVE  = "leave"
	PrivateMessageSystem_TYPE_REJECT = "reject"
	PrivateMessageSystem_TYPE_KICK   = "kick"
	PrivateMessageSystem_TYPE_INIT   = "initiate"
)

type PrivateChannelRequest struct {
	Body            string      `json:"body"`
	Payload         gorm.Hstore `json:"payload,omitempty"`
	GroupName       string      `json:"groupName"`
	Recipients      []string
	AccountId       int64  `json:"accountId,string"`
	ChannelId       int64  `json:"channelId,string"`
	RequestData     string `json:"requestData"`
	ClientRequestId string `json:"ClientRequestId"`
	Purpose         string `json:"purpose"`
	TypeConstant    string `json:"type"`
}

func (p *PrivateChannelRequest) SetSystemMessageType(systemType string) {
	if p.Payload == nil {
		p.Payload = gorm.Hstore{}
	}

	if systemType != "" {
		p.Payload["systemType"] = &systemType
	}
}

func (p *PrivateChannelRequest) Create() (*ChannelContainer, error) {
	// validate the request
	if err := p.validate(); err != nil {
		return nil, err
	}

	// fetch participants
	participantIds, err := p.obtainParticipantIds()
	if err != nil {
		return nil, err
	}

	// create the channel
	c := NewPrivateChannel(p.AccountId, p.GroupName, p.TypeConstant)
	c.Purpose = p.Purpose
	if err := c.Create(); err != nil {
		return nil, err
	}

	groupChannel, err := Cache.Channel.ByGroupName(c.GroupName)
	if err != nil {
		return nil, err
	}

	// add participants to tha channel
	for _, participantId := range participantIds {
		// added accounts should be a member of parent channel
		isParticipant, err := groupChannel.IsParticipant(participantId)
		if err != nil {
			return nil, err
		}

		if !isParticipant {
			return nil, ErrCannotOpenChannel
		}

		cp, err := c.AddParticipant(participantId)
		if err != nil {
			continue
		}

		if participantId != p.AccountId {
			continue
		}

		// do not show unread count to the creator of the private message
		oneSecondLater := time.Now().UTC().Add(time.Second * 1)
		// do not show unread count as 1 to user
{'fraction_non_alphanumeric': 0.11342647407056596, 'fraction_numerical': 0.0028415818138763913, 'mean_word_length': 3.6985539488320356, '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': '4145850', 'n_tokens_mistral': 1458, 'n_tokens_neox': 1356, 'n_words': 350}