2023-02-06 04:26:36 +00:00
|
|
|
package chat
|
|
|
|
|
|
|
|
import (
|
2023-07-30 17:33:04 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
2023-02-06 04:26:36 +00:00
|
|
|
"net/http"
|
2023-09-16 20:46:26 +00:00
|
|
|
"sort"
|
2023-02-06 04:26:36 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/config"
|
2024-10-04 04:23:12 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/encryption"
|
2023-08-06 02:37:15 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/geoip"
|
2023-07-30 17:33:04 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/log"
|
2023-08-17 05:09:04 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/middleware"
|
2023-02-13 06:23:51 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/models"
|
2023-02-06 04:26:36 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/photo"
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/session"
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/templates"
|
2023-06-08 04:59:15 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/worker"
|
2023-02-06 04:26:36 +00:00
|
|
|
"github.com/golang-jwt/jwt/v4"
|
|
|
|
)
|
|
|
|
|
2024-10-04 04:23:12 +00:00
|
|
|
// Claims are the JWT claims for the BareRTC chat room.
|
2023-02-06 04:26:36 +00:00
|
|
|
type Claims struct {
|
|
|
|
// Custom claims.
|
2024-09-20 02:30:02 +00:00
|
|
|
IsAdmin bool `json:"op,omitempty"`
|
|
|
|
VIP bool `json:"vip,omitempty"`
|
|
|
|
Avatar string `json:"img,omitempty"`
|
|
|
|
ProfileURL string `json:"url,omitempty"`
|
|
|
|
Nickname string `json:"nick,omitempty"`
|
|
|
|
Emoji string `json:"emoji,omitempty"`
|
|
|
|
Gender string `json:"gender,omitempty"`
|
|
|
|
Rules []string `json:"rules,omitempty"`
|
2023-02-06 04:26:36 +00:00
|
|
|
|
|
|
|
// Standard claims. Notes:
|
|
|
|
// subject = username
|
|
|
|
jwt.RegisteredClaims
|
|
|
|
}
|
|
|
|
|
2023-08-06 02:37:15 +00:00
|
|
|
// Gender returns the BareRTC gender string for the user's gender selection.
|
|
|
|
func Gender(u *models.User) string {
|
|
|
|
switch u.GetProfileField("gender") {
|
|
|
|
case "Man", "Trans (FTM)":
|
|
|
|
return "m"
|
|
|
|
case "Woman", "Trans (MTF)":
|
|
|
|
return "f"
|
|
|
|
case "Non-binary", "Trans", "Other":
|
|
|
|
return "o"
|
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-06 04:26:36 +00:00
|
|
|
// Landing page for chat rooms.
|
|
|
|
func Landing() http.HandlerFunc {
|
|
|
|
tmpl := templates.Must("chat.html")
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Get the current user.
|
|
|
|
currentUser, err := session.CurrentUser(r)
|
|
|
|
if err != nil {
|
|
|
|
session.FlashError(w, r, "Couldn't get current user: %s", err)
|
|
|
|
templates.Redirect(w, "/")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Are they logging into the chat room?
|
2023-02-14 06:19:18 +00:00
|
|
|
var (
|
2023-10-11 02:30:39 +00:00
|
|
|
intent = r.FormValue("intent")
|
|
|
|
isShy = currentUser.IsShy()
|
|
|
|
birthday = r.FormValue("birthday") == "true" && currentUser.IsBirthday()
|
2023-02-14 06:19:18 +00:00
|
|
|
)
|
2023-02-06 04:26:36 +00:00
|
|
|
if intent == "join" {
|
2023-08-17 05:09:04 +00:00
|
|
|
// Maintenance mode?
|
|
|
|
if middleware.ChatMaintenance(currentUser, w, r) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-06 04:26:36 +00:00
|
|
|
// Get our Chat JWT secret.
|
|
|
|
var (
|
|
|
|
secret = []byte(config.Current.BareRTC.JWTSecret)
|
|
|
|
chatURL = config.Current.BareRTC.URL
|
|
|
|
)
|
|
|
|
if len(secret) == 0 || chatURL == "" {
|
|
|
|
session.FlashError(w, r, "Couldn't sign you into the chat: JWT secret key or chat URL not configured!")
|
|
|
|
templates.Redirect(w, r.URL.Path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-13 06:23:51 +00:00
|
|
|
// Avatar URL - masked if non-public.
|
2024-10-04 05:08:19 +00:00
|
|
|
avatar := photo.SignedPublicAvatarURL(currentUser.ProfilePhoto.CroppedFilename)
|
2024-06-30 04:42:35 +00:00
|
|
|
switch currentUser.ProfilePhoto.Visibility {
|
|
|
|
case models.PhotoPrivate:
|
|
|
|
avatar = "/static/img/shy-private.png"
|
|
|
|
case models.PhotoFriends:
|
|
|
|
avatar = "/static/img/shy-friends.png"
|
2023-02-13 06:23:51 +00:00
|
|
|
}
|
|
|
|
|
2023-08-06 02:37:15 +00:00
|
|
|
// Country flag emoji.
|
2023-08-16 03:56:22 +00:00
|
|
|
emoji, err := geoip.GetChatFlagEmoji(r)
|
2023-08-06 02:37:15 +00:00
|
|
|
if err != nil {
|
|
|
|
emoji, err = geoip.CountryFlagEmojiWithCode("US")
|
|
|
|
if err != nil {
|
|
|
|
emoji = "🏴☠️"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-11 02:30:39 +00:00
|
|
|
// Birthday cake emoji?
|
|
|
|
if birthday {
|
|
|
|
emoji = "🍰 It's my birthday!"
|
|
|
|
}
|
|
|
|
|
2024-09-20 02:30:02 +00:00
|
|
|
// Apply chat moderation rules.
|
|
|
|
var rules = []string{}
|
|
|
|
if isShy {
|
|
|
|
// Shy account: no camera privileges.
|
|
|
|
rules = []string{"novideo", "noimage"}
|
|
|
|
} else if v := currentUser.GetProfileField("chat_moderation_rules"); len(v) > 0 {
|
|
|
|
// Specific mod rules applied to the current user.
|
|
|
|
rules = strings.Split(v, ",")
|
|
|
|
}
|
|
|
|
|
2023-02-06 04:26:36 +00:00
|
|
|
// Create the JWT claims.
|
|
|
|
claims := Claims{
|
2024-10-04 04:23:12 +00:00
|
|
|
IsAdmin: currentUser.HasAdminScope(config.ScopeChatModerator),
|
|
|
|
Avatar: avatar,
|
|
|
|
ProfileURL: "/u/" + currentUser.Username,
|
|
|
|
Nickname: currentUser.NameOrUsername(),
|
|
|
|
Emoji: emoji,
|
|
|
|
Gender: Gender(currentUser),
|
|
|
|
VIP: isShy, // "shy accounts" use the "VIP" status for special icon in chat
|
|
|
|
Rules: rules,
|
|
|
|
RegisteredClaims: encryption.StandardClaims(currentUser.ID, currentUser.Username, 5*time.Minute),
|
2023-02-06 04:26:36 +00:00
|
|
|
}
|
2024-10-04 04:23:12 +00:00
|
|
|
token, err := encryption.SignClaims(claims, []byte(config.Current.BareRTC.JWTSecret))
|
2023-02-06 04:26:36 +00:00
|
|
|
if err != nil {
|
|
|
|
session.FlashError(w, r, "Couldn't sign you into the chat: %s", err)
|
|
|
|
templates.Redirect(w, r.URL.Path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-30 17:33:04 +00:00
|
|
|
// Send over their blocklist to the chat server.
|
|
|
|
if err := SendBlocklist(currentUser); err != nil {
|
|
|
|
log.Error("SendBlocklist: %s", err)
|
|
|
|
}
|
|
|
|
|
2024-01-27 21:57:24 +00:00
|
|
|
// Mark them as online immediately: so e.g. on the Change Username screen we leave no window
|
|
|
|
// of time where they can exist in chat but change their name on the site.
|
|
|
|
worker.GetChatStatistics().SetOnlineNow(currentUser.Username)
|
|
|
|
|
2024-09-12 02:28:52 +00:00
|
|
|
// Ping their chat login usage statistic.
|
|
|
|
go func() {
|
|
|
|
if err := models.LogDailyChatUser(currentUser); err != nil {
|
|
|
|
log.Error("LogDailyChatUser(%s): error logging this user's chat statistic: %s", currentUser.Username, err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-02-06 04:26:36 +00:00
|
|
|
// Redirect them to the chat room.
|
2024-10-04 04:23:12 +00:00
|
|
|
templates.Redirect(w, strings.TrimSuffix(chatURL, "/")+"/?jwt="+token)
|
2023-02-06 04:26:36 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-08 20:35:11 +00:00
|
|
|
// Get the ChatStatistics and select our friend names from it.
|
|
|
|
var (
|
|
|
|
stats = FilteredChatStatistics(currentUser)
|
|
|
|
friendsOnline = models.FilterFriendUsernames(currentUser, stats.Usernames)
|
|
|
|
)
|
|
|
|
|
2023-10-08 20:36:59 +00:00
|
|
|
sort.Strings(friendsOnline)
|
|
|
|
|
2023-02-10 07:07:07 +00:00
|
|
|
var vars = map[string]interface{}{
|
2023-02-14 06:19:18 +00:00
|
|
|
"ChatAPI": strings.TrimSuffix(config.Current.BareRTC.URL, "/") + "/api/statistics",
|
|
|
|
"IsShyUser": isShy,
|
2023-06-08 04:59:15 +00:00
|
|
|
|
|
|
|
// Pre-populate the "who's online" widget from backend cache data
|
2023-10-08 20:35:11 +00:00
|
|
|
"ChatStatistics": stats,
|
|
|
|
"FriendsOnline": friendsOnline,
|
2023-02-10 07:07:07 +00:00
|
|
|
}
|
2023-02-06 04:26:36 +00:00
|
|
|
if err := tmpl.Execute(w, r, vars); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2023-07-30 17:33:04 +00:00
|
|
|
|
2023-09-16 20:46:26 +00:00
|
|
|
// FilteredChatStatistics will return a copy of the cached ChatStatistics but where the Usernames list is
|
|
|
|
// filtered down (and the online user counts, accordingly) by blocklists.
|
|
|
|
func FilteredChatStatistics(currentUser *models.User) worker.ChatStatistics {
|
|
|
|
var stats = worker.GetChatStatistics()
|
|
|
|
var result = worker.ChatStatistics{
|
|
|
|
UserCount: stats.UserCount,
|
|
|
|
Usernames: []string{},
|
|
|
|
Cameras: stats.Cameras,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Who are we blocking?
|
|
|
|
var blockedUsernames = map[string]interface{}{}
|
2023-10-22 22:02:24 +00:00
|
|
|
for _, username := range models.BlockedUsernames(currentUser) {
|
2023-09-16 20:46:26 +00:00
|
|
|
blockedUsernames[username] = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter the online users listing.
|
|
|
|
for _, username := range stats.Usernames {
|
|
|
|
if _, ok := blockedUsernames[username]; !ok {
|
|
|
|
result.Usernames = append(result.Usernames, username)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort the names.
|
|
|
|
sort.Strings(result.Usernames)
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2023-07-30 17:33:04 +00:00
|
|
|
// SendBlocklist syncs the user blocklist to the chat server prior to sending them over.
|
|
|
|
func SendBlocklist(user *models.User) error {
|
|
|
|
// Get the user's blocklist.
|
2023-10-22 22:02:24 +00:00
|
|
|
blockedUsernames := models.BlockedUsernames(user)
|
2023-07-30 17:33:04 +00:00
|
|
|
log.Info("SendBlocklist(%s) to BareRTC: %d blocked usernames", user.Username, len(blockedUsernames))
|
|
|
|
|
|
|
|
// API request struct for BareRTC /api/blocklist endpoint.
|
|
|
|
var request = struct {
|
|
|
|
APIKey string
|
|
|
|
Username string
|
|
|
|
Blocklist []string
|
|
|
|
}{
|
|
|
|
config.Current.CronAPIKey,
|
|
|
|
user.Username,
|
|
|
|
blockedUsernames,
|
|
|
|
}
|
|
|
|
|
|
|
|
// JSON request body.
|
|
|
|
jsonStr, err := json.Marshal(request)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the API request to BareRTC.
|
2023-07-30 17:56:14 +00:00
|
|
|
var url = strings.TrimSuffix(config.Current.BareRTC.URL, "/") + "/api/blocklist"
|
2023-07-30 17:33:04 +00:00
|
|
|
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
Timeout: 10 * time.Second,
|
|
|
|
}
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
body, _ := io.ReadAll(resp.Body)
|
|
|
|
log.Error("SendBlocklist: error syncing blocklist to BareRTC: status %d body %s", resp.StatusCode, body)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-09-30 22:24:14 +00:00
|
|
|
|
|
|
|
// BlockUserNow syncs the new block action to the chat server now, in case the user is already online.
|
|
|
|
func BlockUserNow(currentUser, user *models.User) error {
|
|
|
|
// API request struct for BareRTC /api/block/now endpoint.
|
|
|
|
var request = struct {
|
|
|
|
APIKey string
|
|
|
|
Usernames []string
|
|
|
|
}{
|
|
|
|
config.Current.CronAPIKey,
|
|
|
|
[]string{
|
|
|
|
currentUser.Username,
|
|
|
|
user.Username,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// JSON request body.
|
|
|
|
jsonStr, err := json.Marshal(request)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the API request to BareRTC.
|
|
|
|
var url = strings.TrimSuffix(config.Current.BareRTC.URL, "/") + "/api/block/now"
|
|
|
|
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
Timeout: 10 * time.Second,
|
|
|
|
}
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
body, _ := io.ReadAll(resp.Body)
|
|
|
|
log.Error("BlockUserNow: error syncing block to BareRTC: status %d body %s", resp.StatusCode, body)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|