aboutsummaryrefslogtreecommitdiff
path: root/syncapi/streams/stream_invite.go
blob: ddac9be2c8957441e4c0017552fb85aa465c79dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package streams

import (
	"context"
	"crypto/sha256"
	"encoding/base64"
	"strconv"
	"time"

	"github.com/matrix-org/dendrite/syncapi/types"
	"github.com/matrix-org/gomatrixserverlib"
)

type InviteStreamProvider struct {
	StreamProvider
}

func (p *InviteStreamProvider) Setup() {
	p.StreamProvider.Setup()

	p.latestMutex.Lock()
	defer p.latestMutex.Unlock()

	id, err := p.DB.MaxStreamPositionForInvites(context.Background())
	if err != nil {
		panic(err)
	}
	p.latest = id
}

func (p *InviteStreamProvider) CompleteSync(
	ctx context.Context,
	req *types.SyncRequest,
) types.StreamPosition {
	return p.IncrementalSync(ctx, req, 0, p.LatestPosition(ctx))
}

func (p *InviteStreamProvider) IncrementalSync(
	ctx context.Context,
	req *types.SyncRequest,
	from, to types.StreamPosition,
) types.StreamPosition {
	r := types.Range{
		From: from,
		To:   to,
	}

	invites, retiredInvites, err := p.DB.InviteEventsInRange(
		ctx, req.Device.UserID, r,
	)
	if err != nil {
		req.Log.WithError(err).Error("p.DB.InviteEventsInRange failed")
		return from
	}

	for roomID, inviteEvent := range invites {
		// skip ignored user events
		if _, ok := req.IgnoredUsers.List[inviteEvent.Sender()]; ok {
			continue
		}
		ir := types.NewInviteResponse(inviteEvent)
		req.Response.Rooms.Invite[roomID] = *ir
	}

	for roomID := range retiredInvites {
		if _, ok := req.Response.Rooms.Join[roomID]; !ok {
			lr := types.NewLeaveResponse()
			h := sha256.Sum256(append([]byte(roomID), []byte(strconv.FormatInt(int64(to), 10))...))
			lr.Timeline.Events = append(lr.Timeline.Events, gomatrixserverlib.ClientEvent{
				// fake event ID which muxes in the to position
				EventID:        "$" + base64.RawURLEncoding.EncodeToString(h[:]),
				OriginServerTS: gomatrixserverlib.AsTimestamp(time.Now()),
				RoomID:         roomID,
				Sender:         req.Device.UserID,
				StateKey:       &req.Device.UserID,
				Type:           "m.room.member",
				Content:        gomatrixserverlib.RawJSON(`{"membership":"leave"}`),
			})
			req.Response.Rooms.Leave[roomID] = *lr
		}
	}

	return to
}