Skip to content

Commit

Permalink
feat: Edge function improvement (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
mirland authored Oct 23, 2023
1 parent 7a95ae9 commit b3802c3
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 31 deletions.
5 changes: 5 additions & 0 deletions backend/supabase/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"deno.enable": true,
"deno.lint": true,
"deno.unstable": false
}
44 changes: 28 additions & 16 deletions backend/supabase/functions/uppercase_message/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// https://deno.land/manual/getting_started/setup_your_environment
// This enables autocomplete, go to definition, etc.

import { serve } from "https://deno.land/[email protected]/http/server.ts"
import { serve } from "https://deno.land/[email protected]/http/server.ts";
import { createClient } from "https://esm.sh/@supabase/[email protected]?target=deno";

const supabaseClient = createClient(
Expand All @@ -12,33 +12,45 @@ const supabaseClient = createClient(
global: {
headers: {
Authorization: "Bearer " + Deno.env.get("SUPABASE_SERVICE_ROLE_KEY"),
"Access-Control-Allow-Origin": "*",
},
},
},
);

serve(async (req) => {
const { id } = await req.json()
export const corsHeaders = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers":
"authorization, x-client-info, apikey, content-type",
};

const { data, error: getBodyError } = await supabaseClient.from("messages").select("body").eq("id", id)
const createApiResponse = (statusCode: number, body: { message: string }) => {
const headers = { ...corsHeaders, "Content-Type": "application/json" };
return new Response(JSON.stringify(body), { headers, status: statusCode });
};

if (getBodyError != null) {
return new Response(JSON.stringify({ "message": "Error getting message body" }), { headers: { "Content-Type": "application/json" } },)
serve(async (req) => {
// Required for web browsers
if (req.method === "OPTIONS") {
return createApiResponse(200, { message: "ok" });
}

const body = data[0].body
const { id } = await req.json();
const { data: messages, error: getBodyError } = await supabaseClient.from(
"messages",
).select("body").eq("id", id);

const capitalizedMessage = body.toUpperCase()
if (getBodyError != null || !messages || messages.length !== 1) {
return createApiResponse(400, { message: "Error getting message" });
}

const { error: updateMessageError } = await supabaseClient.from("messages").update({ body: capitalizedMessage }).eq("id", id)
const capitalizedBody = messages[0].body.toUpperCase();
const { error: updateMessageError } = await supabaseClient.from("messages")
.update({ body: capitalizedBody }).eq("id", id);

if (updateMessageError != null) {
return new Response(JSON.stringify({ "message": "Error updating message" }), { headers: { "Content-Type": "application/json" } },)
return createApiResponse(500, { message: "Error updating message" });
}


return new Response(
JSON.stringify({ "message": "Success" }),
{ headers: { "Content-Type": "application/json" } },
)
})
return createApiResponse(200, { "message": "Success" });
});
11 changes: 11 additions & 0 deletions mobile/.idea/runConfigurations/main_dev.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions mobile/.idea/runConfigurations/main_prod.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 15 additions & 2 deletions mobile/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,25 @@
"version": "0.2.0",
"configurations": [
{
"name": "supabase-ws-flutter-development",
"name": "Main Dev",
"request": "launch",
"type": "dart",
"program": "lib/main.dart",
"args": [
"--flavor=dev"
"--flavor=dev",
"--dart-define=ENV=dev",
"--web-port=3003"
]
},
{
"name": "Main prod",
"request": "launch",
"type": "dart",
"program": "lib/main.dart",
"args": [
"--flavor=prod",
"--web-port=3003",
"--dart-define=ENV=prod"
]
}
]
Expand Down
4 changes: 4 additions & 0 deletions mobile/SOLUTION_CODE.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ CREATE TABLE
) tablespace pg_default;


-- Enable Realtime
alter
publication supabase_realtime add table public.users;

-- Insert alias into the new table
INSERT INTO
public.users (id, alias)
Expand Down
14 changes: 10 additions & 4 deletions mobile/lib/core/source/messages_remote_source.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,15 @@ class MessagesRemoteSourceImpl implements MessagesRemoteSource {
}

@override
Future<void> uppercaseMessage({required String id}) =>
supabaseClient.functions.invoke(
'uppercase_message',
body: {'id': id},
Future<void> uppercaseMessage({required String id}) async {
final response = await supabaseClient.functions.invoke(
'uppercase_message',
body: {'id': id},
);
if (response.status! > 299) {
throw Exception(
'Error calling function: code ${response.status} body ${response.data}',
);
}
}
}
11 changes: 11 additions & 0 deletions mobile/lib/ui/extensions/screen_utils_extensions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'dart:math';

import 'package:flutter_screenutil/flutter_screenutil.dart';

extension SizeExtension on num {
double get wf => min(this * 1.08, w);

double get hf => min(this * 1.08, h);

double get rf => min(this * 1.08, r);
}
24 changes: 15 additions & 9 deletions mobile/lib/ui/widgets/message_box.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import 'package:flutter/material.dart';
import 'package:flutter_linkify/flutter_linkify.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_template/core/common/extension/date_time_extensions.dart';
import 'package:flutter_template/core/model/message.dart';
import 'package:flutter_template/core/model/user_message.dart';
import 'package:flutter_template/ui/extensions/context_extensions.dart';
import 'package:flutter_template/ui/extensions/screen_utils_extensions.dart';
import 'package:flutter_template/ui/theme/app_theme.dart';
import 'package:flutter_template/ui/theme/text_styles.dart';
import 'package:url_launcher/url_launcher.dart';

class MessageBox extends StatelessWidget {
final UserMessage userMessage;
Expand Down Expand Up @@ -36,10 +39,10 @@ class MessageBox extends StatelessWidget {
_UserAliasSection(userMessage: userMessage),
Container(
padding: EdgeInsets.only(
right: 12.w,
left: 12.w,
top: 8.h,
bottom: 12.h,
right: 12.rf,
left: 12.rf,
top: 8.rf,
bottom: 12.rf,
),
decoration: BoxDecoration(
color: userMessage.isFromCurrentUser
Expand All @@ -59,11 +62,12 @@ class MessageBox extends StatelessWidget {
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SelectableText(
userMessage.message.body,
SelectableLinkify(
text: userMessage.message.body,
style: context.theme.textStyles.bodyMedium?.copyWith(
color: context.theme.colors.textColor.shade100,
),
onOpen: (link) => launchUrl(Uri.parse(link.url)),
),
SizedBox(height: 2.h),
Text(
Expand Down Expand Up @@ -96,9 +100,11 @@ class _UppercaseButton extends StatelessWidget {
@override
Widget build(BuildContext context) => InkWell(
onTap: () => onTap(message),
child: SizedBox(
height: 20.h,
width: 60.w,
child: Padding(
padding: EdgeInsets.symmetric(
vertical: 7.rf,
horizontal: 5.rf,
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
Expand Down
16 changes: 16 additions & 0 deletions mobile/pubspec.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions mobile/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ dependencies:
floor: 1.4.2
flutter_bloc: 8.1.3
flutter_dotenv: 5.1.0
flutter_linkify: 6.0.0
flutter_native_splash: 2.3.2
flutter_screenutil: 5.9.0
flutter_secure_storage: 9.0.0
Expand Down

0 comments on commit b3802c3

Please sign in to comment.