2021-01-08 17:59:06 +01:00
|
|
|
package streams
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
|
2022-10-05 14:47:13 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
|
2022-03-29 14:14:35 +02:00
|
|
|
"github.com/matrix-org/dendrite/internal/caching"
|
2022-09-30 13:48:10 +02:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/storage"
|
2021-01-08 17:59:06 +01:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TypingStreamProvider struct {
|
2022-09-30 13:48:10 +02:00
|
|
|
DefaultStreamProvider
|
2022-03-29 14:14:35 +02:00
|
|
|
EDUCache *caching.EDUCache
|
2021-01-08 17:59:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *TypingStreamProvider) CompleteSync(
|
|
|
|
ctx context.Context,
|
2022-09-30 13:48:10 +02:00
|
|
|
snapshot storage.DatabaseTransaction,
|
2021-01-08 17:59:06 +01:00
|
|
|
req *types.SyncRequest,
|
|
|
|
) types.StreamPosition {
|
2022-09-30 13:48:10 +02:00
|
|
|
return p.IncrementalSync(ctx, snapshot, req, 0, p.LatestPosition(ctx))
|
2021-01-08 17:59:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *TypingStreamProvider) IncrementalSync(
|
|
|
|
ctx context.Context,
|
2022-09-30 13:48:10 +02:00
|
|
|
snapshot storage.DatabaseTransaction,
|
2021-01-08 17:59:06 +01:00
|
|
|
req *types.SyncRequest,
|
|
|
|
from, to types.StreamPosition,
|
|
|
|
) types.StreamPosition {
|
|
|
|
var err error
|
|
|
|
for roomID, membership := range req.Rooms {
|
|
|
|
if membership != gomatrixserverlib.Join {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-10-05 14:47:13 +02:00
|
|
|
jr, ok := req.Response.Rooms.Join[roomID]
|
|
|
|
if !ok {
|
|
|
|
jr = types.NewJoinResponse()
|
2021-01-13 15:32:49 +01:00
|
|
|
}
|
2021-01-08 17:59:06 +01:00
|
|
|
|
|
|
|
if users, updated := p.EDUCache.GetTypingUsersIfUpdatedAfter(
|
|
|
|
roomID, int64(from),
|
|
|
|
); updated {
|
2022-04-07 16:08:19 +02:00
|
|
|
typingUsers := make([]string, 0, len(users))
|
|
|
|
for i := range users {
|
|
|
|
// skip ignored user events
|
|
|
|
if _, ok := req.IgnoredUsers.List[users[i]]; !ok {
|
|
|
|
typingUsers = append(typingUsers, users[i])
|
|
|
|
}
|
|
|
|
}
|
2021-01-08 17:59:06 +01:00
|
|
|
ev := gomatrixserverlib.ClientEvent{
|
|
|
|
Type: gomatrixserverlib.MTyping,
|
|
|
|
}
|
|
|
|
ev.Content, err = json.Marshal(map[string]interface{}{
|
2022-04-07 16:08:19 +02:00
|
|
|
"user_ids": typingUsers,
|
2021-01-08 17:59:06 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
req.Log.WithError(err).Error("json.Marshal failed")
|
|
|
|
return from
|
|
|
|
}
|
|
|
|
|
|
|
|
jr.Ephemeral.Events = append(jr.Ephemeral.Events, ev)
|
|
|
|
req.Response.Rooms.Join[roomID] = jr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return to
|
|
|
|
}
|