[LLC, Persistence] Add doc comments

Signed-off-by: Sahil Kumar <[email protected]>
This commit is contained in:
Sahil Kumar
2021-01-28 19:48:54 +05:30
parent 6e5894eebe
commit 86a122aa23
30 changed files with 154 additions and 68 deletions
@@ -72,7 +72,7 @@ class Message {
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
final List<Reaction> ownReactions;
/// The ID of the parent message, if the message is a reply.
/// The ID of the parent message, if the message is a thread reply.
final String parentId;
/// A quoted reply message
@@ -29,7 +29,7 @@ class Reaction {
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
final String userId;
/// The score of the reaction (ie. number of reactions sent)
/// Reaction custom extraData
@JsonKey(includeIfNull: false)
final Map<String, dynamic> extraData;
@@ -2,7 +2,8 @@ import 'dart:convert';
import 'package:moor/moor.dart';
///
/// Maps a [List] of type [T] into a [String] understood
/// by the sqlite backend.
class ListConverter<T> extends TypeConverter<List<T>, String> {
@override
List<T> mapToDart(fromDb) {
@@ -2,7 +2,8 @@ import 'dart:convert';
import 'package:moor/moor.dart';
///
/// Maps a [Map] of type [String], [T] into a [String] understood
/// by the sqlite backend.
class MapConverter<T> extends TypeConverter<Map<String, T>, String> {
@override
Map<String, T> mapToDart(fromDb) {
@@ -1,7 +1,8 @@
import 'package:moor/moor.dart';
import 'package:stream_chat/stream_chat.dart';
///
/// Maps a [MessageSendingStatus] into a [int] understood
/// by the sqlite backend.
class MessageSendingStatusConverter
extends TypeConverter<MessageSendingStatus, int> {
@override
@@ -7,11 +7,11 @@ import '../mapper/mapper.dart';
part 'channel_dao.g.dart';
///
/// The Data Access Object for operations in [Channels] table.
@UseDao(tables: [Channels, Users])
class ChannelDao extends DatabaseAccessor<MoorChatDatabase>
with _$ChannelDaoMixin {
///
/// Creates a new channel dao instance
ChannelDao(MoorChatDatabase db) : super(db);
/// Get channel by cid
@@ -44,7 +44,7 @@ class ChannelDao extends DatabaseAccessor<MoorChatDatabase>
.get();
}
///
/// Updates all the channels using the new [channelList] data
Future<void> updateChannels(List<ChannelModel> channelList) {
return batch(
(it) => it.insertAll(
@@ -10,11 +10,11 @@ import '../mapper/mapper.dart';
part 'channel_query_dao.g.dart';
///
/// The Data Access Object for operations in [ChannelQueries] table.
@UseDao(tables: [ChannelQueries, Channels, Users])
class ChannelQueryDao extends DatabaseAccessor<MoorChatDatabase>
with _$ChannelQueryDaoMixin {
///
/// Creates a new channel query dao instance
ChannelQueryDao(this._db) : super(_db);
final MoorChatDatabase _db;
@@ -7,11 +7,11 @@ import '../mapper/mapper.dart';
part 'connection_event_dao.g.dart';
///
/// The Data Access Object for operations in [ConnectionEvents] table.
@UseDao(tables: [ConnectionEvents, Users])
class ConnectionEventDao extends DatabaseAccessor<MoorChatDatabase>
with _$ConnectionEventDaoMixin {
///
/// Creates a new connection event dao instance
ConnectionEventDao(MoorChatDatabase db) : super(db);
/// Get the latest stored connection event
@@ -9,11 +9,11 @@ import '../mapper/mapper.dart';
part 'member_dao.g.dart';
///
/// The Data Access Object for operations in [Members] table.
@UseDao(tables: [Members, Users])
class MemberDao extends DatabaseAccessor<MoorChatDatabase>
with _$MemberDaoMixin {
///
/// Creates a new member dao instance
MemberDao(MoorChatDatabase db) : super(db);
/// Get all members where [members.channelCid] matches [cid]
@@ -30,7 +30,7 @@ class MemberDao extends DatabaseAccessor<MoorChatDatabase>
}).get();
}
///
/// Updates all the members using the new [memberList] data
Future<void> updateMembers(String cid, List<Member> memberList) async {
return batch(
(it) => it.insertAll(
@@ -7,11 +7,11 @@ import '../mapper/mapper.dart';
part 'message_dao.g.dart';
///
/// The Data Access Object for operations in [Messages] table.
@UseDao(tables: [Messages, Users])
class MessageDao extends DatabaseAccessor<MoorChatDatabase>
with _$MessageDaoMixin {
///
/// Creates a new message dao instance
MessageDao(this._db) : super(_db);
final MoorChatDatabase _db;
@@ -52,7 +52,7 @@ class MessageDao extends DatabaseAccessor<MoorChatDatabase>
);
}
///
/// Returns a single message by matching the [messages.id] with [id]
Future<Message> getMessageById(String id) async {
return await (select(messages).join([
leftOuterJoin(users, messages.userId.equalsExp(users.id)),
@@ -62,7 +62,8 @@ class MessageDao extends DatabaseAccessor<MoorChatDatabase>
.getSingle();
}
///
/// Returns all the messages of a particular thread by matching
/// [messages.channelCid] with [cid]
Future<List<Message>> getThreadMessages(String cid) async {
return Future.wait(await (select(messages).join([
leftOuterJoin(users, messages.userId.equalsExp(users.id)),
@@ -74,7 +75,8 @@ class MessageDao extends DatabaseAccessor<MoorChatDatabase>
.get());
}
///
/// Returns all the messages of a particular thread by matching
/// [messages.parentId] with [parentId]
Future<List<Message>> getThreadMessagesByParentId(
String parentId, {
String lessThan,
@@ -94,7 +96,8 @@ class MessageDao extends DatabaseAccessor<MoorChatDatabase>
return msgList;
}
///
/// Returns all the messages of a channel by matching
/// [messages.channelCid] with [parentId]
Future<List<Message>> getMessagesByCid(
String cid, {
PaginationParams messagePagination,
@@ -131,7 +134,8 @@ class MessageDao extends DatabaseAccessor<MoorChatDatabase>
return msgList;
}
///
/// Updates the message data of a particular channel with
/// the new [messageList] data
Future<void> updateMessages(String cid, List<Message> messageList) {
return batch((batch) {
batch.insertAll(
@@ -7,14 +7,15 @@ import '../mapper/mapper.dart';
part 'reaction_dao.g.dart';
///
/// The Data Access Object for operations in [Reactions] table.
@UseDao(tables: [Reactions, Users])
class ReactionDao extends DatabaseAccessor<MoorChatDatabase>
with _$ReactionDaoMixin {
///
/// Creates a new reaction dao instance
ReactionDao(MoorChatDatabase db) : super(db);
///
/// Returns all the reactions of a particular message by matching
/// [reactions.messageId] with [messageId]
Future<List<Reaction>> getReactions(String messageId) {
return (select(reactions).join([
leftOuterJoin(users, reactions.userId.equalsExp(users.id)),
@@ -28,7 +29,10 @@ class ReactionDao extends DatabaseAccessor<MoorChatDatabase>
}).get();
}
///
/// Returns all the reactions of a particular message
/// added by a particular user by matching
/// [reactions.messageId] with [messageId] and
/// [reactions.userId] with [userId]
Future<List<Reaction>> getReactionsByUserId(
String messageId,
String userId,
@@ -37,7 +41,7 @@ class ReactionDao extends DatabaseAccessor<MoorChatDatabase>
return reactions.where((it) => it.userId == userId).toList();
}
///
/// Updates the reactions data with the new [reactionList] data
Future<void> updateReactions(List<Reaction> reactionList) {
return batch((it) {
it.insertAll(
@@ -7,10 +7,10 @@ import '../mapper/mapper.dart';
part 'read_dao.g.dart';
///
/// The Data Access Object for operations in [Reads] table.
@UseDao(tables: [Reads, Users])
class ReadDao extends DatabaseAccessor<MoorChatDatabase> with _$ReadDaoMixin {
///
/// Creates a new read dao instance
ReadDao(MoorChatDatabase db) : super(db);
/// Get all reads where [reads.channelCid] matches [cid]
@@ -29,7 +29,8 @@ class ReadDao extends DatabaseAccessor<MoorChatDatabase> with _$ReadDaoMixin {
}).get();
}
///
/// Updates the read data of a particular channel with
/// the new [readList] data
Future<void> updateReads(String cid, List<Read> readList) {
return batch(
(it) => it.insertAll(
@@ -6,13 +6,13 @@ import '../mapper/user_mapper.dart';
part 'user_dao.g.dart';
///
/// The Data Access Object for operations in [Users] table.
@UseDao(tables: [Users])
class UserDao extends DatabaseAccessor<MoorChatDatabase> with _$UserDaoMixin {
///
/// Creates a new user dao instance
UserDao(MoorChatDatabase db) : super(db);
///
/// Updates the users data with the new [userList] data
Future<void> updateUsers(List<User> userList) {
return batch(
(it) => it.insertAll(
@@ -21,7 +21,7 @@ LazyDatabase _openConnection(
});
}
///
/// A chat database implemented using moor
@UseMoor(tables: [
Channels,
Messages,
@@ -42,7 +42,7 @@ LazyDatabase _openConnection(
ConnectionEventDao,
])
class MoorChatDatabase extends _$MoorChatDatabase {
/// Instantiate a new database instance
/// Creates a new moor chat database instance
MoorChatDatabase(
this._userId, {
logStatements = false,
@@ -1,9 +1,12 @@
import 'package:moor/moor.dart';
/// Represents a [ChannelQueries] table in [MoorChatDatabase].
@DataClassName('ChannelQueryEntity')
class ChannelQueries extends Table {
/// The unique hash of this query
TextColumn get queryHash => text()();
/// The channel cid of this query
TextColumn get channelCid => text()();
@override
@@ -1,30 +1,43 @@
import 'package:moor/moor.dart';
import 'package:stream_chat_persistence/src/converter/map_converter.dart';
/// Represents a [Channels] table in [MoorChatDatabase].
@DataClassName('ChannelEntity')
class Channels extends Table {
/// The id of this channel
TextColumn get id => text()();
/// The type of this channel
TextColumn get type => text()();
/// The cid of this channel
TextColumn get cid => text()();
/// The channel configuration data
TextColumn get config => text().map(MapConverter<Object>())();
/// True if this channel entity is frozen
BoolColumn get frozen => boolean().withDefault(Constant(false))();
/// The date of the last message
DateTimeColumn get lastMessageAt => dateTime().nullable()();
/// The date of channel creation
DateTimeColumn get createdAt => dateTime().nullable()();
/// The date of the last channel update
DateTimeColumn get updatedAt => dateTime().nullable()();
/// The date of channel deletion
DateTimeColumn get deletedAt => dateTime().nullable()();
/// The count of this channel members
IntColumn get memberCount => integer().nullable()();
/// The id of the user that created this channel
TextColumn get createdById => text().nullable()();
/// Map of custom channel extraData
TextColumn get extraData => text().nullable().map(MapConverter<Object>())();
@override
@@ -1,18 +1,25 @@
import 'package:moor/moor.dart';
import 'package:stream_chat_persistence/src/converter/map_converter.dart';
/// Represents a [ConnectionEvents] table in [MoorChatDatabase].
@DataClassName('ConnectionEventEntity')
class ConnectionEvents extends Table {
/// event id
IntColumn get id => integer()();
/// User object of the current user
TextColumn get ownUser => text().nullable().map(MapConverter<Object>())();
/// The number of unread messages for current user
IntColumn get totalUnreadCount => integer().nullable()();
/// User total unread channels for current user
IntColumn get unreadChannels => integer().nullable()();
/// DateTime of the last event
DateTimeColumn get lastEventAt => dateTime().nullable()();
/// DateTime of the last sync
DateTimeColumn get lastSyncAt => dateTime().nullable()();
@override
@@ -1,28 +1,40 @@
import 'package:moor/moor.dart';
/// Represents a [Members] table in [MoorChatDatabase].
@DataClassName('MemberEntity')
class Members extends Table {
/// The interested user id
TextColumn get userId => text()();
/// The channel cid of which this user is part of
TextColumn get channelCid =>
text().customConstraint('REFERENCES channels(cid) ON DELETE CASCADE')();
/// The role of the user in the channel
TextColumn get role => text().nullable()();
/// The date on which the user accepted the invite to the channel
DateTimeColumn get inviteAcceptedAt => dateTime().nullable()();
/// The date on which the user rejected the invite to the channel
DateTimeColumn get inviteRejectedAt => dateTime().nullable()();
/// True if the user has been invited to the channel
BoolColumn get invited => boolean().nullable()();
/// True if the member is banned from the channel
BoolColumn get banned => boolean().nullable()();
/// True if the member is shadow banned from the channel
BoolColumn get shadowBanned => boolean().nullable()();
/// True if the user is a moderator of the channel
BoolColumn get isModerator => boolean().nullable()();
/// The date of creation
DateTimeColumn get createdAt => dateTime()();
/// The last date of update
DateTimeColumn get updatedAt => dateTime().nullable()();
@override
@@ -3,50 +3,72 @@ import 'package:stream_chat_persistence/src/converter/list_converter.dart';
import 'package:stream_chat_persistence/src/converter/map_converter.dart';
import 'package:stream_chat_persistence/src/converter/message_sending_status_converter.dart';
/// Represents a [Messages] table in [MoorChatDatabase].
@DataClassName('MessageEntity')
class Messages extends Table {
/// The message id
TextColumn get id => text()();
/// The text of this message
TextColumn get messageText => text().nullable()();
/// The list of attachments, either provided by the user
/// or generated from a command or as a result of URL scraping.
TextColumn get attachments =>
text().nullable().map(ListConverter<String>())();
/// The status of a sending message
IntColumn get status =>
integer().nullable().map(MessageSendingStatusConverter())();
/// The message type
TextColumn get type => text().nullable()();
/// The list of user mentioned in the message
TextColumn get mentionedUsers =>
text().nullable().map(ListConverter<String>())();
/// A map describing the count of number of every reaction
TextColumn get reactionCounts => text().nullable().map(MapConverter<int>())();
/// A map describing the count of score of every reaction
TextColumn get reactionScores => text().nullable().map(MapConverter<int>())();
/// The ID of the parent message, if the message is a thread reply.
TextColumn get parentId => text().nullable()();
/// The ID of the quoted message, if the message is a quoted reply.
TextColumn get quotedMessageId => text().nullable()();
/// Number of replies for this message.
IntColumn get replyCount => integer().nullable()();
/// Check if this message needs to show in the channel.
BoolColumn get showInChannel => boolean().nullable()();
/// If true the message is shadowed
BoolColumn get shadowed => boolean().nullable()();
/// A used command name.
TextColumn get command => text().nullable()();
/// The DateTime when the message was created.
DateTimeColumn get createdAt => dateTime()();
/// The DateTime when the message was updated last time.
DateTimeColumn get updatedAt => dateTime().nullable()();
/// The DateTime when the message was deleted.
DateTimeColumn get deletedAt => dateTime().nullable()();
/// Id of the User who sent the message
TextColumn get userId => text().nullable()();
/// The channel cid of which this message is part of
TextColumn get channelCid => text().nullable().customConstraint(
'NULLABLE REFERENCES channels(cid) ON DELETE CASCADE')();
/// Message custom extraData
TextColumn get extraData => text().nullable().map(MapConverter<Object>())();
@override
@@ -1,19 +1,26 @@
import 'package:moor/moor.dart';
import 'package:stream_chat_persistence/src/converter/map_converter.dart';
/// Represents a [Reactions] table in [MoorChatDatabase].
@DataClassName('ReactionEntity')
class Reactions extends Table {
/// The id of the user that sent the reaction
TextColumn get userId => text()();
/// The messageId to which the reaction belongs
TextColumn get messageId =>
text().customConstraint('REFERENCES messages(id) ON DELETE CASCADE')();
/// The type of the reaction
TextColumn get type => text()();
/// The DateTime on which the reaction is created
DateTimeColumn get createdAt => dateTime()();
/// The score of the reaction (ie. number of reactions sent)
IntColumn get score => integer().nullable()();
/// Reaction custom extraData
TextColumn get extraData => text().nullable().map(MapConverter<Object>())();
@override
@@ -1,14 +1,19 @@
import 'package:moor/moor.dart';
/// Represents a [Reads] table in [MoorChatDatabase].
@DataClassName('ReadEntity')
class Reads extends Table {
/// Date of the read event
DateTimeColumn get lastRead => dateTime()();
/// Id of the User who sent the event
TextColumn get userId => text()();
/// The channel cid of which this read belongs
TextColumn get channelCid =>
text().customConstraint('REFERENCES channels(cid) ON DELETE CASCADE')();
/// Number of unread messages
IntColumn get unreadMessages => integer().nullable()();
@override
@@ -1,22 +1,31 @@
import 'package:moor/moor.dart';
import 'package:stream_chat_persistence/src/converter/map_converter.dart';
/// Represents a [Users] table in [MoorChatDatabase].
@DataClassName('UserEntity')
class Users extends Table {
/// User id
TextColumn get id => text()();
/// User role
TextColumn get role => text().nullable()();
/// Date of user creation
DateTimeColumn get createdAt => dateTime().nullable()();
/// Date of last user update
DateTimeColumn get updatedAt => dateTime().nullable()();
/// Date of last user connection
DateTimeColumn get lastActive => dateTime().nullable()();
/// True if user is online
BoolColumn get online => boolean().nullable()();
/// True if user is banned from the chat
BoolColumn get banned => boolean().nullable()();
/// Map of custom user extraData
TextColumn get extraData => text().nullable().map(MapConverter<Object>())();
@override
@@ -1,9 +1,9 @@
import 'package:stream_chat/stream_chat.dart';
import 'package:stream_chat_persistence/src/db/moor_chat_database.dart';
///
/// Useful mapping functions for [ChannelEntity]
extension ChannelEntityX on ChannelEntity {
///
/// Maps a [ChannelEntity] into [ChannelModel]
ChannelModel toChannelModel({User createdBy}) {
final config = ChannelConfig.fromJson(this.config ?? {});
return ChannelModel(
@@ -22,7 +22,7 @@ extension ChannelEntityX on ChannelEntity {
);
}
///
/// Maps a [ChannelEntity] into [ChannelState]
ChannelState toChannelState({
User createdBy,
List<Member> members,
@@ -38,9 +38,9 @@ extension ChannelEntityX on ChannelEntity {
}
}
///
/// Useful mapping functions for [ChannelModel]
extension ChannelModelX on ChannelModel {
///
/// Maps a [ChannelModel] into [ChannelEntity]
ChannelEntity toEntity() {
return ChannelEntity(
id: id,
@@ -1,10 +1,9 @@
import 'package:stream_chat/stream_chat.dart';
import 'user_mapper.dart';
import 'package:stream_chat_persistence/src/db/moor_chat_database.dart';
///
/// Useful mapping functions for [ConnectionEventEntity]
extension ConnectionEventX on ConnectionEventEntity {
///
/// Maps a [ConnectionEventEntity] into [Event]
Event toEvent() {
return Event(
me: ownUser != null ? OwnUser.fromJson(ownUser) : null,
@@ -1,10 +1,9 @@
import 'package:stream_chat/stream_chat.dart';
import 'package:stream_chat_persistence/src/db/moor_chat_database.dart';
import 'user_mapper.dart';
///
/// Useful mapping functions for [MemberEntity]
extension MemberEntityX on MemberEntity {
///
/// Maps a [MemberEntity] into [Member]
Member toMember({User user}) {
return Member(
user: user,
@@ -22,9 +21,9 @@ extension MemberEntityX on MemberEntity {
}
}
///
/// Useful mapping functions for [Member]
extension MemberX on Member {
///
/// Maps a [Member] into [MemberEntity]
MemberEntity toEntity({String cid}) {
return MemberEntity(
userId: user?.id,
@@ -3,9 +3,9 @@ import 'dart:convert';
import 'package:stream_chat/stream_chat.dart';
import 'package:stream_chat_persistence/src/db/moor_chat_database.dart';
///
/// Useful mapping functions for [MessageEntity]
extension MessageEntityX on MessageEntity {
///
/// Maps a [MessageEntity] into [Message]
Message toMessage({
User user,
List<Reaction> latestReactions,
@@ -41,9 +41,9 @@ extension MessageEntityX on MessageEntity {
}
}
///
/// Useful mapping functions for [Message]
extension MessageX on Message {
///
/// Maps a [Message] into [MessageEntity]
MessageEntity toEntity({String cid}) {
return MessageEntity(
id: id,
@@ -1,9 +1,9 @@
import 'package:stream_chat/stream_chat.dart';
import 'package:stream_chat_persistence/src/db/moor_chat_database.dart';
///
/// Useful mapping functions for [ReactionEntity]
extension ReactionEntityX on ReactionEntity {
///
/// Maps a [ReactionEntity] into [Reaction]
Reaction toReaction({User user}) {
return Reaction(
extraData: extraData,
@@ -17,10 +17,9 @@ extension ReactionEntityX on ReactionEntity {
}
}
///
/// Useful mapping functions for [Reaction]
extension ReactionX on Reaction {
///
ReactionEntity toEntity() {
ReactionEntity toEntity() {
return ReactionEntity(
extraData: extraData,
type: type,
@@ -1,10 +1,9 @@
import 'package:stream_chat/stream_chat.dart';
import 'package:stream_chat_persistence/src/db/moor_chat_database.dart';
import 'user_mapper.dart';
///
/// Useful mapping functions for [ReadEntity]
extension ReadEntityX on ReadEntity {
///
/// Maps a [ReadEntity] into [Read]
Read toRead({User user}) {
return Read(
user: user,
@@ -14,9 +13,9 @@ extension ReadEntityX on ReadEntity {
}
}
///
/// Useful mapping functions for [Read]
extension ReadX on Read {
///
/// Maps a [Read] into [ReadEntity]
ReadEntity toEntity({String cid}) {
return ReadEntity(
lastRead: lastRead,
@@ -1,9 +1,9 @@
import 'package:stream_chat/stream_chat.dart';
import 'package:stream_chat_persistence/src/db/moor_chat_database.dart';
///
/// Useful mapping functions for [UserEntity]
extension UserEntityX on UserEntity {
///
/// Maps a [UserEntity] into [User]
User toUser() {
return User(
id: id,
@@ -18,9 +18,9 @@ extension UserEntityX on UserEntity {
}
}
///
/// Useful mapping functions for [User]
extension UserX on User {
///
/// Maps a [User] into [UserEntity]
UserEntity toEntity() {
return UserEntity(
id: id,
@@ -12,9 +12,9 @@ enum ConnectionMode {
background,
}
///
/// A [MoorChatDatabase] based implementation of the [ChatPersistenceClient]
class StreamChatPersistenceClient extends ChatPersistenceClient {
///
/// Creates a new instance of the stream chat persistence client
StreamChatPersistenceClient({
/// Connection mode on which the client will work
ConnectionMode connectionMode = ConnectionMode.regular,