Show currently on chat indicators

This commit is contained in:
Noah Petherbridge 2023-09-13 23:13:02 -07:00
parent 5ceeeb5fee
commit fdc6c5c0a7
5 changed files with 55 additions and 0 deletions

View File

@ -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,

View File

@ -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,

View File

@ -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

View File

@ -68,6 +68,13 @@
{{SincePrettyCoarse .User.LastLoginAt}} ago
</span>
</div>
{{if .OnChat}}
<div class="my-1">
<span class="tag is-success is-light">
<i class="fa fa-user mr-2"></i> Currently on chat!
</span>
</div>
{{end}}
{{if .User.Certified}}
<div class="pt-1">
<div class="icon-text" title="This user has been certified via a verification selfie.">

View File

@ -331,6 +331,16 @@
<span class="mr-2">{{.GetProfileField "orientation"}}</span>
{{end}}
<!-- Chat room status -->
{{if $Root.UserOnChatMap.Get .Username}}
<div>
<span class="tag is-success is-light">
<i class="fa fa-user mr-2"></i>
Currently on chat!
</span>
</div>
{{end}}
<!-- Show a subfooter based on ordered by -->
{{if eq $Root.Sort "last_login_at desc"}}
<div>