@@ -15,11 +15,11 @@ class ChannelDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
ChannelDao(MoorChatDatabase db) : super(db);
|
||||
|
||||
/// Get channel by cid
|
||||
Future<ChannelModel> getChannelByCid(String cid) async =>
|
||||
Future<ChannelModel?> getChannelByCid(String cid) async =>
|
||||
(select(channels)..where((c) => c.cid.equals(cid))).join([
|
||||
leftOuterJoin(users, channels.createdById.equalsExp(users.id)),
|
||||
]).map((rows) {
|
||||
final channel = rows.readTableOrNull(channels);
|
||||
final channel = rows.readTable(channels);
|
||||
final createdBy = rows.readTableOrNull(users);
|
||||
return channel.toChannelModel(createdBy: createdBy?.toUser());
|
||||
}).getSingleOrNull();
|
||||
@@ -30,7 +30,7 @@ class ChannelDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
/// 1. Channel Reads
|
||||
/// 2. Channel Members
|
||||
/// 3. Channel Messages -> Messages Reactions
|
||||
Future<void> deleteChannelByCids(List<String> cids) async =>
|
||||
Future<int> deleteChannelByCids(List<String> cids) async =>
|
||||
(delete(channels)..where((tbl) => tbl.cid.isIn(cids))).go();
|
||||
|
||||
/// Get the channel cids saved in the storage
|
||||
|
||||
@@ -18,7 +18,7 @@ class ChannelQueryDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
/// Creates a new channel query dao instance
|
||||
ChannelQueryDao(MoorChatDatabase db) : super(db);
|
||||
|
||||
String _computeHash(Map<String, dynamic> filter) {
|
||||
String _computeHash(Map<String, dynamic>? filter) {
|
||||
if (filter == null) {
|
||||
return 'allchannels';
|
||||
}
|
||||
@@ -58,7 +58,7 @@ class ChannelQueryDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
});
|
||||
|
||||
///
|
||||
Future<List<String>> getCachedChannelCids(Map<String, dynamic> filter) {
|
||||
Future<List<String>> getCachedChannelCids(Map<String, dynamic>? filter) {
|
||||
final hash = _computeHash(filter);
|
||||
return (select(channelQueries)..where((c) => c.queryHash.equals(hash)))
|
||||
.map((c) => c.channelCid)
|
||||
@@ -67,9 +67,9 @@ class ChannelQueryDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
|
||||
/// Get list of channels by filter, sort and paginationParams
|
||||
Future<List<ChannelModel>> getChannels({
|
||||
Map<String, dynamic> filter,
|
||||
List<SortOption<ChannelModel>> sort = const [],
|
||||
PaginationParams paginationParams,
|
||||
Map<String, dynamic>? filter,
|
||||
List<SortOption<ChannelModel>>? sort,
|
||||
PaginationParams? paginationParams,
|
||||
}) async {
|
||||
assert(() {
|
||||
if (sort != null && sort.any((it) => it.comparator == null)) {
|
||||
@@ -86,20 +86,21 @@ class ChannelQueryDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
final cachedChannels = await (query.join([
|
||||
leftOuterJoin(users, channels.createdById.equalsExp(users.id)),
|
||||
]).map((row) {
|
||||
final createdByEntity = row.readTable(users);
|
||||
final createdByEntity = row.readTableOrNull(users);
|
||||
final channelEntity = row.readTable(channels);
|
||||
return channelEntity.toChannelModel(createdBy: createdByEntity?.toUser());
|
||||
})).get();
|
||||
|
||||
final possibleSortingFields = cachedChannels.fold<List<String>>(
|
||||
ChannelModel.topLevelFields,
|
||||
(previousValue, element) =>
|
||||
{...previousValue, ...element.extraData.keys}.toList());
|
||||
ChannelModel.topLevelFields, (previousValue, element) {
|
||||
final extraData = element.extraData ?? {};
|
||||
return {...previousValue, ...extraData.keys}.toList();
|
||||
});
|
||||
|
||||
// ignore: parameter_assignments
|
||||
sort = sort
|
||||
?.where((s) => possibleSortingFields.contains(s.field))
|
||||
?.toList(growable: false);
|
||||
.toList(growable: false);
|
||||
|
||||
var chainedComparator = (ChannelModel a, ChannelModel b) {
|
||||
final dateA = a.lastMessageAt ?? a.createdAt;
|
||||
@@ -110,9 +111,9 @@ class ChannelQueryDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
if (sort != null && sort.isNotEmpty) {
|
||||
chainedComparator = (a, b) {
|
||||
int result;
|
||||
for (final comparator in sort.map((it) => it.comparator)) {
|
||||
for (final comparator in sort!.map((it) => it.comparator)) {
|
||||
try {
|
||||
result = comparator(a, b);
|
||||
result = comparator!(a, b);
|
||||
} catch (e) {
|
||||
result = 0;
|
||||
}
|
||||
@@ -125,11 +126,11 @@ class ChannelQueryDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
cachedChannels.sort(chainedComparator);
|
||||
|
||||
if (paginationParams?.offset != null && cachedChannels.isNotEmpty) {
|
||||
cachedChannels.removeRange(0, paginationParams.offset);
|
||||
cachedChannels.removeRange(0, paginationParams!.offset);
|
||||
}
|
||||
|
||||
if (paginationParams?.limit != null) {
|
||||
return cachedChannels.take(paginationParams.limit).toList();
|
||||
return cachedChannels.take(paginationParams!.limit).toList();
|
||||
}
|
||||
|
||||
return cachedChannels;
|
||||
|
||||
@@ -15,19 +15,18 @@ class ConnectionEventDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
ConnectionEventDao(MoorChatDatabase db) : super(db);
|
||||
|
||||
/// Get the latest stored connection event
|
||||
Future<Event> get connectionEvent => select(connectionEvents)
|
||||
Future<Event?> get connectionEvent => select(connectionEvents)
|
||||
.map((eventEntity) => eventEntity.toEvent())
|
||||
.getSingleOrNull();
|
||||
|
||||
/// Get the latest stored lastSyncAt
|
||||
Future<DateTime> get lastSyncAt =>
|
||||
Future<DateTime?> get lastSyncAt =>
|
||||
select(connectionEvents).getSingleOrNull().then((r) => r?.lastSyncAt);
|
||||
|
||||
/// Update stored connection event with latest data
|
||||
Future<void> updateConnectionEvent(Event event) async =>
|
||||
transaction(() async {
|
||||
Future<int> updateConnectionEvent(Event event) => transaction(() async {
|
||||
final connectionInfo = await select(connectionEvents).getSingleOrNull();
|
||||
await into(connectionEvents).insert(
|
||||
return into(connectionEvents).insert(
|
||||
ConnectionEventEntity(
|
||||
id: 1,
|
||||
lastSyncAt: connectionInfo?.lastSyncAt,
|
||||
|
||||
@@ -26,7 +26,7 @@ class MemberDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
.map((row) {
|
||||
final userEntity = row.readTable(users);
|
||||
final memberEntity = row.readTable(members);
|
||||
return memberEntity.toMember(user: userEntity?.toUser());
|
||||
return memberEntity.toMember(user: userEntity.toUser());
|
||||
}).get();
|
||||
|
||||
/// Updates all the members using the new [memberList] data
|
||||
|
||||
@@ -25,7 +25,7 @@ class MessageDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
///
|
||||
/// This will automatically delete the following linked records
|
||||
/// 1. Message Reactions
|
||||
Future<void> deleteMessageByIds(List<String> messageIds) =>
|
||||
Future<int> deleteMessageByIds(List<String> messageIds) =>
|
||||
(delete(messages)..where((tbl) => tbl.id.isIn(messageIds))).go();
|
||||
|
||||
/// Removes all the messages by matching [Messages.channelCid] in [cids]
|
||||
@@ -38,15 +38,16 @@ class MessageDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
Future<Message> _messageFromJoinRow(TypedResult rows) async {
|
||||
final userEntity = rows.readTableOrNull(_users);
|
||||
final pinnedByEntity = rows.readTableOrNull(_pinnedByUsers);
|
||||
final msgEntity = rows.readTableOrNull(messages);
|
||||
final msgEntity = rows.readTable(messages);
|
||||
final latestReactions = await _db.reactionDao.getReactions(msgEntity.id);
|
||||
final ownReactions = await _db.reactionDao.getReactionsByUserId(
|
||||
msgEntity.id,
|
||||
_db.userId,
|
||||
);
|
||||
Message quotedMessage;
|
||||
if (msgEntity.quotedMessageId != null) {
|
||||
quotedMessage = await getMessageById(msgEntity.quotedMessageId);
|
||||
Message? quotedMessage;
|
||||
final quotedMessageId = msgEntity.quotedMessageId;
|
||||
if (quotedMessageId != null) {
|
||||
quotedMessage = await getMessageById(quotedMessageId);
|
||||
}
|
||||
return msgEntity.toMessage(
|
||||
user: userEntity?.toUser(),
|
||||
@@ -58,7 +59,7 @@ class MessageDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
}
|
||||
|
||||
/// Returns a single message by matching the [Messages.id] with [id]
|
||||
Future<Message> getMessageById(String id) async =>
|
||||
Future<Message?> getMessageById(String id) async =>
|
||||
await (select(messages).join([
|
||||
leftOuterJoin(_users, messages.userId.equalsExp(_users.id)),
|
||||
leftOuterJoin(_pinnedByUsers,
|
||||
@@ -86,7 +87,7 @@ class MessageDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
/// [Messages.parentId] with [parentId]
|
||||
Future<List<Message>> getThreadMessagesByParentId(
|
||||
String parentId, {
|
||||
PaginationParams options,
|
||||
PaginationParams? options,
|
||||
}) async {
|
||||
final msgList = await Future.wait(await (select(messages).join([
|
||||
leftOuterJoin(_users, messages.userId.equalsExp(_users.id)),
|
||||
@@ -102,7 +103,7 @@ class MessageDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
if (msgList.isNotEmpty) {
|
||||
if (options?.lessThan != null) {
|
||||
final lessThanIndex = msgList.indexWhere(
|
||||
(m) => m.id == options.lessThan,
|
||||
(m) => m.id == options!.lessThan,
|
||||
);
|
||||
if (lessThanIndex != -1) {
|
||||
msgList.removeRange(lessThanIndex, msgList.length);
|
||||
@@ -110,14 +111,14 @@ class MessageDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
}
|
||||
if (options?.greaterThanOrEqual != null) {
|
||||
final greaterThanIndex = msgList.indexWhere(
|
||||
(m) => m.id == options.greaterThanOrEqual,
|
||||
(m) => m.id == options!.greaterThanOrEqual,
|
||||
);
|
||||
if (greaterThanIndex != -1) {
|
||||
msgList.removeRange(0, greaterThanIndex);
|
||||
}
|
||||
}
|
||||
if (options?.limit != null) {
|
||||
return msgList.take(options.limit).toList();
|
||||
return msgList.take(options!.limit).toList();
|
||||
}
|
||||
}
|
||||
return msgList;
|
||||
@@ -127,7 +128,7 @@ class MessageDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
/// [Messages.channelCid] with [parentId]
|
||||
Future<List<Message>> getMessagesByCid(
|
||||
String cid, {
|
||||
PaginationParams messagePagination,
|
||||
PaginationParams? messagePagination,
|
||||
}) async {
|
||||
final msgList = await Future.wait(await (select(messages).join([
|
||||
leftOuterJoin(_users, messages.userId.equalsExp(_users.id)),
|
||||
@@ -145,7 +146,7 @@ class MessageDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
if (msgList.isNotEmpty) {
|
||||
if (messagePagination?.lessThan != null) {
|
||||
final lessThanIndex = msgList.indexWhere(
|
||||
(m) => m.id == messagePagination.lessThan,
|
||||
(m) => m.id == messagePagination!.lessThan,
|
||||
);
|
||||
if (lessThanIndex != -1) {
|
||||
msgList.removeRange(lessThanIndex, msgList.length);
|
||||
@@ -153,14 +154,14 @@ class MessageDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
}
|
||||
if (messagePagination?.greaterThanOrEqual != null) {
|
||||
final greaterThanIndex = msgList.indexWhere(
|
||||
(m) => m.id == messagePagination.greaterThanOrEqual,
|
||||
(m) => m.id == messagePagination!.greaterThanOrEqual,
|
||||
);
|
||||
if (greaterThanIndex != -1) {
|
||||
msgList.removeRange(0, greaterThanIndex);
|
||||
}
|
||||
}
|
||||
if (messagePagination?.limit != null) {
|
||||
return msgList.take(messagePagination.limit).toList();
|
||||
return msgList.take(messagePagination!.limit).toList();
|
||||
}
|
||||
}
|
||||
return msgList;
|
||||
@@ -168,17 +169,13 @@ class MessageDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
|
||||
/// Updates the message data of a particular channel with
|
||||
/// the new [messageList] data
|
||||
Future<void> updateMessages(String cid, List<Message> messageList) async {
|
||||
if (messageList == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
return batch((batch) {
|
||||
batch.insertAll(
|
||||
messages,
|
||||
messageList.map((it) => it.toEntity(cid: cid)).toList(),
|
||||
mode: InsertMode.insertOrReplace,
|
||||
Future<void> updateMessages(String cid, List<Message> messageList) => batch(
|
||||
(batch) {
|
||||
batch.insertAll(
|
||||
messages,
|
||||
messageList.map((it) => it.toEntity(cid: cid)).toList(),
|
||||
mode: InsertMode.insertOrReplace,
|
||||
);
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,15 +38,16 @@ class PinnedMessageDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
Future<Message> _messageFromJoinRow(TypedResult rows) async {
|
||||
final userEntity = rows.readTableOrNull(users);
|
||||
final pinnedByEntity = rows.readTableOrNull(_pinnedByUsers);
|
||||
final msgEntity = rows.readTableOrNull(pinnedMessages);
|
||||
final msgEntity = rows.readTable(pinnedMessages);
|
||||
final latestReactions = await _db.reactionDao.getReactions(msgEntity.id);
|
||||
final ownReactions = await _db.reactionDao.getReactionsByUserId(
|
||||
msgEntity.id,
|
||||
_db.userId,
|
||||
);
|
||||
Message quotedMessage;
|
||||
if (msgEntity.quotedMessageId != null) {
|
||||
quotedMessage = await getMessageById(msgEntity.quotedMessageId);
|
||||
Message? quotedMessage;
|
||||
final quotedMessageId = msgEntity.quotedMessageId;
|
||||
if (quotedMessageId != null) {
|
||||
quotedMessage = await getMessageById(quotedMessageId);
|
||||
}
|
||||
return msgEntity.toMessage(
|
||||
user: userEntity?.toUser(),
|
||||
@@ -58,7 +59,7 @@ class PinnedMessageDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
}
|
||||
|
||||
/// Returns a single message by matching the [PinnedMessages.id] with [id]
|
||||
Future<Message> getMessageById(String id) async =>
|
||||
Future<Message?> getMessageById(String id) async =>
|
||||
await (select(pinnedMessages).join([
|
||||
leftOuterJoin(_users, pinnedMessages.userId.equalsExp(_users.id)),
|
||||
leftOuterJoin(_pinnedByUsers,
|
||||
@@ -86,7 +87,7 @@ class PinnedMessageDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
/// [PinnedMessages.parentId] with [parentId]
|
||||
Future<List<Message>> getThreadMessagesByParentId(
|
||||
String parentId, {
|
||||
PaginationParams options,
|
||||
PaginationParams? options,
|
||||
}) async {
|
||||
final msgList = await Future.wait(await (select(pinnedMessages).join([
|
||||
leftOuterJoin(_users, pinnedMessages.userId.equalsExp(_users.id)),
|
||||
@@ -102,7 +103,7 @@ class PinnedMessageDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
if (msgList.isNotEmpty) {
|
||||
if (options?.lessThan != null) {
|
||||
final lessThanIndex = msgList.indexWhere(
|
||||
(m) => m.id == options.lessThan,
|
||||
(m) => m.id == options!.lessThan,
|
||||
);
|
||||
if (lessThanIndex != -1) {
|
||||
msgList.removeRange(lessThanIndex, msgList.length);
|
||||
@@ -110,14 +111,14 @@ class PinnedMessageDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
}
|
||||
if (options?.greaterThanOrEqual != null) {
|
||||
final greaterThanIndex = msgList.indexWhere(
|
||||
(m) => m.id == options.greaterThanOrEqual,
|
||||
(m) => m.id == options!.greaterThanOrEqual,
|
||||
);
|
||||
if (greaterThanIndex != -1) {
|
||||
msgList.removeRange(0, greaterThanIndex);
|
||||
}
|
||||
}
|
||||
if (options?.limit != null) {
|
||||
return msgList.take(options.limit).toList();
|
||||
return msgList.take(options!.limit).toList();
|
||||
}
|
||||
}
|
||||
return msgList;
|
||||
@@ -127,7 +128,7 @@ class PinnedMessageDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
/// [PinnedMessages.channelCid] with [parentId]
|
||||
Future<List<Message>> getMessagesByCid(
|
||||
String cid, {
|
||||
PaginationParams messagePagination,
|
||||
PaginationParams? messagePagination,
|
||||
}) async {
|
||||
final msgList = await Future.wait(await (select(pinnedMessages).join([
|
||||
leftOuterJoin(_users, pinnedMessages.userId.equalsExp(_users.id)),
|
||||
@@ -144,7 +145,7 @@ class PinnedMessageDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
if (msgList.isNotEmpty) {
|
||||
if (messagePagination?.lessThan != null) {
|
||||
final lessThanIndex = msgList.indexWhere(
|
||||
(m) => m.id == messagePagination.lessThan,
|
||||
(m) => m.id == messagePagination!.lessThan,
|
||||
);
|
||||
if (lessThanIndex != -1) {
|
||||
msgList.removeRange(lessThanIndex, msgList.length);
|
||||
@@ -152,14 +153,14 @@ class PinnedMessageDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
}
|
||||
if (messagePagination?.greaterThanOrEqual != null) {
|
||||
final greaterThanIndex = msgList.indexWhere(
|
||||
(m) => m.id == messagePagination.greaterThanOrEqual,
|
||||
(m) => m.id == messagePagination!.greaterThanOrEqual,
|
||||
);
|
||||
if (greaterThanIndex != -1) {
|
||||
msgList.removeRange(0, greaterThanIndex);
|
||||
}
|
||||
}
|
||||
if (messagePagination?.limit != null) {
|
||||
return msgList.take(messagePagination.limit).toList();
|
||||
return msgList.take(messagePagination!.limit).toList();
|
||||
}
|
||||
}
|
||||
return msgList;
|
||||
@@ -167,17 +168,13 @@ class PinnedMessageDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
|
||||
/// Updates the message data of a particular channel with
|
||||
/// the new [messageList] data
|
||||
Future<void> updateMessages(String cid, List<Message> messageList) async {
|
||||
if (messageList == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
return batch((batch) {
|
||||
batch.insertAll(
|
||||
pinnedMessages,
|
||||
messageList.map((it) => it.toPinnedEntity(cid: cid)).toList(),
|
||||
mode: InsertMode.insertOrReplace,
|
||||
Future<void> updateMessages(String cid, List<Message> messageList) => batch(
|
||||
(batch) {
|
||||
batch.insertAll(
|
||||
pinnedMessages,
|
||||
messageList.map((it) => it.toPinnedEntity(cid: cid)).toList(),
|
||||
mode: InsertMode.insertOrReplace,
|
||||
);
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ class ReactionDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
..orderBy([OrderingTerm.asc(reactions.createdAt)]))
|
||||
.map((rows) {
|
||||
final userEntity = rows.readTableOrNull(users);
|
||||
final reactionEntity = rows.readTableOrNull(reactions);
|
||||
final reactionEntity = rows.readTable(reactions);
|
||||
return reactionEntity.toReaction(user: userEntity?.toUser());
|
||||
}).get();
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ class ReadDao extends DatabaseAccessor<MoorChatDatabase> with _$ReadDaoMixin {
|
||||
.map((row) {
|
||||
final userEntity = row.readTable(users);
|
||||
final readEntity = row.readTable(reads);
|
||||
return readEntity.toRead(user: userEntity?.toUser());
|
||||
return readEntity.toRead(user: userEntity.toUser());
|
||||
}).get();
|
||||
|
||||
/// Updates the read data of a particular channel with
|
||||
|
||||
Reference in New Issue
Block a user