diff --git a/chat.go b/chat.go index 18a6375..4a170df 100644 --- a/chat.go +++ b/chat.go @@ -5,6 +5,7 @@ import ( "database/sql" "errors" + chatclasses "git.golem15.com/golem15/sm-chat-plugin/classes" chatcontracts "git.golem15.com/golem15/sm-chat-plugin/contracts" chanclasses "git.golem15.com/golem15/sm-chatchannels-plugin/classes" "git.golem15.com/golem15/summercms/modules/backpack" @@ -42,9 +43,27 @@ func (p *Plugin) registerChat(app *backpack.App) error { if err := reg.Add(p.ID(), chatcontracts.Registration{Alias: classes.ChatContextType, Loader: p.loadChatContext}); err != nil { return err } + if err := p.installChatLimits(app); err != nil { + return err + } return p.installChatAccess(app) } +// installChatLimits makes golem15.chat enforce jz.bm.chat.max_upload_bytes +// as the total size of one message's attachments, the value the bootstrap +// advertises to the student app. golem15.chat publishes its service from +// its own database hook, which runs before this one. +func (p *Plugin) installChatLimits(app *backpack.App) error { + return lagoon.OnDatabase(app, func(_ *sql.DB, _ *gorm.DB) error { + svc, ok := app.Lookup[*chatclasses.Service]() + if !ok || svc == nil { + return errors.New("jz.bm: golem15.chat must be listed before jz.bm in summer.yaml") + } + svc.SetMaxMessageBytes(int64(p.chatMaxUploadBytes())) + return nil + }) +} + // loadChatContext is the loader of the bm alias. The only context is id 1; // any other id is unknown, which golem15.chat answers with its not-a-member // error. diff --git a/config/config.yaml b/config/config.yaml index 2f99da9..88bf9c9 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -19,7 +19,9 @@ public_url: "" chat: group: studenci moderator_group: moderatorzy - # Total size of one message's attachments (60 MiB). It stays under nginx's + # Total size of one message's attachments (60 MiB), enforced by + # golem15.chat when the message is sent (Service.SetMaxMessageBytes) and + # checked by the student app before upload. It stays under nginx's # client_max_body_size 64m so the multipart envelope still fits. max_upload_bytes: 62914560 # Entity embeds (deadline, class, reading) per message.