Skip to content

Commit

Permalink
handling no tokens in pangea message event
Browse files Browse the repository at this point in the history
  • Loading branch information
William Jordan-Cooley authored and William Jordan-Cooley committed Jan 18, 2024
1 parent a9e5082 commit 695374e
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 56 deletions.
6 changes: 3 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"dart.previewLsp": true,
"editor.codeActionsOnSave": {
"source.fixAll": true,
"source.organizeImports": true,
"source.sortMembers": false
"source.fixAll": "explicit",
"source.organizeImports": "explicit",
"source.sortMembers": "never"
},
"editor.formatOnSave": true
}
33 changes: 16 additions & 17 deletions lib/pages/chat/chat_view.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
// Flutter imports:

import 'package:flutter/material.dart';

import 'package:badges/badges.dart';
import 'package:desktop_drop/desktop_drop.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:future_loading_dialog/future_loading_dialog.dart';
import 'package:matrix/matrix.dart';

import 'package:fluffychat/config/app_config.dart';
import 'package:fluffychat/config/themes.dart';
import 'package:fluffychat/pages/chat/chat.dart';
Expand All @@ -26,6 +20,11 @@ import 'package:fluffychat/widgets/chat_settings_popup_menu.dart';
import 'package:fluffychat/widgets/connection_status_header.dart';
import 'package:fluffychat/widgets/matrix.dart';
import 'package:fluffychat/widgets/unread_rooms_badge.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:future_loading_dialog/future_loading_dialog.dart';
import 'package:matrix/matrix.dart';

import '../../utils/stream_extension.dart';
import 'chat_emoji_picker.dart';
import 'chat_input_row.dart';
Expand Down Expand Up @@ -90,17 +89,17 @@ class ChatView extends StatelessWidget {
},
itemBuilder: (context) => [
// #Pangea
// PopupMenuItem(
// value: _EventContextAction.info,
// child: Row(
// mainAxisSize: MainAxisSize.min,
// children: [
// const Icon(Icons.info_outlined),
// const SizedBox(width: 12),
// Text(L10n.of(context)!.messageInfo),
// ],
// ),
// ),
PopupMenuItem(
value: _EventContextAction.info,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
const Icon(Icons.info_outlined),
const SizedBox(width: 12),
Text(L10n.of(context)!.messageInfo),
],
),
),
// Pangea#
if (controller.selectedEvents.single.status.isSent)
PopupMenuItem(
Expand Down
59 changes: 39 additions & 20 deletions lib/pangea/models/pangea_message_event.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:developer';

import 'package:collection/collection.dart';
import 'package:fluffychat/pangea/constants/model_keys.dart';
import 'package:fluffychat/pangea/constants/pangea_message_types.dart';
Expand All @@ -6,6 +8,7 @@ import 'package:fluffychat/pangea/models/choreo_record.dart';
import 'package:fluffychat/pangea/models/message_data_models.dart';
import 'package:fluffychat/pangea/models/pangea_representation_event.dart';
import 'package:fluffychat/pangea/utils/bot_name.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:matrix/matrix.dart';

Expand Down Expand Up @@ -84,20 +87,20 @@ class PangeaMessageEvent {

_representations = [];

final bool latestHasTokens =
_latestEdit.content[ModelKey.tokensSent] != null;

if (_latestEdit.content[ModelKey.originalSent] != null && latestHasTokens) {
if (_latestEdit.content[ModelKey.originalSent] != null) {
try {
_representations!.add(
RepresentationEvent(
content: PangeaRepresentation.fromJson(
_latestEdit.content[ModelKey.originalSent]
as Map<String, dynamic>,
),
tokens: PangeaMessageTokens.fromJson(
_latestEdit.content[ModelKey.tokensSent] as Map<String, dynamic>,
),
tokens: _latestEdit.content[ModelKey.tokensSent] != null
? PangeaMessageTokens.fromJson(
_latestEdit.content[ModelKey.tokensSent]
as Map<String, dynamic>,
)
: null,
choreo: _latestEdit.content[ModelKey.choreoRecord] != null
? ChoreoRecord.fromJson(
_latestEdit.content[ModelKey.choreoRecord]
Expand All @@ -109,26 +112,37 @@ class PangeaMessageEvent {
);
} catch (err, s) {
ErrorHandler.logError(
m: "error parsing originalSent",
e: err,
s: s,
);
}
}

if (_latestEdit.content[ModelKey.originalWritten] != null &&
latestHasTokens) {
_representations!.add(
RepresentationEvent(
content: PangeaRepresentation.fromJson(
_latestEdit.content[ModelKey.originalWritten]
as Map<String, dynamic>,
),
tokens: PangeaMessageTokens.fromJson(
_latestEdit.content[ModelKey.tokensWritten] as Map<String, dynamic>,
if (_latestEdit.content[ModelKey.originalWritten] != null) {
try {
_representations!.add(
RepresentationEvent(
content: PangeaRepresentation.fromJson(
_latestEdit.content[ModelKey.originalWritten]
as Map<String, dynamic>,
),
tokens: _latestEdit.content[ModelKey.tokensWritten] != null
? PangeaMessageTokens.fromJson(
_latestEdit.content[ModelKey.tokensWritten]
as Map<String, dynamic>,
)
: null,
timeline: timeline,
),
timeline: timeline,
),
);
);
} catch (err, s) {
ErrorHandler.logError(
m: "error parsing originalWritten",
e: err,
s: s,
);
}
}

_representations!.addAll(
Expand Down Expand Up @@ -174,6 +188,11 @@ class PangeaMessageEvent {

RepresentationEvent? rep = representationByLanguage(langCode);

//if event is less than 1 minute old, then print new event
if (isNew) {
debugger(when: kDebugMode);
}

while ((isNew || eventId.contains("web")) && tries < 20) {
if (rep != null) return rep;
await Future.delayed(const Duration(milliseconds: 500));
Expand Down
27 changes: 15 additions & 12 deletions lib/pangea/models/pangea_representation_event.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import 'dart:developer';

import 'package:fluffychat/pangea/extensions/pangea_event_extension.dart';
import 'package:fluffychat/pangea/models/pangea_choreo_event.dart';
import 'package:fluffychat/pangea/models/pangea_token_model.dart';
import 'package:fluffychat/pangea/repo/tokens_repo.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import 'package:matrix/matrix.dart';
import 'package:sentry_flutter/sentry_flutter.dart';

import 'package:fluffychat/pangea/extensions/pangea_event_extension.dart';
import 'package:fluffychat/pangea/models/pangea_choreo_event.dart';
import 'package:fluffychat/pangea/models/pangea_token_model.dart';
import 'package:fluffychat/pangea/repo/tokens_repo.dart';
import '../../widgets/matrix.dart';
import '../constants/language_keys.dart';
import '../constants/pangea_event_types.dart';
Expand Down Expand Up @@ -70,22 +69,25 @@ class RepresentationEvent {
return null;
}

final Set<Event> tokenEvents =
_event!.aggregatedEvents(timeline, PangeaEventTypes.tokens);
final Set<Event> tokenEvents = _event?.aggregatedEvents(
timeline,
PangeaEventTypes.tokens,
) ??
{};

if (tokenEvents.isEmpty) return null;

if (tokenEvents.length > 1) {
debugger(when: kDebugMode);
Sentry.addBreadcrumb(
Breadcrumb(
message: "Token events for representation ${_event!.eventId}: "
message: "Token events for representation ${_event?.eventId}: "
"Content: ${tokenEvents.map((e) => e.content).toString()}"
"Type: ${tokenEvents.map((e) => e.type).toString()}",
),
);
ErrorHandler.logError(
m: 'should not have more than one tokenEvent per representation ${_event!.eventId}',
m: 'should not have more than one tokenEvent per representation ${_event?.eventId}',
s: StackTrace.current,
);
}
Expand All @@ -97,6 +99,7 @@ class RepresentationEvent {

Future<List<PangeaToken>?> tokensGlobal(BuildContext context) async {
if (tokens != null) return tokens!;

if (_event == null) {
debugger(when: kDebugMode);
ErrorHandler.logError(
Expand Down Expand Up @@ -141,16 +144,16 @@ class RepresentationEvent {
}

final Set<Event> choreoMatrixEvents =
_event!.aggregatedEvents(timeline, PangeaEventTypes.choreoRecord);
_event?.aggregatedEvents(timeline, PangeaEventTypes.choreoRecord) ?? {};

if (choreoMatrixEvents.isEmpty) return null;

if (choreoMatrixEvents.length > 1) {
debugger(when: kDebugMode);
ErrorHandler.logError(
m: 'should not have more than one choreoEvent per representation ${_event!.eventId}',
m: 'should not have more than one choreoEvent per representation ${_event?.eventId}',
s: StackTrace.current,
data: _event!.toJson(),
data: _event?.toJson(),
);
}

Expand Down
7 changes: 3 additions & 4 deletions lib/pangea/utils/error_handler.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import 'dart:async';

import 'package:fluffychat/pangea/config/environment.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:http/http.dart' as http;
import 'package:sentry_flutter/sentry_flutter.dart';

import 'package:fluffychat/pangea/config/environment.dart';

class ErrorHandler {
ErrorHandler();

Expand Down Expand Up @@ -68,7 +66,8 @@ class ErrorHandler {
String? m,
Map<String, dynamic>? data,
}) async {
if ((e ?? m) != null) debugPrint("error: ${e?.toString() ?? m}");
if (m != null) debugPrint("error message: $m");
if ((e ?? m) != null) debugPrint("error to string: ${e?.toString() ?? m}");
if (data != null) {
Sentry.addBreadcrumb(Breadcrumb.fromJson(data));
}
Expand Down

0 comments on commit 695374e

Please sign in to comment.