Skip to content

Commit

Permalink
refactor ConversationDao
Browse files Browse the repository at this point in the history
  • Loading branch information
YeungKC committed Jul 7, 2023
1 parent c8e548c commit 106eb43
Show file tree
Hide file tree
Showing 27 changed files with 2,252 additions and 2,248 deletions.
2 changes: 1 addition & 1 deletion lib/crypto/signal/dao/session_dao.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:collection/collection.dart';
import 'package:drift/drift.dart';

import '../../../utils/extension/extension.dart';
import '../signal_database.dart';

part 'session_dao.g.dart';
Expand Down
1 change: 1 addition & 0 deletions lib/db/custom_vm_database_wrapper.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:collection/collection.dart';
import 'package:drift/drift.dart';
import 'package:drift/native.dart';
import 'package:flutter/foundation.dart';
Expand Down
129 changes: 47 additions & 82 deletions lib/db/dao/conversation_dao.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import 'participant_dao.dart';

part 'conversation_dao.g.dart';

@DriftAccessor()
@DriftAccessor(include: {'../moor/dao/conversation.drift'})
class ConversationDao extends DatabaseAccessor<MixinDatabase>
with _$ConversationDaoMixin {
ConversationDao(super.db);
Expand All @@ -26,8 +26,7 @@ class ConversationDao extends DatabaseAccessor<MixinDatabase>
.instance.updateConversationIdStream
.asyncMap((event) => allUnseenIgnoreMuteMessageCount().getSingle());

Selectable<int> allUnseenIgnoreMuteMessageCount() =>
db.baseUnseenMessageCount(
Selectable<int> allUnseenIgnoreMuteMessageCount() => _baseUnseenMessageCount(
(conversation, owner, __) {
final now = const MillisDateConverter().toSql(DateTime.now());
final groupExpression =
Expand Down Expand Up @@ -66,45 +65,6 @@ class ConversationDao extends DatabaseAccessor<MixinDatabase>
],
);

Selectable<int> _baseConversationItemCount(
BaseConversationItemCount$where where,
) =>
db.baseConversationItemCount((conversation, owner, circleConversation) =>
where(conversation, owner, circleConversation));

Selectable<ConversationItem> _baseConversationItems(
Expression<bool> Function(
Conversations conversation,
Users owner,
Messages message,
Users lastMessageSender,
Snapshots snapshot,
Users participant)
where,
Limit limit) =>
db.baseConversationItems(
(
Conversations conversation,
Users owner,
Messages message,
Users lastMessageSender,
Snapshots snapshot,
Users participant,
ExpiredMessages em,
) =>
where(
conversation,
owner,
message,
lastMessageSender,
snapshot,
participant,
),
(conversation, _, __, ___, ____, _____, em) =>
_baseConversationItemOrder(conversation),
(_, __, ___, ____, ______, _______, em) => limit,
);

Expression<bool> _conversationPredicateByCategory(SlideCategoryType category,
[Conversations? conversation, Users? owner]) {
final Expression<bool> predicate;
Expand Down Expand Up @@ -150,27 +110,35 @@ class ConversationDao extends DatabaseAccessor<MixinDatabase>
_conversationPredicateByCategory(category, conversation, owner))
.getSingle();

Future<List<ConversationItem>> conversationItemsByCategory(
Selectable<ConversationItem> conversationItemsByCategory(
SlideCategoryType category,
int limit,
int offset,
) =>
_baseConversationItems(
(conversation, owner, message, lastMessageSender, snapshot,
participant) =>
_conversationPredicateByCategory(category, conversation, owner),
Limit(limit, offset),
).get();
(conversation, owner, lastMessage, lastMessageSender, snapshot,
participant, em) =>
_conversationPredicateByCategory(category, conversation, owner),
(conversation, owner, lastMessage, lastMessageSender, snapshot,
participant, em) =>
_baseConversationItemOrder(conversation),
(conversation, owner, lastMessage, lastMessageSender, snapshot,
participant, em) =>
Limit(limit, offset));

Selectable<ConversationItem> unseenConversationByCategory(
SlideCategoryType category) =>
_baseConversationItems(
(conversation, owner, message, lastMessageSender, snapshot,
participant) =>
_conversationPredicateByCategory(category, conversation, owner) &
conversation.unseenMessageCount.isBiggerThanValue(0),
maxLimit,
);
(conversation, owner, lastMessage, lastMessageSender, snapshot,
participant, em) =>
_conversationPredicateByCategory(category, conversation, owner) &
conversation.unseenMessageCount.isBiggerThanValue(0),
(conversation, owner, lastMessage, lastMessageSender, snapshot,
participant, em) =>
_baseConversationItemOrder(conversation),
(conversation, owner, lastMessage, lastMessageSender, snapshot,
participant, em) =>
maxLimit);

Future<bool> _conversationHasData(Expression<bool> predicate) => db.hasData(
db.conversations,
Expand All @@ -180,39 +148,43 @@ class ConversationDao extends DatabaseAccessor<MixinDatabase>
],
predicate);

Selectable<BaseUnseenConversationCountResult> _baseUnseenConversationCount(
Expression<bool> Function(Conversations conversation, Users owner)
where) =>
db.baseUnseenConversationCount((conversation, owner) =>
conversation.unseenMessageCount.isBiggerThanValue(0) &
where(conversation, owner));

