/
room.go
120 lines (100 loc) · 2.79 KB
/
room.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"fmt"
"time"
log "github.com/Sirupsen/logrus"
"github.com/tideland/golib/cells"
"github.com/tideland/golib/identifier"
)
type room struct {
nullBehavior
ctx cells.Context
name string
buildingName string
users map[string]bool
censoredWords []string
censor *censor
censorID string
}
func makeRoomID(name string, buildingName string) string {
return identifier.Identifier("room", buildingName, name)
}
func newRoom(name string, buildingName string, censoredWords []string) *room {
return &room{
name: name,
buildingName: buildingName,
users: make(map[string]bool),
censoredWords: censoredWords,
}
}
func (r *room) Init(ctx cells.Context) (err error) {
r.ctx = ctx
r.censorID = identifier.Identifier("room", r.buildingName, r.name, "censor")
r.censor = newCensor(r.censorID, r.censoredWords)
// TODO: hangs without goroutine
go func() {
roomID := identifier.Identifier("room", r.buildingName, r.name)
env := r.ctx.Environment()
env.StartCell(r.censorID, r.censor)
subscribe(env, r.censorID, roomID)
}()
return
}
func (r *room) ProcessEvent(event cells.Event) (err error) {
switch event.Topic() {
case SAYS_ALL, SAYS_TO:
user, _ := event.Payload().Get("from")
switch user.(string) {
case PublicAddressUserID, r.censorID:
r.ctx.Emit(event)
default:
env := r.ctx.Environment()
ok, err := env.Request(r.censorID, CENSOR, event.Payload(), nil, time.Second)
if err != nil {
log.Error(err)
return err
}
if ok.(bool) {
r.ctx.Emit(event)
}
}
case IN_ROOM:
userID, _ := event.Payload().Get("user")
ok, _ := r.users[userID.(string)]
event.Respond(ok)
case USER_ADDED:
userID, _ := event.Payload().Get("user")
if ok, _ := r.users[userID.(string)]; !ok {
r.users[userID.(string)] = true
r.ctx.Environment().EmitNew(r.ctx.ID(), SAYS_ALL, cells.PayloadValues{
"message": fmt.Sprintf("%s has entered the room", userID),
"from": PublicAddressUserID,
}, nil)
}
case USER_COUNT:
event.Respond(len(r.users))
}
return
}
func addUser(env cells.Environment, buildingName string, roomName string, userName string) string {
roomID := identifier.Identifier("room", buildingName, roomName)
userID := makeUserID(userName)
u := newUser(userName)
env.StartCell(userID, u)
subscribe(env, roomID, userID)
env.EmitNew(roomID, USER_ADDED, cells.PayloadValues{
"user": userID,
}, nil)
return userID
}
func addLogUser(env cells.Environment, buildingName string, roomName string) {
roomID := identifier.Identifier("room", buildingName, roomName)
userID := makeUserID("logger")
lu := &logUser{}
lu.name = "logger"
env.StartCell(userID, lu)
subscribe(env, roomID, userID)
env.EmitNew(roomID, "user-added", cells.PayloadValues{
"user": userID,
}, nil)
}