openConversation method

Future<void> openConversation(
  1. String id, {
  2. VisitorConversationDto? summary,
})

Shows conversation id: its latest 50 items, live updates while it is on screen, earlier ones through loadEarlier. Writing into a resolved conversation reopens it; a closed one is read-only. summary (from listConversations) avoids a flash of the wrong status.

Implementation

Future<void> openConversation(String id, {VisitorConversationDto? summary}) async {
  final token = _token;
  if (_state.conversation?.id == id) return;
  if (token == null || _state.phase != ChatPhase.ready) {
    _wantConversation = (id, summary); // opened once the session is ready
    return;
  }
  _emit(
    _state.copyWith(
      conversation: ConversationSummary(
        id: id,
        siteId: _api.siteId,
        visitorId: _state.visitor?.id ?? '',
        status: summary?.status ?? ConversationStatus.open,
        seq: summary?.seq ?? 0,
      ),
      timeline: Timeline.empty,
      loadingEarlier: true,
      clearTyping: true,
      clearRated: true,
    ),
  );
  try {
    // Latest page: everything before seq + 1 (the gateway stores seq as a 32-bit integer).
    final top = (summary?.seq ?? 0) > 0 ? summary!.seq + 1 : maxSeq;
    final page = await _api.messages(token: token, conversationId: id, beforeSeq: top, limit: 50);
    if (_state.conversation?.id != id) return;
    final timeline = mergeTimeline(Timeline.empty, page.items);
    _emit(_state.copyWith(loadingEarlier: false, timeline: timeline.copyWith(hasMore: page.hasMore)));
    _afterSwitch(id);
  } on HatangException catch (e) {
    _emit(_state.copyWith(loadingEarlier: false, transientError: e));
  }
}