Skip to content

Commit d079ef1

Browse files
author
riccardocucia
committed
Wip views
1 parent c04728a commit d079ef1

File tree

9 files changed

+91
-91
lines changed

9 files changed

+91
-91
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,4 +279,4 @@ Below are some activities to complete to have a more complete and optimized proj
279279
4. Chat room channels
280280
5. Sending audio messages
281281
6. Improve documentation
282-
7. Use rooms view for improvement user parsing performance
282+

doc/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "flutter-supabase-chat-core",
3-
"version": "1.3.2",
3+
"version": "1.4.0",
44
"private": true,
55
"scripts": {
66
"docusaurus": "docusaurus",

example/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: example
22
description: A new Flutter project.
33
publish_to: 'none'
44

5-
version: 1.3.2
5+
version: 1.4.0
66

77
environment:
88
sdk: '>=3.4.0 <4.0.0'
Lines changed: 52 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,59 @@
1+
DROP VIEW IF EXISTS chats.messages_l;
12
DROP VIEW IF EXISTS chats.rooms_l;
3+
24
create or replace view chats.rooms_l
3-
with (security_invoker='on') as
4-
select
5-
r.id,
6-
r."imageUrl",
7-
r.metadata,
8-
case
9-
when r.type = 'direct' and auth.uid() is not null then
10-
(select coalesce(u."firstName", '') || ' ' || coalesce(u."lastName", '')
11-
from chats.users u
12-
where u.id = any(r."userIds") and u.id <> auth.uid()
13-
limit 1)
14-
else
15-
r.name
16-
end as name,
17-
r.type,
18-
r."userIds",
19-
r."lastMessages",
20-
r."userRoles",
21-
r."createdAt",
22-
r."updatedAt",
23-
(select jsonb_agg(to_jsonb(u))
24-
from chats.users u
25-
where u.id = any(r."userIds")) as users
5+
with (security_invoker='on') as
6+
select r.id,
7+
r."imageUrl",
8+
r.metadata,
9+
case
10+
when r.type = 'direct' and auth.uid() is not null then
11+
(select coalesce(u."firstName", '') || ' ' || coalesce(u."lastName", '')
12+
from chats.users u
13+
where u.id = any (r."userIds")
14+
and u.id <> auth.uid()
15+
limit 1)
16+
else
17+
r.name
18+
end as name,
19+
r.type,
20+
r."userIds",
21+
r."lastMessages",
22+
r."userRoles",
23+
r."createdAt",
24+
r."updatedAt",
25+
(select jsonb_agg(to_jsonb(u))
26+
from chats.users u
27+
where u.id = any (r."userIds")) as users
2628
from chats.rooms r;
2729

28-
DROP VIEW IF EXISTS chats.messages_l;
29-
create or replace view chats.messages_l as
30-
select
31-
m.id,
32-
m."createdAt",
33-
m.metadata,
34-
m.duration,
35-
m."mimeType",
36-
m.name,
37-
m."remoteId",
38-
m."repliedMessage",
39-
m."roomId",
40-
m."showStatus",
41-
m.size,
42-
m.status,
43-
m.type,
44-
m."updatedAt",
45-
m.uri,
46-
m."waveForm",
47-
m."isLoading",
48-
m.height,
49-
m.width,
50-
m."previewData",
51-
m."authorId",
52-
m.text,
53-
to_jsonb(u) as author,
54-
to_jsonb(r) as room
30+
31+
create or replace view chats.messages_l
32+
with (security_invoker='on') as
33+
select m.id,
34+
m."createdAt",
35+
m.metadata,
36+
m.duration,
37+
m."mimeType",
38+
m.name,
39+
m."remoteId",
40+
m."repliedMessage",
41+
m."roomId",
42+
m."showStatus",
43+
m.size,
44+
m.status,
45+
m.type,
46+
m."updatedAt",
47+
m.uri,
48+
m."waveForm",
49+
m."isLoading",
50+
m.height,
51+
m.width,
52+
m."previewData",
53+
m."authorId",
54+
m.text,
55+
to_jsonb(u) as author,
56+
to_jsonb(r) as room
5557
from chats.messages m
5658
left join chats.users u on u.id = m."authorId"
5759
left join chats.rooms_l r on r.id = m."roomId";

lib/src/class/supabase_chat_controller.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class SupabaseChatController {
6969

7070
PostgrestTransformBuilder _messagesQuery() => _client
7171
.schema(_config.schema)
72-
.from(_config.messagesTableName)
72+
.from(_config.messagesViewName)
7373
.select()
7474
.eq('roomId', int.parse(_room.id))
7575
.order('createdAt', ascending: false)

lib/src/class/supabase_chat_core.dart

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class SupabaseChatCore {
5050
'rooms',
5151
'rooms_l',
5252
'messages',
53+
'messages_l',
5354
'users',
5455
'online-user-',
5556
//online-user-${uid}
@@ -173,15 +174,7 @@ class SupabaseChatCore {
173174
}) async {
174175
if (loggedSupabaseUser == null) return Future.error('User does not exist');
175176

176-
final currentUser = await fetchUser(
177-
client,
178-
loggedSupabaseUser!.id,
179-
config.usersTableName,
180-
config.schema,
181-
role: creatorRole.toShortString(),
182-
);
183-
184-
final roomUsers = [types.User.fromJson(currentUser)] + users;
177+
final roomUsers = [loggedUser!] + users;
185178

186179
final room =
187180
await client.schema(config.schema).from(config.roomsTableName).insert({

lib/src/class/supabase_chat_core_config.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class SupabaseChatCoreConfig {
1212
this.roomsTableName,
1313
this.roomsViewName,
1414
this.messagesTableName,
15+
this.messagesViewName,
1516
this.usersTableName,
1617
this.realtimeOnlineUserPrefixChannel,
1718
this.realtimeChatTypingUserPrefixChannel,
@@ -30,6 +31,9 @@ class SupabaseChatCoreConfig {
3031
/// Property to set messages table name.
3132
final String messagesTableName;
3233

34+
/// Property to set messages view name.
35+
final String messagesViewName;
36+
3337
/// Property to set users table name.
3438
final String usersTableName;
3539

lib/src/util.dart

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,17 @@ Future<Map<String, dynamic>> fetchUser(
2020
String usersTableName,
2121
String schema, {
2222
String? role,
23-
}) async =>
24-
(await instance
25-
.schema(schema)
26-
.from(usersTableName)
27-
.select()
28-
.eq('id', userId)
29-
.limit(1))
30-
.first;
23+
}) async {
24+
final data = (await instance
25+
.schema(schema)
26+
.from(usersTableName)
27+
.select()
28+
.eq('id', userId)
29+
.limit(1))
30+
.first;
31+
data['role'] = role;
32+
return data;
33+
}
3134

3235
/// Returns a list of [types.Room] created from Firebase query.
3336
/// If room has 2 participants, sets correct room name and image.
@@ -63,30 +66,28 @@ Future<types.Room> processRoomRow(
6366
final type = data['type'] as String;
6467
final userIds = data['userIds'] as List<dynamic>;
6568
final userRoles = data['userRoles'] as Map<String, dynamic>?;
66-
67-
final users = await Future.wait(
68-
userIds.map(
69-
(userId) => fetchUser(
70-
instance,
71-
userId as String,
72-
usersTableName,
73-
schema,
74-
role: userRoles?[userId] as String?,
75-
),
76-
),
77-
);
69+
final users = data['users'] ??
70+
await Future.wait(
71+
userIds.map(
72+
(userId) => fetchUser(
73+
instance,
74+
userId as String,
75+
usersTableName,
76+
schema,
77+
role: userRoles?[userId] as String?,
78+
),
79+
),
80+
);
7881

7982
if (type == types.RoomType.direct.toShortString()) {
80-
try {
81-
final otherUser = users.firstWhere(
82-
(u) => u['id'] != supabaseUser.id,
83-
);
83+
final index = users.indexWhere(
84+
(u) => u['id'] != supabaseUser.id,
85+
);
86+
if (index >= 0) {
87+
final otherUser = users[index];
8488
imageUrl = otherUser['imageUrl'] as String?;
8589
name = '${otherUser['firstName'] ?? ''} ${otherUser['lastName'] ?? ''}'
8690
.trim();
87-
} catch (e) {
88-
// Do nothing if other user is not found, because he should be found.
89-
// Consider falling back to some default values.
9091
}
9192
}
9293
data['imageUrl'] = imageUrl;

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: flutter_supabase_chat_core
22
description: >
33
Actively maintained, community-driven Supabase BaaS for chat applications
44
with an optional chat UI.
5-
version: 1.3.2
5+
version: 1.4.0
66
homepage: https://flutter-supabase-chat-core.insideapp.it
77
repository: https://github.com/insideapp-srl/flutter_supabase_chat_core
88

0 commit comments

Comments
 (0)