fix: chat UI
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
@@ -41,11 +42,29 @@ type SoketiWebhookHandler struct {
|
||||
appKey string
|
||||
appSecret string
|
||||
feed *repository.FeedRepo
|
||||
logger *zap.Logger
|
||||
// chat records presence so a message doesn't push to someone who is
|
||||
// already looking at the app. Optional.
|
||||
chat ChatPresence
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
func NewSoketiWebhookHandler(appKey, appSecret string, feed *repository.FeedRepo, logger *zap.Logger) *SoketiWebhookHandler {
|
||||
return &SoketiWebhookHandler{appKey: appKey, appSecret: appSecret, feed: feed, logger: logger}
|
||||
// ChatPresence is the slice of ChatService this handler needs — narrow so the
|
||||
// webhook doesn't depend on the whole chat surface.
|
||||
type ChatPresence interface {
|
||||
MarkPresence(ctx context.Context, userID string, online bool)
|
||||
}
|
||||
|
||||
func (h *SoketiWebhookHandler) presence(ctx context.Context, userID string, online bool) {
|
||||
if h.chat == nil {
|
||||
return
|
||||
}
|
||||
h.chat.MarkPresence(ctx, userID, online)
|
||||
}
|
||||
|
||||
func NewSoketiWebhookHandler(appKey, appSecret string, feed *repository.FeedRepo, chat ChatPresence, logger *zap.Logger) *SoketiWebhookHandler {
|
||||
return &SoketiWebhookHandler{
|
||||
appKey: appKey, appSecret: appSecret, feed: feed, chat: chat, logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
type soketiWebhookBody struct {
|
||||
@@ -91,12 +110,20 @@ func (h *SoketiWebhookHandler) Handle(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
cleared := 0
|
||||
for _, e := range payload.Events {
|
||||
// A per-user notifications channel emptying = that user is offline.
|
||||
if e.Name != "channel_vacated" {
|
||||
uid, ok := userIDFromNotifChannel(e.Channel)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if uid, ok := userIDFromNotifChannel(e.Channel); ok {
|
||||
switch e.Name {
|
||||
case "channel_occupied":
|
||||
// This user has a live connection, so chat push is suppressed
|
||||
// for them — they will see the message in the app.
|
||||
h.presence(ctx, uid, true)
|
||||
case "channel_vacated":
|
||||
// Their last tab closed: they are offline. Evict the feed
|
||||
// seen-set and let chat push resume.
|
||||
h.feed.Reset(ctx, uid)
|
||||
h.presence(ctx, uid, false)
|
||||
cleared++
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user