Files
odoo-ai/addons/activeblue_ai/__init__.py
Carlos Garcia 79537a5e21 fix(addon): create AI bot as Confirmed (skip invitation email)
The post_init/post_migrate hook now:
- Uses no_reset_password context to skip Odoo's invitation email flow
- Writes a res_users_log row so the user shows as Confirmed instead of
  Pending Invitations (Odoo 18 derives state from log presence)
- Idempotently ensures the partner_activeblue_ai external ID points at
  the bot's partner so env.ref() resolves in ab_ai_mail.py

Discovered while fixing the bot DM flow on miaai: even after the user
was created, Discuss showed it as Pending until a res_users_log row was
inserted manually.
2026-04-24 22:49:42 -04:00

57 lines
2.1 KiB
Python

from . import models, controllers
import logging
_logger = logging.getLogger(__name__)
def _ensure_ai_bot_user(env):
"""Create the ActiveBlue AI internal user so it appears in Discuss DM search.
Skips invitation email (no_reset_password) and marks the user as already
logged-in (res_users_log row) so it shows as Confirmed instead of Pending.
Also ensures the external ID exists so `env.ref('activeblue_ai.partner_activeblue_ai')`
resolves in the discuss.channel message override.
"""
User = env['res.users'].with_context(no_reset_password=True)
user = User.search([('login', 'in', ('activeblue_ai_bot', 'activeblue_ai_bot@local'))], limit=1)
if not user:
user = User.create({
'name': 'ActiveBlue AI',
'login': 'activeblue_ai_bot',
'groups_id': [(6, 0, [env.ref('base.group_user').id])],
'share': False,
'active': True,
})
_logger.info('Created ActiveBlue AI bot user id=%d', user.id)
# Ensure external ID -> partner mapping (needed by ab_ai_mail.py override)
imd = env['ir.model.data']
existing = imd.search([
('module', '=', 'activeblue_ai'),
('name', '=', 'partner_activeblue_ai'),
], limit=1)
if existing:
existing.write({'model': 'res.partner', 'res_id': user.partner_id.id})
else:
imd.create({
'module': 'activeblue_ai',
'name': 'partner_activeblue_ai',
'model': 'res.partner',
'res_id': user.partner_id.id,
'noupdate': True,
})
# Clear "Pending Invitations" state by writing a res_users_log row.
# In Odoo 18 the user state is computed from the existence of a log entry.
env.cr.execute(
"SELECT 1 FROM res_users_log WHERE create_uid = %s LIMIT 1",
(user.id,),
)
if not env.cr.fetchone():
env.cr.execute(
"INSERT INTO res_users_log (create_uid, create_date, write_uid, write_date) "
"VALUES (%s, NOW(), %s, NOW())",
(user.id, user.id),
)
_logger.info('Marked ActiveBlue AI bot user id=%d as confirmed', user.id)