File size: 6,319 Bytes
3bad34d
 
 
 
 
 
f15ff24
3bad34d
 
 
 
 
 
 
 
 
 
 
f15ff24
3bad34d
 
 
 
 
 
 
 
 
 
 
f15ff24
 
 
 
 
 
3bad34d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
710ace6
3bad34d
710ace6
 
 
 
3bad34d
 
 
 
 
 
 
710ace6
3bad34d
710ace6
 
 
 
3bad34d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
710ace6
3bad34d
710ace6
 
 
 
3bad34d
 
 
 
 
 
 
710ace6
3bad34d
710ace6
 
 
 
3bad34d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
create extension if not exists pgcrypto;

create table if not exists public.distillation_requests (
    id uuid primary key default gen_random_uuid (),
    source_dataset text not null,
    student_model text not null,
    submitter_name text null,
    additional_notes text not null default '',
    upvotes integer not null default 0,
    voted_ips text [] not null default '{}',
    owner_id text not null,
    created_at timestamptz not null default now(),
    status text not null default 'pending'
);

create table if not exists public.dataset_requests (
    id uuid primary key default gen_random_uuid (),
    source_model text not null,
    submitter_name text null,
    dataset_size text not null default '250x',
    reasoning_depth text not null default 'high',
    topics text [] not null default '{}',
    additional_notes text not null default '',
    upvotes integer not null default 0,
    voted_ips text [] not null default '{}',
    owner_id text not null,
    created_at timestamptz not null default now(),
    status text not null default 'pending'
);

alter table public.distillation_requests
add column if not exists submitter_name text;

alter table public.dataset_requests
add column if not exists submitter_name text;

create table if not exists public.request_comments (
    id uuid primary key default gen_random_uuid (),
    request_type text not null check (
        request_type in ('distillation', 'dataset')
    ),
    request_id uuid not null,
    body text not null,
    author text not null,
    role text not null check (role in ('admin', 'user')),
    owner_id text not null,
    created_at timestamptz not null default now(),
    edited_at timestamptz null
);

create index if not exists idx_request_comments_request on public.request_comments (request_type, request_id);

create index if not exists idx_request_comments_created_at on public.request_comments (created_at);

create or replace function public.toggle_upvote_distillation(request_id uuid, voter_ip text)
returns table(success boolean, upvotes integer, action text)
language plpgsql
security definer
as $$
declare
    current_ips text[];
    current_upvotes integer;
begin
    select r.voted_ips, r.upvotes
    into current_ips, current_upvotes
    from public.distillation_requests r
    where r.id = request_id
    for update;

    if not found then
        success := false;
        upvotes := 0;
        action := null;
        return next;
        return;
    end if;

    if voter_ip = any(current_ips) then
        update public.distillation_requests as r
        set
            voted_ips = array_remove(r.voted_ips, voter_ip),
            upvotes = greatest(r.upvotes - 1, 0)
        where r.id = request_id
        returning r.upvotes into upvotes;

        success := true;
        action := 'unvoted';
        return next;
        return;
    end if;

    update public.distillation_requests as r
    set
        voted_ips = array_append(r.voted_ips, voter_ip),
        upvotes = r.upvotes + 1
    where r.id = request_id
    returning r.upvotes into upvotes;

    success := true;
    action := 'upvoted';
    return next;
end;
$$;

create or replace function public.toggle_upvote_dataset(request_id uuid, voter_ip text)
returns table(success boolean, upvotes integer, action text)
language plpgsql
security definer
as $$
declare
    current_ips text[];
    current_upvotes integer;
begin
    select r.voted_ips, r.upvotes
    into current_ips, current_upvotes
    from public.dataset_requests r
    where r.id = request_id
    for update;

    if not found then
        success := false;
        upvotes := 0;
        action := null;
        return next;
        return;
    end if;

    if voter_ip = any(current_ips) then
        update public.dataset_requests as r
        set
            voted_ips = array_remove(r.voted_ips, voter_ip),
            upvotes = greatest(r.upvotes - 1, 0)
        where r.id = request_id
        returning r.upvotes into upvotes;

        success := true;
        action := 'unvoted';
        return next;
        return;
    end if;

    update public.dataset_requests as r
    set
        voted_ips = array_append(r.voted_ips, voter_ip),
        upvotes = r.upvotes + 1
    where r.id = request_id
    returning r.upvotes into upvotes;

    success := true;
    action := 'upvoted';
    return next;
end;
$$;

alter table public.distillation_requests enable row level security;

alter table public.dataset_requests enable row level security;

alter table public.request_comments enable row level security;

drop policy if exists public_select on public.distillation_requests;

drop policy if exists public_insert on public.distillation_requests;

drop policy if exists public_update on public.distillation_requests;

drop policy if exists public_delete on public.distillation_requests;

drop policy if exists public_select on public.dataset_requests;

drop policy if exists public_insert on public.dataset_requests;

drop policy if exists public_update on public.dataset_requests;

drop policy if exists public_delete on public.dataset_requests;

drop policy if exists public_select on public.request_comments;

drop policy if exists public_insert on public.request_comments;

drop policy if exists public_update on public.request_comments;

drop policy if exists public_delete on public.request_comments;

create policy public_select on public.distillation_requests for
select using (true);

create policy public_insert on public.distillation_requests for insert
with
    check (true);

create policy public_select on public.dataset_requests for
select using (true);

create policy public_insert on public.dataset_requests for insert
with
    check (true);

create policy public_select on public.request_comments for
select using (true);

create policy public_insert on public.request_comments for insert
with
    check (role = 'user');

grant
select, insert on table public.distillation_requests to anon, authenticated;

grant
select, insert on table public.dataset_requests to anon, authenticated;

grant
select, insert on table public.request_comments to anon, authenticated;

grant
execute on function public.toggle_upvote_distillation (uuid, text) to anon,
authenticated;

grant
execute on function public.toggle_upvote_dataset (uuid, text) to anon,
authenticated;