Debian Codegraph @

id/opaque.go

raw
1 // Copyright (c) 2020 Tulir Asokan
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7 package id
8
9 import (
10 "fmt"
11 )
12
13 // A RoomID is a string starting with ! that references a specific room.
14 // https://matrix.org/docs/spec/appendices#room-ids-and-event-ids
15 type RoomID string
16
17 // A RoomAlias is a string starting with # that can be resolved into.
18 // https://matrix.org/docs/spec/appendices#room-aliases
19 type RoomAlias string
20
21 func NewRoomAlias(localpart, server string) RoomAlias {
22 return RoomAlias(fmt.Sprintf("#%s:%s", localpart, server))
23 }
24
25 // An EventID is a string starting with $ that references a specific event.
26 //
27 // https://matrix.org/docs/spec/appendices#room-ids-and-event-ids
28 // https://matrix.org/docs/spec/rooms/v4#event-ids
29 type EventID string
30
31 // A BatchID is a string identifying a batch of events being backfilled to a room.
32 // https://github.com/matrix-org/matrix-doc/pull/2716
33 type BatchID string
34
35 func (roomID RoomID) String() string {
36 return string(roomID)
37 }
38
39 func (roomID RoomID) URI(via ...string) *MatrixURI {
40 return &MatrixURI{
41 Sigil1: '!',
42 MXID1: string(roomID)[1:],
43 Via: via,
44 }
45 }
46
47 func (roomID RoomID) EventURI(eventID EventID, via ...string) *MatrixURI {
48 return &MatrixURI{
49 Sigil1: '!',
50 MXID1: string(roomID)[1:],
51 Sigil2: '$',
52 MXID2: string(eventID)[1:],
53 Via: via,
54 }
55 }
56
57 func (roomAlias RoomAlias) String() string {
58 return string(roomAlias)
59 }
60
61 func (roomAlias RoomAlias) URI() *MatrixURI {
62 return &MatrixURI{
63 Sigil1: '#',
64 MXID1: string(roomAlias)[1:],
65 }
66 }
67
68 func (roomAlias RoomAlias) EventURI(eventID EventID) *MatrixURI {
69 return &MatrixURI{
70 Sigil1: '#',
71 MXID1: string(roomAlias)[1:],
72 Sigil2: '$',
73 MXID2: string(eventID)[1:],
74 }
75 }
76
77 func (eventID EventID) String() string {
78 return string(eventID)
79 }
80
81 func (batchID BatchID) String() string {
82 return string(batchID)
83 }