Update mautrix-appservice

This commit is contained in:
Tulir Asokan 2018-08-26 23:31:59 +03:00
parent 22e76b6a37
commit e15c3286bd
6 changed files with 81 additions and 24 deletions

4
Gopkg.lock generated
View file

@ -123,7 +123,7 @@
".", ".",
"format" "format"
] ]
revision = "3bc65810c93810440c0a053b9fe5009bb01521c5" revision = "692bae934fcd3b7f4379e89d7271220f1d81f318"
[[projects]] [[projects]]
branch = "master" branch = "master"
@ -141,7 +141,7 @@
branch = "master" branch = "master"
name = "maunium.net/go/mautrix-appservice" name = "maunium.net/go/mautrix-appservice"
packages = ["."] packages = ["."]
revision = "7873866d1709943d6e2ac2d889c845eeca2e7686" revision = "62ccd06944c74b6e526b493ef66fc736d23b1cf2"
[solve-meta] [solve-meta]
analyzer-name = "dep" analyzer-name = "dep"

View file

@ -26,19 +26,19 @@ import (
) )
type AutosavingStateStore struct { type AutosavingStateStore struct {
*appservice.BasicStateStore appservice.StateStore
Path string Path string
} }
func NewAutosavingStateStore(path string) *AutosavingStateStore { func NewAutosavingStateStore(path string) *AutosavingStateStore {
return &AutosavingStateStore{ return &AutosavingStateStore{
BasicStateStore: appservice.NewBasicStateStore(), StateStore: appservice.NewBasicStateStore(),
Path: path, Path: path,
} }
} }
func (store *AutosavingStateStore) Save() error { func (store *AutosavingStateStore) Save() error {
data, err := json.Marshal(store.BasicStateStore) data, err := json.Marshal(store.StateStore)
if err != nil { if err != nil {
return err return err
} }
@ -55,20 +55,20 @@ func (store *AutosavingStateStore) Load() error {
return err return err
} }
return json.Unmarshal(data, store.BasicStateStore) return json.Unmarshal(data, store.StateStore)
} }
func (store *AutosavingStateStore) MarkRegistered(userID string) { func (store *AutosavingStateStore) MarkRegistered(userID string) {
store.BasicStateStore.MarkRegistered(userID) store.StateStore.MarkRegistered(userID)
store.Save() store.Save()
} }
func (store *AutosavingStateStore) SetMembership(roomID, userID, membership string) { func (store *AutosavingStateStore) SetMembership(roomID, userID, membership string) {
store.BasicStateStore.SetMembership(roomID, userID, membership) store.StateStore.SetMembership(roomID, userID, membership)
store.Save() store.Save()
} }
func (store *AutosavingStateStore) SetPowerLevels(roomID string, levels *gomatrix.PowerLevels) { func (store *AutosavingStateStore) SetPowerLevels(roomID string, levels *gomatrix.PowerLevels) {
store.BasicStateStore.SetPowerLevels(roomID, levels) store.StateStore.SetPowerLevels(roomID, levels)
store.Save() store.Save()
} }

View file

