2021-01-08 10:59:06 -06:00
|
|
|
package streams
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
|
2022-03-29 07:14:35 -05:00
|
|
|
"github.com/matrix-org/dendrite/internal/caching"
|
2021-01-08 10:59:06 -06:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/types"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TypingStreamProvider struct {
|
|
|
|
StreamProvider
|
2022-03-29 07:14:35 -05:00
|
|
|
EDUCache *caching.EDUCache
|
2021-01-08 10:59:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *TypingStreamProvider) CompleteSync(
|
|
|
|
ctx context.Context,
|
|
|
|
req *types.SyncRequest,
|
|
|
|
) types.StreamPosition {
|
|
|
|
return p.IncrementalSync(ctx, req, 0, p.LatestPosition(ctx))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *TypingStreamProvider) IncrementalSync(
|
|
|
|
ctx context.Context,
|
|
|
|
req *types.SyncRequest,
|
|
|
|
from, to types.StreamPosition,
|
|
|
|
) types.StreamPosition {
|
|
|
|
var err error
|
|
|
|
for roomID, membership := range req.Rooms {
|
|
|
|
if membership != gomatrixserverlib.Join {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-01-13 08:32:49 -06:00
|
|
|
jr := *types.NewJoinResponse()
|
|
|
|
if existing, ok := req.Response.Rooms.Join[roomID]; ok {
|
|
|
|
jr = existing
|
|
|
|
}
|
2021-01-08 10:59:06 -06:00
|
|
|
|
|
|
|
if users, updated := p.EDUCache.GetTypingUsersIfUpdatedAfter(
|
|
|
|
roomID, int64(from),
|
|
|
|
); updated {
|
|
|
|
ev := gomatrixserverlib.ClientEvent{
|
|
|
|
Type: gomatrixserverlib.MTyping,
|
|
|
|
}
|
|
|
|
ev.Content, err = json.Marshal(map[string]interface{}{
|
|
|
|
"user_ids": users,
|
|
|
|
})
|
|
|
|
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
|
|
|
|
}
|