| 313 | } |
| 314 | |
| 315 | func (m *Manager) initializeSingleUserPlugin(userCtx compat.UserContext, p compat.Plugin) error { |
| 316 | info := p.PluginInfo() |
| 317 | instance := p.NewPluginInstance(userCtx) |
| 318 | userID := userCtx.ID |
| 319 | |
| 320 | pluginConf, err := m.db.GetPluginConfByUserAndPath(userID, info.ModulePath) |
| 321 | if err != nil { |
| 322 | return err |
| 323 | } |
| 324 | |
| 325 | if pluginConf == nil { |
| 326 | var err error |
| 327 | pluginConf, err = m.createPluginConf(instance, info, userID) |
| 328 | if err != nil { |
| 329 | return err |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | m.instances[pluginConf.ID] = instance |
| 334 | |
| 335 | if compat.HasSupport(instance, compat.Messenger) { |
| 336 | instance.SetMessageHandler(redirectToChannel{ |
| 337 | ApplicationID: pluginConf.ApplicationID, |
| 338 | UserID: pluginConf.UserID, |
| 339 | Messages: m.messages, |
| 340 | }) |
| 341 | } |
| 342 | if compat.HasSupport(instance, compat.Storager) { |
| 343 | instance.SetStorageHandler(dbStorageHandler{pluginConf.ID, m.db}) |
| 344 | } |
| 345 | if compat.HasSupport(instance, compat.Configurer) { |
| 346 | m.initializeConfigurerForSingleUserPlugin(instance, pluginConf) |
| 347 | } |
| 348 | if compat.HasSupport(instance, compat.Webhooker) { |
| 349 | id := pluginConf.ID |
| 350 | g := m.mux.Group(pluginConf.Token+"/", requirePluginEnabled(id, m.db)) |
| 351 | instance.RegisterWebhook(strings.Replace(g.BasePath(), ":id", strconv.Itoa(int(id)), 1), g) |
| 352 | } |
| 353 | if pluginConf.Enabled { |
| 354 | err := instance.Enable() |
| 355 | if err != nil { |
| 356 | // Single user plugin cannot be enabled |
| 357 | // Don't panic, disable for now and wait for user to update config |
| 358 | log.Printf("Plugin initialize failed for user %s: %s. Disabling now...", userCtx.Name, err.Error()) |
| 359 | pluginConf.Enabled = false |
| 360 | m.db.UpdatePluginConf(pluginConf) |
| 361 | } |
| 362 | } |
| 363 | return nil |
| 364 | } |
| 365 | |
| 366 | func (m *Manager) initializeConfigurerForSingleUserPlugin(instance compat.PluginInstance, pluginConf *model.PluginConf) { |
| 367 | if len(pluginConf.Config) == 0 { |