-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.go
179 lines (163 loc) · 4.32 KB
/
settings.go
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
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"database/sql"
"encoding/json"
"fmt"
)
type Settings struct {
// Language of the input words.
InputLanguage string
// FIXME: Maybe there is a library to convert?
// InputLanguageISO639_3 is an ISO 639-3 language code for the language in which to
// extract examples.
InputLanguageISO639_3 string
// TranslationLanguages is an ISO 639-3 language codes for the language into
// which to accept the translations.
// true if translation is accepted
TranslationLanguages map[string]bool
TimeZone string
}
func SettingsFromString(s string) *Settings {
var m Settings
if err := json.Unmarshal([]byte(s), &m); err != nil {
panic(err)
}
return &m
}
func DefaultSettings() *Settings {
return &Settings{
InputLanguage: "Hungarian",
InputLanguageISO639_3: "hun",
TranslationLanguages: map[string]bool{
"eng": true,
"rus": true,
"ukr": true,
},
TimeZone: "UTC",
}
}
func (s Settings) String() string {
m, err := json.Marshal(s)
if err != nil {
panic(err)
}
return string(m)
}
type SettingsConfig struct {
db *sql.DB
}
func NewSettingsConfig(dbPath string) (*SettingsConfig, error) {
db, err := sql.Open("sqlite3", dbPath)
if err != nil {
return nil, err
}
if _, err := db.Exec(`
CREATE TABLE IF NOT EXISTS Settings (
chat_id INTEGER PRIMARY KEY,
settings STRING -- json serialized settings
);`); err != nil {
return nil, err
}
return &SettingsConfig{db}, nil
}
func (c *SettingsConfig) GetAll() (map[int64]*Settings, error) {
rows, err := c.db.Query(`
SELECT chat_id, settings
FROM Settings`)
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, err
}
defer rows.Close()
r := make(map[int64]*Settings)
for rows.Next() {
var (
chatID int64
s string
)
if err := rows.Scan(&chatID, &s); err != nil {
return nil, err
}
r[chatID] = SettingsFromString(s)
}
return r, nil
}
func (c *SettingsConfig) Get(chatID int64) (*Settings, error) {
row := c.db.QueryRow(`
SELECT settings
FROM Settings
WHERE chat_id = $0`,
chatID)
var s string
if err := row.Scan(&s); err != nil {
if err == sql.ErrNoRows {
return DefaultSettings(), nil
}
return nil, fmt.Errorf("INTERNAL: retrieving settings for chat id %d: %w", chatID, err)
}
return SettingsFromString(s), nil
}
func (c *SettingsConfig) Set(chatID int64, s *Settings) error {
_, err := c.db.Exec(`
INSERT OR REPLACE INTO Settings(chat_id, settings) VALUES
($0, $1);`,
chatID, s.String())
if err != nil {
return fmt.Errorf("INTERNAL: Failed updating settings: %w", err)
}
return nil
}
func (c *SettingsConfig) ValidateLanguage(language string) error {
_, ok := SupportedInputLanguages[language]
if !ok {
return fmt.Errorf("unsupported language %q", language)
}
return nil
}
func (c *SettingsConfig) SetLanguage(chatid int64, language string) error {
currentSettings, err := c.Get(chatid)
if err != nil {
return err
}
if err := c.ValidateLanguage(language); err != nil {
return err
}
languageSettings := SupportedInputLanguages[language]
currentSettings.InputLanguage = languageSettings.InputLanguage
currentSettings.InputLanguageISO639_3 = languageSettings.InputLanguageISO639_3
currentSettings.TranslationLanguages = languageSettings.TranslationLanguages
return c.Set(chatid, currentSettings)
}
func (c *SettingsConfig) ValidateTimeZone(tz string) error {
set := TimeZones[tz]
if !set {
return fmt.Errorf("unsupported time zone (format should be UTC, UTC+X or UTC-X)")
}
return nil
}
func (c *SettingsConfig) SetTimeZone(chatid int64, tz string) error {
if err := c.ValidateTimeZone(tz); err != nil {
return err
}
currentSettings, err := c.Get(chatid)
if err != nil {
return err
}
currentSettings.TimeZone = tz
return c.Set(chatid, currentSettings)
}