mirror of
https://github.com/hoernschen/dendrite.git
synced 2025-07-31 13:22:46 +00:00
use go module for dependencies (#594)
This commit is contained in:
parent
4d588f7008
commit
74827428bd
6109 changed files with 216 additions and 1114821 deletions
83
typingserver/api/input.go
Normal file
83
typingserver/api/input.go
Normal file
|
@ -0,0 +1,83 @@
|
|||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// Package api provides the types that are used to communicate with the typing server.
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
commonHTTP "github.com/matrix-org/dendrite/common/http"
|
||||
"github.com/matrix-org/gomatrixserverlib"
|
||||
opentracing "github.com/opentracing/opentracing-go"
|
||||
)
|
||||
|
||||
// InputTypingEvent is an event for notifying the typing server about typing updates.
|
||||
type InputTypingEvent struct {
|
||||
// UserID of the user to update typing status.
|
||||
UserID string `json:"user_id"`
|
||||
// RoomID of the room the user is typing (or has stopped).
|
||||
RoomID string `json:"room_id"`
|
||||
// Typing is true if the user is typing, false if they have stopped.
|
||||
Typing bool `json:"typing"`
|
||||
// Timeout is the interval for which the user should be marked as typing.
|
||||
Timeout int64 `json:"timeout"`
|
||||
// OriginServerTS when the server received the update.
|
||||
OriginServerTS gomatrixserverlib.Timestamp `json:"origin_server_ts"`
|
||||
}
|
||||
|
||||
// InputTypingEventRequest is a request to TypingServerInputAPI
|
||||
type InputTypingEventRequest struct {
|
||||
InputTypingEvent InputTypingEvent `json:"input_typing_event"`
|
||||
}
|
||||
|
||||
// InputTypingEventResponse is a response to InputTypingEvents
|
||||
type InputTypingEventResponse struct{}
|
||||
|
||||
// TypingServerInputAPI is used to write events to the typing server.
|
||||
type TypingServerInputAPI interface {
|
||||
InputTypingEvent(
|
||||
ctx context.Context,
|
||||
request *InputTypingEventRequest,
|
||||
response *InputTypingEventResponse,
|
||||
) error
|
||||
}
|
||||
|
||||
// TypingServerInputTypingEventPath is the HTTP path for the InputTypingEvent API.
|
||||
const TypingServerInputTypingEventPath = "/api/typingserver/input"
|
||||
|
||||
// NewTypingServerInputAPIHTTP creates a TypingServerInputAPI implemented by talking to a HTTP POST API.
|
||||
func NewTypingServerInputAPIHTTP(typingServerURL string, httpClient *http.Client) TypingServerInputAPI {
|
||||
if httpClient == nil {
|
||||
httpClient = http.DefaultClient
|
||||
}
|
||||
return &httpTypingServerInputAPI{typingServerURL, httpClient}
|
||||
}
|
||||
|
||||
type httpTypingServerInputAPI struct {
|
||||
typingServerURL string
|
||||
httpClient *http.Client
|
||||
}
|
||||
|
||||
// InputRoomEvents implements TypingServerInputAPI
|
||||
func (h *httpTypingServerInputAPI) InputTypingEvent(
|
||||
ctx context.Context,
|
||||
request *InputTypingEventRequest,
|
||||
response *InputTypingEventResponse,
|
||||
) error {
|
||||
span, ctx := opentracing.StartSpanFromContext(ctx, "InputTypingEvent")
|
||||
defer span.Finish()
|
||||
|
||||
apiURL := h.typingServerURL + TypingServerInputTypingEventPath
|
||||
return commonHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response)
|
||||
}
|
31
typingserver/api/output.go
Normal file
31
typingserver/api/output.go
Normal file
|
@ -0,0 +1,31 @@
|
|||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package api
|
||||
|
||||
// OutputTypingEvent is an entry in typing server output kafka log.
|
||||
// This contains the event with extra fields used to create 'm.typing' event
|
||||
// in clientapi & federation.
|
||||
type OutputTypingEvent struct {
|
||||
// The Event for the typing edu event.
|
||||
Event TypingEvent `json:"event"`
|
||||
// Users typing in the room when the event was generated.
|
||||
TypingUsers []string `json:"typing_users"`
|
||||
}
|
||||
|
||||
// TypingEvent represents a matrix edu event of type 'm.typing'.
|
||||
type TypingEvent struct {
|
||||
Type string `json:"type"`
|
||||
RoomID string `json:"room_id"`
|
||||
UserID string `json:"user_id"`
|
||||
Typing bool `json:"typing"`
|
||||
}
|
108
typingserver/cache/cache.go
vendored
Normal file
108
typingserver/cache/cache.go
vendored
Normal file
|
@ -0,0 +1,108 @@
|
|||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package cache
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
const defaultTypingTimeout = 10 * time.Second
|
||||
|
||||
// userSet is a map of user IDs to a timer, timer fires at expiry.
|
||||
type userSet map[string]*time.Timer
|
||||
|
||||
// TypingCache maintains a list of users typing in each room.
|
||||
type TypingCache struct {
|
||||
sync.RWMutex
|
||||
data map[string]userSet
|
||||
}
|
||||
|
||||
// NewTypingCache returns a new TypingCache initialized for use.
|
||||
func NewTypingCache() *TypingCache {
|
||||
return &TypingCache{data: make(map[string]userSet)}
|
||||
}
|
||||
|
||||
// GetTypingUsers returns the list of users typing in a room.
|
||||
func (t *TypingCache) GetTypingUsers(roomID string) (users []string) {
|
||||
t.RLock()
|
||||
usersMap, ok := t.data[roomID]
|
||||
t.RUnlock()
|
||||
if ok {
|
||||
users = make([]string, 0, len(usersMap))
|
||||
for userID := range usersMap {
|
||||
users = append(users, userID)
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// AddTypingUser sets an user as typing in a room.
|
||||
// expire is the time when the user typing should time out.
|
||||
// if expire is nil, defaultTypingTimeout is assumed.
|
||||
func (t *TypingCache) AddTypingUser(userID, roomID string, expire *time.Time) {
|
||||
expireTime := getExpireTime(expire)
|
||||
if until := time.Until(expireTime); until > 0 {
|
||||
timer := time.AfterFunc(until, t.timeoutCallback(userID, roomID))
|
||||
t.addUser(userID, roomID, timer)
|
||||
}
|
||||
}
|
||||
|
||||
// addUser with mutex lock & replace the previous timer.
|
||||
func (t *TypingCache) addUser(userID, roomID string, expiryTimer *time.Timer) {
|
||||
t.Lock()
|
||||
defer t.Unlock()
|
||||
|
||||
if t.data[roomID] == nil {
|
||||
t.data[roomID] = make(userSet)
|
||||
}
|
||||
|
||||
// Stop the timer to cancel the call to timeoutCallback
|
||||
if timer, ok := t.data[roomID][userID]; ok {
|
||||
// It may happen that at this stage timer fires but now we have a lock on t.
|
||||
// Hence the execution of timeoutCallback will happen after we unlock.
|
||||
// So we may lose a typing state, though this event is highly unlikely.
|
||||
// This can be mitigated by keeping another time.Time in the map and check against it
|
||||
// before removing. This however is not required in most practical scenario.
|
||||
timer.Stop()
|
||||
}
|
||||
|
||||
t.data[roomID][userID] = expiryTimer
|
||||
}
|
||||
|
||||
// Returns a function which is called after timeout happens.
|
||||
// This removes the user.
|
||||
func (t *TypingCache) timeoutCallback(userID, roomID string) func() {
|
||||
return func() {
|
||||
t.RemoveUser(userID, roomID)
|
||||
}
|
||||
}
|
||||
|
||||
// RemoveUser with mutex lock & stop the timer.
|
||||
func (t *TypingCache) RemoveUser(userID, roomID string) {
|
||||
t.Lock()
|
||||
defer t.Unlock()
|
||||
|
||||
if timer, ok := t.data[roomID][userID]; ok {
|
||||
timer.Stop()
|
||||
delete(t.data[roomID], userID)
|
||||
}
|
||||
}
|
||||
|
||||
func getExpireTime(expire *time.Time) time.Time {
|
||||
if expire != nil {
|
||||
return *expire
|
||||
}
|
||||
return time.Now().Add(defaultTypingTimeout)
|
||||
}
|
99
typingserver/cache/cache_test.go
vendored
Normal file
99
typingserver/cache/cache_test.go
vendored
Normal file
|
@ -0,0 +1,99 @@
|
|||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package cache
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/matrix-org/dendrite/common/test"
|
||||
)
|
||||
|
||||
func TestTypingCache(t *testing.T) {
|
||||
tCache := NewTypingCache()
|
||||
if tCache == nil {
|
||||
t.Fatal("NewTypingCache failed")
|
||||
}
|
||||
|
||||
t.Run("AddTypingUser", func(t *testing.T) {
|
||||
testAddTypingUser(t, tCache)
|
||||
})
|
||||
|
||||
t.Run("GetTypingUsers", func(t *testing.T) {
|
||||
testGetTypingUsers(t, tCache)
|
||||
})
|
||||
|
||||
t.Run("RemoveUser", func(t *testing.T) {
|
||||
testRemoveUser(t, tCache)
|
||||
})
|
||||
}
|
||||
|
||||
func testAddTypingUser(t *testing.T, tCache *TypingCache) {
|
||||
present := time.Now()
|
||||
tests := []struct {
|
||||
userID string
|
||||
roomID string
|
||||
expire *time.Time
|
||||
}{ // Set four users typing state to room1
|
||||
{"user1", "room1", nil},
|
||||
{"user2", "room1", nil},
|
||||
{"user3", "room1", nil},
|
||||
{"user4", "room1", nil},
|
||||
//typing state with past expireTime should not take effect or removed.
|
||||
{"user1", "room2", &present},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
tCache.AddTypingUser(tt.userID, tt.roomID, tt.expire)
|
||||
}
|
||||
}
|
||||
|
||||
func testGetTypingUsers(t *testing.T, tCache *TypingCache) {
|
||||
tests := []struct {
|
||||
roomID string
|
||||
wantUsers []string
|
||||
}{
|
||||
{"room1", []string{"user1", "user2", "user3", "user4"}},
|
||||
{"room2", []string{}},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
gotUsers := tCache.GetTypingUsers(tt.roomID)
|
||||
if !test.UnsortedStringSliceEqual(gotUsers, tt.wantUsers) {
|
||||
t.Errorf("TypingCache.GetTypingUsers(%s) = %v, want %v", tt.roomID, gotUsers, tt.wantUsers)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testRemoveUser(t *testing.T, tCache *TypingCache) {
|
||||
tests := []struct {
|
||||
roomID string
|
||||
userIDs []string
|
||||
}{
|
||||
{"room3", []string{"user1"}},
|
||||
{"room4", []string{"user1", "user2", "user3"}},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
for _, userID := range tt.userIDs {
|
||||
tCache.AddTypingUser(userID, tt.roomID, nil)
|
||||
}
|
||||
|
||||
length := len(tt.userIDs)
|
||||
tCache.RemoveUser(tt.userIDs[length-1], tt.roomID)
|
||||
expLeftUsers := tt.userIDs[:length-1]
|
||||
if leftUsers := tCache.GetTypingUsers(tt.roomID); !test.UnsortedStringSliceEqual(leftUsers, expLeftUsers) {
|
||||
t.Errorf("Response after removal is unexpected. Want = %s, got = %s", leftUsers, expLeftUsers)
|
||||
}
|
||||
}
|
||||
}
|
101
typingserver/input/input.go
Normal file
101
typingserver/input/input.go
Normal file
|
@ -0,0 +1,101 @@
|
|||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package input
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/matrix-org/dendrite/common"
|
||||
"github.com/matrix-org/dendrite/typingserver/api"
|
||||
"github.com/matrix-org/dendrite/typingserver/cache"
|
||||
"github.com/matrix-org/gomatrixserverlib"
|
||||
"github.com/matrix-org/util"
|
||||
"gopkg.in/Shopify/sarama.v1"
|
||||
)
|
||||
|
||||
// TypingServerInputAPI implements api.TypingServerInputAPI
|
||||
type TypingServerInputAPI struct {
|
||||
// Cache to store the current typing members in each room.
|
||||
Cache *cache.TypingCache
|
||||
// The kafka topic to output new typing events to.
|
||||
OutputTypingEventTopic string
|
||||
// kafka producer
|
||||
Producer sarama.SyncProducer
|
||||
}
|
||||
|
||||
// InputTypingEvent implements api.TypingServerInputAPI
|
||||
func (t *TypingServerInputAPI) InputTypingEvent(
|
||||
ctx context.Context,
|
||||
request *api.InputTypingEventRequest,
|
||||
response *api.InputTypingEventResponse,
|
||||
) error {
|
||||
ite := &request.InputTypingEvent
|
||||
if ite.Typing {
|
||||
// user is typing, update our current state of users typing.
|
||||
expireTime := ite.OriginServerTS.Time().Add(
|
||||
time.Duration(ite.Timeout) * time.Millisecond,
|
||||
)
|
||||
t.Cache.AddTypingUser(ite.UserID, ite.RoomID, &expireTime)
|
||||
} else {
|
||||
t.Cache.RemoveUser(ite.UserID, ite.RoomID)
|
||||
}
|
||||
|
||||
return t.sendEvent(ite)
|
||||
}
|
||||
|
||||
func (t *TypingServerInputAPI) sendEvent(ite *api.InputTypingEvent) error {
|
||||
userIDs := t.Cache.GetTypingUsers(ite.RoomID)
|
||||
ev := &api.TypingEvent{
|
||||
Type: gomatrixserverlib.MTyping,
|
||||
RoomID: ite.RoomID,
|
||||
UserID: ite.UserID,
|
||||
}
|
||||
ote := &api.OutputTypingEvent{
|
||||
Event: *ev,
|
||||
TypingUsers: userIDs,
|
||||
}
|
||||
|
||||
eventJSON, err := json.Marshal(ote)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
m := &sarama.ProducerMessage{
|
||||
Topic: string(t.OutputTypingEventTopic),
|
||||
Key: sarama.StringEncoder(ite.RoomID),
|
||||
Value: sarama.ByteEncoder(eventJSON),
|
||||
}
|
||||
|
||||
_, _, err = t.Producer.SendMessage(m)
|
||||
return err
|
||||
}
|
||||
|
||||
// SetupHTTP adds the TypingServerInputAPI handlers to the http.ServeMux.
|
||||
func (t *TypingServerInputAPI) SetupHTTP(servMux *http.ServeMux) {
|
||||
servMux.Handle(api.TypingServerInputTypingEventPath,
|
||||
common.MakeInternalAPI("inputTypingEvents", func(req *http.Request) util.JSONResponse {
|
||||
var request api.InputTypingEventRequest
|
||||
var response api.InputTypingEventResponse
|
||||
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
||||
return util.MessageResponse(http.StatusBadRequest, err.Error())
|
||||
}
|
||||
if err := t.InputTypingEvent(req.Context(), &request, &response); err != nil {
|
||||
return util.ErrorResponse(err)
|
||||
}
|
||||
return util.JSONResponse{Code: http.StatusOK, JSON: &response}
|
||||
}),
|
||||
)
|
||||
}
|
40
typingserver/typingserver.go
Normal file
40
typingserver/typingserver.go
Normal file
|
@ -0,0 +1,40 @@
|
|||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package typingserver
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/matrix-org/dendrite/common/basecomponent"
|
||||
"github.com/matrix-org/dendrite/typingserver/api"
|
||||
"github.com/matrix-org/dendrite/typingserver/cache"
|
||||
"github.com/matrix-org/dendrite/typingserver/input"
|
||||
)
|
||||
|
||||
// SetupTypingServerComponent sets up and registers HTTP handlers for the
|
||||
// TypingServer component. Returns instances of the various roomserver APIs,
|
||||
// allowing other components running in the same process to hit the query the
|
||||
// APIs directly instead of having to use HTTP.
|
||||
func SetupTypingServerComponent(
|
||||
base *basecomponent.BaseDendrite,
|
||||
typingCache *cache.TypingCache,
|
||||
) api.TypingServerInputAPI {
|
||||
inputAPI := &input.TypingServerInputAPI{
|
||||
Cache: typingCache,
|
||||
Producer: base.KafkaProducer,
|
||||
OutputTypingEventTopic: string(base.Cfg.Kafka.Topics.OutputTypingEvent),
|
||||
}
|
||||
|
||||
inputAPI.SetupHTTP(http.DefaultServeMux)
|
||||
return inputAPI
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue