-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbeeper_desktop_api1.sql
More file actions
140 lines (126 loc) · 4.32 KB
/
beeper_desktop_api1.sql
File metadata and controls
140 lines (126 loc) · 4.32 KB
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
ALTER TYPE beeper_desktop_api.client_focus_response
ADD ATTRIBUTE success BOOLEAN;
CREATE OR REPLACE FUNCTION beeper_desktop_api.make_client_focus_response(
success BOOLEAN
)
RETURNS beeper_desktop_api.client_focus_response
LANGUAGE SQL
IMMUTABLE
AS $$
SELECT ROW(success)::beeper_desktop_api.client_focus_response;
$$;
ALTER TYPE beeper_desktop_api.client_search_response
ADD ATTRIBUTE results beeper_desktop_api.client_search_response_result;
CREATE OR REPLACE FUNCTION beeper_desktop_api.make_client_search_response(
results beeper_desktop_api.client_search_response_result
)
RETURNS beeper_desktop_api.client_search_response
LANGUAGE SQL
IMMUTABLE
AS $$
SELECT ROW(results)::beeper_desktop_api.client_search_response;
$$;
ALTER TYPE beeper_desktop_api.client_search_response_result
ADD ATTRIBUTE chats beeper_desktop_api_chats.chat[],
ADD ATTRIBUTE in_groups beeper_desktop_api_chats.chat[],
ADD ATTRIBUTE messages beeper_desktop_api.client_search_response_result_message;
CREATE OR REPLACE FUNCTION beeper_desktop_api.make_client_search_response_result(
chats beeper_desktop_api_chats.chat[],
in_groups beeper_desktop_api_chats.chat[],
messages beeper_desktop_api.client_search_response_result_message
)
RETURNS beeper_desktop_api.client_search_response_result
LANGUAGE SQL
IMMUTABLE
AS $$
SELECT ROW(
chats, in_groups, messages
)::beeper_desktop_api.client_search_response_result;
$$;
ALTER TYPE beeper_desktop_api.client_search_response_result_message
ADD ATTRIBUTE chats JSONB,
ADD ATTRIBUTE hasMore BOOLEAN,
ADD ATTRIBUTE items beeper_desktop_api.message[],
ADD ATTRIBUTE newestCursor TEXT,
ADD ATTRIBUTE oldestCursor TEXT;
CREATE OR REPLACE FUNCTION beeper_desktop_api.make_client_search_response_result_message(
chats JSONB,
hasMore BOOLEAN,
items beeper_desktop_api.message[],
newestCursor TEXT DEFAULT NULL,
oldestCursor TEXT DEFAULT NULL
)
RETURNS beeper_desktop_api.client_search_response_result_message
LANGUAGE SQL
IMMUTABLE
AS $$
SELECT ROW(
chats, hasMore, items, newestCursor, oldestCursor
)::beeper_desktop_api.client_search_response_result_message;
$$;
CREATE OR REPLACE FUNCTION beeper_desktop_api._focus(
chat_id TEXT DEFAULT NULL,
draft_attachment_path TEXT DEFAULT NULL,
draft_text TEXT DEFAULT NULL,
message_id TEXT DEFAULT NULL
)
RETURNS JSONB
LANGUAGE plpython3u
AS $$
from beeper_desktop_api._types import not_given
response = GD["__beeper_desktop_api_context__"].client.with_raw_response.focus(
chat_id=not_given if chat_id is None else chat_id,
draft_attachment_path=not_given if draft_attachment_path is None else draft_attachment_path,
draft_text=not_given if draft_text is None else draft_text,
message_id=not_given if message_id is None else message_id,
)
# We don't parse the JSON and let PL/Python perform data mapping because PL/Python errors for omitted
# fields instead of defaulting them to NULL, but we want to be more lenient, which we handle in the
# caller later.
return response.text()
$$;
CREATE OR REPLACE FUNCTION beeper_desktop_api.focus(
chat_id TEXT DEFAULT NULL,
draft_attachment_path TEXT DEFAULT NULL,
draft_text TEXT DEFAULT NULL,
message_id TEXT DEFAULT NULL
)
RETURNS beeper_desktop_api.client_focus_response
LANGUAGE plpgsql
AS $$
BEGIN
PERFORM beeper_desktop_api_internal.ensure_context();
RETURN jsonb_populate_record(
NULL::beeper_desktop_api.client_focus_response,
beeper_desktop_api._focus(
chat_id, draft_attachment_path, draft_text, message_id
)
);
END;
$$;
CREATE OR REPLACE FUNCTION beeper_desktop_api._search(query TEXT)
RETURNS JSONB
LANGUAGE plpython3u
STABLE
AS $$
response = GD["__beeper_desktop_api_context__"].client.with_raw_response.search(
query=query,
)
# We don't parse the JSON and let PL/Python perform data mapping because PL/Python errors for omitted
# fields instead of defaulting them to NULL, but we want to be more lenient, which we handle in the
# caller later.
return response.text()
$$;
CREATE OR REPLACE FUNCTION beeper_desktop_api.search(query TEXT)
RETURNS beeper_desktop_api.client_search_response
LANGUAGE plpgsql
STABLE
AS $$
BEGIN
PERFORM beeper_desktop_api_internal.ensure_context();
RETURN jsonb_populate_record(
NULL::beeper_desktop_api.client_search_response,
beeper_desktop_api._search(query)
);
END;
$$;