watch method

Stream<List<DtoChatItem>> watch(
  1. ChatId chatId, {
  2. int? before,
  3. int? after,
  4. PreciseDateTime? around,
})

Returns the Stream of DtoChatItems being in a historical view order of the provided chatId.

Implementation

Stream<List<DtoChatItem>> watch(
  ChatId chatId, {
  int? before,
  int? after,
  PreciseDateTime? around,
}) {
  return stream((db) {
    if (before == null && after == null) {
      final stmt = db.select(db.chatItemViews).join([
        innerJoin(
          db.chatItems,
          db.chatItems.id.equalsExp(db.chatItemViews.chatItemId),
        ),
      ]);

      stmt.where(db.chatItemViews.chatId.equals(chatId.val));
      stmt.orderBy([OrderingTerm.desc(db.chatItems.at)]);

      return stmt.watch().map(
        (rows) => rows
            .map((e) => _ChatItemDb.fromDb(e.readTable(db.chatItems)))
            .toList(),
      );
    } else if (before != null && after != null) {
      final stmt = db.chatItemsAround(
        chatId.val,
        around ?? PreciseDateTime.now(),
        before.toDouble(),
        after,
      );

      return stmt.watch().map(
        (items) => items
            .map(
              (r) => _ChatItemDb.fromDb(
                ChatItemRow(
                  id: r.id,
                  chatId: r.chatId,
                  authorId: r.authorId,
                  at: r.at,
                  status: r.status,
                  data: r.data,
                  cursor: r.cursor,
                  ver: r.ver,
                ),
              ),
            )
            .toList(),
      );
    } else if (before == null && after != null) {
      final stmt = db.chatItemsAroundTopless(
        chatId.val,
        around ?? PreciseDateTime.now(),
        after,
      );

      return stmt.watch().map(
        (items) => items
            .map(
              (r) => _ChatItemDb.fromDb(
                ChatItemRow(
                  id: r.id,
                  chatId: r.chatId,
                  authorId: r.authorId,
                  at: r.at,
                  status: r.status,
                  data: r.data,
                  cursor: r.cursor,
                  ver: r.ver,
                ),
              ),
            )
            .toList(),
      );
    } else if (before != null && after == null) {
      final stmt = db.chatItemsAroundBottomless(
        chatId.val,
        around ?? PreciseDateTime.now(),
        before.toDouble(),
      );

      return stmt.watch().map(
        (items) => items
            .map(
              (r) => _ChatItemDb.fromDb(
                ChatItemRow(
                  id: r.id,
                  chatId: r.chatId,
                  authorId: r.authorId,
                  at: r.at,
                  status: r.status,
                  data: r.data,
                  cursor: r.cursor,
                  ver: r.ver,
                ),
              ),
            )
            .toList(),
      );
    }

    throw Exception('Unreachable');
  });
}