This repository has been archived by the owner on Nov 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
db.sql
163 lines (135 loc) · 3.82 KB
/
db.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
-- Copyright (c) 2017 ldesgoui
-- read file 'LICENSE' for details
begin;
-- PUBLIC
create table if not exists post
( url text primary key
, date timestamp not null
, title text not null
, content text not null
);
-- INTERNAL
create schema if not exists internal;
create table if not exists internal.subscriber
( webhook text primary key
, tf2 integer not null default 0
, csgo integer not null default 0
, dota2 integer not null default 0
);
create or replace function internal.import_xml(text)
returns setof text as $$
INSERT INTO
post
SELECT
(xpath('guid/text()', x))[1]::text as url,
to_timestamp((xpath('pubDate/text()', x))[1]::text, 'Dy, DD Mon YYYY HH24:MI:SS +0000') as date,
(xpath('title/text()', x))[1]::text as title,
coalesce( (xpath('content:encoded/text()', x, ARRAY[ARRAY['content', 'http://purl.org/rss/1.0/modules/content/']]))[1]::text, (xpath('description/text()', x))[1]::text, '' ) as content
FROM
unnest(xpath('/rss/channel/item', xmlparse(content $1))) as x
ON CONFLICT (url) DO NOTHING
RETURNING
url
;
$$ language sql;
create or replace function internal.tf2_level(post post)
returns integer as $$
BEGIN
if post.url not like '%teamfortress%' then
return 0;
elsif post.content ~ 'teamfortress\.com\/(?!post\.php)\w+' then
return 1; -- contains a massive update (content has link to a unique tf2.com page)
elsif post.title ilike '%update%' then
return 2;
else
return 3;
end if;
END
$$ language plpgsql;
create or replace function internal.csgo_level(post post)
returns integer as $$
BEGIN
if post.url not like '%counter-strike%' then
return 0;
elsif post.content ~ 'counter-strike\.net\/(?!index\.php)\w+' then
return 1;
elsif post.title ilike '%release notes%' then
return 2;
else
return 3;
end if;
END
$$ language plpgsql;
create or replace function internal.dota2_level(post post)
returns integer as $$
BEGIN
if post.url not like '%dota%' then
return 0;
elsif post.content ~ 'www\.dota2\.com\/\d+' then
return 1; -- link to a 70X update
elsif post.title ilike '%update%' or post.title ilike '%patch%' then
return 2;
else
return 3;
end if;
END
$$ language plpgsql;
create or replace function internal.get_subscribers(text)
returns setof text as $$
DECLARE
post post;
BEGIN
SELECT
*
INTO
post
FROM
post
WHERE
url = $1
;
IF NOT FOUND THEN
raise 'Post not found: %', $1;
END IF;
RETURN QUERY SELECT
webhook
FROM
internal.subscriber
WHERE
internal.tf2_level(post) BETWEEN 1 AND tf2
OR internal.csgo_level(post) BETWEEN 1 AND csgo
OR internal.dota2_level(post) BETWEEN 1 AND dota2
;
END;
$$ language plpgsql;
-- USER INPUT
create or replace function subscription(webhook text)
returns internal.subscriber as $$
SELECT
*
FROM
internal.subscriber
WHERE
webhook = $1
;
$$ language sql immutable strict security definer;
create or replace function subscribe(webhook text, tf2 integer default 0, csgo integer default 0, dota2 integer default 0)
returns internal.subscriber as $$
INSERT INTO
internal.subscriber
VALUES
($1, $2, $3, $4)
ON CONFLICT (webhook) DO UPDATE SET
tf2 = $2,
csgo = $3,
dota2 = $4
RETURNING
*
;
$$ language sql security definer;
create role hi_valve;
grant select on table post to hi_valve;
grant execute on function subscribe(text, integer, integer, integer) to hi_valve;
grant execute on function subscription(text) to hi_valve;
grant hi_valve to postgrest;
commit;