-
Notifications
You must be signed in to change notification settings - Fork 1
/
matchedProfiles.go
178 lines (167 loc) · 6.91 KB
/
matchedProfiles.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
package chat
import (
"log"
"net/http"
"sort"
"strings"
"time"
"../../../../lib"
"github.com/jmoiron/sqlx"
)
type dateSorter []outputMatchedProfiles
func (a dateSorter) Len() int { return len(a) }
func (a dateSorter) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a dateSorter) Less(i, j int) bool {
return a[i].LastMessageDate.After(a[j].LastMessageDate)
}
type unreadMessageSorter []outputMatchedProfiles
func (a unreadMessageSorter) Len() int { return len(a) }
func (a unreadMessageSorter) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a unreadMessageSorter) Less(i, j int) bool {
return a[i].TotalUnreadMessages > a[j].TotalUnreadMessages
}
func getMatchesIDs(db *sqlx.DB, userID string) ([]string, int, string) {
var matchesIDs []string
request := `
Select
CASE when Part1 <> $1::text then Part1 else Part2 END as MatchesID
from (
Select Split_part(concat, ',', 1) as Part1,
Split_part(concat, ',', 2) as Part2
From (
Select concat from (
Select userID, liked_userID,
CASE when userID < liked_userID then CONCAT(userID, ',', liked_userID) else CONCAT(liked_userID, ',', userID) END as Concat
From Likes Where userID = $1 OR liked_userID = $1
) list
Group by list.concat having count(list.concat) > 1
) s
) matches`
err := db.Select(&matchesIDs, request, userID)
if err != nil {
log.Println(lib.PrettyError("[DB REQUEST - SELECT] Failed to collect matches ids in database " + err.Error()))
return []string{}, 500, "Failed to gather matches data in the database"
}
return matchesIDs, 0, ""
}
type matchedProfiles struct {
Username string `db:"username" json:"username"`
Firstname string `db:"firstname" json:"firstname"`
Lastname string `db:"lastname" json:"lastname"`
PictureURL string `db:"picture_url" json:"picture_url"`
LastMessageContent string `db:"content" json:"last_message_content"`
LastMessageDate time.Time `db:"created_at" json:"last_message_date"`
IsRead bool `db:"is_read"`
Online bool `db:"online"`
TotalUnreadMessages int `db:"total_unread_messages"`
}
func getMessages(db *sqlx.DB, userID string, matchesIDs []string) (map[string]matchedProfiles, int, string) {
var listMsg []matchedProfiles
request := `Select username, firstname, lastname, picture_url_1 as picture_url, online from Users Where id IN (` + strings.Join(matchesIDs, ", ") + `)`
err := db.Select(&listMsg, request)
if err != nil {
log.Println(lib.PrettyError("[DB REQUEST - SELECT] Failed to collect matches messages in database " + err.Error()))
return map[string]matchedProfiles{}, 500, "Failed to gather matches messages data in the database"
}
listMatches := make(map[string]matchedProfiles)
for _, elem := range listMsg {
listMatches[elem.Username] = elem
}
listMsg = []matchedProfiles{}
request = `Select m.content, m.created_at, m.is_read, u.username
From Messages m
Left Join Users u On m.senderid = u.id
Where senderId IN (` + strings.Join(matchesIDs, ", ") + `) and receiverid = $1
Order By created_at`
err = db.Select(&listMsg, request, userID)
if err != nil {
log.Println(lib.PrettyError("[DB REQUEST - SELECT] Failed to collect matches messages in database " + err.Error()))
return map[string]matchedProfiles{}, 500, "Failed to gather matches messages data in the database"
}
var unreadMessage int
var lastMessageContent string
var lastMessageDate time.Time
for _, elem := range listMsg {
if elem.IsRead == false {
unreadMessage = listMatches[elem.Username].TotalUnreadMessages + 1
} else {
unreadMessage = listMatches[elem.Username].TotalUnreadMessages
}
if listMatches[elem.Username].LastMessageDate == (time.Time{}) ||
elem.LastMessageDate.After(listMatches[elem.Username].LastMessageDate) {
lastMessageContent = elem.LastMessageContent
lastMessageDate = elem.LastMessageDate
} else {
lastMessageContent = listMatches[elem.Username].LastMessageContent
lastMessageDate = listMatches[elem.Username].LastMessageDate
}
listMatches[elem.Username] = matchedProfiles{
Username: listMatches[elem.Username].Username,
Firstname: listMatches[elem.Username].Firstname,
Lastname: listMatches[elem.Username].Lastname,
PictureURL: listMatches[elem.Username].PictureURL,
LastMessageContent: lastMessageContent,
LastMessageDate: lastMessageDate,
Online: listMatches[elem.Username].Online,
TotalUnreadMessages: unreadMessage,
}
}
return listMatches, 0, ""
}
type outputMatchedProfiles struct {
Username string `db:"username" json:"username"`
Firstname string `db:"firstname" json:"firstname"`
Lastname string `db:"lastname" json:"lastname"`
PictureURL string `db:"picture_url" json:"picture_url"`
LastMessageContent string `db:"content" json:"last_message_content"`
LastMessageDate time.Time `db:"created_at" json:"last_message_date"`
Online bool `db:"online" json:"online"`
TotalUnreadMessages int `db:"total_unread_messages" json:"total_unread_messages"`
}
// GetMatchedProfiles is the route '/v1/chat/matches' with the method GET.
// Collect the user's matchesIDs in the database
// If matchesIDs is empty
// -> Return an error - HTTP Code 200 OK - JSON Content "data: No matches"
// Get in the database for each id, the username, firstname, lastname, picture url
// online status, the last message (content/date) and total unread messages
// Everything is stored in a structure, sorted by last message date and unread message count
// Return HTTP Code 200 Status OK - JSON Content Structure
func GetMatchedProfiles(w http.ResponseWriter, r *http.Request) {
db, _, userID, errCode, errContent, ok := lib.GetBasics(r, []string{"GET"})
if !ok {
lib.RespondWithErrorHTTP(w, errCode, errContent)
return
}
matchesIDs, errCode, errContent := getMatchesIDs(db, userID)
if errCode != 0 || errContent != "" {
lib.RespondWithErrorHTTP(w, errCode, errContent)
return
}
if matchesIDs == nil {
lib.RespondWithJSON(w, http.StatusOK, map[string]interface{}{
"data": "No matches",
})
return
}
listMatches, errCode, errContent := getMessages(db, userID, matchesIDs)
if errCode != 0 || errContent != "" {
lib.RespondWithErrorHTTP(w, errCode, errContent)
return
}
var matches []outputMatchedProfiles
for _, profile := range listMatches {
matches = append(matches, outputMatchedProfiles{
Username: profile.Username,
Firstname: profile.Firstname,
Lastname: profile.Lastname,
PictureURL: profile.PictureURL,
LastMessageContent: profile.LastMessageContent,
LastMessageDate: profile.LastMessageDate,
Online: profile.Online,
TotalUnreadMessages: profile.TotalUnreadMessages,
})
}
sort.Sort(dateSorter(matches))
sort.Sort(unreadMessageSorter(matches))
lib.RespondWithJSON(w, http.StatusOK, matches)
}