package bm import ( "context" "errors" usermodels "git.golem15.com/golem15/sm-user-plugin/models" "git.golem15.com/golem15/summercms/modules/backpack" "git.golem15.com/golem15/summercms/modules/lighthouse" "gorm.io/gorm" // The app always ships the Centrifugo driver; config/realtime.yaml // keeps the null driver until rome's .env selects centrifugo. _ "git.golem15.com/golem15/summercms/modules/lighthouse/centrifugo" ) // errDatabaseUnavailable is the user lookup's answer before serve has // published the database. var errDatabaseUnavailable = errors.New("jz.bm: database unavailable") // wireRealtime binds the app to the framework realtime service: it builds // the service selected by realtime.driver and installs the user lookup the // connection token reads its display name from. Channel authorizers belong // to the plugins that own the channels (golem15.presentations). func (p *Plugin) wireRealtime(app *backpack.App) error { svc, err := lighthouse.From(app) if err != nil { return err } p.realtime = svc svc.SetUserLookup(func(ctx context.Context, id uint) (lighthouse.User, bool, error) { // Boot runs before serve publishes the database, so the handle is // resolved per call. gdb, ok := app.Lookup[*gorm.DB]() if !ok || gdb == nil { return lighthouse.User{}, false, errDatabaseUnavailable } var u usermodels.User err := gdb.WithContext(ctx).Select("id", "name").Where("id = ?", id).Take(&u).Error if errors.Is(err, gorm.ErrRecordNotFound) { return lighthouse.User{}, false, nil } if err != nil { return lighthouse.User{}, false, err } return lighthouse.User{ID: u.ID, Name: u.Name}, true, nil }) return nil }