|
| 1 | +INSERT INTO public.conversations (id, type) |
| 2 | +VALUES ('00000000-0000-0000-0000-000000000001', 'global') |
| 3 | +ON CONFLICT (id) DO UPDATE |
| 4 | +SET type = EXCLUDED.type; |
| 5 | +INSERT INTO public.conversation_participants (conversation_id, user_id, email) |
| 6 | +SELECT |
| 7 | + '00000000-0000-0000-0000-000000000001', |
| 8 | + u.id, |
| 9 | + COALESCE(u.email, CONCAT(u.id::text, '@user.local')) |
| 10 | +FROM auth.users u |
| 11 | +ON CONFLICT (conversation_id, user_id) DO UPDATE |
| 12 | +SET email = EXCLUDED.email; |
| 13 | +CREATE OR REPLACE FUNCTION public.ensure_user_in_global_chat() |
| 14 | +RETURNS TRIGGER AS $$ |
| 15 | +BEGIN |
| 16 | + IF EXISTS ( |
| 17 | + SELECT 1 |
| 18 | + FROM public.conversations |
| 19 | + WHERE id = '00000000-0000-0000-0000-000000000001' |
| 20 | + ) THEN |
| 21 | + INSERT INTO public.conversation_participants (conversation_id, user_id, email) |
| 22 | + VALUES ( |
| 23 | + '00000000-0000-0000-0000-000000000001', |
| 24 | + NEW.id, |
| 25 | + COALESCE(NEW.email, CONCAT(NEW.id::text, '@user.local')) |
| 26 | + ) |
| 27 | + ON CONFLICT (conversation_id, user_id) DO UPDATE |
| 28 | + SET email = EXCLUDED.email; |
| 29 | + END IF; |
| 30 | + |
| 31 | + RETURN NEW; |
| 32 | +END; |
| 33 | +$$ LANGUAGE plpgsql SECURITY DEFINER SET search_path = public; |
| 34 | +DROP TRIGGER IF EXISTS on_auth_user_join_global_chat ON auth.users; |
| 35 | +CREATE TRIGGER on_auth_user_join_global_chat |
| 36 | +AFTER INSERT ON auth.users |
| 37 | +FOR EACH ROW |
| 38 | +EXECUTE FUNCTION public.ensure_user_in_global_chat(); |
0 commit comments