Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(neon_talk): Add room avatars #1661

Merged
merged 2 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/neon/neon_talk/lib/src/pages/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:neon_framework/utils.dart';
import 'package:neon_framework/widgets.dart';
import 'package:neon_talk/src/blocs/talk.dart';
import 'package:neon_talk/src/widgets/message_preview.dart';
import 'package:neon_talk/src/widgets/room_avatar.dart';
import 'package:neon_talk/src/widgets/unread_indicator.dart';
import 'package:nextcloud/spreed.dart' as spreed;

Expand Down Expand Up @@ -73,6 +74,9 @@ class _TalkMainPageState extends State<TalkMainPage> {
}

return ListTile(
leading: TalkRoomAvatar(
room: room,
),
title: Text(room.displayName),
subtitle: subtitle,
trailing: trailing,
Expand Down
62 changes: 62 additions & 0 deletions packages/neon/neon_talk/lib/src/widgets/room_avatar.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import 'package:flutter/material.dart';
import 'package:neon_framework/blocs.dart';
import 'package:neon_framework/utils.dart';
import 'package:neon_framework/widgets.dart';
import 'package:nextcloud/spreed.dart' as spreed;

/// Displays the avatar of the [room].
///
/// If the room has a custom avatar it will be displayed. If that is not the case and it is a
/// [spreed.RoomType.oneToOne] the user avatar will be shown, otherwise an appropriate icon is displayed.
class TalkRoomAvatar extends StatelessWidget {
/// Creates a new Talk room avatar.
const TalkRoomAvatar({
required this.room,
super.key,
});

/// The room to display the avatar for.
final spreed.Room room;

@override
Widget build(BuildContext context) {
final account = NeonProvider.of<AccountsBloc>(context).activeAccount.value!;

if (room.isCustomAvatar ?? false) {
final brightness = Theme.of(context).brightness;

return CircleAvatar(
child: ClipOval(
child: NeonApiImage.withAccount(
account: account,
getImage: (client) => switch (brightness) {
Brightness.dark => client.spreed.avatar.getAvatarDarkRaw(token: room.token),
Brightness.light => client.spreed.avatar.getAvatarRaw(token: room.token),
},
cacheKey: 'talk-room-${room.token}-avatar-$brightness',
etag: room.avatarVersion,
expires: null,
),
),
);
}

return switch (spreed.RoomType.fromValue(room.type)) {
spreed.RoomType.oneToOne => NeonUserAvatar(
account: account,
username: room.name,
),
spreed.RoomType.group => _buildIconAvatar(Icons.group),
spreed.RoomType.public => _buildIconAvatar(Icons.link),
spreed.RoomType.changelog => _buildIconAvatar(Icons.text_snippet_outlined),
spreed.RoomType.oneToOneFormer => _buildIconAvatar(Icons.lock),
spreed.RoomType.noteToSelf => _buildIconAvatar(Icons.edit_note),
};
}

Widget _buildIconAvatar(IconData icon) => CircleAvatar(
child: Icon(
icon,
),
);
}
101 changes: 101 additions & 0 deletions packages/neon/neon_talk/test/room_avatar_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import 'package:built_collection/built_collection.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
import 'package:neon_framework/blocs.dart';
import 'package:neon_framework/testing.dart';
import 'package:neon_framework/utils.dart';
import 'package:neon_framework/widgets.dart';
import 'package:neon_talk/src/widgets/room_avatar.dart';
import 'package:nextcloud/nextcloud.dart';
import 'package:nextcloud/spreed.dart' as spreed;
import 'package:rxdart/subjects.dart';

class MockRoom extends Mock implements spreed.Room {}

Widget wrapWidget(AccountsBloc accountsBloc, Widget child) => MaterialApp(
home: NeonProvider<AccountsBloc>(
create: (_) => accountsBloc,
child: child,
),
);

void main() {
setUpAll(() {
final storage = MockNeonStorage();
when(() => storage.requestCache).thenReturn(null);
});

testWidgets('Custom avatar', (tester) async {
final account = MockAccount();
when(() => account.id).thenReturn('');
when(() => account.client).thenReturn(NextcloudClient(Uri.parse('')));

final accountsBloc = MockAccountsBloc();
when(() => accountsBloc.activeAccount).thenAnswer((_) => BehaviorSubject.seeded(account));

final room = MockRoom();
when(() => room.isCustomAvatar).thenReturn(true);
when(() => room.token).thenReturn('abc123');
when(() => room.avatarVersion).thenReturn('');

await tester.pumpWidget(
wrapWidget(
accountsBloc,
TalkRoomAvatar(
room: room,
),
),
);
expect(find.byType(NeonApiImage), findsOne);
});

testWidgets('One to one', (tester) async {
final account = MockAccount();
when(() => account.id).thenReturn('');
when(() => account.client).thenReturn(NextcloudClient(Uri.parse('')));

final userStatusBloc = MockUserStatusBloc();
when(() => userStatusBloc.statuses).thenAnswer((_) => BehaviorSubject.seeded(BuiltMap()));

final accountsBloc = MockAccountsBloc();
when(() => accountsBloc.activeAccount).thenAnswer((_) => BehaviorSubject.seeded(account));
when(() => accountsBloc.getUserStatusBlocFor(account)).thenReturn(userStatusBloc);

final room = MockRoom();
when(() => room.isCustomAvatar).thenReturn(false);
when(() => room.type).thenReturn(spreed.RoomType.oneToOne.value);
when(() => room.name).thenReturn('');

await tester.pumpWidget(
wrapWidget(
accountsBloc,
TalkRoomAvatar(
room: room,
),
),
);
expect(find.byType(NeonUserAvatar), findsOne);
});

testWidgets('Other', (tester) async {
final account = MockAccount();

final accountsBloc = MockAccountsBloc();
when(() => accountsBloc.activeAccount).thenAnswer((_) => BehaviorSubject.seeded(account));

final room = MockRoom();
when(() => room.isCustomAvatar).thenReturn(false);
when(() => room.type).thenReturn(spreed.RoomType.group.value);

await tester.pumpWidget(
wrapWidget(
accountsBloc,
TalkRoomAvatar(
room: room,
),
),
);
expect(find.byIcon(Icons.group), findsOne);
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
POST http://localhost/ocs/v2\.php/apps/spreed/api/v4/room\?roomType=3&invite=&roomName=Test&source=&objectType=&objectId=
accept: application/json
authorization: Bearer mock
ocs-apirequest: true
POST http://localhost/ocs/v2\.php/apps/spreed/api/v1/room/[a-z0-9]{8}/avatar/emoji\?emoji=%F0%9F%98%80
accept: application/json
authorization: Bearer mock
ocs-apirequest: true
DELETE http://localhost/ocs/v2\.php/apps/spreed/api/v1/room/[a-z0-9]{8}/avatar
accept: application/json
authorization: Bearer mock
ocs-apirequest: true
12 changes: 12 additions & 0 deletions packages/nextcloud/test/fixtures/spreed/avatar/get_avatar.regexp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
POST http://localhost/ocs/v2\.php/apps/spreed/api/v4/room\?roomType=3&invite=&roomName=Test&source=&objectType=&objectId=
accept: application/json
authorization: Bearer mock
ocs-apirequest: true
POST http://localhost/ocs/v2\.php/apps/spreed/api/v1/room/[a-z0-9]{8}/avatar/emoji\?emoji=%F0%9F%98%80
accept: application/json
authorization: Bearer mock
ocs-apirequest: true
GET http://localhost/ocs/v2\.php/apps/spreed/api/v1/room/[a-z0-9]{8}/avatar\?darkTheme=0
accept: \*/\*
authorization: Bearer mock
ocs-apirequest: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
POST http://localhost/ocs/v2\.php/apps/spreed/api/v4/room\?roomType=3&invite=&roomName=Test&source=&objectType=&objectId=
accept: application/json
authorization: Bearer mock
ocs-apirequest: true
POST http://localhost/ocs/v2\.php/apps/spreed/api/v1/room/[a-z0-9]{8}/avatar/emoji\?emoji=%F0%9F%98%80
accept: application/json
authorization: Bearer mock
ocs-apirequest: true
GET http://localhost/ocs/v2\.php/apps/spreed/api/v1/room/[a-z0-9]{8}/avatar/dark
accept: \*/\*
authorization: Bearer mock
ocs-apirequest: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
POST http://localhost/ocs/v2\.php/apps/spreed/api/v4/room\?roomType=3&invite=&roomName=Test&source=&objectType=&objectId=
accept: application/json
authorization: Bearer mock
ocs-apirequest: true
POST http://localhost/ocs/v2\.php/apps/spreed/api/v1/room/[a-z0-9]{8}/avatar/emoji\?emoji=%F0%9F%98%80
accept: application/json
authorization: Bearer mock
ocs-apirequest: true
60 changes: 60 additions & 0 deletions packages/nextcloud/test/spreed_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:nextcloud/spreed.dart' as spreed;
import 'package:nextcloud_test/nextcloud_test.dart';
import 'package:test/test.dart';
import 'package:test_api/src/backend/invoker.dart';
import 'package:version/version.dart';

void main() {
presets(
Expand Down Expand Up @@ -174,6 +175,65 @@ void main() {
});
});

group(
'Avatar',
() {
test('Set emoji avatar', () async {
final room = await createTestRoom();

final response = await client1.spreed.avatar.emojiAvatar(
emoji: '😀',
token: room.token,
);
expect(response.statusCode, 200);
expect(response.body.ocs.data.isCustomAvatar, true);
});

test('Get avatar', () async {
final room = await createTestRoom();
await client1.spreed.avatar.emojiAvatar(
emoji: '😀',
token: room.token,
);

final response = await client1.spreed.avatar.getAvatar(
token: room.token,
);
expect(response.statusCode, 200);
expect(response.body, isNotEmpty);
});

test('Get avatar dark', () async {
final room = await createTestRoom();
await client1.spreed.avatar.emojiAvatar(
emoji: '😀',
token: room.token,
);

final response = await client1.spreed.avatar.getAvatarDark(
token: room.token,
);
expect(response.statusCode, 200);
expect(response.body, isNotEmpty);
});

test('Delete avatar', () async {
final room = await createTestRoom();
await client1.spreed.avatar.emojiAvatar(
emoji: '😀',
token: room.token,
);

final response = await client1.spreed.avatar.deleteAvatar(
token: room.token,
);
expect(response.statusCode, 200);
expect(response.body.ocs.data.isCustomAvatar, false);
});
},
skip: preset.version < Version(17, 0, 0),
);

group('Chat', () {
test('Send message', () async {
final startTime = DateTime.now();
Expand Down