diff --git a/pkg/controller/account/profile.go b/pkg/controller/account/profile.go index 8066652..504adfe 100644 --- a/pkg/controller/account/profile.go +++ b/pkg/controller/account/profile.go @@ -10,6 +10,7 @@ import ( "code.nonshy.com/nonshy/website/pkg/models" "code.nonshy.com/nonshy/website/pkg/session" "code.nonshy.com/nonshy/website/pkg/templates" + "code.nonshy.com/nonshy/website/pkg/worker" ) var ProfileRegexp = regexp.MustCompile(`^/u/([^@]+?)$`) @@ -118,6 +119,7 @@ func Profile() http.HandlerFunc { "IsFriend": isFriend, "IsPrivate": isPrivate, "PhotoCount": models.CountPhotosICanSee(user, currentUser), + "OnChat": worker.GetChatStatistics().IsOnline(user.Username), // Details on who likes the photo. "LikeExample": likeExample, diff --git a/pkg/controller/account/search.go b/pkg/controller/account/search.go index 5b20db2..a9c26cb 100644 --- a/pkg/controller/account/search.go +++ b/pkg/controller/account/search.go @@ -10,6 +10,7 @@ import ( "code.nonshy.com/nonshy/website/pkg/models" "code.nonshy.com/nonshy/website/pkg/session" "code.nonshy.com/nonshy/website/pkg/templates" + "code.nonshy.com/nonshy/website/pkg/worker" ) // Search controller. @@ -106,6 +107,12 @@ func Search() http.HandlerFunc { // Who's Nearby feature, get some data. insights, _ := geoip.GetRequestInsights(r) + // Collect usernames to map to chat online status. + var usernames = []string{} + for _, user := range users { + usernames = append(usernames, user.Username) + } + var vars = map[string]interface{}{ "Users": users, "Pager": pager, @@ -132,6 +139,9 @@ func Search() http.HandlerFunc { // Map friendships to these users. "FriendMap": models.MapFriends(currentUser, users), + // Users on the chat room map. + "UserOnChatMap": worker.GetChatStatistics().MapUsersOnline(usernames), + // Current user's location setting. "MyLocation": myLocation, "GeoIPInsights": insights, diff --git a/pkg/worker/barertc.go b/pkg/worker/barertc.go index 04cb672..b165502 100644 --- a/pkg/worker/barertc.go +++ b/pkg/worker/barertc.go @@ -41,6 +41,32 @@ func SetChatStatistics(stats *ChatStatistics) { cachedChatStatistics = stats } +// IsOnline returns whether the username is currently logged-in to chat. +func (cs ChatStatistics) IsOnline(username string) bool { + for _, user := range cs.Usernames { + if user == username { + return true + } + } + return false +} + +type UserOnChatMap map[string]bool + +// MapUsersOnline returns a hashmap of usernames to online status. +func (cs ChatStatistics) MapUsersOnline(usernames []string) UserOnChatMap { + var result = UserOnChatMap{} + for _, user := range cs.Usernames { + result[user] = true + } + return result +} + +// Get a result from the UserOnChatMap. +func (m UserOnChatMap) Get(username string) bool { + return m[username] +} + var ( cachedChatStatistics *ChatStatistics chatStatisticsMu sync.RWMutex diff --git a/web/templates/account/profile.html b/web/templates/account/profile.html index bb4414c..152ae03 100644 --- a/web/templates/account/profile.html +++ b/web/templates/account/profile.html @@ -68,6 +68,13 @@ {{SincePrettyCoarse .User.LastLoginAt}} ago + {{if .OnChat}} +
+ + Currently on chat! + +
+ {{end}} {{if .User.Certified}}
diff --git a/web/templates/account/search.html b/web/templates/account/search.html index a69f01e..2ea8c97 100644 --- a/web/templates/account/search.html +++ b/web/templates/account/search.html @@ -331,6 +331,16 @@ {{.GetProfileField "orientation"}} {{end}} + + {{if $Root.UserOnChatMap.Get .Username}} +
+ + + Currently on chat! + +
+ {{end}} + {{if eq $Root.Sort "last_login_at desc"}}