♻️ Moved the chat page to use local db
This commit is contained in:
@@ -20,28 +20,75 @@ extension MessageHistoryHelper on MessageHistoryDb {
|
||||
));
|
||||
}
|
||||
|
||||
syncMessages(Channel channel, {String? scope}) async {
|
||||
replaceMessage(Message remote) async {
|
||||
await localMessages.update(LocalMessage(
|
||||
remote.id,
|
||||
remote,
|
||||
remote.channelId,
|
||||
));
|
||||
}
|
||||
|
||||
burnMessage(int id) async {
|
||||
await localMessages.delete(id);
|
||||
}
|
||||
|
||||
syncMessages(Channel channel, {String scope = 'global'}) async {
|
||||
final lastOne = await localMessages.findLastByChannel(channel.id);
|
||||
|
||||
final data = await _getRemoteMessages(
|
||||
channel,
|
||||
scope,
|
||||
remainBreath: 5,
|
||||
onBrake: (items) {
|
||||
return items.any((x) => x.id == lastOne?.id);
|
||||
},
|
||||
);
|
||||
await localMessages.insertBulk(
|
||||
data.map((x) => LocalMessage(x.id, x, x.channelId)).toList(),
|
||||
);
|
||||
}
|
||||
|
||||
Future<List<Message>> _getRemoteMessages(
|
||||
Channel channel,
|
||||
String scope, {
|
||||
required int remainBreath,
|
||||
bool Function(List<Message> items)? onBrake,
|
||||
take = 10,
|
||||
offset = 0,
|
||||
}) async {
|
||||
if (remainBreath <= 0) {
|
||||
return List.empty();
|
||||
}
|
||||
|
||||
final AuthProvider auth = Get.find();
|
||||
if (!await auth.isAuthorized) return;
|
||||
if (!await auth.isAuthorized) return List.empty();
|
||||
|
||||
final client = auth.configureClient('messaging');
|
||||
|
||||
final resp = await client
|
||||
.get('/api/channels/$scope/${channel.alias}/messages?take=10&offset=0');
|
||||
final resp = await client.get(
|
||||
'/api/channels/$scope/${channel.alias}/messages?take=$take&offset=$offset');
|
||||
|
||||
if (resp.statusCode != 200) {
|
||||
throw Exception(resp.bodyString);
|
||||
}
|
||||
|
||||
// TODO Continue sync until the last message / the message exists / sync limitation
|
||||
final PaginationResult response = PaginationResult.fromJson(resp.body);
|
||||
final result =
|
||||
response.data?.map((e) => Message.fromJson(e)).toList() ?? List.empty();
|
||||
|
||||
final PaginationResult result = PaginationResult.fromJson(resp.body);
|
||||
final parsed = result.data?.map((e) => Message.fromJson(e)).toList();
|
||||
if (parsed != null) {
|
||||
await localMessages.insertBulk(
|
||||
parsed.map((x) => LocalMessage(x.id, x, x.channelId)).toList(),
|
||||
);
|
||||
if (onBrake != null && onBrake(result)) {
|
||||
return result;
|
||||
}
|
||||
|
||||
final expandResult = await _getRemoteMessages(
|
||||
channel,
|
||||
scope,
|
||||
remainBreath: remainBreath - 1,
|
||||
take: take,
|
||||
offset: offset + result.length,
|
||||
);
|
||||
|
||||
return [...result, ...expandResult];
|
||||
}
|
||||
|
||||
Future<List<LocalMessage>> listMessages(Channel channel) async {
|
||||
|
@@ -31,16 +31,31 @@ class RemoteMessageConverter extends TypeConverter<Message, String> {
|
||||
|
||||
@dao
|
||||
abstract class LocalMessageDao {
|
||||
@Query('SELECT * FROM LocalMessage WHERE channelId = :channelId')
|
||||
@Query('SELECT COUNT(id) FROM LocalMessage WHERE channelId = :channelId')
|
||||
Future<int?> countByChannel(int channelId);
|
||||
|
||||
@Query('SELECT * FROM LocalMessage WHERE channelId = :channelId ORDER BY id DESC')
|
||||
Future<List<LocalMessage>> findAllByChannel(int channelId);
|
||||
|
||||
@Query('SELECT * FROM LocalMessage WHERE channelId = :channelId ORDER BY id DESC LIMIT 1')
|
||||
Future<LocalMessage?> findLastByChannel(int channelId);
|
||||
|
||||
@Insert(onConflict: OnConflictStrategy.replace)
|
||||
Future<void> insert(LocalMessage m);
|
||||
|
||||
@Insert(onConflict: OnConflictStrategy.replace)
|
||||
Future<void> insertBulk(List<LocalMessage> m);
|
||||
|
||||
@Query('DELETE * FROM LocalMessage')
|
||||
@Update(onConflict: OnConflictStrategy.replace)
|
||||
Future<void> update(LocalMessage person);
|
||||
|
||||
@Query('DELETE FROM LocalMessage WHERE id = :id')
|
||||
Future<void> delete(int id);
|
||||
|
||||
@Query('DELETE FROM LocalMessage WHERE channelId = :channelId')
|
||||
Future<List<LocalMessage>> deleteByChannel(int channelId);
|
||||
|
||||
@Query('DELETE FROM LocalMessage')
|
||||
Future<void> wipeLocalMessages();
|
||||
}
|
||||
|
||||
|
@@ -119,6 +119,15 @@ class _$LocalMessageDao extends LocalMessageDao {
|
||||
_localMessageInsertionAdapter = InsertionAdapter(
|
||||
database,
|
||||
'LocalMessage',
|
||||
(LocalMessage item) => <String, Object?>{
|
||||
'id': item.id,
|
||||
'data': _remoteMessageConverter.encode(item.data),
|
||||
'channelId': item.channelId
|
||||
}),
|
||||
_localMessageUpdateAdapter = UpdateAdapter(
|
||||
database,
|
||||
'LocalMessage',
|
||||
['id'],
|
||||
(LocalMessage item) => <String, Object?>{
|
||||
'id': item.id,
|
||||
'data': _remoteMessageConverter.encode(item.data),
|
||||
@@ -133,10 +142,45 @@ class _$LocalMessageDao extends LocalMessageDao {
|
||||
|
||||
final InsertionAdapter<LocalMessage> _localMessageInsertionAdapter;
|
||||
|
||||
final UpdateAdapter<LocalMessage> _localMessageUpdateAdapter;
|
||||
|
||||
@override
|
||||
Future<int?> countByChannel(int channelId) async {
|
||||
return _queryAdapter.query(
|
||||
'SELECT COUNT(id) FROM LocalMessage WHERE channelId = ?1',
|
||||
mapper: (Map<String, Object?> row) => row.values.first as int,
|
||||
arguments: [channelId]);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<LocalMessage>> findAllByChannel(int channelId) async {
|
||||
return _queryAdapter.queryList(
|
||||
'SELECT * FROM LocalMessage WHERE channelId = ?1',
|
||||
'SELECT * FROM LocalMessage WHERE channelId = ?1 ORDER BY id DESC',
|
||||
mapper: (Map<String, Object?> row) => LocalMessage(
|
||||
row['id'] as int,
|
||||
_remoteMessageConverter.decode(row['data'] as String),
|
||||
row['channelId'] as int),
|
||||
arguments: [channelId]);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<LocalMessage?> findLastByChannel(int channelId) async {
|
||||
return _queryAdapter.query(
|
||||
'SELECT * FROM LocalMessage WHERE channelId = ?1 ORDER BY id DESC LIMIT 1',
|
||||
mapper: (Map<String, Object?> row) => LocalMessage(row['id'] as int, _remoteMessageConverter.decode(row['data'] as String), row['channelId'] as int),
|
||||
arguments: [channelId]);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> delete(int id) async {
|
||||
await _queryAdapter.queryNoReturn('DELETE FROM LocalMessage WHERE id = ?1',
|
||||
arguments: [id]);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<LocalMessage>> deleteByChannel(int channelId) async {
|
||||
return _queryAdapter.queryList(
|
||||
'DELETE FROM LocalMessage WHERE channelId = ?1',
|
||||
mapper: (Map<String, Object?> row) => LocalMessage(
|
||||
row['id'] as int,
|
||||
_remoteMessageConverter.decode(row['data'] as String),
|
||||
@@ -146,7 +190,7 @@ class _$LocalMessageDao extends LocalMessageDao {
|
||||
|
||||
@override
|
||||
Future<void> wipeLocalMessages() async {
|
||||
await _queryAdapter.queryNoReturn('DELETE * FROM LocalMessage');
|
||||
await _queryAdapter.queryNoReturn('DELETE FROM LocalMessage');
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -159,6 +203,11 @@ class _$LocalMessageDao extends LocalMessageDao {
|
||||
await _localMessageInsertionAdapter.insertList(
|
||||
m, OnConflictStrategy.replace);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> update(LocalMessage person) async {
|
||||
await _localMessageUpdateAdapter.update(person, OnConflictStrategy.replace);
|
||||
}
|
||||
}
|
||||
|
||||
// ignore_for_file: unused_element
|
||||
|
Reference in New Issue
Block a user