~samwhited/xmpp

xmpp/paging/types.go -rw-r--r-- 6.0 KiB
e9b0a2deSam Whited docs: do a quick editing pass over the docs a day ago
                                                                                
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright 2021 The Mellium Contributors.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.

package paging

import (
	"encoding/xml"
	"strconv"

	"mellium.im/xmlstream"
)

// RequestCount can be added to a query to request the count of elements without
// returning any actual items.
type RequestCount struct {
	XMLName xml.Name `xml:"http://jabber.org/protocol/rsm set"`
}

// TokenReader implements xmlstream.Marshaler.
func (req *RequestCount) TokenReader() xml.TokenReader {
	return xmlstream.Wrap(
		xmlstream.Wrap(
			xmlstream.Token(xml.CharData("0")),
			xml.StartElement{Name: xml.Name{Local: "max"}},
		),
		xml.StartElement{Name: xml.Name{Space: NS, Local: "set"}},
	)
}

// WriteXML implements xmlstream.WriterTo.
func (req *RequestCount) WriteXML(w xmlstream.TokenWriter) (int, error) {
	return xmlstream.Copy(w, req.TokenReader())
}

// MarshalXML implements xml.Marshaler.
func (req *RequestCount) MarshalXML(e *xml.Encoder, _ xml.StartElement) error {
	_, err := req.WriteXML(e)
	return err
}

// RequestNext can be added to a query to request the first page or to page
// forward.
type RequestNext struct {
	XMLName xml.Name `xml:"http://jabber.org/protocol/rsm set"`
	Max     uint64   `xml:"max,omitempty"`
	After   string   `xml:"after,omitempty"`
}

// TokenReader implements xmlstream.Marshaler.
func (req *RequestNext) TokenReader() xml.TokenReader {
	var payloads []xml.TokenReader
	if req.Max > 0 {
		payloads = append(payloads, xmlstream.Wrap(
			xmlstream.Token(xml.CharData(strconv.FormatUint(req.Max, 10))),
			xml.StartElement{Name: xml.Name{Local: "max"}},
		))
	}
	if req.After != "" {
		payloads = append(payloads, xmlstream.Wrap(
			xmlstream.Token(xml.CharData(req.After)),
			xml.StartElement{Name: xml.Name{Local: "after"}},
		))
	}
	return xmlstream.Wrap(
		xmlstream.MultiReader(payloads...),
		xml.StartElement{Name: xml.Name{Space: NS, Local: "set"}},
	)
}

// WriteXML implements xmlstream.WriterTo.
func (req *RequestNext) WriteXML(w xmlstream.TokenWriter) (int, error) {
	return xmlstream.Copy(w, req.TokenReader())
}

// MarshalXML implements xml.Marshaler.
func (req *RequestNext) MarshalXML(e *xml.Encoder, _ xml.StartElement) error {
	_, err := req.WriteXML(e)
	return err
}

// RequestPrev can be added to a query to request the last page or to page
// backward.
type RequestPrev struct {
	XMLName xml.Name `xml:"http://jabber.org/protocol/rsm set"`
	Max     uint64   `xml:"max,omitempty"`
	Before  string   `xml:"before"`
}

// TokenReader implements xmlstream.Marshaler.
func (req *RequestPrev) TokenReader() xml.TokenReader {
	payloads := []xml.TokenReader{xmlstream.Wrap(
		xmlstream.Token(xml.CharData(req.Before)),
		xml.StartElement{Name: xml.Name{Local: "before"}},
	)}
	if req.Max > 0 {
		payloads = append(payloads, xmlstream.Wrap(
			xmlstream.Token(xml.CharData(strconv.FormatUint(req.Max, 10))),
			xml.StartElement{Name: xml.Name{Local: "max"}},
		))
	}
	return xmlstream.Wrap(
		xmlstream.MultiReader(payloads...),
		xml.StartElement{Name: xml.Name{Space: NS, Local: "set"}},
	)
}

