Skip to content

Commit

Permalink
Send notification when user is banned
Browse files Browse the repository at this point in the history
  • Loading branch information
sesposito committed Mar 21, 2023
1 parent 3954113 commit fd1476c
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 13 deletions.
2 changes: 1 addition & 1 deletion server/core_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ func DeleteAccount(ctx context.Context, logger *zap.Logger, db *sql.DB, config C
return err
}
for _, presence := range tracker.ListPresenceIDByStream(PresenceStream{Mode: StreamModeNotifications, Subject: userID}) {
if err = sessionRegistry.Disconnect(ctx, presence.SessionID); err != nil {
if err = sessionRegistry.Disconnect(ctx, presence.SessionID, false); err != nil {
return err
}
}
Expand Down
1 change: 1 addition & 0 deletions server/core_notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const (
NotificationCodeGroupJoinRequest int32 = -5
NotificationCodeFriendJoinGame int32 = -6
NotificationCodeSingleSocket int32 = -7
NotificationCodeUserBanned int32 = -8
)

type notificationCacheableCursor struct {
Expand Down
9 changes: 4 additions & 5 deletions server/core_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,12 @@ func BanUsers(ctx context.Context, logger *zap.Logger, db *sql.DB, config Config
return err
}

sessionCache.Ban(ids)

for _, id := range ids {
// Logout and disconnect.
if err = SessionLogout(config, sessionCache, id, "", ""); err != nil {
return err
}
// Disconnect.
for _, presence := range tracker.ListPresenceIDByStream(PresenceStream{Mode: StreamModeNotifications, Subject: id}) {
if err = sessionRegistry.Disconnect(ctx, presence.SessionID); err != nil {
if err = sessionRegistry.Disconnect(ctx, presence.SessionID, true); err != nil {
return err
}
}
Expand Down
2 changes: 1 addition & 1 deletion server/match_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func NewMatchHandler(logger *zap.Logger, config Config, sessionRegistry SessionR
func (mh *MatchHandler) disconnectClients() {
presences := mh.PresenceList.ListPresences()
for _, presence := range presences {
_ = mh.sessionRegistry.Disconnect(context.Background(), presence.SessionID)
_ = mh.sessionRegistry.Disconnect(context.Background(), presence.SessionID, false)
}
}

Expand Down
2 changes: 1 addition & 1 deletion server/runtime_go_nakama.go
Original file line number Diff line number Diff line change
Expand Up @@ -1448,7 +1448,7 @@ func (n *RuntimeGoNakamaModule) SessionDisconnect(ctx context.Context, sessionID
return errors.New("expects valid session id")
}

return n.sessionRegistry.Disconnect(ctx, sid, reason...)
return n.sessionRegistry.Disconnect(ctx, sid, false, reason...)
}

// @group sessions
Expand Down
2 changes: 1 addition & 1 deletion server/runtime_javascript_nakama.go
Original file line number Diff line number Diff line change
Expand Up @@ -3324,7 +3324,7 @@ func (n *runtimeJavascriptNakamaModule) sessionDisconnect(r *goja.Runtime) func(
reason = append(reason, runtime.PresenceReason(reasonInt))
}

if err := n.sessionRegistry.Disconnect(n.ctx, sessionID, reason...); err != nil {
if err := n.sessionRegistry.Disconnect(n.ctx, sessionID, false, reason...); err != nil {
panic(r.NewGoError(fmt.Errorf("failed to disconnect: %s", err.Error())))
}

Expand Down
2 changes: 1 addition & 1 deletion server/runtime_lua_nakama.go
Original file line number Diff line number Diff line change
Expand Up @@ -4537,7 +4537,7 @@ func (n *RuntimeLuaNakamaModule) sessionDisconnect(l *lua.LState) int {
reason = append(reason, runtime.PresenceReason(reasonInt))
}

if err := n.sessionRegistry.Disconnect(l.Context(), sessionID, reason...); err != nil {
if err := n.sessionRegistry.Disconnect(l.Context(), sessionID, false, reason...); err != nil {
l.RaiseError(fmt.Sprintf("failed to disconnect: %s", err.Error()))
}
return 0
Expand Down
26 changes: 23 additions & 3 deletions server/session_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type SessionRegistry interface {
Get(sessionID uuid.UUID) Session
Add(session Session)
Remove(sessionID uuid.UUID)
Disconnect(ctx context.Context, sessionID uuid.UUID, reason ...runtime.PresenceReason) error
Disconnect(ctx context.Context, sessionID uuid.UUID, ban bool, reason ...runtime.PresenceReason) error
SingleSession(ctx context.Context, tracker Tracker, userID, sessionID uuid.UUID)
}

Expand Down Expand Up @@ -110,15 +110,35 @@ func (r *LocalSessionRegistry) Remove(sessionID uuid.UUID) {
r.metrics.GaugeSessions(float64(count))
}

func (r *LocalSessionRegistry) Disconnect(ctx context.Context, sessionID uuid.UUID, reason ...runtime.PresenceReason) error {
func (r *LocalSessionRegistry) Disconnect(ctx context.Context, sessionID uuid.UUID, ban bool, reason ...runtime.PresenceReason) error {
session, ok := r.sessions.Load(sessionID)
if ok {
// No need to remove the session from the map, session.Close() will do that.
reasonOverride := runtime.PresenceReasonDisconnect
if len(reason) > 0 {
reasonOverride = reason[0]
}
session.Close("server-side session disconnect", reasonOverride)

if ban {
session.Close("server-side session disconnect", runtime.PresenceReasonDisconnect,
&rtapi.Envelope{Message: &rtapi.Envelope_Notifications{
Notifications: &rtapi.Notifications{
Notifications: []*api.Notification{
{
Id: uuid.Must(uuid.NewV4()).String(),
Subject: "banned",
Content: "{}",
Code: NotificationCodeUserBanned,
SenderId: "",
CreateTime: &timestamppb.Timestamp{Seconds: time.Now().Unix()},
Persistent: false,
},
},
},
}})
} else {
session.Close("server-side session disconnect", reasonOverride)
}
}
return nil
}
Expand Down

0 comments on commit fd1476c

Please sign in to comment.