~samwhited/xmpp

ref: 196304307d9d915570ed0c5c4734b8247a75cc9d xmpp/history/history.go -rw-r--r-- 1.3 KiB
19630430Sam Whited history: add initial implementation of MAM 5 months 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
// 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 history

import (
	"context"
	"encoding/xml"

	"mellium.im/xmpp"
	"mellium.im/xmpp/internal/attr"
	"mellium.im/xmpp/jid"
	"mellium.im/xmpp/mux"
	"mellium.im/xmpp/paging"
	"mellium.im/xmpp/stanza"
)

// Handle returns an option that registers a Handler for incoming history query
// results.
func Handle(h mux.MessageHandler) mux.Option {
	return mux.Message("", xml.Name{Space: NS, Local: "result"}, h)
}

// Fetch requests messages from the archive.
// The messages must be handled separately and Fetch blocks until they have all
// been prosessed.
func Fetch(ctx context.Context, filter Query, to jid.JID, s *xmpp.Session) (paging.Set, error) {
	return FetchIQ(ctx, filter, stanza.IQ{
		To: to,
	}, s)
}

// FetchIQ is like fetch but it allows modifying the underlying IQ.
// Changing the type of the IQ has no effect.
func FetchIQ(ctx context.Context, filter Query, iq stanza.IQ, s *xmpp.Session) (paging.Set, error) {
	if filter.ID == "" {
		filter.ID = attr.RandomID()
	}
	iq.Type = stanza.SetIQ
	var result struct {
		XMLName xml.Name `xml:"urn:xmpp:mam:2 fin"`
		Set     paging.Set
	}
	err := s.UnmarshalIQ(
		ctx,
		iq.Wrap(filter.TokenReader()),
		&result,
	)
	return result.Set, err
}