bas 1.0.1 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/Gemfile +2 -0
- data/lib/bas/bot/fetch_media_from_notion.rb +186 -0
- data/lib/bas/bot/review_media.rb +137 -0
- data/lib/bas/bot/update_review_media_state.rb +102 -0
- data/lib/bas/bot/write_media_review_in_notion.rb +132 -0
- data/lib/bas/bot/write_media_review_requests.rb +103 -0
- data/lib/bas/version.rb +1 -1
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 96a0471a8f9a39c6052efb69c6513fef4f3bc25679cef122830e695c51daa06f
|
4
|
+
data.tar.gz: 21e33ec2357be44b132b8839403a5048738b488dabe2ea4fc11b588f4732bc24
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 845bbb146ac9dcfa884fb27bb4877a9896bd27ead8d145d6645769d57736ce3bf78a768bd61a559a444c9526803da0b2f3076fbe8bd02dffa2a12635d2659d27
|
7
|
+
data.tar.gz: 42e5c8fb5d23fa2bf5f4f0589e6b16ec35db9553bcdc4881b5a3376b6fa0d25f7f55058883b5891db24efe284390948c19e6708764d7ee922b35bf0562479f21
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,12 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
# 1.1.0 (01.07.2024)
|
4
|
+
- [Add write media review requests bot](https://github.com/kommitters/bas/issues/51)
|
5
|
+
- [Add write media review in notion bot](https://github.com/kommitters/bas/issues/54)
|
6
|
+
- [Add update review media state bot](https://github.com/kommitters/bas/issues/55)
|
7
|
+
- [Add Media review process bot](https://github.com/kommitters/bas/issues/52)
|
8
|
+
- [Media review notion fetch bot](https://github.com/kommitters/bas/issues/50)
|
9
|
+
|
3
10
|
# 1.0.1 (21.06.2024)
|
4
11
|
- Refactor pto notification format for improving OpenAI response.
|
5
12
|
|
data/Gemfile
CHANGED
@@ -0,0 +1,186 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "./base"
|
4
|
+
require_relative "../read/postgres"
|
5
|
+
require_relative "../utils/notion/request"
|
6
|
+
require_relative "../write/postgres"
|
7
|
+
|
8
|
+
module Bot
|
9
|
+
##
|
10
|
+
# The Bot::FetchMediaFromNotion class serves as a bot implementation to read media (text or images)
|
11
|
+
# from a notion database and write them on a PostgresDB table with a specific format.
|
12
|
+
#
|
13
|
+
# <br>
|
14
|
+
# <b>Example</b>
|
15
|
+
#
|
16
|
+
# options = {
|
17
|
+
# process_options: {
|
18
|
+
# database_id: "notion_database_id",
|
19
|
+
# secret: "notion_secret",
|
20
|
+
# property: "paragraph"
|
21
|
+
# },
|
22
|
+
# write_options: {
|
23
|
+
# connection: {
|
24
|
+
# host: "localhost",
|
25
|
+
# port: 5432,
|
26
|
+
# dbname: "bas",
|
27
|
+
# user: "postgres",
|
28
|
+
# password: "postgres"
|
29
|
+
# },
|
30
|
+
# db_table: "review_media",
|
31
|
+
# tag: "FetchMediaFromNotion"
|
32
|
+
# }
|
33
|
+
# }
|
34
|
+
#
|
35
|
+
# bot = Bot::FetchMediaFromNotion.new(options)
|
36
|
+
# bot.execute
|
37
|
+
#
|
38
|
+
class FetchMediaFromNotion < Bot::Base # rubocop:disable Metrics/ClassLength
|
39
|
+
CONTENT_STATUS = "review"
|
40
|
+
|
41
|
+
# read function to execute the PostgresDB Read component
|
42
|
+
#
|
43
|
+
def read
|
44
|
+
reader = Read::Postgres.new(read_options.merge(conditions))
|
45
|
+
|
46
|
+
reader.execute
|
47
|
+
end
|
48
|
+
|
49
|
+
# Process function to execute the Notion utility to fetch media from a notion database
|
50
|
+
#
|
51
|
+
def process
|
52
|
+
media_response = fetch_media
|
53
|
+
|
54
|
+
return media_response unless media_response[:error].nil?
|
55
|
+
|
56
|
+
{ success: media_response[:results] }
|
57
|
+
end
|
58
|
+
|
59
|
+
# Write function to execute the PostgresDB write component
|
60
|
+
#
|
61
|
+
def write
|
62
|
+
write = Write::Postgres.new(write_options, process_response)
|
63
|
+
|
64
|
+
write.execute
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
def conditions
|
70
|
+
{
|
71
|
+
where: "archived=$1 AND tag=$2 ORDER BY inserted_at DESC",
|
72
|
+
params: [false, read_options[:tag]]
|
73
|
+
}
|
74
|
+
end
|
75
|
+
|
76
|
+
def fetch_media
|
77
|
+
request_ids_response = fetch_requests_ids
|
78
|
+
|
79
|
+
return request_ids_response unless request_ids_response[:error].nil?
|
80
|
+
|
81
|
+
{ results: fetch_pages(request_ids_response[:ids]) }
|
82
|
+
end
|
83
|
+
|
84
|
+
def fetch_requests_ids
|
85
|
+
response = Utils::Notion::Request.execute(params_database)
|
86
|
+
|
87
|
+
return error(response) unless response.code == 200
|
88
|
+
|
89
|
+
{ ids: response["results"]&.map { |result| result["id"] } }
|
90
|
+
end
|
91
|
+
|
92
|
+
def fetch_pages(pages_ids)
|
93
|
+
pages_ids.map do |page_id|
|
94
|
+
content_response = fetch_content(page_id)
|
95
|
+
|
96
|
+
process_content(page_id, content_response)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def process_content(page_id, content)
|
101
|
+
return content unless content[:error].nil?
|
102
|
+
|
103
|
+
filter_media(page_id, content[:results])
|
104
|
+
end
|
105
|
+
|
106
|
+
def filter_media(page_id, page_content)
|
107
|
+
medias = page_content.select { |content| content["type"] == process_options[:property] }
|
108
|
+
|
109
|
+
media = extract_content(medias)
|
110
|
+
created_by = medias.first["created_by"]["id"] unless medias.first.nil?
|
111
|
+
|
112
|
+
{ media:, page_id:, created_by:, property: process_options[:property] }
|
113
|
+
end
|
114
|
+
|
115
|
+
def extract_content(content)
|
116
|
+
case process_options[:property]
|
117
|
+
when "image" then process_images(content)
|
118
|
+
when "paragraph" then process_text(content)
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
def process_images(images)
|
123
|
+
images.map { |image| image["image"]["file"]["url"] }
|
124
|
+
end
|
125
|
+
|
126
|
+
def process_text(texts)
|
127
|
+
texts.reduce("") do |paragraph, text|
|
128
|
+
rich_text = text["paragraph"]["rich_text"].map { |plain_text| plain_text["plain_text"] }
|
129
|
+
|
130
|
+
content = rich_text.empty? ? "" : rich_text.join(" ")
|
131
|
+
|
132
|
+
"#{paragraph}\n#{content}"
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def fetch_content(block_id)
|
137
|
+
response = fetch_block(block_id)
|
138
|
+
|
139
|
+
return error(response) unless response.code == 200
|
140
|
+
|
141
|
+
{ results: response["results"] }
|
142
|
+
end
|
143
|
+
|
144
|
+
def error(response)
|
145
|
+
{ error: { message: response.parsed_response, status_code: response.code } }
|
146
|
+
end
|
147
|
+
|
148
|
+
def params_database
|
149
|
+
{
|
150
|
+
endpoint: "databases/#{process_options[:database_id]}/query",
|
151
|
+
secret: process_options[:secret],
|
152
|
+
method: "post",
|
153
|
+
body: body_database
|
154
|
+
}
|
155
|
+
end
|
156
|
+
|
157
|
+
def body_database
|
158
|
+
{ filter: { and: [] + property_condition + time_condition } }
|
159
|
+
end
|
160
|
+
|
161
|
+
def property_condition
|
162
|
+
[{ property: process_options[:property], select: { equals: CONTENT_STATUS } }]
|
163
|
+
end
|
164
|
+
|
165
|
+
def time_condition
|
166
|
+
return [] if read_response.inserted_at.nil?
|
167
|
+
|
168
|
+
[{ property: "Last edited time", last_edited_time: { on_or_after: read_response.inserted_at } }]
|
169
|
+
end
|
170
|
+
|
171
|
+
def fetch_block(block_id)
|
172
|
+
params = block_params(block_id)
|
173
|
+
|
174
|
+
Utils::Notion::Request.execute(params)
|
175
|
+
end
|
176
|
+
|
177
|
+
def block_params(block_id)
|
178
|
+
{
|
179
|
+
endpoint: "blocks/#{block_id}/children",
|
180
|
+
secret: process_options[:secret],
|
181
|
+
method: "get",
|
182
|
+
body: {}
|
183
|
+
}
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
@@ -0,0 +1,137 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "md_to_notion"
|
4
|
+
require "json"
|
5
|
+
|
6
|
+
require_relative "./base"
|
7
|
+
require_relative "../read/postgres"
|
8
|
+
require_relative "../write/postgres"
|
9
|
+
require_relative "../utils/openai/run_assistant"
|
10
|
+
|
11
|
+
module Bot
|
12
|
+
##
|
13
|
+
# The Bot::WriteMediaReviewRequests class serves as a bot implementation to read from a postgres
|
14
|
+
# shared storage a set of review media requests and create single request on the shared storage to
|
15
|
+
# be processed one by one.
|
16
|
+
#
|
17
|
+
# <br>
|
18
|
+
# <b>Example</b>
|
19
|
+
#
|
20
|
+
# options = {
|
21
|
+
# read_options: {
|
22
|
+
# connection: {
|
23
|
+
# host: "localhost",
|
24
|
+
# port: 5432,
|
25
|
+
# dbname: "bas",
|
26
|
+
# user: "postgres",
|
27
|
+
# password: "postgres"
|
28
|
+
# },
|
29
|
+
# db_table: "review_media",
|
30
|
+
# tag: "ReviewMediaRequest"
|
31
|
+
# },
|
32
|
+
# process_options: {
|
33
|
+
# secret: "openai_secret",
|
34
|
+
# assistant_id: "openai_assistant_id",
|
35
|
+
# media_type: "paragraph"
|
36
|
+
# },
|
37
|
+
# write_options: {
|
38
|
+
# connection: {
|
39
|
+
# host: "localhost",
|
40
|
+
# port: 5432,
|
41
|
+
# dbname: "bas",
|
42
|
+
# user: "postgres",
|
43
|
+
# password: "postgres"
|
44
|
+
# },
|
45
|
+
# db_table: "review_media",
|
46
|
+
# tag: "ReviewText"
|
47
|
+
# }
|
48
|
+
# }
|
49
|
+
#
|
50
|
+
# bot = Bot::ReviewMedia.new(options)
|
51
|
+
# bot.execute
|
52
|
+
#
|
53
|
+
class ReviewMedia < Bot::Base
|
54
|
+
DETAIL = "low"
|
55
|
+
|
56
|
+
# read function to execute the PostgresDB Read component
|
57
|
+
#
|
58
|
+
def read
|
59
|
+
reader = Read::Postgres.new(read_options.merge(conditions))
|
60
|
+
|
61
|
+
reader.execute
|
62
|
+
end
|
63
|
+
|
64
|
+
# process function to execute the OpenaAI utility to process the media reviews
|
65
|
+
#
|
66
|
+
def process
|
67
|
+
return { success: { review: nil } } if unprocessable_response
|
68
|
+
|
69
|
+
response = Utils::OpenAI::RunAssitant.execute(params)
|
70
|
+
|
71
|
+
if response.code != 200 || (!response["status"].nil? && response["status"] != "completed")
|
72
|
+
return error_response(response)
|
73
|
+
end
|
74
|
+
|
75
|
+
sucess_response(response)
|
76
|
+
end
|
77
|
+
|
78
|
+
# write function to execute the PostgresDB write component
|
79
|
+
#
|
80
|
+
def write
|
81
|
+
write = Write::Postgres.new(write_options, process_response)
|
82
|
+
|
83
|
+
write.execute
|
84
|
+
end
|
85
|
+
|
86
|
+
private
|
87
|
+
|
88
|
+
def conditions
|
89
|
+
{
|
90
|
+
where: "archived=$1 AND tag=$2 AND stage=$3 ORDER BY inserted_at ASC",
|
91
|
+
params: [false, read_options[:tag], "unprocessed"]
|
92
|
+
}
|
93
|
+
end
|
94
|
+
|
95
|
+
def params
|
96
|
+
{
|
97
|
+
assistant_id: process_options[:assistant_id],
|
98
|
+
secret: process_options[:secret],
|
99
|
+
prompt: build_prompt
|
100
|
+
}
|
101
|
+
end
|
102
|
+
|
103
|
+
def build_prompt
|
104
|
+
case process_options[:media_type]
|
105
|
+
when "images" then images_media
|
106
|
+
when "paragraph" then text_media
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def images_media
|
111
|
+
read_response.data["media"].map { |url| { type: "image_url", image_url: { url:, detail: DETAIL } } }
|
112
|
+
end
|
113
|
+
|
114
|
+
def text_media
|
115
|
+
read_response.data["media"]
|
116
|
+
end
|
117
|
+
|
118
|
+
def notion_format(response)
|
119
|
+
md_response = response.parsed_response["data"].first["content"].first["text"]["value"]
|
120
|
+
|
121
|
+
MdToNotion::Parser.markdown_to_notion_blocks(md_response).to_json
|
122
|
+
end
|
123
|
+
|
124
|
+
def sucess_response(response)
|
125
|
+
review = notion_format(response)
|
126
|
+
page_id = read_response.data["page_id"]
|
127
|
+
created_by = read_response.data["created_by"]
|
128
|
+
property = read_response.data["property"]
|
129
|
+
|
130
|
+
{ success: { review:, page_id:, created_by:, property:, media_type: process_options[:media_type] } }
|
131
|
+
end
|
132
|
+
|
133
|
+
def error_response(response)
|
134
|
+
{ error: { message: response.parsed_response, status_code: response.code } }
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "./base"
|
4
|
+
require_relative "../read/default"
|
5
|
+
require_relative "../utils/notion/request"
|
6
|
+
require_relative "../write/postgres"
|
7
|
+
|
8
|
+
module Bot
|
9
|
+
##
|
10
|
+
# The Bot::UpdateReviewMediaStatus class serves as a bot implementation to read from a postgres
|
11
|
+
# shared storage updated status and update a parameter on a Notion database.
|
12
|
+
#
|
13
|
+
# <br>
|
14
|
+
# <b>Example</b>
|
15
|
+
#
|
16
|
+
# options = {
|
17
|
+
# read_options: {
|
18
|
+
# connection: {
|
19
|
+
# host: "localhost",
|
20
|
+
# port: 5432,
|
21
|
+
# dbname: "bas",
|
22
|
+
# user: "postgres",
|
23
|
+
# password: "postgres"
|
24
|
+
# },
|
25
|
+
# db_table: "review_media",
|
26
|
+
# tag: "WriteMediaReviewInNotion"
|
27
|
+
# },
|
28
|
+
# process_options: {
|
29
|
+
# secret: "notion_secret"
|
30
|
+
# },
|
31
|
+
# write_options: {
|
32
|
+
# connection: {
|
33
|
+
# host: "localhost",
|
34
|
+
# port: 5432,
|
35
|
+
# dbname: "bas",
|
36
|
+
# user: "postgres",
|
37
|
+
# password: "postgres"
|
38
|
+
# },
|
39
|
+
# db_table: "review_media",
|
40
|
+
# tag: "UpdateReviewMediaStatus"
|
41
|
+
# }
|
42
|
+
# }
|
43
|
+
#
|
44
|
+
# bot = Bot::UpdateReviewMediaStatus.new(options)
|
45
|
+
# bot.execute
|
46
|
+
#
|
47
|
+
class UpdateReviewMediaStatus < Bot::Base
|
48
|
+
READY_STATE = "ready"
|
49
|
+
|
50
|
+
# read function to execute the PostgresDB Read component
|
51
|
+
#
|
52
|
+
def read
|
53
|
+
reader = Read::Postgres.new(read_options.merge(conditions))
|
54
|
+
|
55
|
+
reader.execute
|
56
|
+
end
|
57
|
+
|
58
|
+
# process function to execute the Notion utility to update a Notion database property
|
59
|
+
#
|
60
|
+
def process
|
61
|
+
return { success: { status_updated: nil } } if unprocessable_response
|
62
|
+
|
63
|
+
response = Utils::Notion::Request.execute(params)
|
64
|
+
|
65
|
+
if response.code == 200
|
66
|
+
{ success: { status_updated: true } }
|
67
|
+
else
|
68
|
+
{ error: { message: response.parsed_response, status_code: response.code } }
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
# write function to execute the PostgresDB write component
|
73
|
+
#
|
74
|
+
def write
|
75
|
+
write = Write::Postgres.new(write_options, process_response)
|
76
|
+
|
77
|
+
write.execute
|
78
|
+
end
|
79
|
+
|
80
|
+
private
|
81
|
+
|
82
|
+
def conditions
|
83
|
+
{
|
84
|
+
where: "archived=$1 AND tag=$2 AND stage=$3 ORDER BY inserted_at ASC",
|
85
|
+
params: [false, read_options[:tag], "unprocessed"]
|
86
|
+
}
|
87
|
+
end
|
88
|
+
|
89
|
+
def params
|
90
|
+
{
|
91
|
+
endpoint: "pages/#{read_response.data["page_id"]}",
|
92
|
+
secret: process_options[:secret],
|
93
|
+
method: "patch",
|
94
|
+
body:
|
95
|
+
}
|
96
|
+
end
|
97
|
+
|
98
|
+
def body
|
99
|
+
{ properties: { read_response.data["property"] => { select: { name: READY_STATE } } } }
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
@@ -0,0 +1,132 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "json"
|
4
|
+
|
5
|
+
require_relative "./base"
|
6
|
+
require_relative "../read/default"
|
7
|
+
require_relative "../utils/notion/request"
|
8
|
+
require_relative "../write/postgres"
|
9
|
+
|
10
|
+
module Bot
|
11
|
+
##
|
12
|
+
# The Bot::WriteMediaReviewInNotion class serves as a bot implementation to read from a postgres
|
13
|
+
# shared storage formated notion blocks and send them to a Notion page
|
14
|
+
#
|
15
|
+
# <br>
|
16
|
+
# <b>Example</b>
|
17
|
+
#
|
18
|
+
# options = {
|
19
|
+
# read_options: {
|
20
|
+
# connection: {
|
21
|
+
# host: "localhost",
|
22
|
+
# port: 5432,
|
23
|
+
# dbname: "bas",
|
24
|
+
# user: "postgres",
|
25
|
+
# password: "postgres"
|
26
|
+
# },
|
27
|
+
# db_table: "review_media",
|
28
|
+
# tag: "FormatMediaReview"
|
29
|
+
# },
|
30
|
+
# process_options: {
|
31
|
+
# secret: "notion_secret"
|
32
|
+
# },
|
33
|
+
# write_options: {
|
34
|
+
# connection: {
|
35
|
+
# host: "localhost",
|
36
|
+
# port: 5432,
|
37
|
+
# dbname: "bas",
|
38
|
+
# user: "postgres",
|
39
|
+
# password: "postgres"
|
40
|
+
# },
|
41
|
+
# db_table: "review_media",
|
42
|
+
# tag: "WriteMediaReviewInNotion"
|
43
|
+
# }
|
44
|
+
# }
|
45
|
+
#
|
46
|
+
# bot = Bot::WriteMediaReviewInNotion.new(options)
|
47
|
+
# bot.execute
|
48
|
+
#
|
49
|
+
class WriteMediaReviewInNotion < Bot::Base
|
50
|
+
CHUNK_SIZE = "1000"
|
51
|
+
|
52
|
+
# read function to execute the PostgresDB Read component
|
53
|
+
#
|
54
|
+
def read
|
55
|
+
reader = Read::Postgres.new(read_options.merge(conditions))
|
56
|
+
|
57
|
+
reader.execute
|
58
|
+
end
|
59
|
+
|
60
|
+
# process function to execute the Notion utility to send formated blocks to a page
|
61
|
+
#
|
62
|
+
def process
|
63
|
+
return { success: { review_added: nil } } if unprocessable_response
|
64
|
+
|
65
|
+
response = Utils::Notion::Request.execute(params)
|
66
|
+
|
67
|
+
if response.code == 200
|
68
|
+
{ success: { page_id: read_response.data["page_id"], property: read_response.data["property"] } }
|
69
|
+
else
|
70
|
+
{ error: { message: response.parsed_response, status_code: response.code } }
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
# write function to execute the PostgresDB write component
|
75
|
+
#
|
76
|
+
def write
|
77
|
+
write = Write::Postgres.new(write_options, process_response)
|
78
|
+
|
79
|
+
write.execute
|
80
|
+
end
|
81
|
+
|
82
|
+
private
|
83
|
+
|
84
|
+
def conditions
|
85
|
+
{
|
86
|
+
where: "archived=$1 AND tag=$2 AND stage=$3 ORDER BY inserted_at ASC",
|
87
|
+
params: [false, read_options[:tag], "unprocessed"]
|
88
|
+
}
|
89
|
+
end
|
90
|
+
|
91
|
+
def params
|
92
|
+
{
|
93
|
+
endpoint: "blocks/#{read_response.data["page_id"]}/children",
|
94
|
+
secret: process_options[:secret],
|
95
|
+
method: "patch",
|
96
|
+
body:
|
97
|
+
}
|
98
|
+
end
|
99
|
+
|
100
|
+
def body
|
101
|
+
{ children: [{ object: "block", type: "toggle", toggle: }] }
|
102
|
+
end
|
103
|
+
|
104
|
+
def toggle
|
105
|
+
{
|
106
|
+
rich_text: [{ type: "text", text: { content: toggle_title } }, mention],
|
107
|
+
children: toggle_childrens
|
108
|
+
}
|
109
|
+
end
|
110
|
+
|
111
|
+
def toggle_childrens
|
112
|
+
JSON.parse(read_response.data["review"])
|
113
|
+
end
|
114
|
+
|
115
|
+
def mention
|
116
|
+
{
|
117
|
+
type: "mention",
|
118
|
+
mention: {
|
119
|
+
type: "user",
|
120
|
+
user: { id: read_response.data["created_by"] }
|
121
|
+
}
|
122
|
+
}
|
123
|
+
end
|
124
|
+
|
125
|
+
def toggle_title
|
126
|
+
case read_response.data["media_type"]
|
127
|
+
when "images" then "Image review results/"
|
128
|
+
when "paragraph" then "Text review results/"
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "./base"
|
4
|
+
require_relative "../read/postgres"
|
5
|
+
require_relative "../write/postgres"
|
6
|
+
|
7
|
+
module Bot
|
8
|
+
##
|
9
|
+
# The Bot::WriteMediaReviewRequests class serves as a bot implementation to read from a postgres
|
10
|
+
# shared storage a set of review media requests and create single request on the shared storage to
|
11
|
+
# be processed one by one.
|
12
|
+
#
|
13
|
+
# <br>
|
14
|
+
# <b>Example</b>
|
15
|
+
#
|
16
|
+
# options = {
|
17
|
+
# read_options: {
|
18
|
+
# connection: {
|
19
|
+
# host: "localhost",
|
20
|
+
# port: 5432,
|
21
|
+
# dbname: "bas",
|
22
|
+
# user: "postgres",
|
23
|
+
# password: "postgres"
|
24
|
+
# },
|
25
|
+
# db_table: "review_media",
|
26
|
+
# tag: "FetchMediaFromNotion"
|
27
|
+
# },
|
28
|
+
# process_options: {
|
29
|
+
# connection: {
|
30
|
+
# host: "localhost",
|
31
|
+
# port: 5432,
|
32
|
+
# dbname: "bas",
|
33
|
+
# user: "postgres",
|
34
|
+
# password: "postgres"
|
35
|
+
# },
|
36
|
+
# db_table: "review_media",
|
37
|
+
# tag: "ReviewMediaRequest"
|
38
|
+
# },
|
39
|
+
# write_options: {
|
40
|
+
# connection: {
|
41
|
+
# host: "localhost",
|
42
|
+
# port: 5432,
|
43
|
+
# dbname: "bas",
|
44
|
+
# user: "postgres",
|
45
|
+
# password: "postgres"
|
46
|
+
# },
|
47
|
+
# db_table: "review_media",
|
48
|
+
# tag: "WriteMediaReviewRequests"
|
49
|
+
# }
|
50
|
+
# }
|
51
|
+
#
|
52
|
+
# bot = Bot::WriteMediaReviewRequests.new(options)
|
53
|
+
# bot.execute
|
54
|
+
#
|
55
|
+
class WriteMediaReviewRequests < Bot::Base
|
56
|
+
# read function to execute the PostgresDB Read component
|
57
|
+
#
|
58
|
+
def read
|
59
|
+
reader = Read::Postgres.new(read_options.merge(conditions))
|
60
|
+
|
61
|
+
reader.execute
|
62
|
+
end
|
63
|
+
|
64
|
+
# Process function to execute the Notion utility create single review requests
|
65
|
+
#
|
66
|
+
def process
|
67
|
+
return { success: { created: nil } } if unprocessable_response
|
68
|
+
|
69
|
+
read_response.data.each { |request| create_request(request) }
|
70
|
+
|
71
|
+
{ success: { created: true } }
|
72
|
+
end
|
73
|
+
|
74
|
+
# Write function to execute the PostgresDB write component
|
75
|
+
#
|
76
|
+
def write
|
77
|
+
write = Write::Postgres.new(write_options, process_response)
|
78
|
+
|
79
|
+
write.execute
|
80
|
+
end
|
81
|
+
|
82
|
+
private
|
83
|
+
|
84
|
+
def conditions
|
85
|
+
{
|
86
|
+
where: "archived=$1 AND tag=$2 AND stage=$3 ORDER BY inserted_at ASC",
|
87
|
+
params: [false, read_options[:tag], "unprocessed"]
|
88
|
+
}
|
89
|
+
end
|
90
|
+
|
91
|
+
def create_request(request)
|
92
|
+
write_data = write_request(request)
|
93
|
+
|
94
|
+
Write::Postgres.new(process_options, write_data).execute
|
95
|
+
end
|
96
|
+
|
97
|
+
def write_request(request)
|
98
|
+
return { error: request } if request["media"].empty? || !request["error"].nil?
|
99
|
+
|
100
|
+
{ success: request }
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
data/lib/bas/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bas
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- kommitters Open Source
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-07-02 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: A versatile business automation system offering key components for building
|
14
14
|
various use cases. It provides an easy-to-use tool for implementing automation
|
@@ -37,6 +37,7 @@ files:
|
|
37
37
|
- lib/bas/bot/fetch_domains_wip_counts_from_notion.rb
|
38
38
|
- lib/bas/bot/fetch_domains_wip_limit_from_notion.rb
|
39
39
|
- lib/bas/bot/fetch_emails_from_imap.rb
|
40
|
+
- lib/bas/bot/fetch_media_from_notion.rb
|
40
41
|
- lib/bas/bot/fetch_next_week_birthdays_from_notion.rb
|
41
42
|
- lib/bas/bot/fetch_next_week_ptos_from_notion.rb
|
42
43
|
- lib/bas/bot/fetch_ptos_from_notion.rb
|
@@ -46,6 +47,10 @@ files:
|
|
46
47
|
- lib/bas/bot/garbage_collector.rb
|
47
48
|
- lib/bas/bot/humanize_pto.rb
|
48
49
|
- lib/bas/bot/notify_discord.rb
|
50
|
+
- lib/bas/bot/review_media.rb
|
51
|
+
- lib/bas/bot/update_review_media_state.rb
|
52
|
+
- lib/bas/bot/write_media_review_in_notion.rb
|
53
|
+
- lib/bas/bot/write_media_review_requests.rb
|
49
54
|
- lib/bas/read/base.rb
|
50
55
|
- lib/bas/read/default.rb
|
51
56
|
- lib/bas/read/postgres.rb
|