mirror of
https://gitlab.com/famedly/fluffychat.git
synced 2025-02-03 08:44:14 +01:00
chore: Enhance room pills
This commit is contained in:
parent
692d6042c5
commit
1a1c166ab0
@ -114,8 +114,7 @@ class HtmlMessage extends StatelessWidget {
|
|||||||
),
|
),
|
||||||
},
|
},
|
||||||
extensions: [
|
extensions: [
|
||||||
UserPillExtension(context, room),
|
RoomPillExtension(context, room),
|
||||||
RoomPillExtension(context, room.client),
|
|
||||||
CodeExtension(fontSize: fontSize),
|
CodeExtension(fontSize: fontSize),
|
||||||
MatrixMathExtension(
|
MatrixMathExtension(
|
||||||
style: TextStyle(fontSize: fontSize, color: textColor),
|
style: TextStyle(fontSize: fontSize, color: textColor),
|
||||||
@ -378,24 +377,21 @@ class CodeExtension extends HtmlExtension {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
class UserPillExtension extends HtmlExtension {
|
class RoomPillExtension extends HtmlExtension {
|
||||||
final Room room;
|
final Room room;
|
||||||
final BuildContext context;
|
final BuildContext context;
|
||||||
|
|
||||||
UserPillExtension(this.context, this.room);
|
RoomPillExtension(this.context, this.room);
|
||||||
@override
|
@override
|
||||||
Set<String> get supportedTags => {'a'};
|
Set<String> get supportedTags => {'a'};
|
||||||
|
|
||||||
@override
|
@override
|
||||||
bool matches(ExtensionContext context) {
|
bool matches(ExtensionContext context) {
|
||||||
if (context.elementName != 'a') return false;
|
if (context.elementName != 'a') return false;
|
||||||
final href = context.element?.attributes['href'];
|
final userId = context.element?.attributes['href']
|
||||||
if (href == null) return false;
|
?.parseIdentifierIntoParts()
|
||||||
final uri = Uri.tryParse(href);
|
?.primaryIdentifier;
|
||||||
if (uri == null || uri.host != 'matrix.to') return false;
|
return userId != null;
|
||||||
final userId = uri.fragment.split('/').last;
|
|
||||||
if (userId.sigil != '@') return false;
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@ -403,12 +399,16 @@ class UserPillExtension extends HtmlExtension {
|
|||||||
ExtensionContext context,
|
ExtensionContext context,
|
||||||
Map<StyledElement, InlineSpan> Function() parseChildren,
|
Map<StyledElement, InlineSpan> Function() parseChildren,
|
||||||
) {
|
) {
|
||||||
final href = context.element!.attributes['href']!;
|
final href = context.element?.attributes['href'];
|
||||||
final uri = Uri.parse(href);
|
final matrixId = href?.parseIdentifierIntoParts()?.primaryIdentifier;
|
||||||
final userId = uri.fragment.split('/').last;
|
if (href == null || matrixId == null) {
|
||||||
final user = room.unsafeGetUserFromMemoryOrFallback(userId);
|
return TextSpan(text: context.innerHtml);
|
||||||
|
}
|
||||||
|
if (matrixId.sigil == '@') {
|
||||||
|
final user = room.unsafeGetUserFromMemoryOrFallback(matrixId);
|
||||||
return WidgetSpan(
|
return WidgetSpan(
|
||||||
child: MatrixPill(
|
child: MatrixPill(
|
||||||
|
key: Key('user_pill_$matrixId'),
|
||||||
name: user.calcDisplayname(),
|
name: user.calcDisplayname(),
|
||||||
avatar: user.avatarUrl,
|
avatar: user.avatarUrl,
|
||||||
uri: href,
|
uri: href,
|
||||||
@ -416,40 +416,10 @@ class UserPillExtension extends HtmlExtension {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
if (matrixId.sigil == '#' || matrixId.sigil == '!') {
|
||||||
|
final room = matrixId.sigil == '!'
|
||||||
class RoomPillExtension extends HtmlExtension {
|
? this.room.client.getRoomById(matrixId)
|
||||||
final Client client;
|
: this.room.client.getRoomByAlias(matrixId);
|
||||||
final BuildContext context;
|
|
||||||
|
|
||||||
RoomPillExtension(this.context, this.client);
|
|
||||||
@override
|
|
||||||
Set<String> get supportedTags => {'a'};
|
|
||||||
|
|
||||||
@override
|
|
||||||
bool matches(ExtensionContext context) {
|
|
||||||
if (context.elementName != 'a') return false;
|
|
||||||
final href = context.element?.attributes['href'];
|
|
||||||
if (href == null) return false;
|
|
||||||
final uri = Uri.tryParse(href);
|
|
||||||
if (uri == null || uri.host != 'matrix.to') return false;
|
|
||||||
final roomId = Uri.decodeComponent(uri.fragment.split('/').last);
|
|
||||||
if (!{'#', '!'}.contains(roomId.sigil)) return false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
InlineSpan build(
|
|
||||||
ExtensionContext context,
|
|
||||||
Map<StyledElement, InlineSpan> Function() parseChildren,
|
|
||||||
) {
|
|
||||||
final href = context.element!.attributes['href']!;
|
|
||||||
final uri = Uri.parse(href);
|
|
||||||
final roomId = Uri.decodeComponent(uri.fragment.split('/').last);
|
|
||||||
|
|
||||||
final room = roomId.sigil == '!'
|
|
||||||
? client.getRoomById(roomId)
|
|
||||||
: client.getRoomByAlias(roomId);
|
|
||||||
if (room != null) {
|
if (room != null) {
|
||||||
return WidgetSpan(
|
return WidgetSpan(
|
||||||
child: MatrixPill(
|
child: MatrixPill(
|
||||||
@ -460,26 +430,9 @@ class RoomPillExtension extends HtmlExtension {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return WidgetSpan(
|
return TextSpan(text: context.innerHtml);
|
||||||
child: FutureBuilder<QueryPublicRoomsResponse>(
|
|
||||||
future: client.queryPublicRooms(
|
|
||||||
server: roomId.domain,
|
|
||||||
filter: PublicRoomQueryFilter(
|
|
||||||
genericSearchTerm: roomId,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
builder: (context, snapshot) {
|
|
||||||
final room = snapshot.data;
|
|
||||||
return MatrixPill(
|
|
||||||
name: room?.chunk.singleOrNull?.name ?? roomId,
|
|
||||||
avatar: room?.chunk.singleOrNull?.avatarUrl,
|
|
||||||
uri: href,
|
|
||||||
outerContext: this.context,
|
|
||||||
);
|
|
||||||
},
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user