@ -231,8 +231,8 @@ func (pl *PowerLevels) StateDefault() int {
func (pl *PowerLevels) GetUserLevel(userID string) int { func (pl *PowerLevels) GetUserLevel(userID string) int {
pl.usersLock.RLock() pl.usersLock.RLock()
defer pl.usersLock.RUnlock()
level, ok := pl.Users[userID] level, ok := pl.Users[userID]
pl.usersLock.RUnlock()
if !ok { if !ok {
return pl.UsersDefault return pl.UsersDefault
} }
@ -241,12 +241,12 @@ func (pl *PowerLevels) GetUserLevel(userID string) int {
func (pl *PowerLevels) SetUserLevel(userID string, level int) { func (pl *PowerLevels) SetUserLevel(userID string, level int) {
pl.usersLock.Lock() pl.usersLock.Lock()
defer pl.usersLock.Unlock()
if level == pl.UsersDefault { if level == pl.UsersDefault {
delete(pl.Users, userID) delete(pl.Users, userID)
} else { } else {
pl.Users[userID] = level pl.Users[userID] = level
} }
pl.usersLock.Unlock()
} }
func (pl *PowerLevels) EnsureUserLevel(userID string, level int) bool { func (pl *PowerLevels) EnsureUserLevel(userID string, level int) bool {
@ -260,8 +260,8 @@ func (pl *PowerLevels) EnsureUserLevel(userID string, level int) bool {
func (pl *PowerLevels) GetEventLevel(eventType EventType, isState bool) int { func (pl *PowerLevels) GetEventLevel(eventType EventType, isState bool) int {
pl.eventsLock.RLock() pl.eventsLock.RLock()
defer pl.eventsLock.RUnlock()
level, ok := pl.Events[eventType] level, ok := pl.Events[eventType]
pl.eventsLock.RUnlock()
if !ok { if !ok {
if isState { if isState {
return pl.StateDefault() return pl.StateDefault()
@ -273,12 +273,12 @@ func (pl *PowerLevels) GetEventLevel(eventType EventType, isState bool) int {
func (pl *PowerLevels) SetEventLevel(eventType EventType, isState bool, level int) { func (pl *PowerLevels) SetEventLevel(eventType EventType, isState bool, level int) {
pl.eventsLock.Lock() pl.eventsLock.Lock()
defer pl.eventsLock.Unlock()
if (isState && level == pl.StateDefault()) || (!isState && level == pl.EventsDefault) { if (isState && level == pl.StateDefault()) || (!isState && level == pl.EventsDefault) {
delete(pl.Events, eventType) delete(pl.Events, eventType)
} else { } else {
pl.Events[eventType] = level pl.Events[eventType] = level
} }
pl.eventsLock.Unlock()
} }
func (pl *PowerLevels) EnsureEventLevel(eventType EventType, isState bool, level int) bool { func (pl *PowerLevels) EnsureEventLevel(eventType EventType, isState bool, level int) bool {

View file

@ -24,7 +24,7 @@ func Create() *AppService {
LogConfig: CreateLogConfig(), LogConfig: CreateLogConfig(),
clients: make(map[string]*gomatrix.Client), clients: make(map[string]*gomatrix.Client),
intents: make(map[string]*IntentAPI), intents: make(map[string]*IntentAPI),
StateStore: &BasicStateStore{}, StateStore: NewBasicStateStore(),
} }
} }

View file

@ -150,6 +150,21 @@ func (intent *IntentAPI) SetPowerLevel(roomID, userID string, level int) (*gomat
return nil, nil return nil, nil
} }
func (intent *IntentAPI) UserTyping(roomID string, typing bool, timeout int64) (resp *gomatrix.RespTyping, err error) {
if intent.as.StateStore.IsTyping(roomID, intent.UserID) == typing {
return
}
resp, err = intent.Client.UserTyping(roomID, typing, timeout)
if err != nil {
return
}
if !typing {
timeout = -1
}
intent.as.StateStore.SetTyping(roomID, intent.UserID, timeout)
return
}
func (intent *IntentAPI) SendText(roomID, text string) (*gomatrix.RespSendEvent, error) { func (intent *IntentAPI) SendText(roomID, text string) (*gomatrix.RespSendEvent, error) {
if err := intent.EnsureJoined(roomID); err != nil { if err := intent.EnsureJoined(roomID); err != nil {
return nil, err return nil, err

View file

@ -4,12 +4,16 @@ import (
"maunium.net/go/gomatrix" "maunium.net/go/gomatrix"
"strings" "strings"
"sync" "sync"
"time"
) )
type StateStore interface { type StateStore interface {
IsRegistered(userID string) bool IsRegistered(userID string) bool
MarkRegistered(userID string) MarkRegistered(userID string)
IsTyping(roomID, userID string) bool
SetTyping(roomID, userID string, timeout int64)
IsInRoom(roomID, userID string) bool IsInRoom(roomID, userID string) bool
SetMembership(roomID, userID, membership string) SetMembership(roomID, userID, membership string)
@ -28,33 +32,70 @@ func (as *AppService) UpdateState(evt *gomatrix.Event) {
} }
type BasicStateStore struct { type BasicStateStore struct {
registrationsLock sync.RWMutex `json:"-"` registrationsLock sync.RWMutex `json:"-"`
Registrations map[string]bool `json:"registrations"` Registrations map[string]bool `json:"registrations"`
membershipsLock sync.RWMutex `json:"-"` membershipsLock sync.RWMutex `json:"-"`
Memberships map[string]map[string]string `json:"memberships"` Memberships map[string]map[string]string `json:"memberships"`
powerLevelsLock sync.RWMutex `json:"-"` powerLevelsLock sync.RWMutex `json:"-"`
PowerLevels map[string]*gomatrix.PowerLevels `json:"power_levels"` PowerLevels map[string]*gomatrix.PowerLevels `json:"power_levels"`
Typing map[string]map[string]int64 `json:"-"`
typingLock sync.RWMutex `json:"-"`
} }
func NewBasicStateStore() *BasicStateStore { func NewBasicStateStore() StateStore {
return &BasicStateStore{ return &BasicStateStore{
Registrations: make(map[string]bool), Registrations: make(map[string]bool),
Memberships: make(map[string]map[string]string), Memberships: make(map[string]map[string]string),
PowerLevels: make(map[string]*gomatrix.PowerLevels), PowerLevels: make(map[string]*gomatrix.PowerLevels),
Typing: make(map[string]map[string]int64),
} }
} }
func (store *BasicStateStore) IsRegistered(userID string) bool { func (store *BasicStateStore) IsRegistered(userID string) bool {
store.registrationsLock.RLock() store.registrationsLock.RLock()
defer store.registrationsLock.RUnlock()
registered, ok := store.Registrations[userID] registered, ok := store.Registrations[userID]
store.registrationsLock.RUnlock()
return ok && registered return ok && registered
} }
func (store *BasicStateStore) MarkRegistered(userID string) { func (store *BasicStateStore) MarkRegistered(userID string) {
store.registrationsLock.Lock() store.registrationsLock.Lock()
defer store.registrationsLock.Unlock()
store.Registrations[userID] = true store.Registrations[userID] = true
store.registrationsLock.Unlock() }
func (store *BasicStateStore) IsTyping(roomID, userID string) bool {
store.typingLock.RLock()
defer store.typingLock.RUnlock()
roomTyping, ok := store.Typing[roomID]
if !ok {
return false
}
typingEndsAt, _ := roomTyping[userID]
return typingEndsAt >= time.Now().Unix()
}
func (store *BasicStateStore) SetTyping(roomID, userID string, timeout int64) {
store.typingLock.Lock()
defer store.typingLock.Unlock()
roomTyping, ok := store.Typing[roomID]
if !ok {
if timeout >= 0 {
roomTyping = map[string]int64{
userID: time.Now().Unix() + timeout,
}
} else {
roomTyping = make(map[string]int64)
}
} else {
if timeout >= 0 {
roomTyping[userID] = time.Now().Unix() + timeout
} else {
delete(roomTyping, userID)
}
}
store.Typing[roomID] = roomTyping
} }
func (store *BasicStateStore) GetRoomMemberships(roomID string) map[string]string { func (store *BasicStateStore) GetRoomMemberships(roomID string) map[string]string {
@ -88,12 +129,13 @@ func (store *BasicStateStore) SetMembership(roomID, userID, membership string) {
store.membershipsLock.Lock() store.membershipsLock.Lock()
memberships, ok := store.Memberships[roomID] memberships, ok := store.Memberships[roomID]
if !ok { if !ok {
store.Memberships[roomID] = map[string]string{ memberships = map[string]string{
userID: strings.ToLower(membership), userID: strings.ToLower(membership),
} }
} else { } else {
memberships[userID] = strings.ToLower(membership) memberships[userID] = strings.ToLower(membership)
} }
store.Memberships[roomID] = memberships
store.membershipsLock.Unlock() store.membershipsLock.Unlock()
} }