// WriteXML implements xmlstream.WriterTo.
func (req *RequestPrev) WriteXML(w xmlstream.TokenWriter) (int, error) {
	return xmlstream.Copy(w, req.TokenReader())
}

// MarshalXML implements xml.Marshaler.
func (req *RequestPrev) MarshalXML(e *xml.Encoder, _ xml.StartElement) error {
	_, err := req.WriteXML(e)
	return err
}

// RequestIndex can be added to a query to skip to a specific page.
// It is not always supported.
type RequestIndex struct {
	XMLName xml.Name `xml:"http://jabber.org/protocol/rsm set"`
	Max     uint64   `xml:"max"`
	Index   uint64   `xml:"index"`
}

// TokenReader implements xmlstream.Marshaler.
func (req *RequestIndex) TokenReader() xml.TokenReader {
	payloads := []xml.TokenReader{xmlstream.Wrap(
		xmlstream.Token(xml.CharData(strconv.FormatUint(req.Index, 10))),
		xml.StartElement{Name: xml.Name{Local: "index"}},
	), xmlstream.Wrap(
		xmlstream.Token(xml.CharData(strconv.FormatUint(req.Max, 10))),
		xml.StartElement{Name: xml.Name{Local: "max"}},
	)}
	return xmlstream.Wrap(
		xmlstream.MultiReader(payloads...),
		xml.StartElement{Name: xml.Name{Space: NS, Local: "set"}},
	)
}

// WriteXML implements xmlstream.WriterTo.
func (req *RequestIndex) WriteXML(w xmlstream.TokenWriter) (int, error) {
	return xmlstream.Copy(w, req.TokenReader())
}

// MarshalXML implements xml.Marshaler.
func (req *RequestIndex) MarshalXML(e *xml.Encoder, _ xml.StartElement) error {
	_, err := req.WriteXML(e)
	return err
}

// Set describes a page from a returned result set.
type Set struct {
	XMLName xml.Name `xml:"http://jabber.org/protocol/rsm set"`
	First   struct {
		ID    string  `xml:",cdata"`
		Index *uint64 `xml:"index,attr,omitempty"`
	} `xml:"first"`
	Last  string  `xml:"last"`
	Count *uint64 `xml:"count,omitempty"`
}

// TokenReader implements xmlstream.Marshaler.
func (s *Set) TokenReader() xml.TokenReader {
	var payloads []xml.TokenReader
	start := xml.StartElement{Name: xml.Name{Local: "first"}}
	if s.First.Index != nil {
		start.Attr = append(start.Attr, xml.Attr{
			Name:  xml.Name{Local: "index"},
			Value: strconv.FormatUint(*s.First.Index, 10),
		})
	}
	payloads = append(payloads, xmlstream.Wrap(
		xmlstream.Token(xml.CharData(s.First.ID)),
		start,
	))
	payloads = append(payloads, xmlstream.Wrap(
		xmlstream.Token(xml.CharData(s.Last)),
		xml.StartElement{Name: xml.Name{Local: "last"}},
	))
	if s.Count != nil {
		payloads = append(payloads, xmlstream.Wrap(
			xmlstream.Token(xml.CharData(strconv.FormatUint(*s.Count, 10))),
			xml.StartElement{Name: xml.Name{Local: "count"}},
		))
	}
	return xmlstream.Wrap(
		xmlstream.MultiReader(payloads...),
		xml.StartElement{Name: xml.Name{Space: NS, Local: "set"}},
	)
}

// WriteXML implements xmlstream.WriterTo.
func (s *Set) WriteXML(w xmlstream.TokenWriter) (int, error) {
	return xmlstream.Copy(w, s.TokenReader())
}

// MarshalXML satisfies the xml.Marshaler interface.
func (s *Set) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
	_, err := s.WriteXML(e)
	return err
}