-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
118 lines (99 loc) · 2.4 KB
/
types.go
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
package core
import (
"encoding/json"
"time"
"github.com/hood-chat/core/entity"
"github.com/hood-chat/core/protocol"
"github.com/libp2p/go-libp2p/core/event"
"github.com/libp2p/go-libp2p/core/peer"
pl "github.com/libp2p/go-libp2p/core/protocol"
)
func Of[E any](e E) *E {
return &e
}
type Emitter = event.Emitter
type Bus = event.Bus
type SearchChatOpt struct {
Name *string `json:"name,omitempty"`
Members []entity.ID `json:"members,omitempty"`
Type *entity.ChatType `json:"type,omitempty"`
}
func (m *SearchChatOpt) Json() ([]byte, error) {
bytes, err := json.Marshal(m)
if err != nil {
return nil, err
}
return bytes, nil
}
func (m *SearchChatOpt) DTO() SearchChatOpt {
return *m
}
func WithPrivateChatContact(contactID entity.ID) SearchChatOpt {
return SearchChatOpt{nil, []entity.ID{contactID}, Of(entity.Private)}
}
type NewChatOpt struct {
Name *string `json:"name,omitempty"`
Members []entity.Contact `json:"members,omitempty"`
Type *entity.ChatType `json:"type,omitempty"`
}
func (m *NewChatOpt) Json() ([]byte, error) {
bytes, err := json.Marshal(m)
if err != nil {
return nil, err
}
return bytes, nil
}
func (m *NewChatOpt) DTO() NewChatOpt {
return *m
}
func NewPrivateChat(contact entity.Contact) NewChatOpt {
return NewChatOpt{nil, []entity.Contact{contact}, Of(entity.Private)}
}
func NewGroupChat(n string, c []entity.Contact) NewChatOpt {
return NewChatOpt{Name: &n, Members: c, Type: Of(entity.Group)}
}
type ChatRequest struct {
ID entity.ID
Name string
Members []entity.Contact
Type entity.ChatType
Admins []entity.Contact
}
type Envelop struct {
To entity.Contact
Message entity.ProtoMessage
ID string
CreatedAt int64
Protocol pl.ID
}
func (e Envelop) createdAt() time.Time {
return time.Unix(e.CreatedAt, 0)
}
func (e Envelop) id() string {
return e.ID
}
func (e Envelop) PeerID() peer.ID {
pi, _ := e.To.PeerID()
return pi
}
type PubSubEnvelop struct {
Topic string
Message entity.ProtoMessage
CreatedAt int64
}
func (e PubSubEnvelop) createdAt(t time.Time) time.Time {
return time.Unix(e.CreatedAt, 0)
}
func NewMessageEnvelop(c entity.Contact, m entity.Message) (*Envelop, error) {
_, err := c.PeerID()
if err != nil {
return nil, err
}
return &Envelop{
To: c,
Message: m,
ID: m.ID.String(),
CreatedAt: m.CreatedAt,
Protocol: protocol.Message.ID(),
}, nil
}