<commit_before>package chatblast

import (
	"log"
	"sync"
)

type Room struct {
	sync.RWMutex
	Name        string           `json:"name,omitempty"`
	Id          string           `json:"id,omitempty"`
	Private     bool             `json:"private,omitempty"`
	Owner       *User            `json:"owner,omitempty"`
	Subscribers map[string]*User `json:"subscribers,omitempty"`
	Channel     chan *Message    `json:"-"`
}

func (r *Room) GetSubscriber(userId string) (*User, bool) {
	r.RLock()
	defer r.RUnlock()
	user, ok := r.Subscribers[userId]
	return user, ok
}

func (r *Room) SetSubscriber(userId string, u *User) {
	r.Lock()
	defer r.Unlock()
	r.Subscribers[userId] = u
}

func (r *Room) RemoveSubscriber(userId string) {
	r.Lock()
	defer r.Unlock()
	delete(r.Subscribers, userId)
}

func NewRoom(name string, owner *User) *Room {
	newRoom := &Room{
		Name:        name,
		Id:          GUID(),
		Owner:       owner,
		Subscribers: map[string]*User{},
		Channel:     make(chan *Message),
	}
	go newRoom.Listen()
	log.Println("New room", name, "created")
	return newRoom
}

func (r *Room) Listen() {
	for incoming := range r.Channel {
		log.Println("Message sent in room", r.Name)
		for _, user := range r.Subscribers {
			user.Tell(incoming)
		}
	}
}

func (r *Room) Broadcast(msg *Message) {
	r.Channel <- msg
}

func (r *Room) Whisper(msg *Message) {
	if whisperee, ok := r.Subscribers[msg.UserId]; ok {
		whisperee.Tell(msg)
	}
	if self, ok := r.Subscribers[msg.User.Id]; ok {
		self.Tell(msg)
	}
}

func (r *Room) RequestInvite(u *User) {
	msg := &Message{
		Cmd:    "inviterequest",<commit_msg>Prevent whisper sending if whisperee has logged off<commit_after>package chatblast

import (
	"log"
	"sync"
)

type Room struct {
	sync.RWMutex
	Name        string           `json:"name,omitempty"`
	Id          string           `json:"id,omitempty"`
	Private     bool             `json:"private,omitempty"`
	Owner       *User            `json:"owner,omitempty"`
	Subscribers map[string]*User `json:"subscribers,omitempty"`
	Channel     chan *Message    `json:"-"`
}

func (r *Room) GetSubscriber(userId string) (*User, bool) {
	r.RLock()
	defer r.RUnlock()
	user, ok := r.Subscribers[userId]
	return user, ok
}

func (r *Room) SetSubscriber(userId string, u *User) {
	r.Lock()
	defer r.Unlock()
	r.Subscribers[userId] = u
}

func (r *Room) RemoveSubscriber(userId string) {
	r.Lock()
	defer r.Unlock()
	delete(r.Subscribers, userId)
}

func NewRoom(name string, owner *User) *Room {
	newRoom := &Room{
		Name:        name,
		Id:          GUID(),
		Owner:       owner,
		Subscribers: map[string]*User{},
		Channel:     make(chan *Message),
	}
	go newRoom.Listen()
	log.Println("New room", name, "created")
	return newRoom
}

func (r *Room) Listen() {
	for incoming := range r.Channel {
		log.Println("Message sent in room", r.Name)
		for _, user := range r.Subscribers {
			user.Tell(incoming)
		}
	}
}

func (r *Room) Broadcast(msg *Message) {
	r.Channel <- msg
}

func (r *Room) Whisper(msg *Message) {
	if whisperee, ok := r.Subscribers[msg.UserId]; ok {
		msg.Whisperee = whisperee
		whisperee.Tell(msg)
		if self, ok := r.Subscribers[msg.User.Id]; ok {
			self.Tell(msg)
		}
	}
}

func (r *Room) RequestInvite(u *User) {
	msg := &Message{
		Cmd:    "inviterequest",
{'fraction_non_alphanumeric': 0.15111654940348732, 'fraction_numerical': 0.0, 'mean_word_length': 3.2084942084942085, 'pattern_counts': {'":': 0, '<': 5, '<?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': '1112983', 'n_tokens_mistral': 1234, 'n_tokens_neox': 1216, 'n_words': 296}