/
json.go
executable file
·227 lines (196 loc) · 7.35 KB
/
json.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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package groupme
import (
"encoding/json"
"fmt"
)
// Meta is the error type returned in the GroupMe response.
// Meant for clients that can't read HTTP status codes
type Meta struct {
Code int `json:"code,omitempty"`
Errors []string `json:"errors,omitempty"`
}
// Error returns the code and the error list as a string.
// Satisfies the error interface
func (m Meta) Error() string {
return fmt.Sprintf("Error Code %d: %v", m.Code, m.Errors)
}
// Group is a GroupMe group, returned in JSON API responses
type Group struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
// Type of group (private|public)
Type string `json:"type,omitempty"`
Description string `json:"description,omitempty"`
ImageURL string `json:"image_url,omitempty"`
CreatorUserID string `json:"creator_user_id,omitempty"`
CreatedAt Timestamp `json:"created_at,omitempty"`
UpdatedAt Timestamp `json:"updated_at,omitempty"`
Members []*Member `json:"members,omitempty"`
ShareURL string `json:"share_url,omitempty"`
Messages GroupMessages `json:"messages,omitempty"`
}
// GroupMessages is a Group field, only returned in Group JSON API responses
type GroupMessages struct {
Count uint `json:"count,omitempty"`
LastMessageID string `json:"last_message_id,omitempty"`
LastMessageCreatedAt Timestamp `json:"last_message_created_at,omitempty"`
Preview MessagePreview `json:"preview,omitempty"`
}
// MessagePreview is a GroupMessages field, only returned in Group JSON API responses.
// Abbreviated form of Message type
type MessagePreview struct {
Nickname string `json:"nickname,omitempty"`
Text string `json:"text,omitempty"`
ImageURL string `json:"image_url,omitempty"`
Attachments []*Attachment `json:"attachments,omitempty"`
}
// GetMemberByUserID gets the group member by their UserID,
// nil if no member matches
func (g *Group) GetMemberByUserID(userID string) *Member {
for _, member := range g.Members {
if member.UserID == userID {
return member
}
}
return nil
}
// GetMemberByNickname gets the group member by their Nickname,
// nil if no member matches
func (g *Group) GetMemberByNickname(nickname string) *Member {
for _, member := range g.Members {
if member.Nickname == nickname {
return member
}
}
return nil
}
func (g *Group) String() string {
return marshal(g)
}
// Member is a GroupMe group member, returned in JSON API responses
type Member struct {
ID string `json:"id,omitempty"`
UserID string `json:"user_id,omitempty"`
Nickname string `json:"nickname,omitempty"`
Muted bool `json:"muted,omitempty"`
ImageURL string `json:"image_url,omitempty"`
AutoKicked bool `json:"autokicked,omitempty"`
AppInstalled bool `json:"app_installed,omitempty"`
GUID string `json:"guid,omitempty"`
PhoneNumber string `json:"phone_number,omitempty"` // Only used when searching for the member to add to a group.
Email string `json:"email,omitempty"` // Only used when searching for the member to add to a group.
}
func (m *Member) String() string {
return marshal(m)
}
// Message is a GroupMe group message, returned in JSON API responses
type Message struct {
ID string `json:"id,omitempty"`
SourceGUID string `json:"source_guid,omitempty"`
CreatedAt Timestamp `json:"created_at,omitempty"`
GroupID string `json:"group_id,omitempty"`
UserID string `json:"user_id,omitempty"`
BotID string `json:"bot_id,omitempty"`
SenderID string `json:"sender_id,omitempty"`
SenderType senderType `json:"sender_type,omitempty"`
System bool `json:"system,omitempty"`
Name string `json:"name,omitempty"`
RecipientID string `json:"recipient_id,omitempty"`
ConversationID string `json:"conversation_id,omitempty"`
AvatarURL string `json:"avatar_url,omitempty"`
// Maximum length of 1000 characters
Text string `json:"text,omitempty"`
// Must be an image service URL (i.groupme.com)
ImageURL string `json:"image_url,omitempty"`
FavoritedBy []string `json:"favorited_by,omitempty"`
Attachments []*Attachment `json:"attachments,omitempty"`
}
func (m *Message) String() string {
return marshal(m)
}
type senderType string
// SenderType constants
const (
SenderTypeUser senderType = "user"
SenderTypeBot senderType = "bot"
SenderTypeSystem senderType = "system"
)
type attachmentType string
// AttachmentType constants
const (
Mentions attachmentType = "mentions"
Image attachmentType = "image"
Location attachmentType = "location"
Emoji attachmentType = "emoji"
)
// Attachment is a GroupMe message attachment, returned in JSON API responses
type Attachment struct {
Type attachmentType `json:"type,omitempty"`
Loci [][]int `json:"loci,omitempty"`
UserIDs []string `json:"user_ids,omitempty"`
URL string `json:"url,omitempty"`
Name string `json:"name,omitempty"`
Latitude string `json:"lat,omitempty"`
Longitude string `json:"lng,omitempty"`
Placeholder string `json:"placeholder,omitempty"`
Charmap [][]int `json:"charmap,omitempty"`
}
func (a *Attachment) String() string {
return marshal(a)
}
// User is a GroupMe user, returned in JSON API responses
type User struct {
ID string `json:"id,omitempty"`
PhoneNumber PhoneNumber `json:"phone_number,omitempty"`
ImageURL string `json:"image_url,omitempty"`
Name string `json:"name,omitempty"`
CreatedAt Timestamp `json:"created_at,omitempty"`
UpdatedAt Timestamp `json:"updated_at,omitempty"`
AvatarURL string `json:"avatar_url,omitempty"`
Email string `json:"email,omitempty"`
SMS bool `json:"sms,omitempty"`
}
func (u *User) String() string {
return marshal(u)
}
// Chat is a GroupMe direct message conversation between two users,
// returned in JSON API responses
type Chat struct {
CreatedAt Timestamp `json:"created_at,omitempty"`
UpdatedAt Timestamp `json:"updated_at,omitempty"`
LastMessage *Message `json:"last_message,omitempty"`
MessagesCount int `json:"messages_count,omitempty"`
OtherUser User `json:"other_user,omitempty"`
}
func (c *Chat) String() string {
return marshal(c)
}
// Bot is a GroupMe bot, it is connected to a specific group which it can send messages to
type Bot struct {
BotID string `json:"bot_id,omitempty"`
GroupID string `json:"group_id,omitempty"`
Name string `json:"name,omitempty"`
AvatarURL string `json:"avatar_url,omitempty"`
CallbackURL string `json:"callback_url,omitempty"`
DMNotification bool `json:"dm_notification,omitempty"`
}
func (b *Bot) String() string {
return marshal(b)
}
// Block is a GroupMe block between two users, direct messages are not allowed
type Block struct {
UserID string `json:"user_id,omitempty"`
BlockedUserID string `json:"blocked_user_id,omitempty"`
CreatedAT Timestamp `json:"created_at,omitempty"`
}
func (b Block) String() string {
return marshal(&b)
}
// Superficially increases test coverage
func marshal(i interface{}) string {
bytes, err := json.MarshalIndent(i, "", "\t")
if err != nil {
return ""
}
return string(bytes)
}