Cleaned up code, implemented message queue to notify subscribers of document updates.

This commit is contained in:
Anton Stubenbord
2023-02-06 01:04:13 +01:00
parent 337c178be8
commit 4d7fab1839
111 changed files with 1412 additions and 1029 deletions

View File

@@ -146,7 +146,20 @@ class DocumentFilter extends Equatable {
///
/// Checks whether the properties of [document] match the current filter criteria.
///
bool includes(DocumentModel document) {}
bool matches(DocumentModel document) {
return correspondent.matches(document.correspondent) &&
documentType.matches(document.documentType) &&
storagePath.matches(document.storagePath) &&
tags.matches(document.tags) &&
created.matches(document.created) &&
added.matches(document.added) &&
modified.matches(document.modified) &&
query.matches(
title: document.title,
content: document.content,
asn: document.archiveSerialNumber,
);
}
int get appliedFiltersCount => [
documentType != initial.documentType,

View File

@@ -1,6 +1,5 @@
import 'package:equatable/equatable.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:paperless_api/src/models/document_model.dart';
const pageRegex = r".*page=(\d+).*";
@@ -108,5 +107,10 @@ class PagedSearchResult<T> extends Equatable {
}
@override
List<Object?> get props => [count, next, previous, results];
List<Object?> get props => [
count,
next,
previous,
results,
];
}

View File

@@ -52,4 +52,17 @@ class AbsoluteDateRangeQuery extends DateRangeQuery {
@override
Map<String, dynamic> toJson() => _$AbsoluteDateRangeQueryToJson(this);
@override
bool matches(DateTime dt) {
//TODO: Check if after and before are inclusive or exclusive definitions.
bool matches = true;
if (after != null) {
matches &= dt.isAfter(after!) || dt == after;
}
if (before != null) {
matches &= dt.isBefore(before!) || dt == before;
}
return matches;
}
}

View File

@@ -7,4 +7,6 @@ abstract class DateRangeQuery extends Equatable {
Map<String, String> toQueryParameter(DateRangeQueryField field);
Map<String, dynamic> toJson();
bool matches(DateTime dt);
}

View File

@@ -1,3 +1,4 @@
import 'package:jiffy/jiffy.dart';
import 'package:json_annotation/json_annotation.dart';
import 'date_range_query.dart';
@@ -35,9 +36,28 @@ class RelativeDateRangeQuery extends DateRangeQuery {
);
}
/// Returns the datetime when subtracting the offset given the unit from now.
DateTime get dateTime {
switch (unit) {
case DateRangeUnit.day:
return Jiffy().subtract(days: offset).dateTime;
case DateRangeUnit.week:
return Jiffy().subtract(weeks: offset).dateTime;
case DateRangeUnit.month:
return Jiffy().subtract(months: offset).dateTime;
case DateRangeUnit.year:
return Jiffy().subtract(years: offset).dateTime;
}
}
@override
Map<String, dynamic> toJson() => _$RelativeDateRangeQueryToJson(this);
factory RelativeDateRangeQuery.fromJson(Map<String, dynamic> json) =>
_$RelativeDateRangeQueryFromJson(json);
@override
bool matches(DateTime dt) {
return dt.isAfter(dateTime) || dt == dateTime;
}
}

View File

@@ -14,4 +14,7 @@ class UnsetDateRangeQuery extends DateRangeQuery {
Map<String, dynamic> toJson() {
return {};
}
@override
bool matches(DateTime dt) => true;
}

View File

@@ -1,5 +1,4 @@
import 'package:equatable/equatable.dart';
import 'package:flutter/foundation.dart';
import 'package:json_annotation/json_annotation.dart';
part 'id_query_parameter.g.dart';
@@ -19,9 +18,7 @@ class IdQueryParameter extends Equatable {
: assignmentStatus = 0,
id = null;
const IdQueryParameter.fromId(int? id)
: assignmentStatus = null,
id = id;
const IdQueryParameter.fromId(this.id) : assignmentStatus = null;
const IdQueryParameter.unset() : this.fromId(null);
@@ -45,6 +42,13 @@ class IdQueryParameter extends Equatable {
return params;
}
bool matches(int? id) {
return onlyAssigned && id != null ||
onlyNotAssigned && id == null ||
isSet && id == this.id ||
isUnset;
}
@override
List<Object?> get props => [assignmentStatus, id];

View File

@@ -27,4 +27,9 @@ class AnyAssignedTagsQuery extends TagsQuery {
factory AnyAssignedTagsQuery.fromJson(Map<String, dynamic> json) =>
_$AnyAssignedTagsQueryFromJson(json);
@override
bool matches(Iterable<int> ids) {
return ids.isNotEmpty;
}
}

View File

@@ -1,5 +1,5 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:collection/collection.dart';
import 'exclude_tag_id_query.dart';
import 'include_tag_id_query.dart';
import 'tag_id_query.dart';
@@ -85,4 +85,10 @@ class IdsTagsQuery extends TagsQuery {
(json['queries'] as List).map((e) => TagIdQuery.fromJson(e)),
);
}
@override
bool matches(Iterable<int> ids) {
return includedIds.toSet().difference(ids.toSet()).isEmpty &&
excludedIds.toSet().intersection(ids.toSet()).isEmpty;
}
}

View File

@@ -14,4 +14,9 @@ class OnlyNotAssignedTagsQuery extends TagsQuery {
Map<String, dynamic> toJson() {
return {};
}
@override
bool matches(Iterable<int> ids) {
return ids.isEmpty;
}
}

View File

@@ -4,4 +4,6 @@ abstract class TagsQuery extends Equatable {
const TagsQuery();
Map<String, String> toQueryParameter();
Map<String, dynamic> toJson();
bool matches(Iterable<int> ids);
}

View File

@@ -58,6 +58,26 @@ class TextQuery extends Equatable {
return null;
}
bool matches({
required String title,
String? content,
int? asn,
}) {
if (queryText?.isEmpty ?? true) return true;
switch (queryType) {
case QueryType.title:
return title.contains(queryText!);
case QueryType.titleAndContent:
return title.contains(queryText!) ||
(content?.contains(queryText!) ?? false);
case QueryType.extended:
//TODO: Implement. Might be too complex...
return true;
case QueryType.asn:
return int.tryParse(queryText!) == asn;
}
}
Map<String, dynamic> toJson() => _$TextQueryToJson(this);
factory TextQuery.fromJson(Map<String, dynamic> json) =>

View File

@@ -19,6 +19,7 @@ dependencies:
intl: ^0.17.0
dio: ^4.0.6
collection: ^1.17.0
jiffy: ^5.0.0
dev_dependencies:
flutter_test: