Skip to content

Commit

Permalink
Merge pull request #244 from syphon-org/hot-fix-message-edits
Browse files Browse the repository at this point in the history
[0.1.7+1] Hot Fixes
  • Loading branch information
ereio authored Mar 21, 2021
2 parents 4079387 + 858753c commit ab30669
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 49 deletions.
9 changes: 9 additions & 0 deletions lib/global/libs/matrix/events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ abstract class Events {

Map<String, String> headers = {
'Authorization': 'Bearer $accessToken',
...Values.defaultHeaders,
};

final response = await http.get(
Expand Down Expand Up @@ -64,6 +65,7 @@ abstract class Events {

Map<String, String> headers = {
'Authorization': 'Bearer $accessToken',
...Values.defaultHeaders,
};

final response = await http.get(
Expand Down Expand Up @@ -121,6 +123,7 @@ abstract class Events {

Map<String, String> headers = {
'Authorization': 'Bearer $accessToken',
...Values.defaultHeaders,
};

Map body = {
Expand Down Expand Up @@ -171,6 +174,7 @@ abstract class Events {

Map<String, String> headers = {
'Authorization': 'Bearer $accessToken',
...Values.defaultHeaders,
};

final response = await http.put(
Expand Down Expand Up @@ -206,6 +210,7 @@ abstract class Events {

Map<String, String> headers = {
'Authorization': 'Bearer $accessToken',
...Values.defaultHeaders,
};

Map body = {
Expand Down Expand Up @@ -259,6 +264,7 @@ abstract class Events {

Map<String, String> headers = {
'Authorization': 'Bearer $accessToken',
...Values.defaultHeaders,
};

Map body = {
Expand Down Expand Up @@ -296,6 +302,7 @@ abstract class Events {

Map<String, String> headers = {
'Authorization': 'Bearer $accessToken',
...Values.defaultHeaders,
};

Map body = {};
Expand Down Expand Up @@ -335,6 +342,7 @@ abstract class Events {

Map<String, String> headers = {
'Authorization': 'Bearer $accessToken',
...Values.defaultHeaders,
};

// Use astrick to send to all known devices for user
Expand Down Expand Up @@ -367,6 +375,7 @@ abstract class Events {

Map<String, String> headers = {
'Authorization': 'Bearer $accessToken',
...Values.defaultHeaders,
};

Map body = {
Expand Down
1 change: 1 addition & 0 deletions lib/store/auth/actions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import 'package:syphon/store/auth/credential/model.dart';
import 'package:syphon/store/auth/homeserver/actions.dart';
import 'package:syphon/store/auth/homeserver/model.dart';
import 'package:syphon/store/crypto/actions.dart';
import 'package:syphon/store/events/messages/actions.dart';
import 'package:syphon/store/index.dart';
import 'package:syphon/store/media/actions.dart';
import 'package:syphon/store/rooms/actions.dart';
Expand Down
34 changes: 20 additions & 14 deletions lib/store/events/messages/actions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'package:syphon/global/algos.dart';

// Project imports:
import 'package:syphon/global/libs/matrix/index.dart';
import 'package:syphon/global/print.dart';
import 'package:syphon/store/alerts/actions.dart';
import 'package:syphon/store/crypto/actions.dart';
import 'package:syphon/store/crypto/events/actions.dart';
Expand Down Expand Up @@ -62,19 +63,24 @@ ThunkAction<AppState> mutateMessagesAll({List<String> messages}) {
final roomMessages = store.state.eventStore.messages;

await Future.wait(roomMessages.entries.map((entry) async {
final roomId = entry.key;
final allMessages = entry.value;

final revisedMessages = await compute(reviseMessagesBackground, {
'reactions': reactions,
'redactions': redactions,
'messages': allMessages,
});

await store.dispatch(setMessages(
room: Room(id: roomId),
messages: revisedMessages,
));
try {
final roomId = entry.key;
final allMessages = entry.value;

final revisedMessages = await compute(reviseMessagesBackground, {
'reactions': reactions,
'redactions': redactions,
'messages': allMessages,
});

await store.dispatch(setMessages(
room: Room(id: roomId),
messages: revisedMessages,
));
} catch (error) {
// TODO: Error handling for mutating messages per room
debugPrint(error.toString());
}
}));
};
}
Expand All @@ -100,7 +106,7 @@ ThunkAction<AppState> mutateMessagesRoom({Room room}) {
'messages': messages,
});

store.dispatch(setMessages(
await store.dispatch(setMessages(
room: Room(id: room.id),
messages: revisedMessages,
));
Expand Down
62 changes: 30 additions & 32 deletions lib/store/events/selectors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,22 @@ List<Message> filterMessages(
) {
final blocked = state.userStore.blocked;

// TODO: remove the replacement filter here, should be managed by the mutators
return messages
..removeWhere(
(message) => blocked.contains(message.sender),
(message) => blocked.contains(message.sender) || message.replacement,
);
}

List<Message> reviseMessages(
List<Message> messages,
AppState state,
) {
final reactions = selectReactions(state);
final redactions = state.eventStore.redactions;
List<Message> reviseMessagesBackground(Map params) {
List<Message> messages = params['messages'];
Map<String, Redaction> redactions = params['redactions'];
Map<String, List<Reaction>> reactions = params['reactions'];

return reviseMessagesAlt(messages, redactions, reactions);
return reviseMessagesFilter(messages, redactions, reactions);
}

List<Message> reviseMessagesAlt(
List<Message> reviseMessagesFilter(
List<Message> messages,
Map<String, Redaction> redactions,
Map<String, List<Reaction>> reactions,
Expand All @@ -55,14 +54,6 @@ List<Message> reviseMessagesAlt(
return List.from(messagesMap.values);
}

List<Message> reviseMessagesBackground(Map params) {
List<Message> messages = params['messages'];
Map<String, Redaction> redactions = params['redactions'];
Map<String, List<Reaction>> reactions = params['reactions'];

return reviseMessagesAlt(messages, redactions, reactions);
}

Map<String, Message> filterRedactions(
Map<String, Message> messages, {
Map<String, Redaction> redactions,
Expand Down Expand Up @@ -106,7 +97,7 @@ Map<String, Message> appendReactions(
Map<String, Message> replaceEdited(List<Message> messages) {
final replacements = List<Message>();

// create a map of messages for O(1) when replacing (O(N))
// create a map of messages for O(1) when replacing O(N)
final messagesMap = Map<String, Message>.fromIterable(
messages ?? [],
key: (msg) => msg.id,
Expand All @@ -123,21 +114,28 @@ Map<String, Message> replaceEdited(List<Message> messages) {
// iterate through replacements and modify messages as needed O(M + M)
replacements.sort((b, a) => a.timestamp.compareTo(b.timestamp));

for (Message replacement in replacements) {
final messageId = replacement.relatedEventId;
if (messagesMap.containsKey(messageId)) {
final messageEdited = messagesMap[messageId];

messagesMap[messageId] = messageEdited.copyWith(
edited: true,
body: replacement.body,
msgtype: replacement.msgtype,
edits: [messageEdited, ...(messageEdited.edits ?? List<Message>())],
);

// remove replacements from the returned messages
messagesMap.remove(replacement.id);
for (Message messageEdited in replacements) {
final messageIdOriginal = messageEdited.relatedEventId;
final messageOriginal = messagesMap[messageIdOriginal];

if (messageOriginal != null) {
final validEdit = messageEdited.sender == messageOriginal.sender;

if (validEdit) {
messagesMap[messageIdOriginal] = messageOriginal.copyWith(
edited: true,
body: messageEdited.body,
msgtype: messageEdited.msgtype,
edits: [
messageOriginal,
...(messageOriginal.edits ?? List<Message>())
],
);
}
}

// remove replacements from the returned messages
messagesMap.remove(messageEdited.id);
}

return messagesMap;
Expand Down
6 changes: 3 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ description: a privacy focused matrix client
# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
version: 0.1.7+170
version: 0.1.7+171

environment:
sdk: ">=2.9.0 <3.0.0" # <- modified to solve build_runner
Expand Down Expand Up @@ -99,7 +99,7 @@ dependencies:
easy_localization: 2.3.3
flutter_dotenv: 2.1.0
android_alarm_manager: 0.4.5+15
uni_links: 0.4.0
uni_links: 0.4.0
# flutter_apns: 1.1.0 # TODO: extract only the iOS code and remove

# Desktop UI Only - UNCOMMENT FOR BUILDS
Expand Down Expand Up @@ -174,7 +174,7 @@ flutter:
- ./.env.debug
- ./.env.release
- assets/translations/en.json
- assets/translations/ru.json
- assets/translations/ru.json
- assets/graphics/undraw_sync_files.svg
- assets/graphics/undraw_accept_terms.svg
- assets/graphics/undraw_mobile_user.svg
Expand Down

0 comments on commit ab30669

Please sign in to comment.