36 lines
882 B
Go
36 lines
882 B
Go
package v1
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
)
|
|
|
|
type contactPresenceReader interface {
|
|
GetContactStatus(ctx context.Context, contactID, accountID uint) (string, error)
|
|
}
|
|
|
|
type contactPresenceContextKey struct{}
|
|
|
|
func withContactPresence(ctx context.Context, presence contactPresenceReader) context.Context {
|
|
if presence == nil {
|
|
return ctx
|
|
}
|
|
return context.WithValue(ctx, contactPresenceContextKey{}, presence)
|
|
}
|
|
|
|
func contactAvailabilityStatus(ctx context.Context, contact *model.Contact) string {
|
|
if ctx == nil || contact == nil {
|
|
return "offline"
|
|
}
|
|
presence, ok := ctx.Value(contactPresenceContextKey{}).(contactPresenceReader)
|
|
if !ok || presence == nil {
|
|
return "offline"
|
|
}
|
|
status, err := presence.GetContactStatus(ctx, contact.ID, contact.AccountID)
|
|
if err != nil || status != "online" {
|
|
return "offline"
|
|
}
|
|
return "online"
|
|
}
|