-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel.go
43 lines (37 loc) · 909 Bytes
/
channel.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package main
import "fmt"
const (
HEAD = "[%s]"
DESCRIPTION = ">> %s <<"
)
type ChatChannel struct {
name string
topic string
subscribers []User
}
func (self *ChatChannel) UserSubscribe(user User) error {
for _, suser := range self.subscribers {
if suser.name == user.name {
return fmt.Errorf("%s is already subscribed to %s", user.name, self.name)
}
}
subscribers := append(self.subscribers, user)
self.subscribers = subscribers
return nil
}
func (self ChatChannel) ChannelHeader() Message {
header := fmt.Sprintf(HEAD, self.name)
description := fmt.Sprintf(DESCRIPTION, self.topic)
return Message{
content: fmt.Sprintln(header) + fmt.Sprintln(description),
}
}
func (self ChatChannel) FromServer(server Server) Server {
return Server{
name: self.name,
ip: server.ip,
port: server.port,
listener: nil,
users: make([]User, 0),
}
}