Browse Source

Cleaned up event targetting, changed Event.MarshalJSON, added Event.RenderTags

master
Gisle Aune 7 years ago
parent
commit
c17a5e59bc
  1. 76
      client.go
  2. 51
      event.go

76
client.go

@ -476,6 +476,28 @@ func (client *Client) RemoveTarget(target Target) (id string, err error) {
return return
} }
// FindUser checks each channel to find user info about a user.
func (client *Client) FindUser(nick string) (u list.User, ok bool) {
client.mutex.RLock()
defer client.mutex.RUnlock()
for _, target := range client.targets {
channel, ok := target.(*Channel)
if !ok {
continue
}
user, ok := channel.UserList().User(nick)
if !ok {
continue
}
return user, true
}
return list.User{}, false
}
func (client *Client) handleEventLoop() { func (client *Client) handleEventLoop() {
ticker := time.NewTicker(time.Second * 30) ticker := time.NewTicker(time.Second * 30)
@ -814,7 +836,7 @@ func (client *Client) handleEvent(event *Event) {
channel = client.Channel(event.Arg(0)) channel = client.Channel(event.Arg(0))
} }
event.targets = append(event.targets, channel)
client.handleInTarget(channel, event)
if channel != nil { if channel != nil {
channel.Handle(event, client) channel.Handle(event, client)
@ -833,7 +855,7 @@ func (client *Client) handleEvent(event *Event) {
if event.Nick == client.nick { if event.Nick == client.nick {
client.RemoveTarget(channel) client.RemoveTarget(channel)
} else { } else {
event.targets = append(event.targets, channel)
client.handleInTarget(channel, event)
} }
} }
@ -846,7 +868,7 @@ func (client *Client) handleEvent(event *Event) {
{ {
channel := client.Channel(event.Arg(2)) channel := client.Channel(event.Arg(2))
if channel != nil { if channel != nil {
channel.Handle(event, client)
client.handleInTarget(channel, event)
} }
} }
@ -854,7 +876,7 @@ func (client *Client) handleEvent(event *Event) {
{ {
channel := client.Channel(event.Arg(1)) channel := client.Channel(event.Arg(1))
if channel != nil { if channel != nil {
channel.Handle(event, client)
client.handleInTarget(channel, event)
} }
} }
@ -865,7 +887,7 @@ func (client *Client) handleEvent(event *Event) {
if client.isupport.IsChannel(targetName) { if client.isupport.IsChannel(targetName) {
channel := client.Channel(targetName) channel := client.Channel(targetName)
if channel != nil { if channel != nil {
channel.Handle(event, client)
client.handleInTarget(channel, event)
} }
} }
} }
@ -893,14 +915,19 @@ func (client *Client) handleEvent(event *Event) {
target = query target = query
} }
} else { } else {
target = client.Channel(targetName)
if target == nil {
channel := client.Channel(targetName)
if channel != nil {
if user, ok := channel.UserList().User(event.Nick); ok {
event.RenderTags["prefixedNick"] = user.PrefixedNick
}
target = channel
} else {
target = client.status target = client.status
} }
} }
target.Handle(event, client)
event.targets = append(event.targets, target)
client.handleInTarget(target, event)
if spawned { if spawned {
// TODO: Message has higher importance // 0:Normal, 1:Important, 2:Highlight // TODO: Message has higher importance // 0:Normal, 1:Important, 2:Highlight
@ -917,8 +944,11 @@ func (client *Client) handleEvent(event *Event) {
continue continue
} }
channel.Handle(event, client)
event.targets = append(event.targets, channel)
if user, ok := channel.UserList().User(event.Nick); ok {
event.RenderTags["prefixedNick"] = user.PrefixedNick
}
client.handleInTarget(channel, event)
break break
} }
} }
@ -926,7 +956,7 @@ func (client *Client) handleEvent(event *Event) {
// Otherwise, it belongs in the status target // Otherwise, it belongs in the status target
if len(event.targets) == 0 { if len(event.targets) == 0 {
client.status.Handle(event, client) client.status.Handle(event, client)
event.targets = append(event.targets, client.status)
client.handleInTarget(client.status, event)
} }
} }
@ -944,7 +974,7 @@ func (client *Client) handleEvent(event *Event) {
} }
if len(event.targets) == 0 { if len(event.targets) == 0 {
event.targets = append(event.targets, client.status)
client.handleInTarget(client.status, event)
} }
} }
@ -960,27 +990,43 @@ func (client *Client) handleInTargets(nick string, event *Event) {
} }
} }
event.targets = append(event.targets, target)
target.Handle(event, client) target.Handle(event, client)
event.targets = append(event.targets, target)
event.targetIds[target] = client.targteIds[target]
} }
case *Query: case *Query:
{ {
if target.user.Nick == nick { if target.user.Nick == nick {
target.Handle(event, client) target.Handle(event, client)
event.targets = append(event.targets, target) event.targets = append(event.targets, target)
event.targetIds[target] = client.targteIds[target]
} }
} }
case *Status: case *Status:
{ {
if client.nick == event.Nick { if client.nick == event.Nick {
target.Handle(event, client) target.Handle(event, client)
event.targets = append(event.targets, target) event.targets = append(event.targets, target)
event.targetIds[target] = client.targteIds[target]
}
} }
} }
} }
client.mutex.RUnlock()
} }
func (client *Client) handleInTarget(target Target, event *Event) {
client.mutex.RLock()
target.Handle(event, client)
event.targets = append(event.targets, target)
event.targetIds[target] = client.targteIds[target]
client.mutex.RUnlock() client.mutex.RUnlock()
} }

51
event.go

@ -27,6 +27,9 @@ type Event struct {
hidden bool hidden bool
targets []Target targets []Target
targetIds map[Target]string
RenderTags map[string]string
} }
// NewEvent makes a new event with Kind, Verb, Time set and Args and Tags initialized. // NewEvent makes a new event with Kind, Verb, Time set and Args and Tags initialized.
@ -39,6 +42,10 @@ func NewEvent(kind, verb string) Event {
Time: time.Now(), Time: time.Now(),
Args: make([]string, 0, 4), Args: make([]string, 0, 4),
Tags: make(map[string]string), Tags: make(map[string]string),
targetIds: make(map[Target]string),
RenderTags: make(map[string]string),
} }
} }
@ -120,13 +127,39 @@ func (event *Event) Arg(index int) string {
// MarshalJSON makes a JSON object from the event. // MarshalJSON makes a JSON object from the event.
func (event *Event) MarshalJSON() ([]byte, error) { func (event *Event) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"kind": event.kind,
"verb": event.verb,
"text": event.Text,
"args": event.Args,
"tags": event.Tags,
"killed": event.killed,
"hidden": event.hidden,
})
data := eventJSONData{
Name: event.Name(),
Kind: event.kind,
Verb: event.verb,
Time: event.Time,
Nick: event.Nick,
User: event.User,
Host: event.Host,
Args: event.Args,
Text: event.Text,
Tags: event.Tags,
RenderTags: event.RenderTags,
}
data.Targets = make([]string, 0, len(event.targets))
for _, target := range event.targets {
data.Targets = append(data.Targets, event.targetIds[target])
}
return json.Marshal(data)
}
type eventJSONData struct {
Name string `json:"name"`
Kind string `json:"kind"`
Verb string `json:"verb"`
Time time.Time `json:"time"`
Nick string `json:"nick"`
User string `json:"user"`
Host string `json:"host"`
Args []string `json:"args"`
Text string `json:"text"`
Tags map[string]string `json:"tags"`
Targets []string `json:"targets"`
RenderTags map[string]string `json:"renderTags"`
} }
Loading…
Cancel
Save