summary refs log tree commit diff
path: root/vendor/maunium.net/go/mautrix/event/eventsource.go
blob: 86c1cebe06e5064723c808f15d77b7cac0b73743 (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
// Copyright (c) 2024 Tulir Asokan
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

package event

import (
	"fmt"
)

// Source represents the part of the sync response that an event came from.
type Source int

const (
	SourcePresence Source = 1 << iota
	SourceJoin
	SourceInvite
	SourceLeave
	SourceAccountData
	SourceTimeline
	SourceState
	SourceEphemeral
	SourceToDevice
	SourceDecrypted
)

const primaryTypes = SourcePresence | SourceAccountData | SourceToDevice | SourceTimeline | SourceState
const roomSections = SourceJoin | SourceInvite | SourceLeave
const roomableTypes = SourceAccountData | SourceTimeline | SourceState
const encryptableTypes = roomableTypes | SourceToDevice

func (es Source) String() string {
	var typeName string
	switch es & primaryTypes {
	case SourcePresence:
		typeName = "presence"
	case SourceAccountData:
		typeName = "account data"
	case SourceToDevice:
		typeName = "to-device"
	case SourceTimeline:
		typeName = "timeline"
	case SourceState:
		typeName = "state"
	default:
		return fmt.Sprintf("unknown (%d)", es)
	}
	if es&roomableTypes != 0 {
		switch es & roomSections {
		case SourceJoin:
			typeName = "joined room " + typeName
		case SourceInvite:
			typeName = "invited room " + typeName
		case SourceLeave:
			typeName = "left room " + typeName
		default:
			return fmt.Sprintf("unknown (%s+%d)", typeName, es)
		}
		es &^= roomSections
	}
	if es&encryptableTypes != 0 && es&SourceDecrypted != 0 {
		typeName += " (decrypted)"
		es &^= SourceDecrypted
	}
	es &^= primaryTypes
	if es != 0 {
		return fmt.Sprintf("unknown (%s+%d)", typeName, es)
	}
	return typeName
}