Files
sm-bm-plugin/console/chat_sync.go
Jakub Zych 0ff943b6af fix: keep DM participant rows when a user leaves the chat group
The participant sync and the non-member prune now remove only ogólny's
rows. A user taken out of studenci, even briefly, keeps their DMs (the
live membership check denies access while they are out) and gets them
back when re-added, instead of losing them for good.
2026-10-26 19:34:53 +01:00

60 lines
2.1 KiB
Go

package console
import (
"context"
"fmt"
"git.golem15.com/golem15/summercms/modules/bonfire"
"git.golem15.com/jakub/sm-bm-plugin/classes"
"gorm.io/gorm"
)
// chatSyncName is the name of the chat sync command.
const chatSyncName = "bm:chat-sync"
// ChatSyncCommand returns the chat sync console command. withDB runs the
// command's work with the application's database; groups returns the chat
// group code and the moderator group code. It never adds anyone to a group:
// members are assigned by hand in the backend.
func ChatSyncCommand(withDB func(ctx context.Context, fn func(*gorm.DB) error) error, groups func() (string, string)) bonfire.Command {
return bonfire.Command{
Name: chatSyncName,
Description: "Create the chat groups if missing, sync ogólny participants with the chat group and give members a handle",
Run: func(ctx context.Context, _ bonfire.Input, out bonfire.Output) error {
group, moderatorGroup := groups()
return withDB(ctx, func(db *gorm.DB) error {
created, err := classes.EnsureChatGroups(ctx, db, group, moderatorGroup)
if err != nil {
return fmt.Errorf("%s: groups: %w", chatSyncName, err)
}
general, err := classes.FindGeneral(ctx, db)
if err != nil {
return fmt.Errorf("%s: ogólny: %w", chatSyncName, err)
}
var generalID uint
if general != nil {
generalID = general.ID
}
added, removed, err := classes.SyncChatParticipants(ctx, db, generalID, group)
if err != nil {
return fmt.Errorf("%s: participants: %w", chatSyncName, err)
}
handles, err := classes.AssignMissingHandles(ctx, db, group)
if err != nil {
return fmt.Errorf("%s: handles: %w", chatSyncName, err)
}
out.Printf("groups created: %d\n", created)
if general == nil {
out.Printf("ogólny does not exist yet; it is created when a member first opens the chat\n")
} else {
out.Printf("ogólny participants added: %d\n", added)
}
out.Printf("ogólny participants removed: %d\n", removed)
out.Printf("handles assigned: %d\n", handles)
out.Success(chatSyncName + ": chat group " + group + " synced")
return nil
})
},
}
}