Selectable<BaseUnseenConversationCountResult>
unseenConversationCountByCategory(SlideCategoryType category) =>
_baseUnseenConversationCount((conversation, owner) =>
conversation.unseenMessageCount.isBiggerThanValue(0) &
_conversationPredicateByCategory(category, conversation, owner));

Selectable<ConversationItem> conversationItem(String conversationId) =>
_baseConversationItems(
(conversation, _, __, ___, ____, ______) =>
conversation.conversationId.equals(conversationId),
Limit(1, null),
);
(conversation, owner, lastMessage, lastMessageSender, snapshot,
participant, em) =>
conversation.conversationId.equals(conversationId),
(conversation, owner, lastMessage, lastMessageSender, snapshot,
participant, em) =>
_baseConversationItemOrder(conversation),
(conversation, owner, lastMessage, lastMessageSender, snapshot,
participant, em) =>
Limit(1, null));

Selectable<ConversationItem> conversationItems() => _baseConversationItems(
(conversation, _, __, ___, ____, ______) =>
conversation.category.isIn(['CONTACT', 'GROUP']) &
conversation.status.equalsValue(ConversationStatus.success),
maxLimit,
);
(conversation, owner, lastMessage, lastMessageSender, snapshot,
participant, em) =>
conversation.category.isIn(['CONTACT', 'GROUP']) &
conversation.status.equalsValue(ConversationStatus.success),
(conversation, owner, lastMessage, lastMessageSender, snapshot,
participant, em) =>
_baseConversationItemOrder(conversation),
(conversation, owner, lastMessage, lastMessageSender, snapshot,
participant, em) =>
maxLimit);

Selectable<int> conversationsCountByCircleId(String circleId) =>
_baseConversationItemCount((_, __, circleConversation) =>
circleConversation.circleId.equals(circleId));

Selectable<ConversationItem> conversationsByCircleId(
String circleId, int limit, int offset) =>
db.baseConversationItemsByCircleId(
_baseConversationItemsByCircleId(
(conversation, o, circleConversation, lm, ls, s, p, em) =>
circleConversation.circleId.equals(circleId),
(conversation, _, __, ___, ____, _____, _____i, em) =>
Expand All @@ -234,7 +206,7 @@ class ConversationDao extends DatabaseAccessor<MixinDatabase>
db.circleConversations.circleId.equals(circleId));

Selectable<ConversationItem> unseenConversationsByCircleId(String circleId) =>
db.baseConversationItemsByCircleId(
_baseConversationItemsByCircleId(
(conversation, o, circleConversation, lm, ls, s, p, em) =>
circleConversation.circleId.equals(circleId) &
conversation.unseenMessageCount.isBiggerThanValue(0),
Expand Down Expand Up @@ -310,7 +282,7 @@ class ConversationDao extends DatabaseAccessor<MixinDatabase>
SlideCategoryState? category,
}) {
if (category?.type == SlideCategoryType.circle) {
return db.fuzzySearchConversationInCircle(
return _fuzzySearchConversationInCircle(
query.trim().escapeSql(),
category!.id,
(conversation, owner, message, _, cc, __) => filterUnseen
Expand All @@ -319,7 +291,7 @@ class ConversationDao extends DatabaseAccessor<MixinDatabase>
(conversation, owner, message, _, cc, __) => Limit(limit, null),
);
}
return db.fuzzySearchConversation(query.trim().escapeSql(),
return _fuzzySearchConversation(query.trim().escapeSql(),
(Conversations conversation, Users owner, Messages message, _, __) {
Expression<bool> predicate = ignoreWhere;
switch (category?.type) {
Expand Down Expand Up @@ -350,7 +322,7 @@ class ConversationDao extends DatabaseAccessor<MixinDatabase>

Selectable<SearchConversationItem> searchConversationItemByIn(
List<String> ids) =>
db.searchConversationItemByIn(ids, (conversation, _, __, ___, ____) {
_searchConversationItemByIn(ids, (conversation, _, __, ___, ____) {
if (ids.isEmpty) return ignoreOrderBy;

final conversationId =
Expand All @@ -369,9 +341,6 @@ class ConversationDao extends DatabaseAccessor<MixinDatabase>
..limit(1))
.map((row) => row.read(db.conversations.announcement));

Selectable<ConversationStorageUsage> conversationStorageUsage() =>
db.conversationStorageUsage();

Future<void> updateConversation(
ConversationResponse conversation, String currentUserId) {
var ownerId = conversation.creatorId;
Expand Down Expand Up @@ -482,10 +451,6 @@ class ConversationDao extends DatabaseAccessor<MixinDatabase>
db.conversations.conversationId.equals(conversationId),
);

Selectable<GroupMinimal> findTheSameConversations(
String selfId, String userId) =>
db.findSameConversations(selfId, userId);

Future<int> updateConversationExpireIn(String conversationId, int expireIn) =>
(update(db.conversations)
..where((tbl) => tbl.conversationId.equals(conversationId)))
Expand Down
Loading

0 comments on commit 106eb43

Please sign in to comment.