3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-12-22 18:52:41 +01:00

Remember when client joins channels, expose in EXTJWT

This commit is contained in:
Daniel Oaks 2020-04-15 21:51:33 +10:00 committed by Shivaram Lingamneni
parent 9b998a7582
commit 4164c643e6
2 changed files with 26 additions and 6 deletions

View File

@ -30,6 +30,7 @@ type Channel struct {
key string key string
members MemberSet members MemberSet
membersCache []*Client // allow iteration over channel members without holding the lock membersCache []*Client // allow iteration over channel members without holding the lock
memberJoinTimes map[*Client]time.Time
name string name string
nameCasefolded string nameCasefolded string
server *Server server *Server
@ -57,11 +58,12 @@ func NewChannel(s *Server, name, casefoldedName string, registered bool) *Channe
config := s.Config() config := s.Config()
channel := &Channel{ channel := &Channel{
createdTime: time.Now().UTC(), // may be overwritten by applyRegInfo createdTime: time.Now().UTC(), // may be overwritten by applyRegInfo
members: make(MemberSet), members: make(MemberSet),
name: name, memberJoinTimes: make(map[*Client]time.Time),
nameCasefolded: casefoldedName, name: name,
server: s, nameCasefolded: casefoldedName,
server: s,
} }
channel.initializeLists() channel.initializeLists()
@ -550,6 +552,16 @@ func (channel *Channel) ClientModeStrings(client *Client) []string {
} }
} }
func (channel *Channel) ClientJoinTime(client *Client) *time.Time {
channel.stateMutex.RLock()
defer channel.stateMutex.RUnlock()
time, present := channel.memberJoinTimes[client]
if present {
return &time
}
return nil
}
func (channel *Channel) ClientHasPrivsOver(client *Client, target *Client) bool { func (channel *Channel) ClientHasPrivsOver(client *Client, target *Client) bool {
channel.stateMutex.RLock() channel.stateMutex.RLock()
founder := channel.registeredFounder founder := channel.registeredFounder
@ -726,6 +738,7 @@ func (channel *Channel) Join(client *Client, key string, isSajoin bool, rb *Resp
defer channel.stateMutex.Unlock() defer channel.stateMutex.Unlock()
channel.members.Add(client) channel.members.Add(client)
channel.memberJoinTimes[client] = time.Now()
firstJoin := len(channel.members) == 1 firstJoin := len(channel.members) == 1
newChannel := firstJoin && channel.registeredFounder == "" newChannel := firstJoin && channel.registeredFounder == ""
if newChannel { if newChannel {
@ -1364,6 +1377,7 @@ func (channel *Channel) Quit(client *Client) {
channel.stateMutex.Lock() channel.stateMutex.Lock()
channel.members.Remove(client) channel.members.Remove(client)
delete(channel.memberJoinTimes, client)
channelEmpty := len(channel.members) == 0 channelEmpty := len(channel.members) == 0
channel.stateMutex.Unlock() channel.stateMutex.Unlock()
channel.regenerateMembersCache() channel.regenerateMembersCache()

View File

@ -939,7 +939,13 @@ func extjwtHandler(server *Server, client *Client, msg ircmsg.IrcMessage, rb *Re
claims["joined"] = 0 claims["joined"] = 0
claims["cmodes"] = []string{} claims["cmodes"] = []string{}
if channel.hasClient(client) { if channel.hasClient(client) {
claims["joined"] = time.Now().Unix() - 100 //TODO(dan): um we need to store when clients joined for reals joinTime := channel.ClientJoinTime(client)
if joinTime == nil {
// shouldn't happen, only in races
rb.Add(nil, server.name, "FAIL", "EXTJWT", "UNKNOWN_ERROR", client.t("Channel join time is inconsistent, JWT not generated"))
return false
}
claims["joined"] = joinTime.Unix()
claims["cmodes"] = channel.ClientModeStrings(client) claims["cmodes"] = channel.ClientModeStrings(client)
} }
} }