Commit 90ba4814 authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Discard old history entries.

parent 865848c7
...@@ -103,6 +103,8 @@ fields, all of which are optional. ...@@ -103,6 +103,8 @@ fields, all of which are optional.
displayed on the landing page for public groups; displayed on the landing page for public groups;
- `max-clients`: the maximum number of clients that may join the group at - `max-clients`: the maximum number of clients that may join the group at
a time; a time;
- `max-history-age`: the time, in seconds, during which chat history is
kept (default 14400, i.e. 4 hours);
- `allow-recording`: if true, then recording is allowed in this group; - `allow-recording`: if true, then recording is allowed in this group;
- `allow-anonymous`: if true, then users may connect with an empty - `allow-anonymous`: if true, then users may connect with an empty
username; this is not recommended, since anonymous users are not username; this is not recommended, since anonymous users are not
......
...@@ -65,7 +65,7 @@ func IceConfiguration() webrtc.Configuration { ...@@ -65,7 +65,7 @@ func IceConfiguration() webrtc.Configuration {
type ChatHistoryEntry struct { type ChatHistoryEntry struct {
Id string Id string
User string User string
Time uint64 Time int64
Kind string Kind string
Value string Value string
} }
...@@ -396,6 +396,18 @@ func (g *Group) Shutdown(message string) { ...@@ -396,6 +396,18 @@ func (g *Group) Shutdown(message string) {
}) })
} }
func FromJSTime(tm int64) time.Time {
if tm == 0 {
return time.Time{}
}
return time.Unix(int64(tm)/1000, (int64(tm)%1000)*1000000)
}
func ToJSTime(tm time.Time) int64 {
return int64((tm.Sub(time.Unix(0, 0)) + time.Millisecond/2) /
time.Millisecond)
}
const maxChatHistory = 50 const maxChatHistory = 50
func (g *Group) ClearChatHistory() { func (g *Group) ClearChatHistory() {
...@@ -404,7 +416,7 @@ func (g *Group) ClearChatHistory() { ...@@ -404,7 +416,7 @@ func (g *Group) ClearChatHistory() {
g.history = nil g.history = nil
} }
func (g *Group) AddToChatHistory(id, user string, time uint64, kind, value string) { func (g *Group) AddToChatHistory(id, user string, time int64, kind, value string) {
g.mu.Lock() g.mu.Lock()
defer g.mu.Unlock() defer g.mu.Unlock()
...@@ -417,10 +429,34 @@ func (g *Group) AddToChatHistory(id, user string, time uint64, kind, value strin ...@@ -417,10 +429,34 @@ func (g *Group) AddToChatHistory(id, user string, time uint64, kind, value strin
) )
} }
func discardObsoleteHistory(h []ChatHistoryEntry, seconds int) []ChatHistoryEntry {
now := time.Now()
d := 4 * time.Hour
if seconds > 0 {
d = time.Duration(seconds) * time.Second
}
i := 0
for i < len(h) {
log.Println(h[i].Time, FromJSTime(h[i].Time), now.Sub(FromJSTime(h[i].Time)))
if now.Sub(FromJSTime(h[i].Time)) <= d {
break
}
i++
}
if i > 0 {
copy(h, h[i:])
h = h[:len(h)-i]
}
return h
}
func (g *Group) GetChatHistory() []ChatHistoryEntry { func (g *Group) GetChatHistory() []ChatHistoryEntry {
g.mu.Lock() g.mu.Lock()
defer g.mu.Unlock() defer g.mu.Unlock()
g.history = discardObsoleteHistory(g.history, g.description.MaxHistoryAge)
h := make([]ChatHistoryEntry, len(g.history)) h := make([]ChatHistoryEntry, len(g.history))
copy(h, g.history) copy(h, g.history)
return h return h
...@@ -448,6 +484,7 @@ type description struct { ...@@ -448,6 +484,7 @@ type description struct {
Redirect string `json:"redirect,omitempty"` Redirect string `json:"redirect,omitempty"`
Public bool `json:"public,omitempty"` Public bool `json:"public,omitempty"`
MaxClients int `json:"max-clients,omitempty"` MaxClients int `json:"max-clients,omitempty"`
MaxHistoryAge int `json:"max-history-age",omitempty`
AllowAnonymous bool `json:"allow-anonymous,omitempty"` AllowAnonymous bool `json:"allow-anonymous,omitempty"`
AllowRecording bool `json:"allow-recording,omitempty"` AllowRecording bool `json:"allow-recording,omitempty"`
Op []ClientCredentials `json:"op,omitempty"` Op []ClientCredentials `json:"op,omitempty"`
......
package rtpconn package group
import ( import (
"testing" "testing"
...@@ -7,9 +7,9 @@ import ( ...@@ -7,9 +7,9 @@ import (
func TestJSTime(t *testing.T) { func TestJSTime(t *testing.T) {
tm := time.Now() tm := time.Now()
js := toJSTime(tm) js := ToJSTime(tm)
tm2 := fromJSTime(js) tm2 := FromJSTime(js)
js2 := toJSTime(tm2) js2 := ToJSTime(tm2)
if js != js2 { if js != js2 {
t.Errorf("%v != %v", js, js2) t.Errorf("%v != %v", js, js2)
......
...@@ -147,7 +147,7 @@ type clientMessage struct { ...@@ -147,7 +147,7 @@ type clientMessage struct {
Permissions group.ClientPermissions `json:"permissions,omitempty"` Permissions group.ClientPermissions `json:"permissions,omitempty"`
Group string `json:"group,omitempty"` Group string `json:"group,omitempty"`
Value string `json:"value,omitempty"` Value string `json:"value,omitempty"`
Time uint64 `json:"time,omitempty"` Time int64 `json:"time,omitempty"`
Offer *webrtc.SessionDescription `json:"offer,omitempty"` Offer *webrtc.SessionDescription `json:"offer,omitempty"`
Answer *webrtc.SessionDescription `json:"answer,omitempty"` Answer *webrtc.SessionDescription `json:"answer,omitempty"`
Candidate *webrtc.ICECandidateInit `json:"candidate,omitempty"` Candidate *webrtc.ICECandidateInit `json:"candidate,omitempty"`
...@@ -155,20 +155,6 @@ type clientMessage struct { ...@@ -155,20 +155,6 @@ type clientMessage struct {
Request rateMap `json:"request,omitempty"` Request rateMap `json:"request,omitempty"`
} }
func fromJSTime(tm uint64) time.Time {
if tm == 0 {
return time.Time{}
}
return time.Unix(int64(tm)/1000, (int64(tm)%1000)*1000000)
}
func toJSTime(tm time.Time) uint64 {
if tm.Before(time.Unix(0, 0)) {
return 0
}
return uint64((tm.Sub(time.Unix(0, 0)) + time.Millisecond/2) / time.Millisecond)
}
type closeMessage struct { type closeMessage struct {
data []byte data []byte
} }
...@@ -1060,7 +1046,7 @@ func handleClientMessage(c *webClient, m clientMessage) error { ...@@ -1060,7 +1046,7 @@ func handleClientMessage(c *webClient, m clientMessage) error {
log.Printf("ICE: %v", err) log.Printf("ICE: %v", err)
} }
case "chat": case "chat":
tm := toJSTime(time.Now()) tm := group.ToJSTime(time.Now())
if m.Dest == "" { if m.Dest == "" {
c.group.AddToChatHistory( c.group.AddToChatHistory(
m.Id, m.Username, tm, m.Kind, m.Value, m.Id, m.Username, tm, m.Kind, m.Value,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment