Message threads and search
Message threads provide the inbox view for SMS activity, while message search provides a wider view across a user’s stored records. The service groups activity by connected phone and contact, keeps a short summary for each conversation, and exposes APIs that let dashboard clients list, inspect, update, and remove that data. Thread rows can also include matching saved contact details when the client asks for them. This page is a child of Messaging platform and relates to the end-to-end SMS message lifecycle, contacts and conversation recipients, and bulk and scheduled messaging.
Conversation model
Message records
A message record is one outbound SMS or MMS, inbound message, or missed-call activity for an authenticated user. It keeps the owner phone, contact, content, direction, current delivery state, event times, attachments, and request ID. The record is the detailed history that clients open after they select a thread. api/…/message.go• L85-122
The owner and contact form the conversation key used by message history. The same pair is also used when the service finds the thread that should show the message. Message history is therefore directional: the connected phone is the owner, and the other number or contact value is the contact. api/…/gorm_message_repository.go• L80-100, api/…/gorm_message_thread_repository.go• L197-219
References: api/…/message.go• L85-122 api/…/gorm_message_repository.go• L80-100 api/…/gorm_message_thread_repository.go• L197-219
Thread summaries
A message thread groups all activity for one user, one owner phone, and one contact. It stores the latest message ID and content together with the latest status, activity time, unread count, archive state, and display color. Clients can render an inbox row from this summary without loading the full conversation. api/…/message_thread.go• L9-26
The thread’s activity time is the message event time used for inbox order. Each new message or delivery event can replace the latest-message fields, so the thread moves with the activity that it represents. If the latest message is deleted, the service restores the prior message summary when one exists or removes the empty thread. api/…/message_thread_service.go• L87-140, api/…/message_thread_service.go• L168-206
Thread contact details are optional read-time data. They identify a saved contact that contains a name, email addresses, phone numbers, and custom properties; they do not change the owner/contact pair that defines the thread. api/…/contact.go• L16-26, api/…/message_thread.go• L24-26
References: api/…/message_thread.go• L9-26 api/…/contact.go• L16-26 api/…/message_thread_service.go• L87-140 api/…/message_thread_service.go• L168-206
Saved contact records
Saved contacts give clients a user-scoped name and other details for a phone number. The contacts API supports paged listing with text search across names, email addresses, and phone numbers, plus create, update, CSV import, and delete actions. api/…/contact_handler.go• L46-53, api/…/contact_handler.go• L55-104, api/…/contact_handler.go• L106-206, api/…/contact_handler.go• L209-310
Contact phone values are trimmed, normalized, and made unique before storage. A contact update invalidates lookups for both its old and new phone values, so later thread reads can use the current record. Contact removal and account deletion also clear the related lookup state. api/…/contact_store.go• L49-96, api/…/contact_update.go• L20-43, api/…/contact_service.go• L176-220, api/…/contact_listener.go• L14-56
References: api/…/contact.go• L66-80 api/…/contact_handler.go• L46-53 api/…/contact_handler.go• L55-104 api/…/contact_handler.go• L106-206 api/…/contact_handler.go• L209-310 api/…/contact_store.go• L49-96 api/…/contact_update.go• L20-43 api/…/contact_service.go• L176-220 api/…/contact_listener.go• L14-56
Thread activity
Event-driven updates
Thread state follows message activity rather than a client write for each delivery change. The thread listener consumes outbound, inbound, missed-call, scheduling, expiry, failure, and deletion events, then sends the owner, contact, message ID, content, status, and event time to the thread service. api/…/message_thread_listener.go• L16-47, api/…/message_thread_listener.go• L50-75, api/…/message_thread_listener.go• L202-309
When no thread exists for a message pair, the service creates one. Later events update the existing summary. The service protects a newer summary from an older event and does not replace a delivered latest message with a weaker state for that same message. The event timestamp also controls whether the update can affect the thread’s unread count. api/…/message_thread_service.go• L87-140
This flow connects the detailed message lifecycle to the compact inbox view. For the phone events that produce these updates, read SMS message lifecycle. api/…/message_thread_listener.go• L94-228
References: api/…/message_thread_listener.go• L16-47 api/…/message_thread_listener.go• L50-75 api/…/message_thread_listener.go• L94-228 api/…/message_thread_listener.go• L231-309 api/…/message_thread_service.go• L87-140
Read and archive state
Read and archive state belongs to the thread because it describes how the user views a conversation. An inbound message or missed call marks the thread unread. Marking a thread read updates its unread count, while archive changes remove or restore the thread from the selected inbox view. api/…/message_thread_listener.go• L202-255, api/…/message_thread_service.go• L143-166, api/…/gorm_message_thread_repository.go• L66-77
An inbound message can also unarchive a thread when the connected phone has the unarchive_thread setting enabled. The setting is checked only for received activity on an archived thread. If the phone cannot be loaded, the thread stays archived. The phone lookup is skipped for other thread updates. api/…/message_thread_service.go• L66-72, api/…/message_thread_service.go• L114-133
References: api/…/message_thread_listener.go• L202-255 api/…/message_thread_service.go• L66-72 api/…/message_thread_service.go• L114-133 api/…/message_thread_service.go• L143-166 api/…/gorm_message_thread_repository.go• L38-77
Client query paths
Thread listing
GET /v1/message-threads returns thread summaries for one owner phone. The request can select active or archived threads, filter the summary by content or phone number, include matching saved contact details, and page through results. The service scopes the query to the authenticated user and sorts the result by thread activity time in descending order. api/…/message_thread_handler.go• L42-92, api/…/message_thread_index_request.go• L13-67, api/…/message_thread_service.go• L273-311, api/…/gorm_message_thread_repository.go• L246-276
The archive filter defaults to active threads. An archived request returns only archived rows, while an active request also accepts older rows whose archive value is empty. Contact enrichment is opt-in through contacts=true; if the contact lookup fails, the service still returns the thread summaries without the optional details. api/…/message_thread_index_request.go• L24-50, api/…/message_thread_service.go• L292-308, api/…/message_thead_responses.go• L5-9
Message history listing
GET /v1/messages returns the detailed history for one owner and contact. It supports a content filter plus skip and limit values, and the service returns the newest records first by activity time. The dashboard reverses that result for conversation display so the oldest visible message appears first. api/…/message_handler.go• L221-263, api/…/message_index_request.go• L14-53, api/…/gorm_message_repository.go• L80-100, web/…/threads.ts• L57-75
The list is intentionally separate from thread listing. A client first gets a compact thread row, then uses its owner and contact to request the full history. This keeps the inbox query small while preserving access to message content and delivery details. web/…/threads.ts• L34-75
References: api/…/message_thread_handler.go• L42-92 api/…/message_thread_index_request.go• L13-67 api/…/message_thread_service.go• L273-311 api/…/gorm_message_thread_repository.go• L246-276 api/…/message_thead_responses.go• L5-9 api/…/message_handler.go• L221-263 api/…/message_index_request.go• L14-53 api/…/gorm_message_repository.go• L80-100 web/…/threads.ts• L34-75
Message search
GET /v1/messages/search searches a user’s messages across conversations. It can filter by one or more owner phones, message types, and statuses. Its text query checks content, contact, failure reason, and request ID. A query that is a valid UUID also matches the message ID. api/…/message_handler.go• L507-550, api/…/message_search_request.go• L13-69, api/…/gorm_message_repository.go• L127-169
Search uses server-side paging and sorting. The API accepts skip and limit values up to 200, and the sort field is limited to created time, owner, contact, type, or status. The dashboard normally sorts by created time in descending order and sends a fresh Cloudflare Turnstile token, which the API validates before it runs the search. api/…/message_handler_validator.go• L285-348, web/…/index.vue• L160-197
The response is a data array of full message records. This lets the dashboard select records for CSV export, permanent deletion, or resend when all selected records are failed or expired outbound messages. For the screen-level behavior, read bulk and scheduled messaging. api/…/message_responses.go• L11-15, web/…/index.vue• L217-320
The query paths turn stored data into two views: thread listing answers “which conversations need attention?” while message history and search answer “what happened in this conversation or across the account?”
flowchart LR
Activity[Message activity] --> Records[Message records]
Activity --> Updates[Thread activity updates]
Updates --> Threads[Thread summaries]
Threads --> Inbox[Thread listing]
Threads --> History[Owner and contact history]
Records --> History
Records --> Search[Filtered message search]
References: api/…/message_handler.go• L507-550 api/…/message_search_request.go• L13-69 api/…/message_handler_validator.go• L285-348 api/…/gorm_message_repository.go• L127-169 api/…/message_responses.go• L11-15 web/…/index.vue• L160-197 web/…/index.vue• L217-320
Thread actions
Thread updates
PUT /v1/message-threads/:messageThreadID changes user-facing thread state. A request must provide at least one of is_archived or is_read; the response returns the updated thread. These changes are scoped to the authenticated user and use the thread ID as the resource key. api/…/message_thread_handler.go• L94-136, api/…/message_thread_update_request.go• L10-26, api/…/message_thread_handler_validator.go• L63-83
The dashboard uses this boundary when a user opens a thread, archives or unarchives it, or changes the read state. It removes an archived thread from the active local list and replaces a successfully marked-read row with the returned summary. web/…/threads.ts• L77-133
References: api/…/message_thread_handler.go• L94-136 api/…/message_thread_update_request.go• L10-26 api/…/message_thread_handler_validator.go• L63-83 web/…/threads.ts• L77-133
Message deletion
DELETE /v1/messages/:messageID removes one message and emits deletion activity. If that message was the thread’s latest record, the thread service uses the previous message as the new summary. This prevents a thread row from pointing at content that no longer exists. api/…/message_handler.go• L376-419, api/…/message_service.go• L136-181, api/…/message_thread_service.go• L168-206
References: api/…/message_handler.go• L376-419 api/…/message_service.go• L136-181 api/…/message_thread_service.go• L168-206
Thread deletion
DELETE /v1/message-threads/:messageThreadID removes the thread and emits a thread-deleted event. A message listener receives that event and deletes all messages for the same user, owner, and contact. Thread deletion is therefore a conversation-level operation rather than only a removal of the inbox summary. api/…/message_thread_handler.go• L138-179, api/…/message_thread_service.go• L327-356, api/…/message_listener.go• L276-290
References: api/…/message_thread_handler.go• L138-179 api/…/message_thread_service.go• L327-356 api/…/message_listener.go• L276-290
Client synchronization
Dashboard refreshes
The dashboard keeps the thread list as shared client state and loads message history only for the selected thread. After a send, it reloads the thread list so a new conversation or latest-message summary appears. The conversation view reloads its history after relevant send, failure, receive, and missed-call events. web/…/threads.ts• L7-75, web/…/messages.ts• L16-41, web/…/index.vue• L124-153, web/…/index.vue• L230-255
The live view uses event signals to decide when to read again; the API remains the source of the current message and thread data. This avoids treating a live notification as a complete record and lets the client recover by loading the latest state. For the wider dashboard flow, read bulk and scheduled messaging. web/…/index.vue• L230-260, api/…/message_handler.go• L239-263
References: web/…/threads.ts• L7-75 web/…/messages.ts• L16-41 web/…/index.vue• L124-153 web/…/index.vue• L230-260 api/…/message_handler.go• L239-263
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the owner/contact grouping rule aligned across message history, thread creation, thread listing, and dashboard selection. A change in phone normalization can split one conversation into multiple threads.
- Keep every message event’s owner, contact, message ID, status, content, and event time aligned with the thread listener. Missing one field can make the inbox summary differ from message history.
- When status names or message types change, update search validation, repository filters, dashboard labels, and thread status updates together.
- When changing archive or read behavior, check the API update contract, the unread event watermark, the phone unarchive setting, and the dashboard’s local list updates.
- When changing deletion behavior, preserve the link between message deletion and prior-message summary repair. Preserve the thread-deleted event that removes all messages.
- When changing search paging or sorting, align API limits and allowed sort fields with the dashboard table options and its result-count behavior.
- Keep the Turnstile requirement aligned between the search page, the shared message store, and the search validator.
- Keep the
contactsthread-list flag, user scoping, and read-time contact enrichment aligned with contact phone normalization. - When contact phone numbers change or contacts are removed, invalidate both old and new lookup values so thread rows do not show stale recipient details.
- Keep contact listing and its total count on the same user and text filter so contact selection and pagination remain consistent.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| The owner or contact value is changed in only one query path. | Message history and thread summaries appear in different conversations. | Keep the same owner/contact values and normalization rules in message and thread repositories. api/…/gorm_message_repository.go• L80-100, api/…/gorm_message_thread_repository.go• L197-219 |
| A thread activity event carries an older timestamp or a different latest-message ID. | An old delivery state or message can replace the current inbox summary. | Reject stale activity and protect a delivered latest message in the thread service. api/…/message_thread_service.go• L104-111 |
| The dashboard requests a thread page larger than the API limit. | Thread loading fails validation instead of showing the inbox. | Keep the client limit within the thread validator’s maximum or raise both limits as one contract. web/…/threads.ts• L41-49, api/…/message_thread_handler_validator.go• L31-60 |
| A received event cannot load the phone settings for an archived thread. | The new message stays in the archive even though unarchive behavior may be enabled. | Treat the setting lookup failure as a protected archived state and monitor the phone lookup path. api/…/message_thread_service.go• L125-133 |
| The latest message is deleted without processing its deletion event. | The thread can show deleted content or an invalid latest message ID. | Emit deletion activity and restore the previous summary or delete the empty thread. api/…/message_service.go• L147-180, api/…/message_thread_service.go• L168-206 |
| Contact enrichment is requested but the contact lookup fails. | The inbox still loads, but thread rows lack optional names and contact details. | Keep enrichment optional and preserve the user-scoped contact lookup path. api/…/message_thread_service.go• L292-308, api/…/contact_service.go• L223-282 |
| A contact number is updated without invalidating its old lookup key. | A thread can show a stale contact after the contact record changes. | Invalidate both old and new phone numbers after contact updates. api/…/contact_service.go• L176-189 |
| Search sends no valid Turnstile token or an unsupported filter value. | The search returns a validation error and no results. | Create a fresh token in the dashboard and keep request validation aligned with supported types, statuses, and sort fields. api/…/message_handler_validator.go• L285-348, web/…/index.vue• L128-182 |
Key files
| File | Why to read it |
|---|---|
| api/…/message.go | Defines the detailed message record, direction, and delivery status values. |
| api/…/message_thread.go | Defines the compact conversation summary, its user-facing state, and optional contact details. |
| api/…/contact.go | Defines the saved contact data that can enrich a thread row. |
| api/…/message_thread_listener.go | Shows which message events update thread summaries. |
| api/…/message_thread_service.go | Explains thread creation, activity ordering, unread state, contact enrichment, unarchive behavior, and deletion repair. |
| api/…/contact_service.go | Explains user-scoped contact lookup, cache invalidation, and contact CRUD behavior. |
| api/…/gorm_message_thread_repository.go | Defines thread listing and the durable updates for activity, read state, archive state, and deletion. |
| api/…/message_thread_handler.go | Defines the thread listing, update, and delete API boundary. |
| api/…/contact_handler.go | Defines the contacts API used to maintain recipient details. |
| api/…/message_handler.go | Defines message history, search, individual message reads, and message deletion. |
| api/…/gorm_message_repository.go | Defines conversation history queries and cross-account message search. |
| api/…/message_handler_validator.go | Defines search filters, paging limits, sort fields, and the Turnstile check. |
| web/…/threads.ts | Shows how the dashboard loads threads and history and applies thread actions. |
| web/…/messages.ts | Shows the shared dashboard boundary for message search and send refreshes. |
| web/…/index.vue | Shows how a selected thread becomes a live conversation and how its history refreshes. |
| web/…/index.vue | Shows the search filters, server paging, selection, export, deletion, and resend behavior. |