-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers_chats.sql
328 lines (232 loc) · 9.04 KB
/
users_chats.sql
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
--
-- PostgreSQL database dump
--
-- Dumped from database version 16rc1
-- Dumped by pg_dump version 16rc1
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;
--
-- Name: group_messages; Type: SCHEMA; Schema: -; Owner: postgres
--
CREATE SCHEMA group_messages;
ALTER SCHEMA group_messages OWNER to postgres;
--
-- Name: group_profile; Type: SCHEMA; Schema: -; Owner: postgres
--
CREATE SCHEMA group_profile;
ALTER SCHEMA group_profile OWNER to postgres;
--
-- Name: private_messages; Type: SCHEMA; Schema: -; Owner: postgres
--
CREATE SCHEMA private_messages;
ALTER SCHEMA private_messages OWNER to postgres;
--
-- Name: users_chats; Type: SCHEMA; Schema: -; Owner: postgres
--
CREATE SCHEMA users_chats;
ALTER SCHEMA users_chats OWNER to postgres;
--
-- Name: dblink; Type: EXTENSION; Schema: -; Owner: -
--
CREATE EXTENSION IF NOT EXISTS dblink WITH SCHEMA public;
--
-- Name: EXTENSION dblink; Type: COMMENT; Schema: -; Owner:
--
COMMENT ON EXTENSION dblink IS 'connect to other PostgreSQL databases from within a database';
--
-- Name: fetch_user_chats(text, integer, integer, text, text, integer, text); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION public.fetch_user_chats(currentid text, currentlength integer, paginationlimit integer, username text, ip text, port integer, password text) RETURNS TABLE(chat_data json)
LANGUAGE plpgsql
AS $_$
declare
begin
return query select row_to_json(f) from users_chats.chats_history as f
where f.user_id = $1 and f.deleted = false and (f.recipient = '' or
(f.recipient != '' and not is_blocked_user($1, f.recipient, username, ip, port, password)
and not is_blocked_user(f.recipient, $1, username, ip, port, password)
and is_exists_user($1, f.recipient, username, ip, port, password)))
offset $2 limit $3;
end;
$_$;
ALTER FUNCTION public.fetch_user_chats(currentid text, currentlength integer, paginationlimit integer, username text, ip text, port integer, password text) OWNER TO postgres;
--
-- Name: is_blocked_user(text, text, text, text, integer, text); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION public.is_blocked_user(checkingid text, checkedid text, username text, ip text, port integer, password text) RETURNS boolean
LANGUAGE plpgsql
AS $_$
declare
res bool;
users_profiles_path text := 'dbname=users_profiles user='||username||' hostaddr='||ip||' port='||port||' password='||password;
begin
if checkingid = checkedid then return false; end if;
select exists (
select * from dblink(users_profiles_path, 'select * from blocked_users.block_history') as b(user_id text, blocked_id text)
where b.user_id = $1 and b.blocked_id = $2
) into res;
return res;
end;
$_$;
ALTER FUNCTION public.is_blocked_user(checkingid text, checkedid text, username text, ip text, port integer, password text) OWNER TO postgres;
--
-- Name: is_exists_user(text, text, text, text, integer, text); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION public.is_exists_user(checkingid text, checkedid text, username text, ip text, port integer, password text) RETURNS boolean
LANGUAGE plpgsql
AS $_$
declare
res bool;
users_profiles_path text := 'dbname=users_profiles user='||username||' hostaddr='||ip||' port='||port||' password='||password;
begin
if checkingid = checkedid then return true; end if;
select exists (
select * from dblink(users_profiles_path, 'select user_id, deleted, suspended from basic_data.user_profile') as pr(user_id text, deleted bool, suspended bool)
where pr.user_id = $2 and pr.deleted = false and pr.suspended = false
) into res;
return res;
end;
$_$;
ALTER FUNCTION public.is_exists_user(checkingid text, checkedid text, username text, ip text, port integer, password text) OWNER TO postgres;
--
-- Name: is_muted_user(text, text, text, text, integer, text); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION public.is_muted_user(checkingid text, checkedid text, username text, ip text, port integer, password text) RETURNS boolean
LANGUAGE plpgsql
AS $_$
declare
res bool;
users_profiles_path text := 'dbname=users_profiles user='||username||' hostaddr='||ip||' port='||port||' password='||password;
begin
if checkingid = checkedid then return false; end if;
select exists (
select * from dblink(users_profiles_path, 'select * from muted_users.mute_history') as m(user_id text, muted_id text)
where m.user_id = $1 and m.muted_id = $2
) into res;
return res;
end;
$_$;
ALTER FUNCTION public.is_muted_user(checkingid text, checkedid text, username text, ip text, port integer, password text) OWNER TO postgres;
SET default_tablespace = '';
SET default_table_access_method = heap;
--
-- Name: messages_history; Type: TABLE; Schema: group_messages; Owner: postgres
--
CREATE TABLE group_messages.messages_history (
chat_id text NOT NULL,
message_id text NOT NULL,
type text NOT NULL,
content text NOT NULL,
sender text NOT NULL,
upload_time text NOT NULL,
medias_datas text NOT NULL,
deleted_list text[] NOT NULL
);
ALTER TABLE group_messages.messages_history OWNER TO postgres;
--
-- Name: group_info; Type: TABLE; Schema: group_profile; Owner: postgres
--
CREATE TABLE group_profile.group_info (
chat_id text NOT NULL,
name text NOT NULL,
profile_pic_link text NOT NULL,
description text NOT NULL,
members text[] NOT NULL
);
ALTER TABLE group_profile.group_info OWNER TO postgres;
--
-- Name: messages_history; Type: TABLE; Schema: private_messages; Owner: postgres
--
CREATE TABLE private_messages.messages_history (
chat_id text NOT NULL,
message_id text NOT NULL,
type text NOT NULL,
content text NOT NULL,
sender text NOT NULL,
upload_time text NOT NULL,
medias_datas text NOT NULL,
deleted_list text[] NOT NULL
);
ALTER TABLE private_messages.messages_history OWNER TO postgres;
--
-- Name: chats_history; Type: TABLE; Schema: users_chats; Owner: postgres
--
CREATE TABLE users_chats.chats_history (
user_id text NOT NULL,
chat_id text NOT NULL,
type text NOT NULL,
recipient text NOT NULL,
deleted boolean NOT NULL
);
ALTER TABLE users_chats.chats_history OWNER TO postgres;
--
-- Data for Name: messages_history; Type: TABLE DATA; Schema: group_messages; Owner: postgres
--
COPY group_messages.messages_history (chat_id, message_id, type, content, sender, upload_time, medias_datas, deleted_list) FROM stdin;
\.
--
-- Data for Name: group_info; Type: TABLE DATA; Schema: group_profile; Owner: postgres
--
COPY group_profile.group_info (chat_id, name, profile_pic_link, description, members) FROM stdin;
\.
--
-- Data for Name: messages_history; Type: TABLE DATA; Schema: private_messages; Owner: postgres
--
COPY private_messages.messages_history (chat_id, message_id, type, content, sender, upload_time, medias_datas, deleted_list) FROM stdin;
\.
--
-- Data for Name: chats_history; Type: TABLE DATA; Schema: users_chats; Owner: postgres
--
COPY users_chats.chats_history (user_id, chat_id, type, recipient, deleted) FROM stdin;
\.
--
-- Name: messages_history group_messages_constraint; Type: CONSTRAINT; Schema: group_messages; Owner: postgres
--
ALTER TABLE ONLY group_messages.messages_history
ADD CONSTRAINT group_messages_constraint UNIQUE (message_id);
--
-- Name: messages_history messages_history_pkey; Type: CONSTRAINT; Schema: group_messages; Owner: postgres
--
ALTER TABLE ONLY group_messages.messages_history
ADD CONSTRAINT messages_history_pkey PRIMARY KEY (message_id);
--
-- Name: group_info group_info_constraints; Type: CONSTRAINT; Schema: group_profile; Owner: postgres
--
ALTER TABLE ONLY group_profile.group_info
ADD CONSTRAINT group_info_constraints UNIQUE (chat_id);
--
-- Name: group_info group_info_pkey; Type: CONSTRAINT; Schema: group_profile; Owner: postgres
--
ALTER TABLE ONLY group_profile.group_info
ADD CONSTRAINT group_info_pkey PRIMARY KEY (chat_id);
--
-- Name: messages_history messages_history_pkey; Type: CONSTRAINT; Schema: private_messages; Owner: postgres
--
ALTER TABLE ONLY private_messages.messages_history
ADD CONSTRAINT messages_history_pkey PRIMARY KEY (message_id);
--
-- Name: messages_history private_messages_constraints; Type: CONSTRAINT; Schema: private_messages; Owner: postgres
--
ALTER TABLE ONLY private_messages.messages_history
ADD CONSTRAINT private_messages_constraints UNIQUE (message_id);
--
-- Name: chats_history chats_constraint; Type: CONSTRAINT; Schema: users_chats; Owner: postgres
--
ALTER TABLE ONLY users_chats.chats_history
ADD CONSTRAINT chats_constraint UNIQUE (user_id, chat_id);
--
-- Name: chats_history chats_primary_key; Type: CONSTRAINT; Schema: users_chats; Owner: postgres
--
ALTER TABLE ONLY users_chats.chats_history
ADD CONSTRAINT chats_primary_key PRIMARY KEY (user_id, chat_id);
--
-- PostgreSQL database dump complete
--