Skip to content

Commit

Permalink
v1.4.0 (#19)
Browse files Browse the repository at this point in the history
## [1.4.0] - 2024-11-21
#### [@rickypid](https://github.com/rickypid)

⚠️⚠️ **Need schema migration** ⚠️⚠️

### Improvements

* Now when we get the rooms the `rooms_l` view is used so that we can get all the information without having to do multiple queries

### Fixed

* Fixed #20 Chat creator role is null instead of admin
* Fixed online user status realtime subscription
  • Loading branch information
rickypid authored Nov 21, 2024
1 parent 05cb266 commit d1b72c7
Show file tree
Hide file tree
Showing 12 changed files with 223 additions and 127 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
## [1.4.0] - 2024-11-21
#### [@rickypid](https://github.com/rickypid)

⚠️⚠️ **Need schema migration** ⚠️⚠️

### Improvements

* Now when we get the rooms the `rooms_l` view is used so that we can get all the information without having to do multiple queries

### Fixed

* Fixed #20 Chat creator role is null instead of admin
* Fixed online user status realtime subscription

## [1.3.2] - 2024-11-13
#### [@rickypid](https://github.com/rickypid)

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,4 @@ Below are some activities to complete to have a more complete and optimized proj
4. Chat room channels
5. Sending audio messages
6. Improve documentation
7. Use rooms view for improvement user parsing performance

85 changes: 62 additions & 23 deletions doc/docs/guides/supabse-views.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,69 @@ title: Database Views

## Rooms view

This is a view of `rooms` table, this view allows you to obtain the name of the sender of the message dynamically in direct rooms, based on the logged-in user the name of the correspondent is displayed.
This is a view of `rooms` table, this view allows you to obtain the name of the sender of the message dynamically in direct rooms, based on the logged-in user the name of the correspondent is displayed, it is also included the list of uses member objects.

```sql
DROP VIEW IF EXISTS chats.rooms_l;
create view chats.rooms_l
WITH (security_invoker='on') as
select
r.id,
r."imageUrl",
r.metadata,
case
when r.type = 'direct' and auth.uid() is not null then
(select coalesce(u."firstName", '') || ' ' || coalesce(u."lastName", '')
from chats.users u
where u.id = any(r."userIds") and u.id <> auth.uid()
limit 1)
else
r.name
end as name,
r.type,
r."userIds",
r."lastMessages",
r."userRoles",
r."createdAt",
r."updatedAt"
create or replace view chats.rooms_l
with (security_invoker='on') as
select r.id,
r."imageUrl",
r.metadata,
case
when r.type = 'direct' and auth.uid() is not null then
(select coalesce(u."firstName", '') || ' ' || coalesce(u."lastName", '')
from chats.users u
where u.id = any (r."userIds")
and u.id <> auth.uid()
limit 1)
else
r.name
end as name,
r.type,
r."userIds",
r."lastMessages",
r."userRoles",
r."createdAt",
r."updatedAt",
(select jsonb_agg(to_jsonb(u))
from chats.users u
where u.id = any (r."userIds")) as users
from chats.rooms r;
```


## Messages view

This is a view of `messages` table, this view allows you to obtain the author user object and room object.

```sql
create or replace view chats.messages_l
with (security_invoker='on') as
select m.id,
m."createdAt",
m.metadata,
m.duration,
m."mimeType",
m.name,
m."remoteId",
m."repliedMessage",
m."roomId",
m."showStatus",
m.size,
m.status,
m.type,
m."updatedAt",
m.uri,
m."waveForm",
m."isLoading",
m.height,
m.width,
m."previewData",
m."authorId",
m.text,
to_jsonb(u) as author,
to_jsonb(r) as room
from chats.messages m
left join chats.users u on u.id = m."authorId"
left join chats.rooms_l r on r.id = m."roomId";
```
2 changes: 1 addition & 1 deletion doc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "flutter-supabase-chat-core",
"version": "1.3.2",
"version": "1.4.0",
"private": true,
"scripts": {
"docusaurus": "docusaurus",
Expand Down
4 changes: 2 additions & 2 deletions example/lib/src/pages/auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:flutter_login/flutter_login.dart';
import 'package:flutter_supabase_chat_core/flutter_supabase_chat_core.dart';
import 'package:supabase_flutter/supabase_flutter.dart';

import 'rooms.dart';
import 'home.dart';

class AuthScreen extends StatefulWidget {
const AuthScreen({
Expand Down Expand Up @@ -83,7 +83,7 @@ class _AuthScreenState extends State<AuthScreen> {
onSubmitAnimationCompleted: () {
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (context) => const RoomsPage(),
builder: (context) => const HomePage(),
),
);
},
Expand Down
12 changes: 6 additions & 6 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: example
description: A new Flutter project.
publish_to: 'none'

version: 1.3.2
version: 1.4.0

environment:
sdk: '>=3.4.0 <4.0.0'
Expand All @@ -11,7 +11,7 @@ dependencies:
cupertino_icons: ^1.0.8
dio: ^5.7.0
faker: ^2.2.0
file_picker: ^8.1.3
file_picker: ^8.1.4
file_saver: ^0.2.14
flutter:
sdk: flutter
Expand All @@ -20,13 +20,13 @@ dependencies:
flutter_login: ^5.0.0
flutter_supabase_chat_core:
path: ../
flutter_svg: ^2.0.10+1
flutter_svg: ^2.0.14
http: ^1.2.2
image_picker: ^1.1.2
infinite_scroll_pagination: ^4.0.0
infinite_scroll_pagination: ^4.1.0
open_filex: ^4.5.0
path_provider: ^2.1.4
supabase_flutter: ^2.8.0
path_provider: ^2.1.5
supabase_flutter: ^2.8.1
timeago: ^3.7.0


Expand Down
80 changes: 58 additions & 22 deletions example/utils/sql/05_database_view.sql
Original file line number Diff line number Diff line change
@@ -1,23 +1,59 @@
DROP VIEW IF EXISTS chats.messages_l;
DROP VIEW IF EXISTS chats.rooms_l;
create view chats.rooms_l
WITH (security_invoker='on') as
select
r.id,
r."imageUrl",
r.metadata,
case
when r.type = 'direct' and auth.uid() is not null then
(select coalesce(u."firstName", '') || ' ' || coalesce(u."lastName", '')
from chats.users u
where u.id = any(r."userIds") and u.id <> auth.uid()
limit 1)
else
r.name
end as name,
r.type,
r."userIds",
r."lastMessages",
r."userRoles",
r."createdAt",
r."updatedAt"
from chats.rooms r;

create or replace view chats.rooms_l
with (security_invoker='on') as
select r.id,
r."imageUrl",
r.metadata,
case
when r.type = 'direct' and auth.uid() is not null then
(select coalesce(u."firstName", '') || ' ' || coalesce(u."lastName", '')
from chats.users u
where u.id = any (r."userIds")
and u.id <> auth.uid()
limit 1)
else
r.name
end as name,
r.type,
r."userIds",
r."lastMessages",
r."userRoles",
r."createdAt",
r."updatedAt",
(select jsonb_agg(to_jsonb(u))
from chats.users u
where u.id = any (r."userIds")) as users
from chats.rooms r;


create or replace view chats.messages_l
with (security_invoker='on') as
select m.id,
m."createdAt",
m.metadata,
m.duration,
m."mimeType",
m.name,
m."remoteId",
m."repliedMessage",
m."roomId",
m."showStatus",
m.size,
m.status,
m.type,
m."updatedAt",
m.uri,
m."waveForm",
m."isLoading",
m.height,
m.width,
m."previewData",
m."authorId",
m.text,
to_jsonb(u) as author,
to_jsonb(r) as room
from chats.messages m
left join chats.users u on u.id = m."authorId"
left join chats.rooms_l r on r.id = m."roomId";
2 changes: 1 addition & 1 deletion lib/src/class/supabase_chat_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class SupabaseChatController {

PostgrestTransformBuilder _messagesQuery() => _client
.schema(_config.schema)
.from(_config.messagesTableName)
.from(_config.messagesViewName)
.select()
.eq('roomId', int.parse(_room.id))
.order('createdAt', ascending: false)
Expand Down
Loading

0 comments on commit d1b72c7

Please sign in to comment.