From 3aca86aa43afa6f4844c89c2f35ffbb3f7d9c06d Mon Sep 17 00:00:00 2001 From: Gabriel Jablonski Date: Sat, 11 Apr 2026 13:50:15 -0300 Subject: [PATCH] feat(internal-chat): implement internal chat system for agents (#247) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(internal-chat): implement internal chat system for agents (Phase 1+2 MVP) Add a Slack/Discord-style internal messaging system for Chatwoot agents with text channels (public/private), direct messages, reactions, typing indicators, and real-time updates via ActionCable. Backend: - 6 database migrations (categories, channels, members, messages, attachments, reactions) - 6 models under InternalChat:: namespace with validations and associations - API controllers for categories, channels, messages, members, and reactions - Pundit policies for authorization (public/private/DM access control) - MessageCreateService, TypingStatusManager, DefaultChannelSetupService - InternalChatListener for real-time broadcasting to channel members - Event types for internal chat events - Default category/channel setup for new and existing accounts Frontend: - Vuex store modules for channels, messages, and typing status - API clients for channels and messages - Vue 3 components: InternalChatLayout, ChannelSidebar, ChannelView, ChannelHeader, MessageList, MessageBubble, MessageEditor, EmojiReactionPicker, ReactionDisplay, TypingIndicator - Sidebar integration with "Internal Chat" menu item - ActionCable handlers for real-time message/reaction/typing events - Route definitions and i18n translations Co-Authored-By: Claude Opus 4.6 (1M context) * test(internal-chat): add comprehensive specs for models, controllers, policies, services, and listener - 6 model specs (74 examples) covering associations, validations, scopes, methods - 5 request specs for all API controllers (categories, channels, messages, members, reactions) - 4 policy specs testing authorization rules for all actions - 3 service specs (DefaultChannelSetupService, MessageCreateService, TypingStatusManager) - 1 listener spec testing real-time broadcasting for all event types - 6 FactoryBot factories with traits for all InternalChat models Co-Authored-By: Claude Opus 4.6 (1M context) * fix(internal-chat): fix dispatcher mock in service specs and cursor pagination test - Allow dispatcher.dispatch in service specs to handle Account.created callbacks from factory setup before asserting specific event dispatch - Fix after-cursor pagination test by adding 1 second offset to avoid timestamp precision issues with iso8601 rounding Co-Authored-By: Claude Opus 4.6 (1M context) * fix(internal-chat): address CodeRabbit review — 7 critical security/correctness fixes - Scope member creation through Current.account.users to prevent cross-account membership - Scope member_ids in DM creation through Current.account to prevent cross-account invites - Scope reaction message lookup through channel account to prevent cross-account access - Fix Vuex store to commit messages array instead of response envelope - Add UUID generation callback on Channel model (before_validation) - Add channel access check to reaction deletion policy - Validate parent_id belongs to same channel in MessageCreateService Co-Authored-By: Claude Opus 4.6 (1M context) * fix(internal-chat): address CodeRabbit round 2 + fix ChannelSidebar runtime error - Re-throw error in fetchMessages instead of swallowing with empty array - Wrap message + attachment creation in transaction for atomicity - Fix factory to derive account from message (prevent cross-account fixtures) - Guard listener against cross-account mismatch (not just missing records) - Add cross-account regression tests to listener spec - Fix ChannelSidebar computed properties to default to empty arrays Co-Authored-By: Claude Opus 4.6 (1M context) * feat(internal-chat): auto-setup default channels on account creation and migration - Add after_create_commit :setup_internal_chat callback on Account model - Add data migration to create default channels for existing accounts - Make DefaultChannelSetupService convergent (find_or_create) instead of bail-on-exists, so it can sync new members on subsequent runs - Fix specs to handle default category/channel created by callback Co-Authored-By: Claude Opus 4.6 (1M context) * fix(internal-chat): avoid Vuex state mutation in sort + align muted styling in fallback section Co-Authored-By: Claude Opus 4.6 (1M context) * fix(internal-chat): fix store module name mismatch — register as 'internalChat' not 'internalChatChannels' Components dispatch to 'internalChat/get' but the module was registered as 'internalChatChannels'. Also fix ActionCable handlers to use 'internalChat/messages/' nested module path. Co-Authored-By: Claude Opus 4.6 (1M context) * i18n(internal-chat): add pt-BR translations for internal chat feature Backend: default_category_name ('Canais') and default_channel_name ('Geral') Frontend: all 40+ keys translated to Brazilian Portuguese Co-Authored-By: Claude Opus 4.6 (1M context) * fix(internal-chat): handle ISO 8601 timestamps in MessageBubble and MessageList The API returns created_at as ISO strings but messageTimestamp() expects Unix seconds and MessageList used `* 1000`. Now handles both formats. Co-Authored-By: Claude Opus 4.6 (1M context) * docs(internal-chat): build swagger output for internal chat API endpoints Co-Authored-By: Claude Opus 4.6 (1M context) * docs(internal-chat): register internal chat tags and paths in swagger index Add tag definitions and path entries for all 5 internal chat resource groups in swagger/index.yml and swagger/paths/index.yml. Rebuild output. Co-Authored-By: Claude Opus 4.6 (1M context) * i18n(internal-chat): add SIDEBAR.INTERNAL_CHAT key to pt-BR settings Co-Authored-By: Claude Opus 4.6 (1M context) * fix(internal-chat): comprehensive review fixes — backend and frontend Backend: - Add attachments to message API responses in both controllers - Add internal_chat_channel_updated listener handler - Include reactions in message event broadcast data Frontend: - Fix ActionCable dispatch paths to use correct action names (addMessageFromCable, updateMessageFromCable, deleteMessageFromCable) - Connect typingUsers to internalChatTypingStatus store getter - Fix message field references (edited → content_attributes.edited_at) - Fix channel type comparisons (use 'private_channel'/'dm' strings) - Add parent 'internal_chat' to sidebar activeOn array - Increment unread_count on ActionCable message receive - Add loadMore handler for cursor-based pagination - Remove unused is-direct-message prop from InternalChatLayout Co-Authored-By: Claude Opus 4.6 (1M context) * feat(internal-chat): implement phases 3-5 — threads, mentions, notifications, polls, drafts Phase 3 — Threads, Mentions, Notifications: - MentionService: parse @user mentions, @all (admin only), generate notifications - NotificationService: create notifications for channel messages (respects mute) - Add internal_chat_message/mention notification types to Notification model - ThreadPanel.vue: slide-out panel for threaded replies - Integrate mentions + notifications into MessageCreateService Phase 4 — Polls: - 3 new migrations: polls, poll_options, poll_votes tables - 3 new models: Poll, PollOption, PollVote with validations - PollsController: create poll, vote, unvote with routes - PollService: voting logic with multiple choice + revote support - PollCreator.vue: modal for creating polls with options - PollDisplay.vue: vote UI with progress bars and results - Polls Vuex store module - INTERNAL_CHAT_POLL_VOTED event type Phase 5 — Drafts: - 1 new migration: drafts table - Draft model with validations - DraftsController: full CRUD (replace stub) - DraftsList.vue: list all user drafts with navigation - Drafts Vuex store module with auto-save - Draft route and sidebar integration Phase 6 — Feature Flag: - Add INTERNAL_CHAT feature flag to features.yml and featureFlags.js Co-Authored-By: Claude Opus 4.6 (1M context) * fix(internal-chat): fix API routing for drafts and polls, add poll voting ActionCable handler - Fix draft API client to use channel-scoped PATCH/DELETE endpoints - Create dedicated polls API client with correct poll-based endpoints - Update polls store to use InternalChatPollsAPI with pollId-based voting - Add ActionCable handler for internal_chat.poll.voted events - Add thread and drafts routes to sidebar activeOn array - Fix drafts store to pass channelId to API calls Co-Authored-By: Claude Opus 4.6 (1M context) * fix(internal-chat): fix poll response format and API client routing (review round 2) - Return message with embedded poll data instead of raw poll response - Add poll data to message_response in messages controller - Create dedicated InternalChatPollsAPI client with correct endpoints - Update PollDisplay.vue to read from message.poll or content_attributes.poll - Use option.voted flag instead of checking voters array - Add missing PERCENTAGE i18n key to pt-BR - Remove unused currentUserId prop from PollDisplay Co-Authored-By: Claude Opus 4.6 (1M context) * fix(internal-chat): fix poll voting and draft lookup bugs (review round 3) - Fix draft getter to use internal_chat_channel_id field name - Split poll set_poll into vote/unvote variants — unvote doesn't need option_id - Unvote finds user's vote by user_id across all poll options - Fix ChannelView to extract pollId from message.poll before dispatching - Fix unvote handler to not require optionId Co-Authored-By: Claude Opus 4.6 (1M context) * fix: address CodeRabbit review feedback (round 3) - Expose is_dm, favorited, muted on channel API responses - Normalize poll cable updates into message-shaped patch - Add file presence validation to MessageAttachment - Remove duplicate mention notifications from MentionService - Make data migration rollback safe (IrreversibleMigration) - Update factory to include file by default Co-Authored-By: Claude Opus 4.6 (1M context) * fix: return 201 for channel creation and optimize DM lookup - Return :created (201) instead of :ok (200) for channel creation - Replace O(n) Ruby scan with SQL-based DM lookup using ARRAY_AGG Co-Authored-By: Claude Opus 4.6 (1M context) * fix: address CodeRabbit review feedback (round 5) - Broadcast channel event after create for real-time notifications - Separate create/update strong params to prevent channel_type transitions - Use strong params for typing_status input - Replace find_by with detect on preloaded collections to fix N+1 - Preload attachments with blobs in show response Co-Authored-By: Claude Opus 4.6 (1M context) * fix: address CodeRabbit review feedback (round 6) - Serialize DM creation with advisory lock to prevent duplicates - Broadcast channel deletion event for real-time UI updates - Use strong params for mark_unread message_id - Batch unread count computation to eliminate N+1 in index Co-Authored-By: Claude Opus 4.6 (1M context) * fix: eliminate N+1 in compute_unread_counts with single JOIN query Replace per-membership COUNT loop with a single JOIN + GROUP BY query that returns all unread counts in one database call. Co-Authored-By: Claude Opus 4.6 (1M context) * fix(internal-chat): quality fixes, missing tests, and Playwright E2E setup Addresses quality issues found during review and fills test coverage gaps for the internal chat feature. Backend fixes: - Return 201 for all create endpoints (messages, categories, polls, reactions, members) - Fix N+1 queries: replies.size, poll votes, category channels.count, votes.exists? - Fix pagination has_more logic to check page size instead of total count - Scope poll vote/unvote to current account (security fix) - Add internal_chat.messages.deleted i18n key - Use find_by! in mark_unread for proper 404 on non-members - Guard time param parsing with rescue ArgumentError - Align message response format between channels and messages controllers - Switch notification service to ActionCable-only (avoid push/email crashes) Frontend fixes: - Fix pinned message detection (content_attributes.pinned, not message.pinned) - Fix thread reply count key (replies_count, not thread_replies_count) - Fix markUnread to pass message_id parameter - Fix pagination: PREPEND_MESSAGES mutation instead of overwriting - Fix typing status to read Vuex reactive state, not stale closure - Fix deleteDraft argument shape (pass { channelId, draftId }) - Fix DM channel filtering (check both is_dm and channel_type) - Fix DraftsList navigation to use correct channel ID key - Wire PollCreator to poll button in MessageEditor - Wire settings event handler on ChannelHeader - Reset PollCreator isSubmitting on timeout New RSpec tests (67 examples): - Factories: polls, poll_options, poll_votes, drafts - Model specs: Poll, PollOption, PollVote, Draft - Controller specs: PollsController, DraftsController - Service specs: PollService, NotificationService, MentionService Playwright E2E setup (37 tests): - Install Playwright with Chromium - Auth helper with Devise Token Auth login flow - 8 test suites: navigation, channels, messaging, DMs, reactions, threads, polls, mark-read-unread Co-Authored-By: Claude Opus 4.6 (1M context) * fix: address CodeRabbit review feedback (round 7) Backend: - Use lambda for UUID default in channels migration - Wrap poll creation in transaction for atomicity - Preload replies in thread action to avoid N+1 - Broadcast replies_count + attachments in listener (match REST shape) - Scope draft listing through accessible channels - Key draft upserts/deletes by parent_id for thread drafts Frontend: - Remove duplicate poll methods from internalChatMessages.js (use internalChatPolls.js) - Persist toggleMute/toggleFavorite to backend via updateMember API - Clear active channel on DELETE_CHANNEL mutation - Skip unread increment for active channel in ActionCable handler - Filter archived channels from sidebar getters - Fix ChannelHeader isArchived to check status === 'archived' - Prevent duplicate reactions in ADD_REACTION mutation - Merge poll data into existing content_attributes on cable updates - Guard infinite scroll against duplicate loads - Add response.ok() check in E2E auth helper, remove hardcoded account ID Co-Authored-By: Claude Opus 4.6 (1M context) * fix: address CodeRabbit review feedback (round 8) - Remove unused nested typingStatus module from internalChat store - Add parent_id to draft uniqueness scope and migration index - Exclude reaction creator from reaction_created broadcast - Preload attachments and poll associations in thread/messages queries - Handle `after` fetches with APPEND_MESSAGES mutation - Wrap channel creation payloads under `channel` key in E2E helpers Co-Authored-By: Claude Opus 4.6 (1M context) * fix: rewrite Playwright E2E tests to use actual UI interactions Completely rewrote all 8 E2E test suites to work with the live app: - Test through actual UI interactions, not API bypass - Use correct Portuguese (pt_BR) locale strings - Use structural selectors matching real Vue component DOM - Dynamic account ID from login response (no hardcoded values) - 3 parallel workers, increased timeouts for reliability - API calls only for preconditions (seeding test data) 29 tests passing across navigation, channels, messaging, DMs, reactions, threads, polls, and mark-read-unread suites. Co-Authored-By: Claude Opus 4.6 (1M context) * fix: use partial unique indexes for draft uniqueness with NULL parent_id PostgreSQL treats NULL as distinct in unique constraints, so a composite index on (user_id, channel_id, parent_id) allows duplicate root drafts. Split into two partial indexes: one for root drafts (WHERE parent_id IS NULL) and one for thread drafts (WHERE parent_id IS NOT NULL). Co-Authored-By: Claude Opus 4.6 (1M context) * fix: address CodeRabbit review feedback (round 9) - Remove duplicate index on internal_chat_polls.internal_chat_message_id (keep only unique index) - Add options validation in polls create (return 400 instead of 500) - Add expiration check to unvote action (match vote behavior) - Use strong params in messages update action Co-Authored-By: Claude Opus 4.6 (1M context) * fix: address CodeRabbit review feedback (round 10) - Change channel associations from destroy_async to destroy (FK constraints are ON DELETE RESTRICT, blocking async deletion) - Remove unused internal_chat notification types and PRIMARY_ACTORS entry (notification service uses ActionCable only, no DB records) Co-Authored-By: Claude Opus 4.6 (1M context) * fix: address CodeRabbit review feedback (round 11) - Scope category_id to current account in channels controller (security) - Defer message-created event in poll creation until after transaction - Change message associations from destroy_async to destroy (FK compat) - Validate option belongs to poll in poll_service - Use strong params for emoji in reactions controller Co-Authored-By: Claude Opus 4.6 (1M context) * fix: address CodeRabbit review feedback (round 12) Backend (9 fixes): - Gate message update/destroy by channel accessibility in policy - Guard content_attributes nil before merge in polls controller - Fix after-cursor pagination to use limit() instead of last() - Wrap revote in transaction for atomicity in poll service - Make unvote option-specific for multi-choice polls - Exclude own messages from unread count - Make channel activity update monotonic (only write if newer) - Include actor in message/reaction broadcasts (multi-tab support) - Return 400 for empty member create instead of 201 Frontend (8 fixes): - Show uncategorized channels even when categories exist - Clear editor on channel switch when no draft exists - Soft-delete messages in store (update in place, don't remove) - Guard ThreadPanel against out-of-order fetch responses - Replace hardcoded channel label with i18n key in DraftsList - Add accessible name to settings button in ChannelHeader - Add aria-label to search field in ChannelSidebar - Make MessageBubble actions keyboard-accessible via focus-within Co-Authored-By: Claude Opus 4.6 (1M context) * fix: address CodeRabbit review feedback (round 13) - Fix keyword argument mismatch in reactions dispatch_reaction_event - Add user_id to reaction cable broadcast for shape consistency with REST Co-Authored-By: Claude Opus 4.6 (1M context) * fix(internal-chat): quality fixes, expanded RSpec + Playwright E2E tests Fix isArchived computed (checked .archived instead of .status), fix ReactionDisplay user identification (.user?.id vs .user_id), update 17 spec assertions from :success to :created on create endpoints, add 32 new RSpec examples (polls, drafts, services), and rewrite 8 Playwright E2E test files with correct selectors, proper test isolation, and dynamic user ID discovery. Co-Authored-By: Claude Opus 4.6 (1M context) * fix: address CodeRabbit review feedback (round 14) Prevent duplicate votes on same option in multi-choice polls with explicit BadRequest guard. Add internal_chat webhook events to ALLOWED_WEBHOOK_EVENTS so users can subscribe to them. Co-Authored-By: Claude Opus 4.6 (1M context) * fix: include poll data in ActionCable broadcast for poll messages Extract base_message_data helper and enrich message_event_data with poll options when the message has an associated poll, ensuring realtime subscribers receive the same poll data as REST API clients. Co-Authored-By: Claude Opus 4.6 (1M context) * fix: address CodeRabbit review feedback (round 15) Backend: wrap single-choice revotes in transaction, capture member tokens before channel destroy, exclude own messages from unread count, strip attachments from deleted messages, enrich poll broadcast payload. Frontend: use getCurrentRole getter, fix public-results poll display, sync thread replies via store, add close button a11y, pass option_id to unvote API, pass parent_id to deleteDraft API. Models: handle nil last_read_at for new members, skip content validation for attachment-only messages, align PollService guards with controller, change category dependent to nullify. Swagger: add attachments to message schema, fix create status to 201. E2E: remove fragile waitForTimeout. Co-Authored-By: Claude Opus 4.6 (1M context) * fix(internal-chat): fix 21 UX/functional issues Address 21 UX gaps discovered during product testing: Sidebar & Navigation: - Fix search icon overlap, extend search to description + DM members - Add create channel/DM/category buttons and modals - Show DM member names instead of null - Include members data in channel index API for DMs Message Interactions: - Add delete confirmation dialog - Implement inline message editing with cancel support - Toggle emoji reactions (add/remove) - Support multiple pinned messages with click-to-scroll - Prevent thread replies from appearing in main chat - Fix reply count live updates - Hide pin button on thread messages - Improve deleted message styling with greyed-out card - Replace spinner with skeleton loading - Add markdown toolbar (bold/italic/code) - Fix thread editing and add vote/unvote handlers Features & Polish: - Implement channel settings slide-over panel - Fix thread loading not affecting main channel spinner - Fix poll creation field name mismatch with backend API - Fix drafts: show channel names, handle DM navigation Co-Authored-By: Claude Opus 4.6 (1M context) * fix(internal-chat): use Dialog modal for delete confirmation Replace window.confirm with the project's Dialog component for message delete confirmation, providing a consistent UI experience. Co-Authored-By: Claude Opus 4.6 (1M context) * fix: address CodeRabbit review feedback (round 16) - Require content field in message update OpenAPI schema Co-Authored-By: Claude Opus 4.6 (1M context) * fix: address CodeRabbit review feedback (round 17) - Sanitize advisory lock SQL with sanitize_sql_array - Use semantic button for pinned message banner - Add aria-label to ChannelSettings close button - Add type="button" to all ChannelSettings buttons - Gate channel/DM/category creation to admins - Replace hardcoded 'Direct Message' with i18n key Co-Authored-By: Claude Opus 4.6 (1M context) * fix: address CodeRabbit review feedback (round 18) - Wrap DM creation payload in channel key for consistency - Replace raw text in category select with i18n key - Add IME composition guard to prevent premature send Co-Authored-By: Claude Opus 4.6 (1M context) * feat(internal-chat): UX round 2, rich editor, team members, drag-and-drop - Reduce sidebar spacing between search bar and drafts - Fix search icon overlapping placeholder text - Replace inline category form with Dialog modal - Add collapsible sidebar sections with localStorage persistence - Add drag-and-drop channels across categories (admin-only, vuedraggable) - Replace textarea editor with WootWriter ProseMirror rich text editor - Replace regex markdown rendering with shared MessageFormatter - Wire draft auto-save pipeline with WootWriter (3s debounce watcher) - Add team + agent selection when creating private channels - Auto-add all agents when creating public channels - Sync team members to linked channels via TeamMember callback - Fix member list not loading on first settings panel open - Complete PT-BR translations for all internal chat strings Co-Authored-By: Claude Opus 4.6 (1M context) * feat(internal-chat): UX round 3, Enter-to-send, mentions, copy link, poll modal - Send with Enter (not Cmd+Enter), Shift+Enter for newlines - Enable @mentions via WootWriter suggestions plugin - Refocus editor after sending a message - Copy link to message button in hover toolbar - Poll creator refactored to Dialog with confirm-discard on close - Channel type uses Switch instead of dropdown - Category uses components-next Select instead of native select - Skeleton loading: only on initial load, spinner for pagination - Scroll position preserved when loading older messages - Mute/Favorite buttons fixed (store members updated after fetch) - Add/remove channel members after creation (admin-only) - Save draft immediately when switching channels - Settings sidebar remembers open/closed state via localStorage - Search icon overlap fixed (increased padding) Co-Authored-By: Claude Opus 4.6 (1M context) * fix(internal-chat): DM settings, copy updates, input refocus, member UX Co-Authored-By: Claude Opus 4.6 (1M context) * fix(internal-chat): member edit for private only, emoji overflow, reaction tooltips Co-Authored-By: Claude Opus 4.6 (1M context) * fix(internal-chat): thread count sync, scroll loading, copy link, thread/settings exclusivity - Fix thread reply count doubling (remove duplicate INCREMENT_REPLY_COUNT from sendThreadReply, cable handles it) - Fix copy link button (use window.location.origin + pathname as fallback) - Hide poll button in thread editor - Add "Also send in #channel" checkbox for thread replies - Increase scroll threshold for loading older messages (100px instead of 0) - Track and stop loading when oldest message reached - Thread and settings panels are mutually exclusive - Refocus editor after send with delayed focus Co-Authored-By: Claude Opus 4.6 (1M context) * fix(internal-chat): scroll to linked message via ?messageId= query param Read messageId from route query on mount, scroll to and highlight the target message after messages load, then clean the query param. Co-Authored-By: Claude Opus 4.6 (1M context) * fix(internal-chat): prevent editor from becoming unfocusable after send Root cause: passing disabled prop to WootWriter applies pointer-events-none and ProseMirror does not re-enable contenteditable when disabled returns to false. Fix: never disable the WootWriter, use a local isSending guard to prevent double-sends. Refocus 300ms after send for ProseMirror state reload. Co-Authored-By: Claude Opus 4.6 (1M context) * fix(internal-chat): simplify send guard, no artificial timeout Content is cleared immediately before emit, so canSend naturally returns false (empty content). No isSending guard needed. Co-Authored-By: Claude Opus 4.6 (1M context) * fix(internal-chat): align poll option remove buttons vertically Increase padding to p-1.5 and add flex-shrink-0 for consistent sizing. Co-Authored-By: Claude Opus 4.6 (1M context) * fix(internal-chat): close poll modal after creating poll Co-Authored-By: Claude Opus 4.6 (1M context) * fix(internal-chat): poll option X button alignment, discard modal on submit - Button uses explicit 34px height matching input, no items-center - Reset form before closing dialog so hasUnsavedChanges is false Co-Authored-By: Claude Opus 4.6 (1M context) * fix(internal-chat): close settings sidebar when clicking reply Co-Authored-By: Claude Opus 4.6 (1M context) * fix(internal-chat): update poll UI after voting, fix re-vote error Vote/unvote actions now dispatch updateMessageFromCable with the API response to update poll state locally. Pass channelId to enable this. Clicking an already-voted option correctly triggers unvote. Co-Authored-By: Claude Opus 4.6 (1M context) * fix(internal-chat): include poll data in message response, add timer and voters Backend: message_response now includes poll data (options, votes, voted status, voters for public polls) via eager-loaded poll association. This fixes polls not rendering after page reload. Frontend PollDisplay: - Countdown timer showing time remaining until poll closes - Read-only state when expired (div instead of button, no hover) - Voter names shown below each option (public polls or admin) - Prefer content_attributes.poll over message.poll for fresh data Co-Authored-By: Claude Opus 4.6 (1M context) * fix(internal-chat): include channel_id in poll voted cable broadcast The poll_event_data was missing internal_chat_channel_id, so the frontend cable handler could not route the update to the correct channel's message store. Co-Authored-By: Claude Opus 4.6 (1M context) * fix(internal-chat): poll vote highlight, typing off, reaction broadcast, translations - Preserve per-user voted flags when merging cable poll broadcast - Send typing_off after 3s of no typing activity - Include internal_chat_channel_id in reaction event broadcasts - Fix reaction deleted handler to also check channel_id field - Simplify "also send in" copy (works for both channels and DMs) - Add PT-BR translation for ALSO_SEND_IN_CHANNEL Co-Authored-By: Claude Opus 4.6 (1M context) * feat(internal-chat): unified reaction popover with all emoji groups Clicking any reaction badge opens a single popover showing all reactions grouped by emoji with user names. Current user can remove their own reaction via X button. Replaces per-reaction popover with unified view. Co-Authored-By: Claude Opus 4.6 (1M context) * fix(internal-chat): wire close DM button to archive and navigate home Co-Authored-By: Claude Opus 4.6 (1M context) * feat(internal-chat): close DM via hidden flag on channel membership Add hidden boolean to channel_members table. Close DM sets hidden=true via member update API. Sidebar filters out hidden DMs. New messages on a DM channel automatically unhide all members via listener callback. Co-Authored-By: Claude Opus 4.6 (1M context) * fix(internal-chat): reaction popover, user names, file upload support - Include user name in reaction API response (was missing) - Redesign reaction popover: flat list with emoji + name per row, aligned X button for removing own reactions - Add file upload: paperclip button opens file picker, attached files shown as chips with remove, sent via FormData with message - Store action and API client support files parameter Co-Authored-By: Claude Opus 4.6 (1M context) * fix(internal-chat): reaction user names, unreact button, attachment rendering - Include user name in reactions across all endpoints (messages_controller, listener base_message_data) - Make unreact X button always visible (bg-n-alpha-2 background) - Render message attachments as downloadable links with paperclip icon Co-Authored-By: Claude Opus 4.6 (1M context) * feat(internal-chat): image preview for attachments in messages and editor Messages: images render inline with max-h-60, non-images as download links. Editor: image files show thumbnail preview, non-images show file icon + name. Remove button as floating circle on top-right corner of each attachment. Co-Authored-By: Claude Opus 4.6 (1M context) * fix(internal-chat): attachment preview matches conversation pattern - File previews show name + size (e.g. "2 MB") in a horizontal card - Image thumbnails as 32px squares, non-images show document emoji - Remove button is a visible X icon (not a floating circle) - Layout matches AttachmentsPreview from conversation ReplyBox Co-Authored-By: Claude Opus 4.6 (1M context) * fix(internal-chat): auto-detect image file type from MIME on upload MessageCreateService now detects file type from content_type instead of defaulting to :file. Images are correctly tagged as :image so they render inline in message bubbles. Co-Authored-By: Claude Opus 4.6 (1M context) * fix(internal-chat): include file_url in cable broadcast, fix filename display Listener attachment_event_data now includes file_url so attachments render correctly on real-time messages without page refresh. MessageBubble extracts filename from URL or falls back to file_type+ext. Co-Authored-By: Claude Opus 4.6 (1M context) * fix(internal-chat): pin attachments, edit members modal, settings persistence - Skip content validation when pinning/unpinning (fixes pin on file-only messages) - Add EditMembersModal with search, add, and remove members for private channels - Fix settings sidebar always opening: @close now calls handleToggleSettings which updates localStorage, not just sets ref to false Co-Authored-By: Claude Opus 4.6 (1M context) * fix(internal-chat): fix X buttons for attachment remove and reaction unreact Replace Icon component with inline SVG cross for reliable rendering. Both attachment remove and reaction unreact buttons now show a visible X icon at all times with proper vertical alignment. Co-Authored-By: Claude Opus 4.6 (1M context) * fix(internal-chat): allow any user to pin messages, not just sender/admin Co-Authored-By: Claude Opus 4.6 (1M context) * fix(internal-chat): restore Icon component for X buttons (size-4 in size-6 container) SVG inline approach didn't render. Reverted to Icon i-lucide-x with larger sizes (size-4 icon in size-6 button) which renders reliably. Co-Authored-By: Claude Opus 4.6 (1M context) * fix(internal-chat): use p-1 + size-4 pattern for X buttons (matches message toolbar) Co-Authored-By: Claude Opus 4.6 (1M context) * feat(internal-chat): thread indicator on messages from threads, allow pinning all - Show "Thread" badge with icon on messages that have parent_id, clicking it opens the parent thread - Remove parent_id restriction from canPin, any non-deleted message can be pinned Co-Authored-By: Claude Opus 4.6 (1M context) * fix(internal-chat): thread indicator, poll close loop, thread navigation - Hide thread indicator inside thread panel (inThread prop) - Open parent thread when clicking thread badge on messages with parent_id - Fix PollCreator infinite close loop (handleClose no longer calls dialogRef.close, since Dialog already triggered the close) - Look up parent message in store when opening thread from indicator Co-Authored-By: Claude Opus 4.6 (1M context) * fix(internal-chat): poll duration translations and clickable switch labels - Duration options use i18n keys (EN + PT-BR: 24 horas, 7 dias, etc.) - Multiple choice and Public results switches toggle by clicking label Co-Authored-By: Claude Opus 4.6 (1M context) * feat(internal_chat): enhance message handling and search functionality - Added broadcasting of typing off events in InternalChatListener. - Included member user IDs in channel data for better context. - Updated message model to allow optional sender association. - Implemented team mention expansion in MentionService to include team members. - Enhanced message creation service to store mentioned user IDs in content attributes. - Introduced a new SearchService for searching channels, DMs, and messages. - Updated API responses to include has_unread_mention flag for channels. - Added tests for user deletion behavior in internal chat, ensuring message preservation and reaction handling. - Improved draft model to allow coexistence of root and thread drafts. - Added unique indexes for drafts to prevent duplicate entries. - Implemented foreign key constraints with appropriate delete behaviors for internal chat models. * feat(internal-chat): swagger docs, webhook events, search UX improvements Add Swagger documentation for drafts, polls, and search endpoints. Wire internal_chat_message_deleted and internal_chat_channel_updated webhook events to the UI and listener. Improve search empty state with min-chars hint and friendly no-results message. Update CLAUDE.md to include pt_BR translations. Co-Authored-By: Claude Opus 4.6 (1M context) * feat(internal-chat): add draft count display in channel sidebar * chore: remove playwright config and dependencies * feat(internal-chat): polish UX, swagger updates, and migration consolidation - Editor toolbar shortcuts (@ and #) with instant popover trigger, including accent-insensitive matching and wider conversation popover - Localized last activity time and inbox name on conversation preview cards - Thread + main interplay: also-send-in-channel mirror, parent_id filter, per-message conversation link, hidden buttons inside thread view, reactions update across both lists, scroll-to-message behavior - Search service uses f_unaccent for messages, channel names and user names via dedicated GIN trigram functional indexes - Renamed InternalChat::ProGating to InternalChat::Limits with neutral semantics - Consolidated 17 internal chat migrations into 3 (tables, default channels, unaccent search) and added a rake task to ensure the f_unaccent function exists before db:schema:load - Swagger paths and definitions updated to match the current state of the feature (also_send_in_channel, status codes, pro-required responses, hidden member flag, search meta fields, etc.) * fix(internal-chat): use Rake task augmentation for db:schema:load hook The previous `Rake::Task['db:schema:load'].enhance(...)` guarded by `task_defined?` silently no-op'd in CI when the rake file loaded before ActiveRecord's rake_tasks block ran. Re-opening `db:schema:load` via Rake's `task name => deps` DSL augments the existing task regardless of load order, ensuring the f_unaccent function is created before schema.rb references it. * fix(internal-chat): enhance db:schema:load from Rakefile after load_tasks Adding the prereq inside lib/tasks/internal_chat_search.rake (via either `Rake::Task#enhance` or task DSL augmentation) was being silently dropped in CI, presumably due to load order between application rake files and ActiveRecord's `rake_tasks` block. Moving the `enhance` to the Rakefile itself, after `Rails.application.load_tasks`, guarantees both `db:schema:load` and `db:internal_chat:ensure_search_functions` are defined before the prereq is added. Also leaves a debug `puts` in the task body so future regressions are visible from CI logs. * chore(internal-chat): add diagnostic logging to f_unaccent hook * fix(internal-chat): install f_unaccent on all envs iterated by db:schema:load Rails' `db:schema:load` in development env iterates over BOTH the development and test databases (see `ActiveRecord::Tasks::DatabaseTasks.each_current_environment`), but our hook was only installing the function on the currently-connected database. CI defaults to development env (no `RAILS_ENV` set), so the function landed on `chatwoot_dev` while `chatwoot_test` remained without it, causing the schema load to fail when creating the functional indexes against the test DB. The hook now mirrors the same iteration logic and installs the function on every relevant config, restoring the original AR connection afterwards. * fix(internal-chat): align listener spec with current broadcast payload - internal_chat_message_created now emits two broadcasts (the message itself plus an automatic typing_off), so the spec switches to `allow`/`have_received` to assert the message broadcast without caring about the additional typing_off call. - internal_chat_reaction_created payload uses `message_id`, not `internal_chat_message_id`. Update the spec expectation to match. * chore(internal-chat): remove redundant DSL augmentation in rake task * fix(internal-chat): harden gates, kill N+1s and reduce race risk Closes review findings raised on the internal chat PR: - Restrict role mass-assignment in ChannelMembersController so only account administrators can promote new members to channel admin. - Wrap private-channel create/unarchive in a Postgres advisory lock per account so concurrent requests can no longer bypass the freemium limit. - Replace `replies.size` and `votes.size` (per-broadcast queries) with `replies_count` / `votes_count` counter caches. - Make `update_channel_activity` an atomic compare-and-set update so concurrent message creates can never regress `last_activity_at`. - Optimize `Poll#total_votes_count` to use the cached column and eager- loaded options instead of a per-poll `votes.count` query. - Add `internal_chat_messages.account_id` foreign key (`ON DELETE CASCADE`) to prevent orphan rows. - Escape HTML in `ChannelSidebar.highlightMatch` to close a v-html XSS via incomplete tags in message search snippets. - Cleanup `typingOffTimer` on `ChannelView` unmount. - Add stable sort to `getChannelsByCategory` (alphabetical) and `getDMChannels` (last activity) to prevent UI reorder thrash. - Localize `PollDisplay` time-remaining strings (en + pt-BR). - Add specs covering the 90-day search history filter and the search controller endpoint, plus regenerate the consolidated migration with the new columns and FK. * docs(swagger): note role mass-assignment restriction on channel members Document that the `role` field on the channel member create payload is silently coerced to `member` for callers that are not account administrators, matching the controller behavior introduced in the previous commit. --------- Co-authored-by: Claude Opus 4.6 (1M context) --- AGENTS.md | 4 +- Rakefile | 9 + .../accounts/internal_chat/base_controller.rb | 19 + .../internal_chat/categories_controller.rb | 49 + .../channel_members_controller.rb | 107 + .../internal_chat/channels_controller.rb | 495 +++ .../internal_chat/drafts_controller.rb | 55 + .../internal_chat/messages_controller.rb | 191 + .../internal_chat/polls_controller.rb | 177 + .../internal_chat/reactions_controller.rb | 54 + .../internal_chat/search_controller.rb | 19 + app/dispatchers/async_dispatcher.rb | 1 + .../dashboard/api/internalChatChannels.js | 72 + .../dashboard/api/internalChatDrafts.js | 24 + .../dashboard/api/internalChatMessages.js | 62 + .../dashboard/api/internalChatPolls.js | 24 + .../components-next/dialog/Dialog.vue | 17 +- .../components-next/sidebar/Sidebar.vue | 17 + .../components/widgets/WootWriter/Editor.vue | 58 +- .../widgets/conversation/TagAgents.vue | 5 + .../widgets/conversation/TagConversations.vue | 159 + .../specs/TagConversations.spec.js | 125 + .../composables/useInternalChatPro.js | 26 + app/javascript/dashboard/featureFlags.js | 1 + .../dashboard/helper/actionCable.js | 114 + .../dashboard/i18n/locale/en/index.js | 2 + .../i18n/locale/en/integrations.json | 6 +- .../i18n/locale/en/internalChat.json | 192 + .../dashboard/i18n/locale/en/settings.json | 1 + .../dashboard/i18n/locale/pt_BR/index.js | 2 + .../i18n/locale/pt_BR/integrations.json | 6 +- .../i18n/locale/pt_BR/internalChat.json | 192 + .../dashboard/i18n/locale/pt_BR/settings.json | 1 + .../routes/dashboard/dashboard.routes.js | 2 + .../dashboard/internalChat/ChannelHeader.vue | 172 + .../internalChat/ChannelSettings.vue | 399 ++ .../dashboard/internalChat/ChannelSidebar.vue | 1105 +++++ .../dashboard/internalChat/ChannelView.vue | 682 +++ .../internalChat/ConversationPreviewCard.vue | 243 + .../internalChat/CreateCategoryModal.vue | 65 + .../internalChat/CreateChannelModal.vue | 237 + .../dashboard/internalChat/CreateDMModal.vue | 121 + .../dashboard/internalChat/DraftsList.vue | 175 + .../internalChat/EditMembersModal.vue | 167 + .../internalChat/EmojiReactionPicker.vue | 78 + .../internalChat/InternalChatLayout.vue | 71 + .../dashboard/internalChat/MessageBubble.vue | 467 ++ .../dashboard/internalChat/MessageEditor.vue | 326 ++ .../dashboard/internalChat/MessageList.vue | 445 ++ .../internalChat/MessageSkeleton.vue | 22 + .../dashboard/internalChat/PollCreator.vue | 215 + .../dashboard/internalChat/PollDisplay.vue | 207 + .../internalChat/ProFeatureNudge.vue | 143 + .../internalChat/ReactionDisplay.vue | 117 + .../dashboard/internalChat/ThreadPanel.vue | 387 ++ .../internalChat/TypingIndicator.vue | 44 + .../internalChat/internalChat.routes.js | 64 + .../specs/ConversationPreviewCard.spec.js | 159 + .../integrations/Webhooks/WebhookForm.vue | 4 + app/javascript/dashboard/store/index.js | 4 + .../store/modules/internalChat/actions.js | 213 + .../store/modules/internalChat/drafts.js | 129 + .../store/modules/internalChat/getters.js | 87 + .../store/modules/internalChat/index.js | 33 + .../store/modules/internalChat/messages.js | 460 ++ .../store/modules/internalChat/mutations.js | 84 + .../store/modules/internalChat/polls.js | 123 + .../store/modules/internalChat/search.js | 86 + .../modules/internalChat/typingStatus.js | 56 + .../shared/helpers/markdownIt/link.js | 13 +- .../helpers/specs/MessageFormatter.spec.js | 27 + app/jobs/agents/destroy_job.rb | 9 + .../internal/setup_default_channels_job.rb | 11 + app/listeners/internal_chat_listener.rb | 201 + app/listeners/webhook_listener.rb | 38 + app/models/account.rb | 9 +- app/models/account_user.rb | 10 +- app/models/channel/whatsapp.rb | 2 +- app/models/conversation.rb | 6 - app/models/internal_chat/category.rb | 30 + app/models/internal_chat/channel.rb | 95 + app/models/internal_chat/channel_member.rb | 48 + app/models/internal_chat/channel_team.rb | 28 + app/models/internal_chat/draft.rb | 42 + app/models/internal_chat/limits.rb | 17 + app/models/internal_chat/message.rb | 90 + .../internal_chat/message_attachment.rb | 37 + app/models/internal_chat/poll.rb | 46 + app/models/internal_chat/poll_option.rb | 36 + app/models/internal_chat/poll_vote.rb | 31 + app/models/internal_chat/reaction.rb | 32 + app/models/reporting_events_rollup.rb | 22 +- app/models/super_admin.rb | 1 + app/models/team_member.rb | 12 + app/models/user.rb | 3 + app/models/webhook.rb | 5 +- app/policies/internal_chat/category_policy.rb | 27 + app/policies/internal_chat/channel_policy.rb | 70 + app/policies/internal_chat/message_policy.rb | 54 + app/policies/internal_chat/reaction_policy.rb | 32 + .../default_channel_setup_service.rb | 37 + app/services/internal_chat/mention_service.rb | 55 + .../internal_chat/message_create_service.rb | 87 + .../internal_chat/notification_service.rb | 53 + app/services/internal_chat/poll_service.rb | 45 + app/services/internal_chat/search_service.rb | 139 + .../internal_chat/typing_status_manager.rb | 20 + .../search/conversations.json.jbuilder | 2 +- config/locales/en.yml | 5 + config/locales/pt_BR.yml | 3 + config/routes.rb | 33 + ...60410170001_create_internal_chat_tables.rb | 131 + ...02_setup_internal_chat_default_channels.rb | 13 + ...03_add_unaccent_search_to_internal_chat.rb | 45 + db/schema.rb | 177 +- e2e/helpers/auth.ts | 246 + e2e/internal-chat/channels.spec.ts | 222 + e2e/internal-chat/direct-messages.spec.ts | 123 + e2e/internal-chat/mark-read-unread.spec.ts | 136 + e2e/internal-chat/messaging.spec.ts | 235 + e2e/internal-chat/navigation.spec.ts | 110 + e2e/internal-chat/polls.spec.ts | 174 + e2e/internal-chat/reactions.spec.ts | 153 + e2e/internal-chat/threads.spec.ts | 168 + lib/events/types.rb | 11 + lib/tasks/internal_chat_search.rake | 44 + .../categories_controller_spec.rb | 191 + .../channel_members_controller_spec.rb | 233 + .../internal_chat/channels_controller_spec.rb | 634 +++ .../internal_chat/drafts_controller_spec.rb | 198 + .../internal_chat/messages_controller_spec.rb | 445 ++ .../internal_chat/polls_controller_spec.rb | 304 ++ .../reactions_controller_spec.rb | 127 + .../internal_chat/search_controller_spec.rb | 84 + spec/factories/internal_chat/categories.rb | 9 + .../internal_chat/channel_members.rb | 40 + spec/factories/internal_chat/channels.rb | 31 + spec/factories/internal_chat/drafts.rb | 10 + .../internal_chat/message_attachments.rb | 22 + spec/factories/internal_chat/messages.rb | 23 + spec/factories/internal_chat/poll_options.rb | 9 + spec/factories/internal_chat/poll_votes.rb | 8 + spec/factories/internal_chat/polls.rb | 12 + spec/factories/internal_chat/reactions.rb | 9 + spec/jobs/agents/destroy_job_spec.rb | 26 + spec/listeners/internal_chat_listener_spec.rb | 224 + spec/models/internal_chat/category_spec.rb | 44 + .../internal_chat/channel_member_spec.rb | 95 + spec/models/internal_chat/channel_spec.rb | 126 + spec/models/internal_chat/draft_spec.rb | 93 + spec/models/internal_chat/limits_spec.rb | 29 + .../internal_chat/message_attachment_spec.rb | 42 + spec/models/internal_chat/message_spec.rb | 130 + spec/models/internal_chat/poll_option_spec.rb | 67 + spec/models/internal_chat/poll_spec.rb | 84 + spec/models/internal_chat/poll_vote_spec.rb | 16 + spec/models/internal_chat/reaction_spec.rb | 53 + .../internal_chat/user_deletion_spec.rb | 81 + .../internal_chat/category_policy_spec.rb | 69 + .../internal_chat/channel_policy_spec.rb | 197 + .../internal_chat/message_policy_spec.rb | 114 + .../internal_chat/reaction_policy_spec.rb | 84 + .../default_channel_setup_service_spec.rb | 54 + .../internal_chat/mention_service_spec.rb | 254 ++ .../message_create_service_spec.rb | 101 + .../notification_service_spec.rb | 174 + .../internal_chat/poll_service_spec.rb | 114 + .../internal_chat/search_service_spec.rb | 226 + .../typing_status_manager_spec.rb | 41 + swagger/definitions/index.yml | 38 + .../category_create_update_payload.yml | 8 + .../internal_chat/channel_create_payload.yml | 46 + .../internal_chat/channel_update_payload.yml | 14 + .../internal_chat/draft_save_payload.yml | 10 + .../internal_chat/member_create_payload.yml | 19 + .../internal_chat/message_create_payload.yml | 35 + .../internal_chat/poll_create_payload.yml | 45 + .../resource/internal_chat/category.yml | 25 + .../resource/internal_chat/channel.yml | 50 + .../resource/internal_chat/channel_index.yml | 40 + .../resource/internal_chat/channel_member.yml | 50 + .../resource/internal_chat/channel_show.yml | 34 + .../resource/internal_chat/draft.yml | 24 + .../resource/internal_chat/message.yml | 90 + .../resource/internal_chat/poll.yml | 78 + .../resource/internal_chat/reaction.yml | 27 + .../resource/internal_chat/search_result.yml | 108 + swagger/index.yml | 24 + swagger/parameters/index.yml | 21 + .../parameters/internal_chat_category_id.yml | 6 + .../parameters/internal_chat_channel_id.yml | 6 + .../internal_chat_channel_id_path.yml | 6 + .../parameters/internal_chat_member_id.yml | 6 + .../parameters/internal_chat_message_id.yml | 6 + swagger/parameters/internal_chat_poll_id.yml | 6 + .../parameters/internal_chat_reaction_id.yml | 6 + .../internal_chat/categories/create.yml | 32 + .../internal_chat/categories/delete.yml | 28 + .../internal_chat/categories/index.yml | 23 + .../internal_chat/categories/update.yml | 38 + .../internal_chat/channel_members/create.yml | 41 + .../internal_chat/channel_members/delete.yml | 28 + .../internal_chat/channel_members/index.yml | 29 + .../internal_chat/channel_members/update.yml | 48 + .../internal_chat/channels/archive.yml | 38 + .../internal_chat/channels/create.yml | 38 + .../internal_chat/channels/delete.yml | 28 + .../internal_chat/channels/index.yml | 45 + .../internal_chat/channels/mark_read.yml | 22 + .../internal_chat/channels/mark_unread.yml | 31 + .../internal_chat/channels/show.yml | 26 + .../channels/toggle_typing_status.yml | 34 + .../internal_chat/channels/unarchive.yml | 38 + .../internal_chat/channels/update.yml | 38 + .../internal_chat/drafts/delete.yml | 28 + .../internal_chat/drafts/index.yml | 23 + .../internal_chat/drafts/update.yml | 32 + .../internal_chat/messages/create.yml | 32 + .../internal_chat/messages/delete.yml | 28 + .../internal_chat/messages/index.yml | 56 + .../internal_chat/messages/pin.yml | 32 + .../internal_chat/messages/thread.yml | 34 + .../internal_chat/messages/unpin.yml | 32 + .../internal_chat/messages/update.yml | 44 + .../internal_chat/polls/create.yml | 44 + .../internal_chat/polls/unvote.yml | 38 + .../application/internal_chat/polls/vote.yml | 44 + .../internal_chat/reactions/create.yml | 38 + .../internal_chat/reactions/delete.yml | 28 + .../application/internal_chat/search/show.yml | 33 + swagger/paths/index.yml | 201 + swagger/swagger.json | 3943 ++++++++++++++++- swagger/tag_groups/application_swagger.json | 3943 ++++++++++++++++- swagger/tag_groups/client_swagger.json | 1670 ++++++- swagger/tag_groups/other_swagger.json | 1670 ++++++- swagger/tag_groups/platform_swagger.json | 1670 ++++++- 236 files changed, 33208 insertions(+), 44 deletions(-) create mode 100644 app/controllers/api/v1/accounts/internal_chat/base_controller.rb create mode 100644 app/controllers/api/v1/accounts/internal_chat/categories_controller.rb create mode 100644 app/controllers/api/v1/accounts/internal_chat/channel_members_controller.rb create mode 100644 app/controllers/api/v1/accounts/internal_chat/channels_controller.rb create mode 100644 app/controllers/api/v1/accounts/internal_chat/drafts_controller.rb create mode 100644 app/controllers/api/v1/accounts/internal_chat/messages_controller.rb create mode 100644 app/controllers/api/v1/accounts/internal_chat/polls_controller.rb create mode 100644 app/controllers/api/v1/accounts/internal_chat/reactions_controller.rb create mode 100644 app/controllers/api/v1/accounts/internal_chat/search_controller.rb create mode 100644 app/javascript/dashboard/api/internalChatChannels.js create mode 100644 app/javascript/dashboard/api/internalChatDrafts.js create mode 100644 app/javascript/dashboard/api/internalChatMessages.js create mode 100644 app/javascript/dashboard/api/internalChatPolls.js create mode 100644 app/javascript/dashboard/components/widgets/conversation/TagConversations.vue create mode 100644 app/javascript/dashboard/components/widgets/conversation/specs/TagConversations.spec.js create mode 100644 app/javascript/dashboard/composables/useInternalChatPro.js create mode 100644 app/javascript/dashboard/i18n/locale/en/internalChat.json create mode 100644 app/javascript/dashboard/i18n/locale/pt_BR/internalChat.json create mode 100644 app/javascript/dashboard/routes/dashboard/internalChat/ChannelHeader.vue create mode 100644 app/javascript/dashboard/routes/dashboard/internalChat/ChannelSettings.vue create mode 100644 app/javascript/dashboard/routes/dashboard/internalChat/ChannelSidebar.vue create mode 100644 app/javascript/dashboard/routes/dashboard/internalChat/ChannelView.vue create mode 100644 app/javascript/dashboard/routes/dashboard/internalChat/ConversationPreviewCard.vue create mode 100644 app/javascript/dashboard/routes/dashboard/internalChat/CreateCategoryModal.vue create mode 100644 app/javascript/dashboard/routes/dashboard/internalChat/CreateChannelModal.vue create mode 100644 app/javascript/dashboard/routes/dashboard/internalChat/CreateDMModal.vue create mode 100644 app/javascript/dashboard/routes/dashboard/internalChat/DraftsList.vue create mode 100644 app/javascript/dashboard/routes/dashboard/internalChat/EditMembersModal.vue create mode 100644 app/javascript/dashboard/routes/dashboard/internalChat/EmojiReactionPicker.vue create mode 100644 app/javascript/dashboard/routes/dashboard/internalChat/InternalChatLayout.vue create mode 100644 app/javascript/dashboard/routes/dashboard/internalChat/MessageBubble.vue create mode 100644 app/javascript/dashboard/routes/dashboard/internalChat/MessageEditor.vue create mode 100644 app/javascript/dashboard/routes/dashboard/internalChat/MessageList.vue create mode 100644 app/javascript/dashboard/routes/dashboard/internalChat/MessageSkeleton.vue create mode 100644 app/javascript/dashboard/routes/dashboard/internalChat/PollCreator.vue create mode 100644 app/javascript/dashboard/routes/dashboard/internalChat/PollDisplay.vue create mode 100644 app/javascript/dashboard/routes/dashboard/internalChat/ProFeatureNudge.vue create mode 100644 app/javascript/dashboard/routes/dashboard/internalChat/ReactionDisplay.vue create mode 100644 app/javascript/dashboard/routes/dashboard/internalChat/ThreadPanel.vue create mode 100644 app/javascript/dashboard/routes/dashboard/internalChat/TypingIndicator.vue create mode 100644 app/javascript/dashboard/routes/dashboard/internalChat/internalChat.routes.js create mode 100644 app/javascript/dashboard/routes/dashboard/internalChat/specs/ConversationPreviewCard.spec.js create mode 100644 app/javascript/dashboard/store/modules/internalChat/actions.js create mode 100644 app/javascript/dashboard/store/modules/internalChat/drafts.js create mode 100644 app/javascript/dashboard/store/modules/internalChat/getters.js create mode 100644 app/javascript/dashboard/store/modules/internalChat/index.js create mode 100644 app/javascript/dashboard/store/modules/internalChat/messages.js create mode 100644 app/javascript/dashboard/store/modules/internalChat/mutations.js create mode 100644 app/javascript/dashboard/store/modules/internalChat/polls.js create mode 100644 app/javascript/dashboard/store/modules/internalChat/search.js create mode 100644 app/javascript/dashboard/store/modules/internalChat/typingStatus.js create mode 100644 app/jobs/internal/setup_default_channels_job.rb create mode 100644 app/listeners/internal_chat_listener.rb create mode 100644 app/models/internal_chat/category.rb create mode 100644 app/models/internal_chat/channel.rb create mode 100644 app/models/internal_chat/channel_member.rb create mode 100644 app/models/internal_chat/channel_team.rb create mode 100644 app/models/internal_chat/draft.rb create mode 100644 app/models/internal_chat/limits.rb create mode 100644 app/models/internal_chat/message.rb create mode 100644 app/models/internal_chat/message_attachment.rb create mode 100644 app/models/internal_chat/poll.rb create mode 100644 app/models/internal_chat/poll_option.rb create mode 100644 app/models/internal_chat/poll_vote.rb create mode 100644 app/models/internal_chat/reaction.rb create mode 100644 app/policies/internal_chat/category_policy.rb create mode 100644 app/policies/internal_chat/channel_policy.rb create mode 100644 app/policies/internal_chat/message_policy.rb create mode 100644 app/policies/internal_chat/reaction_policy.rb create mode 100644 app/services/internal_chat/default_channel_setup_service.rb create mode 100644 app/services/internal_chat/mention_service.rb create mode 100644 app/services/internal_chat/message_create_service.rb create mode 100644 app/services/internal_chat/notification_service.rb create mode 100644 app/services/internal_chat/poll_service.rb create mode 100644 app/services/internal_chat/search_service.rb create mode 100644 app/services/internal_chat/typing_status_manager.rb create mode 100644 db/migrate/20260410170001_create_internal_chat_tables.rb create mode 100644 db/migrate/20260410170002_setup_internal_chat_default_channels.rb create mode 100644 db/migrate/20260410170003_add_unaccent_search_to_internal_chat.rb create mode 100644 e2e/helpers/auth.ts create mode 100644 e2e/internal-chat/channels.spec.ts create mode 100644 e2e/internal-chat/direct-messages.spec.ts create mode 100644 e2e/internal-chat/mark-read-unread.spec.ts create mode 100644 e2e/internal-chat/messaging.spec.ts create mode 100644 e2e/internal-chat/navigation.spec.ts create mode 100644 e2e/internal-chat/polls.spec.ts create mode 100644 e2e/internal-chat/reactions.spec.ts create mode 100644 e2e/internal-chat/threads.spec.ts create mode 100644 lib/tasks/internal_chat_search.rake create mode 100644 spec/controllers/api/v1/accounts/internal_chat/categories_controller_spec.rb create mode 100644 spec/controllers/api/v1/accounts/internal_chat/channel_members_controller_spec.rb create mode 100644 spec/controllers/api/v1/accounts/internal_chat/channels_controller_spec.rb create mode 100644 spec/controllers/api/v1/accounts/internal_chat/drafts_controller_spec.rb create mode 100644 spec/controllers/api/v1/accounts/internal_chat/messages_controller_spec.rb create mode 100644 spec/controllers/api/v1/accounts/internal_chat/polls_controller_spec.rb create mode 100644 spec/controllers/api/v1/accounts/internal_chat/reactions_controller_spec.rb create mode 100644 spec/controllers/api/v1/accounts/internal_chat/search_controller_spec.rb create mode 100644 spec/factories/internal_chat/categories.rb create mode 100644 spec/factories/internal_chat/channel_members.rb create mode 100644 spec/factories/internal_chat/channels.rb create mode 100644 spec/factories/internal_chat/drafts.rb create mode 100644 spec/factories/internal_chat/message_attachments.rb create mode 100644 spec/factories/internal_chat/messages.rb create mode 100644 spec/factories/internal_chat/poll_options.rb create mode 100644 spec/factories/internal_chat/poll_votes.rb create mode 100644 spec/factories/internal_chat/polls.rb create mode 100644 spec/factories/internal_chat/reactions.rb create mode 100644 spec/listeners/internal_chat_listener_spec.rb create mode 100644 spec/models/internal_chat/category_spec.rb create mode 100644 spec/models/internal_chat/channel_member_spec.rb create mode 100644 spec/models/internal_chat/channel_spec.rb create mode 100644 spec/models/internal_chat/draft_spec.rb create mode 100644 spec/models/internal_chat/limits_spec.rb create mode 100644 spec/models/internal_chat/message_attachment_spec.rb create mode 100644 spec/models/internal_chat/message_spec.rb create mode 100644 spec/models/internal_chat/poll_option_spec.rb create mode 100644 spec/models/internal_chat/poll_spec.rb create mode 100644 spec/models/internal_chat/poll_vote_spec.rb create mode 100644 spec/models/internal_chat/reaction_spec.rb create mode 100644 spec/models/internal_chat/user_deletion_spec.rb create mode 100644 spec/policies/internal_chat/category_policy_spec.rb create mode 100644 spec/policies/internal_chat/channel_policy_spec.rb create mode 100644 spec/policies/internal_chat/message_policy_spec.rb create mode 100644 spec/policies/internal_chat/reaction_policy_spec.rb create mode 100644 spec/services/internal_chat/default_channel_setup_service_spec.rb create mode 100644 spec/services/internal_chat/mention_service_spec.rb create mode 100644 spec/services/internal_chat/message_create_service_spec.rb create mode 100644 spec/services/internal_chat/notification_service_spec.rb create mode 100644 spec/services/internal_chat/poll_service_spec.rb create mode 100644 spec/services/internal_chat/search_service_spec.rb create mode 100644 spec/services/internal_chat/typing_status_manager_spec.rb create mode 100644 swagger/definitions/request/internal_chat/category_create_update_payload.yml create mode 100644 swagger/definitions/request/internal_chat/channel_create_payload.yml create mode 100644 swagger/definitions/request/internal_chat/channel_update_payload.yml create mode 100644 swagger/definitions/request/internal_chat/draft_save_payload.yml create mode 100644 swagger/definitions/request/internal_chat/member_create_payload.yml create mode 100644 swagger/definitions/request/internal_chat/message_create_payload.yml create mode 100644 swagger/definitions/request/internal_chat/poll_create_payload.yml create mode 100644 swagger/definitions/resource/internal_chat/category.yml create mode 100644 swagger/definitions/resource/internal_chat/channel.yml create mode 100644 swagger/definitions/resource/internal_chat/channel_index.yml create mode 100644 swagger/definitions/resource/internal_chat/channel_member.yml create mode 100644 swagger/definitions/resource/internal_chat/channel_show.yml create mode 100644 swagger/definitions/resource/internal_chat/draft.yml create mode 100644 swagger/definitions/resource/internal_chat/message.yml create mode 100644 swagger/definitions/resource/internal_chat/poll.yml create mode 100644 swagger/definitions/resource/internal_chat/reaction.yml create mode 100644 swagger/definitions/resource/internal_chat/search_result.yml create mode 100644 swagger/parameters/internal_chat_category_id.yml create mode 100644 swagger/parameters/internal_chat_channel_id.yml create mode 100644 swagger/parameters/internal_chat_channel_id_path.yml create mode 100644 swagger/parameters/internal_chat_member_id.yml create mode 100644 swagger/parameters/internal_chat_message_id.yml create mode 100644 swagger/parameters/internal_chat_poll_id.yml create mode 100644 swagger/parameters/internal_chat_reaction_id.yml create mode 100644 swagger/paths/application/internal_chat/categories/create.yml create mode 100644 swagger/paths/application/internal_chat/categories/delete.yml create mode 100644 swagger/paths/application/internal_chat/categories/index.yml create mode 100644 swagger/paths/application/internal_chat/categories/update.yml create mode 100644 swagger/paths/application/internal_chat/channel_members/create.yml create mode 100644 swagger/paths/application/internal_chat/channel_members/delete.yml create mode 100644 swagger/paths/application/internal_chat/channel_members/index.yml create mode 100644 swagger/paths/application/internal_chat/channel_members/update.yml create mode 100644 swagger/paths/application/internal_chat/channels/archive.yml create mode 100644 swagger/paths/application/internal_chat/channels/create.yml create mode 100644 swagger/paths/application/internal_chat/channels/delete.yml create mode 100644 swagger/paths/application/internal_chat/channels/index.yml create mode 100644 swagger/paths/application/internal_chat/channels/mark_read.yml create mode 100644 swagger/paths/application/internal_chat/channels/mark_unread.yml create mode 100644 swagger/paths/application/internal_chat/channels/show.yml create mode 100644 swagger/paths/application/internal_chat/channels/toggle_typing_status.yml create mode 100644 swagger/paths/application/internal_chat/channels/unarchive.yml create mode 100644 swagger/paths/application/internal_chat/channels/update.yml create mode 100644 swagger/paths/application/internal_chat/drafts/delete.yml create mode 100644 swagger/paths/application/internal_chat/drafts/index.yml create mode 100644 swagger/paths/application/internal_chat/drafts/update.yml create mode 100644 swagger/paths/application/internal_chat/messages/create.yml create mode 100644 swagger/paths/application/internal_chat/messages/delete.yml create mode 100644 swagger/paths/application/internal_chat/messages/index.yml create mode 100644 swagger/paths/application/internal_chat/messages/pin.yml create mode 100644 swagger/paths/application/internal_chat/messages/thread.yml create mode 100644 swagger/paths/application/internal_chat/messages/unpin.yml create mode 100644 swagger/paths/application/internal_chat/messages/update.yml create mode 100644 swagger/paths/application/internal_chat/polls/create.yml create mode 100644 swagger/paths/application/internal_chat/polls/unvote.yml create mode 100644 swagger/paths/application/internal_chat/polls/vote.yml create mode 100644 swagger/paths/application/internal_chat/reactions/create.yml create mode 100644 swagger/paths/application/internal_chat/reactions/delete.yml create mode 100644 swagger/paths/application/internal_chat/search/show.yml diff --git a/AGENTS.md b/AGENTS.md index 3db1c34a0..1a570ea73 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -79,9 +79,9 @@ ## Project-Specific - **Translations**: - - Only update `en.yml` and `en.json` + - Update `en.yml`/`en.json` and `pt_BR.yml`/`pt_BR.json` - Other languages are handled by the community - - Backend i18n → `en.yml`, Frontend i18n → `en.json` + - Backend i18n → `.yml`, Frontend i18n → `.json` - **Frontend**: - Use `components-next/` for message bubbles (the rest is being deprecated) diff --git a/Rakefile b/Rakefile index 2e996417e..99dd20aa6 100644 --- a/Rakefile +++ b/Rakefile @@ -7,3 +7,12 @@ enterprise_tasks_path = Rails.root.join('enterprise/tasks_railtie.rb').to_s require enterprise_tasks_path if File.exist?(enterprise_tasks_path) Rails.application.load_tasks + +# Ensure the f_unaccent function used by internal chat search indexes is created +# before db:schema:load runs. This must happen after Rails.application.load_tasks +# so that both `db:schema:load` and `db:internal_chat:ensure_search_functions` +# are guaranteed to be defined. +if Rake::Task.task_defined?('db:schema:load') && + Rake::Task.task_defined?('db:internal_chat:ensure_search_functions') + Rake::Task['db:schema:load'].enhance(['db:internal_chat:ensure_search_functions']) +end diff --git a/app/controllers/api/v1/accounts/internal_chat/base_controller.rb b/app/controllers/api/v1/accounts/internal_chat/base_controller.rb new file mode 100644 index 000000000..f8f928bb8 --- /dev/null +++ b/app/controllers/api/v1/accounts/internal_chat/base_controller.rb @@ -0,0 +1,19 @@ +class Api::V1::Accounts::InternalChat::BaseController < Api::V1::Accounts::BaseController + private + + def current_channel + @current_channel ||= Current.account.internal_chat_channels.find(params[:channel_id] || params[:id]) + end + + def current_membership + @current_membership ||= current_channel.channel_members.find_by(user_id: Current.user.id) + end + + def channel_member? + current_channel.channel_type_public_channel? || current_membership.present? + end + + def render_pro_required(feature) + render json: { error: 'pro_feature_required', feature: feature }, status: :payment_required + end +end diff --git a/app/controllers/api/v1/accounts/internal_chat/categories_controller.rb b/app/controllers/api/v1/accounts/internal_chat/categories_controller.rb new file mode 100644 index 000000000..96619cb38 --- /dev/null +++ b/app/controllers/api/v1/accounts/internal_chat/categories_controller.rb @@ -0,0 +1,49 @@ +class Api::V1::Accounts::InternalChat::CategoriesController < Api::V1::Accounts::InternalChat::BaseController + before_action :fetch_category, only: [:update, :destroy] + + def index + authorize InternalChat::Category, :index? + @categories = Current.account.internal_chat_categories.ordered.includes(:channels) + render json: @categories.map { |category| category_response(category) } + end + + def create + authorize InternalChat::Category, :create? + @category = Current.account.internal_chat_categories.create!(category_params) + render json: category_response(@category), status: :created + end + + def update + authorize @category, :update? + @category.update!(category_params) + render json: category_response(@category) + end + + def destroy + authorize @category, :destroy? + @category.destroy! + head :ok + end + + private + + def fetch_category + @category = Current.account.internal_chat_categories.find(params[:id]) + end + + def category_params + params.require(:category).permit(:name, :position) + end + + def category_response(category) + { + id: category.id, + name: category.name, + position: category.position, + account_id: category.account_id, + channels_count: category.channels.size, + created_at: category.created_at, + updated_at: category.updated_at + } + end +end diff --git a/app/controllers/api/v1/accounts/internal_chat/channel_members_controller.rb b/app/controllers/api/v1/accounts/internal_chat/channel_members_controller.rb new file mode 100644 index 000000000..7401454f3 --- /dev/null +++ b/app/controllers/api/v1/accounts/internal_chat/channel_members_controller.rb @@ -0,0 +1,107 @@ +class Api::V1::Accounts::InternalChat::ChannelMembersController < Api::V1::Accounts::InternalChat::BaseController + include Events::Types + + before_action :current_channel + before_action :fetch_member, only: [:update, :destroy] + + def index + authorize current_channel, :show?, policy_class: InternalChat::ChannelPolicy + @members = current_channel.channel_members.includes(user: :account_users) + render json: @members.map { |member| member_response(member) } + end + + def create + authorize current_channel, :update?, policy_class: InternalChat::ChannelPolicy + members = create_channel_members(validated_user_ids, requested_role) + dispatch_member_update + render json: members.map { |member| member_response(member) }, status: :created + end + + def update + authorize_member_update! + @member.update!(member_update_params) + render json: member_response(@member) + end + + def destroy + authorize_member_destroy! + removed_user = @member.user + @member.destroy! + dispatch_member_update(removed_user: removed_user) + head :ok + end + + private + + def validated_user_ids + user_ids = Array(params[:user_ids] || [params[:user_id]]).compact.map(&:to_i) + valid_user_ids = Current.account.users.where(id: user_ids).pluck(:id) + raise ActionController::BadRequest, 'No valid user IDs provided' if valid_user_ids.empty? + + valid_user_ids + end + + def create_channel_members(user_ids, role) + ActiveRecord::Base.transaction do + user_ids.map do |user_id| + current_channel.channel_members.find_or_create_by!(user_id: user_id) do |m| + m.role = role + end + end + end + end + + # Only account administrators can promote a new member to channel admin via params. + # Channel admins (without account-admin) always create plain members. + def requested_role + return :member unless Current.account_user&.administrator? + return :member if params[:role].blank? + + InternalChat::ChannelMember.roles.key?(params[:role].to_s) ? params[:role] : :member + end + + def fetch_member + @member = current_channel.channel_members.find(params[:id]) + end + + def authorize_member_update! + raise Pundit::NotAuthorizedError unless @member.user_id == Current.user.id || Current.account_user&.administrator? + end + + def authorize_member_destroy! + raise Pundit::NotAuthorizedError unless @member.user_id == Current.user.id || Current.account_user&.administrator? + end + + def dispatch_member_update(removed_user: nil) + # Capture tokens before the broadcast so the removed user also receives the event + tokens = current_channel.members.pluck(:pubsub_token) + tokens << removed_user.pubsub_token if removed_user.present? + + Rails.configuration.dispatcher.dispatch( + INTERNAL_CHAT_CHANNEL_UPDATED, + Time.zone.now, + channel: current_channel, + member_tokens: tokens.uniq + ) + end + + def member_update_params + params.permit(:muted, :favorited, :hidden) + end + + def member_response(member) + { + id: member.id, + user_id: member.user_id, + role: member.role, + muted: member.muted, + favorited: member.favorited, + last_read_at: member.last_read_at, + name: member.user.name, + avatar_url: member.user.avatar_url, + availability_status: member.user.availability_status, + created_at: member.created_at, + updated_at: member.updated_at + } + end +end diff --git a/app/controllers/api/v1/accounts/internal_chat/channels_controller.rb b/app/controllers/api/v1/accounts/internal_chat/channels_controller.rb new file mode 100644 index 000000000..2deddaba7 --- /dev/null +++ b/app/controllers/api/v1/accounts/internal_chat/channels_controller.rb @@ -0,0 +1,495 @@ +class Api::V1::Accounts::InternalChat::ChannelsController < Api::V1::Accounts::InternalChat::BaseController # rubocop:disable Metrics/ClassLength + include Events::Types + + before_action :current_channel, only: [:show, :update, :destroy, :archive, :unarchive, :toggle_typing_status, :mark_read, :mark_unread] + + RECENT_MESSAGES_LIMIT = 20 + # Arbitrary 32-bit namespace for the private-channel limit advisory lock; paired with account id. + PRIVATE_CHANNEL_LOCK_KEY = 0x49434C4D # 'ICLM' + + def index + authorize InternalChat::Channel, :index? + @channels = filtered_channels + @unread_counts = compute_unread_counts(@channels) + @mention_channel_ids = compute_mention_channel_ids(@channels) + render json: @channels.map { |channel| channel_index_response(channel) } + end + + def show + authorize @current_channel, :show? + render json: channel_show_response(@current_channel) + end + + def create + @channel = build_channel + authorize @channel, :create? + created = @channel.new_record? + + if dm_params? && created + create_dm_with_lock + else + with_private_channel_limit_lock(@channel) do + return if enforce_private_channel_limit(@channel) + + ActiveRecord::Base.transaction do + @channel.save! + add_creator_as_admin + add_initial_members + add_channel_type_members + end + end + end + + dispatch_channel_event(@channel) if created + render json: channel_show_response(@channel), status: :created + end + + def update + authorize @current_channel, :update? + attrs = update_channel_params + validate_category!(attrs[:category_id]) + @current_channel.update!(attrs) + dispatch_channel_event(@current_channel) + render json: channel_show_response(@current_channel) + end + + def destroy + authorize @current_channel, :destroy? + # Capture member tokens before destroying so the listener can broadcast to them + cached_tokens = channel_member_tokens(@current_channel) + @current_channel.destroy! + Rails.configuration.dispatcher.dispatch(INTERNAL_CHAT_CHANNEL_UPDATED, Time.zone.now, channel: @current_channel, + member_tokens: cached_tokens) + head :ok + end + + def archive + authorize @current_channel, :archive? + head(:unprocessable_entity) and return if @current_channel.channel_type_dm? + + @current_channel.archived! + dispatch_channel_event(@current_channel) + render json: channel_show_response(@current_channel) + end + + def unarchive + authorize @current_channel, :unarchive? + + with_private_channel_limit_lock(@current_channel) do + return if enforce_private_channel_limit(@current_channel) + + @current_channel.active! + end + + dispatch_channel_event(@current_channel) + render json: channel_show_response(@current_channel) + end + + def toggle_typing_status + authorize @current_channel, :toggle_typing_status? + InternalChat::TypingStatusManager.new( + channel: @current_channel, user: Current.user, params: { typing_status: typing_status_param } + ).perform + head :ok + end + + def mark_read + authorize @current_channel, :mark_read? + membership = @current_channel.channel_members.find_by(user_id: Current.user.id) + membership&.update!(last_read_at: Time.current) + head :ok + end + + def mark_unread + authorize @current_channel, :mark_unread? + msg_id = mark_unread_params[:message_id] + return head(:ok) if msg_id.blank? + + membership = @current_channel.channel_members.find_by!(user_id: Current.user.id) + message = @current_channel.messages.find(msg_id) + membership.update!(last_read_at: message.created_at - 1.second) + head :ok + end + + private + + def enforce_private_channel_limit(channel) + return unless channel.channel_type_private_channel? + + max = InternalChat::Limits.max_private_channels + return if max.blank? + + count = Current.account.internal_chat_channels.where(channel_type: :private_channel).active.count + render_pro_required('private_channels') if count >= max + end + + # Postgres advisory transaction lock keyed by account so concurrent create/unarchive + # cannot bypass the private-channel limit by racing between count and save. + def with_private_channel_limit_lock(channel) + return yield unless channel.channel_type_private_channel? && InternalChat::Limits.max_private_channels.present? + + ActiveRecord::Base.transaction do + ActiveRecord::Base.connection.execute( + ActiveRecord::Base.sanitize_sql_array(['SELECT pg_advisory_xact_lock(?, ?)', PRIVATE_CHANNEL_LOCK_KEY, Current.account.id]) + ) + yield + end + end + + def filtered_channels + channels = Current.account.internal_chat_channels.includes(channel_members: { user: :account_users }, category: []) + channels = apply_type_filter(channels) + channels = apply_category_filter(channels) + channels = apply_status_filter(channels) + channels = apply_visibility_filter(channels) + channels.order(last_activity_at: :desc) + end + + def apply_type_filter(channels) + case params[:type] + when 'text_channels' + channels.text_channels + when 'direct_messages' + channels.direct_messages + else + channels + end + end + + def apply_category_filter(channels) + return channels if params[:category_id].blank? + + channels.where(category_id: params[:category_id]) + end + + def apply_status_filter(channels) + case params[:status] + when 'archived' + channels.archived + else + channels.active + end + end + + def apply_visibility_filter(channels) + user_channels = channels.where(id: Current.user.internal_chat_channels.select(:id)) + + return channels.where(channel_type: %i[public_channel private_channel]).or(user_channels) if Current.account_user&.administrator? + + channels.where(channel_type: :public_channel).or(user_channels) + end + + def build_channel + if dm_params? + find_or_build_dm + else + attrs = create_channel_params.except(:member_ids, :team_ids) + validate_category!(attrs[:category_id]) + Current.account.internal_chat_channels.build(attrs.merge(created_by: Current.user)) + end + end + + def dm_params? + params[:channel_type] == 'dm' || params.dig(:channel, :channel_type) == 'dm' + end + + def find_or_build_dm + user_ids = dm_member_ids + existing_dm = find_existing_dm(user_ids) + return existing_dm if existing_dm.present? + + Current.account.internal_chat_channels.build( + channel_type: :dm, + name: nil, + created_by: Current.user + ) + end + + def find_existing_dm(user_ids) + sorted_ids = user_ids.sort + member_count = sorted_ids.size + + Current.account.internal_chat_channels + .where(channel_type: :dm) + .joins(:channel_members) + .group('internal_chat_channels.id') + .having('COUNT(internal_chat_channel_members.id) = ?', member_count) + .having( + 'ARRAY_AGG(internal_chat_channel_members.user_id ORDER BY internal_chat_channel_members.user_id) = ARRAY[?]::bigint[]', + sorted_ids + ) + .first + end + + def dm_member_ids + ids = Array(permitted_member_ids).map(&:to_i) + ids = Current.account.users.where(id: ids).pluck(:id) + ids << Current.user.id unless ids.include?(Current.user.id) + ids + end + + def add_creator_as_admin + return if @channel.channel_type_dm? + return if @channel.channel_members.exists?(user_id: Current.user.id) + + @channel.channel_members.create!(user_id: Current.user.id, role: :admin) + end + + def add_initial_members + member_ids = Array(permitted_member_ids).map(&:to_i) + member_ids = Current.account.users.where(id: member_ids).pluck(:id) + member_ids << Current.user.id if @channel.channel_type_dm? && member_ids.exclude?(Current.user.id) + + member_ids.uniq.each do |user_id| + next if @channel.channel_members.exists?(user_id: user_id) + + @channel.channel_members.create!(user_id: user_id, role: :member) + end + end + + def add_channel_type_members + return if @channel.channel_type_dm? + + if @channel.channel_type_public_channel? + add_all_agents_as_members + else + add_team_members + end + end + + def add_all_agents_as_members + agent_ids = Current.account.agents.where.not(id: Current.user.id).pluck(:id) + agent_ids.each do |uid| + @channel.channel_members.find_or_create_by!(user_id: uid) { |m| m.role = :member } + end + end + + def add_team_members + team_ids = permitted_team_ids + return if team_ids.blank? + + team_ids.each do |team_id| + team = Current.account.teams.find_by(id: team_id) + next unless team + + @channel.channel_teams.find_or_create_by!(team: team) + team.members.each do |user| + @channel.channel_members.find_or_create_by!(user_id: user.id) { |m| m.role = :member } + end + end + end + + def create_channel_params + @create_channel_params ||= params.require(:channel).permit(:name, :description, :channel_type, :category_id, member_ids: [], team_ids: []) + end + + def update_channel_params + params.require(:channel).permit(:name, :description, :category_id) + end + + def permitted_member_ids + params.permit(member_ids: [])[:member_ids] || create_channel_params[:member_ids] + end + + def permitted_team_ids + ids = params.permit(team_ids: [])[:team_ids] || create_channel_params[:team_ids] + Array(ids).map(&:to_i).compact_blank + end + + def mark_unread_params + params.permit(:message_id) + end + + def typing_status_param + params.permit(:typing_status)[:typing_status] + end + + def create_dm_with_lock + lock_key = "internal_chat_dm_#{Current.account.id}_#{dm_member_ids.sort.join('_')}" + ActiveRecord::Base.transaction do + ActiveRecord::Base.connection.execute( + ActiveRecord::Base.sanitize_sql_array(['SELECT pg_advisory_xact_lock(?)', Zlib.crc32(lock_key)]) + ) + existing = find_existing_dm(dm_member_ids) + if existing + @channel = existing + else + @channel.save! + add_initial_members + end + end + end + + def compute_mention_channel_ids(channels) + user_id = Current.user.id + InternalChat::ChannelMember + .joins( + 'INNER JOIN internal_chat_messages ' \ + 'ON internal_chat_messages.internal_chat_channel_id = internal_chat_channel_members.internal_chat_channel_id ' \ + 'AND internal_chat_messages.created_at > internal_chat_channel_members.last_read_at' + ) + .where(internal_chat_channel_id: channels.select(:id), user_id: user_id) + .where.not(last_read_at: nil) + .where.not('internal_chat_messages.sender_id' => user_id) + .where("internal_chat_messages.content_attributes->'mentioned_user_ids' @> ?", [user_id].to_json) + .pluck(Arel.sql('DISTINCT internal_chat_channel_members.internal_chat_channel_id')) + end + + def compute_unread_counts(channels) + InternalChat::ChannelMember + .joins( + 'INNER JOIN internal_chat_messages ' \ + 'ON internal_chat_messages.internal_chat_channel_id = internal_chat_channel_members.internal_chat_channel_id ' \ + 'AND internal_chat_messages.created_at > internal_chat_channel_members.last_read_at' + ) + .where(internal_chat_channel_id: channels.select(:id), user_id: Current.user.id) + .where.not(last_read_at: nil) + .where.not('internal_chat_messages.sender_id' => Current.user.id) + .group('internal_chat_channel_members.internal_chat_channel_id') + .count('internal_chat_messages.id') + end + + def channel_base_response(channel) + { + id: channel.id, + name: channel.name, + description: channel.description, + channel_type: channel.channel_type, + status: channel.status, + category_id: channel.category_id, + last_activity_at: channel.last_activity_at, + created_at: channel.created_at, + updated_at: channel.updated_at + } + end + + def channel_index_response(channel) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity + membership = channel.channel_members.detect { |member| member.user_id == Current.user.id } + response = channel_base_response(channel).merge( + is_dm: channel.channel_type_dm?, + muted: membership&.muted || false, + favorited: membership&.favorited || false, + hidden: membership&.hidden || false, + members_count: channel.channel_members.size, + unread_count: @unread_counts&.dig(channel.id) || 0, + has_unread_mention: @mention_channel_ids&.include?(channel.id) || false + ) + if channel.channel_type_dm? + response[:members] = channel.channel_members.map do |m| + { user_id: m.user_id, name: m.user.name, avatar_url: m.user.avatar_url, availability_status: m.user.availability_status } + end + end + response + end + + def channel_show_response(channel) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity + members = channel.channel_members.includes(:user).load + membership = members.detect { |member| member.user_id == Current.user.id } + recent_messages = channel.messages + .includes(:sender, :reactions, :replies, { poll: { options: { votes: :user } } }, + attachments: { file_attachment: :blob }) + .recent.limit(RECENT_MESSAGES_LIMIT).reverse + + channel_base_response(channel).merge( + is_dm: channel.channel_type_dm?, + muted: membership&.muted || false, + favorited: membership&.favorited || false, + account_id: channel.account_id, + created_by_id: channel.created_by_id, + members_count: members.size, + unread_count: membership&.unread_messages_count || 0, + members: members.map { |m| member_response(m) }, + messages: recent_messages.map { |msg| message_response(msg) } + ) + end + + def member_response(member) + { + id: member.id, + user_id: member.user_id, + role: member.role, + muted: member.muted, + favorited: member.favorited, + name: member.user.name, + avatar_url: member.user.avatar_url + } + end + + def message_response(message) + deleted = message.content_attributes&.dig('deleted') + attrs = message.content_attributes || {} + attrs = attrs.merge(poll: poll_response_for(message.poll)) if message.poll.present? + { + id: message.id, + content: message.content, + content_type: message.content_type, + content_attributes: attrs, + sender: message.sender&.push_event_data, + parent_id: message.parent_id, + echo_id: message.echo_id, + replies_count: message.replies_count, + created_at: message.created_at, + updated_at: message.updated_at, + reactions: reaction_responses(message), + attachments: deleted ? [] : message.attachments.map { |a| attachment_response(a) } + } + end + + def poll_response_for(poll) + { + id: poll.id, + question: poll.question, + multiple_choice: poll.multiple_choice, + public_results: poll.public_results, + allow_revote: poll.allow_revote, + expires_at: poll.expires_at, + internal_chat_message_id: poll.internal_chat_message_id, + options: poll.options.ordered.includes(votes: :user).map { |opt| poll_option_response(opt, poll) }, + total_votes: poll.total_votes_count, + created_at: poll.created_at, + updated_at: poll.updated_at + } + end + + def poll_option_response(option, poll) + response = { + id: option.id, + text: option.text, + votes_count: option.votes_count, + voted: option.votes.any? { |v| v.user_id == Current.user.id } + } + response[:voters] = option.votes.map { |v| { id: v.user_id, name: v.user.name } } if poll.public_results + response + end + + def reaction_responses(message) + message.reactions.includes(:user).map do |r| + { id: r.id, emoji: r.emoji, user_id: r.user_id, user: { name: r.user&.name } } + end + end + + def attachment_response(attachment) + { + id: attachment.id, + file_type: attachment.file_type, + external_url: attachment.external_url, + extension: attachment.extension, + file_url: attachment.file.attached? ? url_for(attachment.file) : nil + } + end + + def channel_member_tokens(channel) + users = channel.channel_type_public_channel? ? channel.account.users : channel.members + users.pluck(:pubsub_token) + end + + def validate_category!(category_id) + return if category_id.blank? + + Current.account.internal_chat_categories.find(category_id) + end + + def dispatch_channel_event(channel) + Rails.configuration.dispatcher.dispatch(INTERNAL_CHAT_CHANNEL_UPDATED, Time.zone.now, channel: channel) + end +end diff --git a/app/controllers/api/v1/accounts/internal_chat/drafts_controller.rb b/app/controllers/api/v1/accounts/internal_chat/drafts_controller.rb new file mode 100644 index 000000000..0334d6f8b --- /dev/null +++ b/app/controllers/api/v1/accounts/internal_chat/drafts_controller.rb @@ -0,0 +1,55 @@ +class Api::V1::Accounts::InternalChat::DraftsController < Api::V1::Accounts::InternalChat::BaseController + before_action :current_channel, only: [:update, :destroy] + + def index + accessible_channel_ids = Current.account.internal_chat_channels + .where(channel_type: :public_channel) + .or(Current.account.internal_chat_channels.where(id: Current.user.internal_chat_channels.select(:id))) + .select(:id) + @drafts = InternalChat::Draft.where(user: Current.user, account: Current.account, + internal_chat_channel_id: accessible_channel_ids).recent + render json: @drafts.map { |draft| draft_response(draft) } + end + + def update + authorize current_channel, :show?, policy_class: InternalChat::ChannelPolicy + + @draft = InternalChat::Draft.find_or_initialize_by( + user: Current.user, + internal_chat_channel_id: current_channel.id, + parent_id: draft_params[:parent_id] + ) + @draft.assign_attributes( + account: Current.account, + content: draft_params[:content] + ) + @draft.save! + + render json: draft_response(@draft), status: :ok + end + + def destroy + authorize current_channel, :show?, policy_class: InternalChat::ChannelPolicy + + @draft = InternalChat::Draft.find_by!(user: Current.user, internal_chat_channel_id: current_channel.id, parent_id: params[:parent_id]) + @draft.destroy! + head :ok + end + + private + + def draft_params + params.permit(:content, :parent_id) + end + + def draft_response(draft) + { + id: draft.id, + content: draft.content, + internal_chat_channel_id: draft.internal_chat_channel_id, + parent_id: draft.parent_id, + created_at: draft.created_at, + updated_at: draft.updated_at + } + end +end diff --git a/app/controllers/api/v1/accounts/internal_chat/messages_controller.rb b/app/controllers/api/v1/accounts/internal_chat/messages_controller.rb new file mode 100644 index 000000000..9f0495177 --- /dev/null +++ b/app/controllers/api/v1/accounts/internal_chat/messages_controller.rb @@ -0,0 +1,191 @@ +class Api::V1::Accounts::InternalChat::MessagesController < Api::V1::Accounts::InternalChat::BaseController + include Events::Types + + before_action :current_channel + before_action :fetch_message, only: [:update, :destroy, :pin, :unpin, :thread] + + MESSAGES_PER_PAGE = 50 + + def index + authorize current_channel, :show?, policy_class: InternalChat::ChannelPolicy + @messages = paginated_messages + render json: { + messages: @messages.map { |msg| message_response(msg) }, + meta: pagination_meta + } + end + + def create + authorize current_channel, :show?, policy_class: InternalChat::ChannelPolicy + @message = InternalChat::MessageCreateService.new( + channel: current_channel, + sender: Current.user, + params: message_params + ).perform + render json: message_response(@message), status: :created + end + + def update + authorize @message, :update?, policy_class: InternalChat::MessagePolicy + previous_content = @message.content + @message.update!( + content: update_params[:content], + content_attributes: (@message.content_attributes || {}).merge('edited_at' => Time.current.iso8601, 'previous_content' => previous_content) + ) + dispatch_message_event(INTERNAL_CHAT_MESSAGE_UPDATED, message: @message) + render json: message_response(@message) + end + + def destroy + authorize @message, :destroy?, policy_class: InternalChat::MessagePolicy + message_data = { + id: @message.id, + channel_id: @message.internal_chat_channel_id, + account_id: @message.account_id + } + @message.update!(content: I18n.t('internal_chat.messages.deleted'), content_attributes: { deleted: true }) + dispatch_message_event(INTERNAL_CHAT_MESSAGE_DELETED, message_data: message_data) + head :ok + end + + def pin + authorize @message, :pin?, policy_class: InternalChat::MessagePolicy + @message.skip_content_validation = true + @message.update!(content_attributes: (@message.content_attributes || {}).merge('pinned' => true, 'pinned_by' => Current.user.id, + 'pinned_at' => Time.current.iso8601)) + dispatch_message_event(INTERNAL_CHAT_MESSAGE_UPDATED, message: @message) + render json: message_response(@message) + end + + def unpin + authorize @message, :unpin?, policy_class: InternalChat::MessagePolicy + @message.skip_content_validation = true + attrs = (@message.content_attributes || {}).except('pinned', 'pinned_by', 'pinned_at') + @message.update!(content_attributes: attrs) + dispatch_message_event(INTERNAL_CHAT_MESSAGE_UPDATED, message: @message) + render json: message_response(@message) + end + + def thread + authorize @message, :thread?, policy_class: InternalChat::MessagePolicy + replies = @message.replies.includes(:sender, :reactions, :replies, :attachments, :poll).ordered + render json: { + parent: message_response(@message), + replies: replies.map { |msg| message_response(msg) } + } + end + + private + + def fetch_message + @message = current_channel.messages.find(params[:id]) + end + + def paginated_messages + return fetch_around_messages if params[:around].present? + + messages = apply_time_filters(base_messages_scope) + if params[:after].present? + messages.ordered.limit(MESSAGES_PER_PAGE) + else + messages.ordered.last(MESSAGES_PER_PAGE) + end + rescue ArgumentError + base_messages_scope.ordered.last(MESSAGES_PER_PAGE) + end + + def fetch_around_messages + target = current_channel.messages.find_by(id: params[:around]) + return base_messages_scope.ordered.last(MESSAGES_PER_PAGE) unless target + + half = MESSAGES_PER_PAGE / 2 + before_msgs = base_messages_scope.where('internal_chat_messages.created_at <= ?', target.created_at) + .ordered.last(half) + after_msgs = base_messages_scope.where('internal_chat_messages.created_at > ?', target.created_at) + .ordered.limit(half) + (before_msgs + after_msgs).uniq(&:id).sort_by(&:created_at) + end + + def base_messages_scope + current_channel.messages + .includes(:sender, :reactions, :replies, :attachments, :poll) + .where("parent_id IS NULL OR (content_attributes->>'also_send_in_channel')::boolean = true") + end + + def apply_time_filters(messages) + messages = messages.where('internal_chat_messages.created_at < ?', Time.zone.parse(params[:before])) if params[:before].present? + messages = messages.where('internal_chat_messages.created_at > ?', Time.zone.parse(params[:after])) if params[:after].present? + messages + end + + def pagination_meta + { + has_more: @messages.size >= MESSAGES_PER_PAGE + } + end + + def message_params + params.permit(:content, :content_type, :parent_id, :echo_id, :also_send_in_channel, attachments: [:file, :file_type]) + end + + def update_params + params.permit(:content) + end + + def message_response(message) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity + deleted = message.content_attributes&.dig('deleted') + response = { + id: message.id, + content: message.content, + content_type: message.content_type, + content_attributes: message.content_attributes, + internal_chat_channel_id: message.internal_chat_channel_id, + sender: message.sender&.push_event_data, + parent_id: message.parent_id, + echo_id: message.echo_id, + replies_count: message.replies_count, + created_at: message.created_at, + updated_at: message.updated_at, + reactions: message.reactions.includes(:user).map { |r| { id: r.id, emoji: r.emoji, user_id: r.user_id, user: { name: r.user&.name } } }, + attachments: deleted ? [] : message.attachments.map { |a| attachment_response(a) } + } + response[:poll] = poll_data(message.poll) if !deleted && message.poll? + response + end + + def poll_data(poll) + return nil unless poll + + { + id: poll.id, + question: poll.question, + multiple_choice: poll.multiple_choice, + public_results: poll.public_results, + allow_revote: poll.allow_revote, + expires_at: poll.expires_at, + options: poll.options.ordered.includes(votes: :user).map { |o| poll_option_data(o, poll) }, + total_votes: poll.total_votes_count + } + end + + def poll_option_data(option, poll) + data = { id: option.id, text: option.text, emoji: option.emoji, votes_count: option.votes_count, + voted: option.votes.any? { |v| v.user_id == Current.user.id } } + data[:voters] = option.votes.map { |v| v.user.push_event_data } if poll.public_results + data + end + + def attachment_response(attachment) + { + id: attachment.id, + file_type: attachment.file_type, + external_url: attachment.external_url, + extension: attachment.extension, + file_url: attachment.file.attached? ? url_for(attachment.file) : nil + } + end + + def dispatch_message_event(event, data) + Rails.configuration.dispatcher.dispatch(event, Time.zone.now, **data) + end +end diff --git a/app/controllers/api/v1/accounts/internal_chat/polls_controller.rb b/app/controllers/api/v1/accounts/internal_chat/polls_controller.rb new file mode 100644 index 000000000..12849f144 --- /dev/null +++ b/app/controllers/api/v1/accounts/internal_chat/polls_controller.rb @@ -0,0 +1,177 @@ +class Api::V1::Accounts::InternalChat::PollsController < Api::V1::Accounts::InternalChat::BaseController + include Events::Types + + before_action :set_poll, only: [:vote] + before_action :set_poll_for_unvote, only: [:unvote] + + def create + return render_pro_required('polls') unless InternalChat::Limits.polls_enabled? + + @channel = Current.account.internal_chat_channels.find(params[:channel_id]) + authorize @channel, :show?, policy_class: InternalChat::ChannelPolicy + raise ActionController::BadRequest, 'Options are required' if poll_params[:options].blank? + + ActiveRecord::Base.transaction do + @message = create_poll_message + @poll = build_poll + create_poll_options + end + + dispatch_message_created_event + + render json: message_with_poll_response(@message, @poll), status: :created + end + + def vote + ActiveRecord::Base.transaction do + validate_vote! + @vote = @option.votes.create!(user: Current.user) + end + dispatch_poll_event + + render json: message_with_poll_response(@poll.message, @poll.reload), status: :ok + end + + def unvote + raise ActionController::BadRequest, 'Poll has expired' if @poll.expired? + + @vote = if params[:option_id].present? + option = @poll.options.find(params[:option_id]) + option.votes.find_by!(user_id: Current.user.id) + else + InternalChat::PollVote.joins(:option) + .where(internal_chat_poll_options: { internal_chat_poll_id: @poll.id }, user_id: Current.user.id) + .first! + end + @vote.destroy! + dispatch_poll_event + + render json: message_with_poll_response(@poll.message, @poll.reload), status: :ok + end + + private + + def set_poll + @poll = InternalChat::Poll.joins(:message).where(internal_chat_messages: { account_id: Current.account.id }).find(params[:id]) + @option = @poll.options.find(params[:option_id]) + channel = @poll.message.channel + authorize channel, :show?, policy_class: InternalChat::ChannelPolicy + end + + def set_poll_for_unvote + @poll = InternalChat::Poll.joins(:message).where(internal_chat_messages: { account_id: Current.account.id }).find(params[:id]) + channel = @poll.message.channel + authorize channel, :show?, policy_class: InternalChat::ChannelPolicy + end + + def create_poll_message + @channel.messages.create!( + account: Current.account, + sender: Current.user, + content: poll_params[:question], + content_type: :poll + ) + end + + def build_poll + @message.create_poll!( + question: poll_params[:question], + multiple_choice: poll_params[:multiple_choice] || false, + public_results: poll_params.fetch(:public_results, true), + allow_revote: poll_params.fetch(:allow_revote, true), + expires_at: poll_params[:expires_at] + ) + end + + def validate_vote! + raise ActionController::BadRequest, 'Poll has expired' if @poll.expired? + + existing_votes = existing_user_votes + return unless existing_votes.exists? + + raise ActionController::BadRequest, 'Revoting is not allowed' unless @poll.allow_revote + + if @poll.multiple_choice + raise ActionController::BadRequest, 'Already voted for this option' if @option.votes.exists?(user_id: Current.user.id) + else + existing_votes.destroy_all + end + end + + def existing_user_votes + InternalChat::PollVote.joins(:option).where( + internal_chat_poll_options: { internal_chat_poll_id: @poll.id }, + user_id: Current.user.id + ) + end + + def create_poll_options + poll_params[:options].each_with_index do |option_attrs, index| + @poll.options.create!( + text: option_attrs[:text], + emoji: option_attrs[:emoji], + image_url: option_attrs[:image_url], + position: index + ) + end + end + + def poll_params + params.permit(:question, :multiple_choice, :public_results, :allow_revote, :expires_at, :channel_id, + options: [:text, :emoji, :image_url]) + end + + def message_with_poll_response(message, poll) + { + id: message.id, + content: message.content, + content_type: message.content_type, + content_attributes: (message.content_attributes || {}).merge(poll: poll_response(poll)), + internal_chat_channel_id: message.internal_chat_channel_id, + sender: message.sender.push_event_data, + parent_id: message.parent_id, + created_at: message.created_at, + updated_at: message.updated_at, + attachments: [], + reactions: [] + } + end + + def poll_response(poll) + { + id: poll.id, + question: poll.question, + multiple_choice: poll.multiple_choice, + public_results: poll.public_results, + allow_revote: poll.allow_revote, + expires_at: poll.expires_at, + internal_chat_message_id: poll.internal_chat_message_id, + options: poll.options.ordered.includes(votes: :user).map { |option| option_response(option, poll) }, + total_votes: poll.total_votes_count, + created_at: poll.created_at, + updated_at: poll.updated_at + } + end + + def option_response(option, poll) + response = { + id: option.id, + text: option.text, + emoji: option.emoji, + image_url: option.image_url, + position: option.position, + votes_count: option.votes_count, + voted: option.votes.any? { |v| v.user_id == Current.user.id } + } + response[:voters] = option.votes.map { |v| v.user.push_event_data } if poll.public_results + response + end + + def dispatch_message_created_event + Rails.configuration.dispatcher.dispatch(INTERNAL_CHAT_MESSAGE_CREATED, Time.zone.now, message: @message) + end + + def dispatch_poll_event + Rails.configuration.dispatcher.dispatch(INTERNAL_CHAT_POLL_VOTED, Time.zone.now, poll: @poll, message: @poll.message) + end +end diff --git a/app/controllers/api/v1/accounts/internal_chat/reactions_controller.rb b/app/controllers/api/v1/accounts/internal_chat/reactions_controller.rb new file mode 100644 index 000000000..ef8c95ec7 --- /dev/null +++ b/app/controllers/api/v1/accounts/internal_chat/reactions_controller.rb @@ -0,0 +1,54 @@ +class Api::V1::Accounts::InternalChat::ReactionsController < Api::V1::Accounts::InternalChat::BaseController + include Events::Types + + before_action :fetch_message + + def create + @reaction = @message.reactions.build(user: Current.user, emoji: reaction_params[:emoji]) + authorize @reaction, :create?, policy_class: InternalChat::ReactionPolicy + @reaction.save! + dispatch_reaction_event(INTERNAL_CHAT_REACTION_CREATED, reaction: @reaction) + render json: reaction_response(@reaction), status: :created + end + + def destroy + @reaction = @message.reactions.find(params[:id]) + authorize @reaction, :destroy?, policy_class: InternalChat::ReactionPolicy + reaction_data = { + id: @reaction.id, + message_id: @reaction.internal_chat_message_id, + channel_id: @message.internal_chat_channel_id, + account_id: @message.account_id, + user_id: @reaction.user_id, + emoji: @reaction.emoji + } + @reaction.destroy! + dispatch_reaction_event(INTERNAL_CHAT_REACTION_DELETED, reaction_data: reaction_data) + head :ok + end + + private + + def fetch_message + @message = InternalChat::Message.joins(:channel).where(internal_chat_channels: { account_id: Current.account.id }).find(params[:message_id]) + end + + def reaction_response(reaction) + { + id: reaction.id, + emoji: reaction.emoji, + user_id: reaction.user_id, + user: { name: reaction.user&.name }, + internal_chat_message_id: reaction.internal_chat_message_id, + created_at: reaction.created_at + } + end + + def reaction_params + params.permit(:emoji) + end + + def dispatch_reaction_event(event, **data) + Rails.configuration.dispatcher.dispatch(event, Time.zone.now, **data) + end +end diff --git a/app/controllers/api/v1/accounts/internal_chat/search_controller.rb b/app/controllers/api/v1/accounts/internal_chat/search_controller.rb new file mode 100644 index 000000000..aa721b3c2 --- /dev/null +++ b/app/controllers/api/v1/accounts/internal_chat/search_controller.rb @@ -0,0 +1,19 @@ +class Api::V1::Accounts::InternalChat::SearchController < Api::V1::Accounts::BaseController + def show + authorize InternalChat::Channel, :index? + + result = InternalChat::SearchService.new( + current_user: Current.user, + current_account: Current.account, + params: search_params + ).perform + + render json: result + end + + private + + def search_params + params.permit(:q, :page) + end +end diff --git a/app/dispatchers/async_dispatcher.rb b/app/dispatchers/async_dispatcher.rb index f46928d4e..12f728453 100644 --- a/app/dispatchers/async_dispatcher.rb +++ b/app/dispatchers/async_dispatcher.rb @@ -15,6 +15,7 @@ class AsyncDispatcher < BaseDispatcher CsatSurveyListener.instance, HookListener.instance, InstallationWebhookListener.instance, + InternalChatListener.instance, NotificationListener.instance, ParticipationListener.instance, ReportingEventListener.instance, diff --git a/app/javascript/dashboard/api/internalChatChannels.js b/app/javascript/dashboard/api/internalChatChannels.js new file mode 100644 index 000000000..5855a4bf2 --- /dev/null +++ b/app/javascript/dashboard/api/internalChatChannels.js @@ -0,0 +1,72 @@ +/* global axios */ +import ApiClient from './ApiClient'; + +class InternalChatChannelsAPI extends ApiClient { + constructor() { + super('internal_chat/channels', { accountScoped: true }); + } + + getWithParams(params) { + return axios.get(this.url, { params }); + } + + getCategories() { + return axios.get(`${this.url.replace('/channels', '/categories')}`); + } + + createCategory(data) { + return axios.post(`${this.url.replace('/channels', '/categories')}`, data); + } + + deleteCategory(categoryId) { + return axios.delete( + `${this.url.replace('/channels', '/categories')}/${categoryId}` + ); + } + + archive(channelId) { + return axios.post(`${this.url}/${channelId}/archive`); + } + + unarchive(channelId) { + return axios.post(`${this.url}/${channelId}/unarchive`); + } + + getMembers(channelId) { + return axios.get(`${this.url}/${channelId}/members`); + } + + addMember(channelId, userId) { + return axios.post(`${this.url}/${channelId}/members`, { user_id: userId }); + } + + removeMember(channelId, memberId) { + return axios.delete(`${this.url}/${channelId}/members/${memberId}`); + } + + updateMember(channelId, memberId, data) { + return axios.patch(`${this.url}/${channelId}/members/${memberId}`, data); + } + + toggleTypingStatus(channelId, typingStatus) { + return axios.post(`${this.url}/${channelId}/toggle_typing_status`, { + typing_status: typingStatus, + }); + } + + markRead(channelId) { + return axios.post(`${this.url}/${channelId}/mark_read`); + } + + markUnread(channelId, messageId) { + return axios.post(`${this.url}/${channelId}/mark_unread`, { + message_id: messageId, + }); + } + + search(params) { + return axios.get(`${this.url.replace('/channels', '/search')}`, { params }); + } +} + +export default new InternalChatChannelsAPI(); diff --git a/app/javascript/dashboard/api/internalChatDrafts.js b/app/javascript/dashboard/api/internalChatDrafts.js new file mode 100644 index 000000000..4eaffeb35 --- /dev/null +++ b/app/javascript/dashboard/api/internalChatDrafts.js @@ -0,0 +1,24 @@ +/* global axios */ +import ApiClient from './ApiClient'; + +class InternalChatDraftsAPI extends ApiClient { + constructor() { + super('internal_chat', { accountScoped: true }); + } + + getDrafts() { + return axios.get(`${this.url}/drafts`); + } + + saveDraft(channelId, data) { + return axios.patch(`${this.url}/channels/${channelId}/draft`, data); + } + + deleteDraft(channelId, { parentId } = {}) { + return axios.delete(`${this.url}/channels/${channelId}/draft`, { + params: { parent_id: parentId }, + }); + } +} + +export default new InternalChatDraftsAPI(); diff --git a/app/javascript/dashboard/api/internalChatMessages.js b/app/javascript/dashboard/api/internalChatMessages.js new file mode 100644 index 000000000..efa32bc04 --- /dev/null +++ b/app/javascript/dashboard/api/internalChatMessages.js @@ -0,0 +1,62 @@ +/* global axios */ +import ApiClient from './ApiClient'; + +class InternalChatMessagesAPI extends ApiClient { + constructor() { + super('internal_chat/channels', { accountScoped: true }); + } + + getMessages(channelId, params = {}) { + return axios.get(`${this.url}/${channelId}/messages`, { params }); + } + + createMessage(channelId, data, files = []) { + if (files.length === 0) { + return axios.post(`${this.url}/${channelId}/messages`, data); + } + const formData = new FormData(); + if (data.content) formData.append('content', data.content); + if (data.parent_id) formData.append('parent_id', data.parent_id); + if (data.echo_id) formData.append('echo_id', data.echo_id); + files.forEach(file => { + formData.append('attachments[][file]', file); + }); + return axios.post(`${this.url}/${channelId}/messages`, formData); + } + + updateMessage(channelId, messageId, data) { + return axios.patch(`${this.url}/${channelId}/messages/${messageId}`, data); + } + + deleteMessage(channelId, messageId) { + return axios.delete(`${this.url}/${channelId}/messages/${messageId}`); + } + + getThread(channelId, messageId) { + return axios.get(`${this.url}/${channelId}/messages/${messageId}/thread`); + } + + pinMessage(channelId, messageId) { + return axios.post(`${this.url}/${channelId}/messages/${messageId}/pin`); + } + + unpinMessage(channelId, messageId) { + return axios.delete(`${this.url}/${channelId}/messages/${messageId}/unpin`); + } + + addReaction(messageId, emoji) { + const baseUrl = this.url.replace('/channels', ''); + return axios.post(`${baseUrl}/messages/${messageId}/reactions`, { + emoji, + }); + } + + removeReaction(messageId, reactionId) { + const baseUrl = this.url.replace('/channels', ''); + return axios.delete( + `${baseUrl}/messages/${messageId}/reactions/${reactionId}` + ); + } +} + +export default new InternalChatMessagesAPI(); diff --git a/app/javascript/dashboard/api/internalChatPolls.js b/app/javascript/dashboard/api/internalChatPolls.js new file mode 100644 index 000000000..14734cbdc --- /dev/null +++ b/app/javascript/dashboard/api/internalChatPolls.js @@ -0,0 +1,24 @@ +/* global axios */ +import ApiClient from './ApiClient'; + +class InternalChatPollsAPI extends ApiClient { + constructor() { + super('internal_chat/polls', { accountScoped: true }); + } + + createPoll(data) { + return axios.post(this.url, data); + } + + vote(pollId, optionId) { + return axios.post(`${this.url}/${pollId}/vote`, { option_id: optionId }); + } + + unvote(pollId, optionId) { + return axios.delete(`${this.url}/${pollId}/vote`, { + params: { option_id: optionId }, + }); + } +} + +export default new InternalChatPollsAPI(); diff --git a/app/javascript/dashboard/components-next/dialog/Dialog.vue b/app/javascript/dashboard/components-next/dialog/Dialog.vue index ca3ce41d4..fa5ee26ce 100644 --- a/app/javascript/dashboard/components-next/dialog/Dialog.vue +++ b/app/javascript/dashboard/components-next/dialog/Dialog.vue @@ -118,22 +118,25 @@ defineExpose({ open, close });
-
+

{{ title }}

@@ -143,12 +146,14 @@ defineExpose({ open, close });

- +
+ +