From a16894a1b1274c7b1630630864a7e9127ea75e16 Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Sun, 8 Oct 2023 13:35:11 -0700 Subject: [PATCH] Chat landing page: show online friends --- pkg/controller/chat/chat.go | 9 +++++++- pkg/models/friend.go | 33 +++++++++++++++++++++++++++ pkg/models/user.go | 45 +++++++++++++++++++++++++++++++++++++ web/templates/chat.html | 15 ++++++++++++- 4 files changed, 100 insertions(+), 2 deletions(-) diff --git a/pkg/controller/chat/chat.go b/pkg/controller/chat/chat.go index a352bf6..b4231e7 100644 --- a/pkg/controller/chat/chat.go +++ b/pkg/controller/chat/chat.go @@ -150,12 +150,19 @@ func Landing() http.HandlerFunc { return } + // Get the ChatStatistics and select our friend names from it. + var ( + stats = FilteredChatStatistics(currentUser) + friendsOnline = models.FilterFriendUsernames(currentUser, stats.Usernames) + ) + var vars = map[string]interface{}{ "ChatAPI": strings.TrimSuffix(config.Current.BareRTC.URL, "/") + "/api/statistics", "IsShyUser": isShy, // Pre-populate the "who's online" widget from backend cache data - "ChatStatistics": FilteredChatStatistics(currentUser), + "ChatStatistics": stats, + "FriendsOnline": friendsOnline, } if err := tmpl.Execute(w, r, vars); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) diff --git a/pkg/models/friend.go b/pkg/models/friend.go index 6f49ea8..d0ae1fa 100644 --- a/pkg/models/friend.go +++ b/pkg/models/friend.go @@ -127,6 +127,39 @@ func FilterFriendIDs(userIDs []uint64, friendIDs []uint64) []uint64 { return filtered } +// FilterFriendUsernames takes a list of usernames and returns only the ones who are your friends. +func FilterFriendUsernames(currentUser *User, usernames []string) []string { + var ( + fs = []*Friend{} + userIDs = []uint64{} + userMap = map[uint64]string{} + result = []string{} + ) + + // Map usernames to user IDs. + users, err := GetUsersByUsernames(currentUser, usernames) + if err != nil { + log.Error("FilterFriendUsernames: GetUsersByUsernames: %s", err) + return result + } + + for _, user := range users { + userIDs = append(userIDs, user.ID) + userMap[user.ID] = user.Username + } + + if len(userIDs) == 0 { + return result + } + + DB.Where("source_user_id = ? AND approved = ? AND target_user_id IN ?", currentUser.ID, true, userIDs).Find(&fs) + for _, row := range fs { + result = append(result, userMap[row.TargetUserID]) + } + + return result +} + // FriendIDsAreExplicit returns friend IDs who have opted-in for Explicit content, // e.g. to notify only them when you uploaded a new Explicit photo so that non-explicit // users don't need to see that notification. diff --git a/pkg/models/user.go b/pkg/models/user.go index f14fa41..c1853c1 100644 --- a/pkg/models/user.go +++ b/pkg/models/user.go @@ -124,6 +124,51 @@ func GetUsers(currentUser *User, userIDs []uint64) ([]*User, error) { return users, nil } +// GetUsersByUsernames queries for multiple usernames and returns users in the same order. +func GetUsersByUsernames(currentUser *User, usernames []string) ([]*User, error) { + // Map the usernames. + var ( + usermap = map[string]*User{} + set = map[string]interface{}{} + distinct = []string{} + ) + + // Uniqueify usernames. + for _, name := range usernames { + if _, ok := set[name]; ok { + continue + } + set[name] = nil + distinct = append(distinct, name) + } + + var ( + users = []*User{} + result = (&User{}).Preload().Where("username IN ?", distinct).Find(&users) + ) + if result.Error != nil { + return nil, result.Error + } + + // Map users. + for _, user := range users { + usermap[user.Username] = user + } + + // Inject relationships. + SetUserRelationships(currentUser, users) + + // Re-order them per the original sequence. + var ordered = []*User{} + for _, name := range usernames { + if user, ok := usermap[name]; ok { + ordered = append(ordered, user) + } + } + + return ordered, nil +} + // FindUser by username or email. func FindUser(username string) (*User, error) { if username == "" { diff --git a/web/templates/chat.html b/web/templates/chat.html index 42c0e7f..7b50132 100644 --- a/web/templates/chat.html +++ b/web/templates/chat.html @@ -47,9 +47,22 @@ + + {{if .FriendsOnline}} +
+ + {{len .FriendsOnline}} friend{{Pluralize (len .FriendsOnline)}} + {{Pluralize (len .FriendsOnline) "is" "are"}} currently online: + {{$FriendsOnline := .FriendsOnline}} + {{range $i, $name := .FriendsOnline -}} + {{- if $i -}}, {{end}}{{ $name }} + {{- end}} +
+ {{end}} +