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
|
package services
import (
"fmt"
"regexp"
"shrine/enums"
"shrine/messages"
"shrine/models"
"shrine/repositories"
"shrine/types/hypertext"
"shrine/types/letter"
"shrine/types/ticket"
"shrine/types/user"
"shrine/utils/sanitize"
"strings"
)
func ResolveUser(username string) (*models.User, *hypertext.ServiceError) {
citizen, err := repositories.FindUserByUsername(username)
if err != nil {
return nil, fail(enums.NotFound, messages.UserNotFound)
}
return citizen, nil
}
func mapRegistrationError(err error) *hypertext.ServiceError {
return mapUserError(err)
}
func mapUserError(err error) *hypertext.ServiceError {
if strings.Contains(err.Error(), "users.username") {
return fail(enums.Conflict, messages.UsernameAlreadyExists)
}
if strings.Contains(err.Error(), "users.email") {
return fail(enums.Conflict, messages.EmailAlreadyExists)
}
return fail(enums.BadRequest, err.Error())
}
func assembleLetterResponse(record *models.Letter, viewerID uint) letter.LetterResponse {
participants := repositories.GetLetterParticipants(record.ID)
lastMessage := repositories.GetLastMessage(record.ID)
viewer, _ := repositories.GetParticipantRecord(record.ID, viewerID)
var unread bool
if viewer != nil && lastMessage != nil {
unread = viewer.LastReadAt == nil || lastMessage.CreatedAt.After(*viewer.LastReadAt)
}
var lastMessageResponse *letter.MessageResponse
if lastMessage != nil {
response := lastMessage.ToResponse()
lastMessageResponse = &response
}
return letter.LetterResponse{
Ref: record.Ref,
Title: computeTitle(record, participants, viewerID),
IsSystem: record.IsSystem,
SystemRef: record.SystemRef,
Participants: buildParticipantResponses(participants),
LastMessage: lastMessageResponse,
Unread: unread,
UpdatedAt: record.UpdatedAt,
}
}
func computeTitle(record *models.Letter, participants []models.LetterParticipant, viewerID uint) string {
if record.Title != "" {
return record.Title
}
if record.IsSystem {
return messages.SystemMessageTitle
}
var others []string
for _, participant := range participants {
if participant.UserID != viewerID {
others = append(others, participant.User.DisplayName)
}
}
switch len(others) {
case 0:
return messages.EmptyConversationTitle
case 1:
return others[0]
case 2:
return fmt.Sprintf(messages.LetterTitleTwo, others[0], others[1])
default:
return fmt.Sprintf(messages.LetterTitleMany, others[0], others[1], len(others)-2)
}
}
func buildParticipantResponses(participants []models.LetterParticipant) []letter.ParticipantResponse {
responses := make([]letter.ParticipantResponse, len(participants))
for index, participant := range participants {
responses[index] = participant.ToResponse()
}
return responses
}
func buildMessageResponses(letterMessages []models.LetterMessage) []letter.MessageResponse {
responses := make([]letter.MessageResponse, len(letterMessages))
for index, message := range letterMessages {
responses[index] = message.ToResponse()
}
return responses
}
func resolveLetter(ref string, userID uint) (*models.Letter, *hypertext.ServiceError) {
record, err := repositories.FindLetterByRef(ref)
if err != nil {
return nil, fail(enums.NotFound, messages.LetterNotFound)
}
if !repositories.IsLetterParticipant(record.ID, userID) {
return nil, fail(enums.NotFound, messages.LetterNotFound)
}
return record, nil
}
func resolveTicket(ref string) (*models.Ticket, *hypertext.ServiceError) {
record, err := repositories.FindTicketByRef(ref)
if err != nil {
return nil, fail(enums.NotFound, messages.TicketNotFound)
}
return record, nil
}
func sanitizeRequiredBody(body string) (string, *hypertext.ServiceError) {
sanitized := sanitize.HTML(body)
if sanitized == "" {
return "", fail(enums.BadRequest, messages.MessageBodyRequired)
}
return sanitized, nil
}
func buildTicketMessageResponses(ticketMessages []models.TicketMessage) []ticket.MessageResponse {
responses := make([]ticket.MessageResponse, len(ticketMessages))
for index, message := range ticketMessages {
responses[index] = message.ToResponse()
}
return responses
}
func buildCitizenSummaries(citizens []models.User) []user.CitizenSummaryResponse {
summaries := make([]user.CitizenSummaryResponse, len(citizens))
for index, citizen := range citizens {
summaries[index] = citizen.ToSummary()
}
return summaries
}
var imgSrcPattern = regexp.MustCompile(`<img\s+[^>]*src="([^"]+)"`)
func extractImageURL(html string) string {
match := imgSrcPattern.FindStringSubmatch(html)
if len(match) < 2 {
return ""
}
return match[1]
}
|