attachments method

  1. @override
Paginated<ChatItemId, Rx<ChatItem>> attachments({
  1. ChatItemId? item,
})
override

Returns the Paginated of ChatItems having any Attachments posted in this chat around the provided item, if any.

Implementation

@override
Paginated<ChatItemId, Rx<ChatItem>> attachments({ChatItemId? item}) {
  Log.debug('attachments(item: $item)', '$runtimeType($id)');

  ChatItemsCursor? cursor;
  ChatItemId? key = item;

  if (item != null) {
    final DtoChatItem? dto = _pagination?.items[item];
    cursor = dto?.cursor;
  }

  AttachmentsPaginated? fragment;

  _attachments.add(
    fragment = AttachmentsPaginated(
      initialKey: key,
      initialCursor: cursor,
      transform: ({required DtoChatItem data, Rx<ChatItem>? previous}) {
        if (previous != null) {
          return previous..value = data.value;
        }

        return Rx(data.value);
      },
      pagination: Pagination(
        onKey: (e) => e.value.id,
        fulfilled: (edges) {
          return edges.any((e) {
            if (e.value is ChatMessage) {
              final msg = e.value as ChatMessage;

              return msg.attachments.any((a) {
                if (a is ImageAttachment) {
                  return true;
                } else if (a is FileAttachment) {
                  return a.isVideo;
                } else if (a is LocalAttachment) {
                  return a.file.isImage || a.file.isSvg || a.file.isVideo;
                }

                return false;
              });
            }

            return false;
          });
        },
        provider: DriftGraphQlPageProvider(
          graphQlProvider: GraphQlPageProvider(
            reversed: true,
            fetch: ({after, before, first, last}) async {
              if (id.isLocal) {
                return Page([], PageInfo());
              }

              final Page<DtoChatItem, ChatItemsCursor> reversed =
                  await _chatRepository.messages(
                    chat.value.id,
                    after: after,
                    first: first,
                    before: before,
                    last: last,
                    onlyAttachments: true,
                  );

              return reversed;
            },
          ),
          driftProvider: DriftPageProvider(
            fetch: ({
              required after,
              required before,
              ChatItemId? around,
            }) async {
              PreciseDateTime? at;

              if (around != null) {
                final DtoChatItem? item = await get(around);
                at = item?.value.at;
              }

              return await _driftItems.attachments(
                id,
                before: before,
                after: after,
                around: at,
              );
            },
            onKey: (e) => e.value.id,
            onCursor: (e) => e?.cursor,
            isFirst: (e) {
              if (e.value.id.isLocal) {
                return null;
              }

              return chat.value.firstItem?.id == e.value.id;
            },
            isLast: (e) {
              if (e.value.id.isLocal) {
                return null;
              }

              return chat.value.lastItem?.id == e.value.id;
            },
            compare: (a, b) => a.value.key.compareTo(b.value.key),
          ),
        ),
        compare: (a, b) => a.value.key.compareTo(b.value.key),
        perPage: 10,
      ),
      onDispose: () {
        _attachments.remove(fragment);
      },
    ),
  );

  return fragment;
}