Remove setting, add persistence

This commit is contained in:
binwiederhier 2023-07-08 15:14:35 -04:00
parent ce01a66ff3
commit b7679c7826
6 changed files with 60 additions and 67 deletions

View file

@ -45,6 +45,7 @@ const (
attachment_deleted INT NOT NULL, attachment_deleted INT NOT NULL,
sender TEXT NOT NULL, sender TEXT NOT NULL,
user TEXT NOT NULL, user TEXT NOT NULL,
content_type TEXT NOT NULL,
encoding TEXT NOT NULL, encoding TEXT NOT NULL,
published INT NOT NULL published INT NOT NULL
); );
@ -63,43 +64,43 @@ const (
COMMIT; COMMIT;
` `
insertMessageQuery = ` insertMessageQuery = `
INSERT INTO messages (mid, time, expires, topic, message, title, priority, tags, click, icon, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, attachment_deleted, sender, user, encoding, published) INSERT INTO messages (mid, time, expires, topic, message, title, priority, tags, click, icon, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, attachment_deleted, sender, user, content_type, encoding, published)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
` `
deleteMessageQuery = `DELETE FROM messages WHERE mid = ?` deleteMessageQuery = `DELETE FROM messages WHERE mid = ?`
updateMessagesForTopicExpiryQuery = `UPDATE messages SET expires = ? WHERE topic = ?` updateMessagesForTopicExpiryQuery = `UPDATE messages SET expires = ? WHERE topic = ?`
selectRowIDFromMessageID = `SELECT id FROM messages WHERE mid = ?` // Do not include topic, see #336 and TestServer_PollSinceID_MultipleTopics selectRowIDFromMessageID = `SELECT id FROM messages WHERE mid = ?` // Do not include topic, see #336 and TestServer_PollSinceID_MultipleTopics
selectMessagesByIDQuery = ` selectMessagesByIDQuery = `
SELECT mid, time, expires, topic, message, title, priority, tags, click, icon, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, sender, user, encoding SELECT mid, time, expires, topic, message, title, priority, tags, click, icon, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, sender, user, content_type, encoding
FROM messages FROM messages
WHERE mid = ? WHERE mid = ?
` `
selectMessagesSinceTimeQuery = ` selectMessagesSinceTimeQuery = `
SELECT mid, time, expires, topic, message, title, priority, tags, click, icon, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, sender, user, encoding SELECT mid, time, expires, topic, message, title, priority, tags, click, icon, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, sender, user, content_type, encoding
FROM messages FROM messages
WHERE topic = ? AND time >= ? AND published = 1 WHERE topic = ? AND time >= ? AND published = 1
ORDER BY time, id ORDER BY time, id
` `
selectMessagesSinceTimeIncludeScheduledQuery = ` selectMessagesSinceTimeIncludeScheduledQuery = `
SELECT mid, time, expires, topic, message, title, priority, tags, click, icon, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, sender, user, encoding SELECT mid, time, expires, topic, message, title, priority, tags, click, icon, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, sender, user, content_type, encoding
FROM messages FROM messages
WHERE topic = ? AND time >= ? WHERE topic = ? AND time >= ?
ORDER BY time, id ORDER BY time, id
` `
selectMessagesSinceIDQuery = ` selectMessagesSinceIDQuery = `
SELECT mid, time, expires, topic, message, title, priority, tags, click, icon, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, sender, user, encoding SELECT mid, time, expires, topic, message, title, priority, tags, click, icon, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, sender, user, content_type, encoding
FROM messages FROM messages
WHERE topic = ? AND id > ? AND published = 1 WHERE topic = ? AND id > ? AND published = 1
ORDER BY time, id ORDER BY time, id
` `
selectMessagesSinceIDIncludeScheduledQuery = ` selectMessagesSinceIDIncludeScheduledQuery = `
SELECT mid, time, expires, topic, message, title, priority, tags, click, icon, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, sender, user, encoding SELECT mid, time, expires, topic, message, title, priority, tags, click, icon, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, sender, user, content_type, encoding
FROM messages FROM messages
WHERE topic = ? AND (id > ? OR published = 0) WHERE topic = ? AND (id > ? OR published = 0)
ORDER BY time, id ORDER BY time, id
` `
selectMessagesDueQuery = ` selectMessagesDueQuery = `
SELECT mid, time, expires, topic, message, title, priority, tags, click, icon, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, sender, user, encoding SELECT mid, time, expires, topic, message, title, priority, tags, click, icon, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, sender, user, content_type, encoding
FROM messages FROM messages
WHERE time <= ? AND published = 0 WHERE time <= ? AND published = 0
ORDER BY time, id ORDER BY time, id
@ -121,7 +122,7 @@ const (
// Schema management queries // Schema management queries
const ( const (
currentSchemaVersion = 11 currentSchemaVersion = 12
createSchemaVersionTableQuery = ` createSchemaVersionTableQuery = `
CREATE TABLE IF NOT EXISTS schemaVersion ( CREATE TABLE IF NOT EXISTS schemaVersion (
id INT PRIMARY KEY, id INT PRIMARY KEY,
@ -240,6 +241,11 @@ const (
); );
INSERT INTO stats (key, value) VALUES ('messages', 0); INSERT INTO stats (key, value) VALUES ('messages', 0);
` `
// 11 -> 12
migrate11To12AlterMessagesTableQuery = `
ALTER TABLE messages ADD COLUMN content_type TEXT NOT NULL DEFAULT('');
`
) )
var ( var (
@ -255,6 +261,7 @@ var (
8: migrateFrom8, 8: migrateFrom8,
9: migrateFrom9, 9: migrateFrom9,
10: migrateFrom10, 10: migrateFrom10,
11: migrateFrom11,
} }
) )
@ -384,6 +391,7 @@ func (c *messageCache) addMessages(ms []*message) error {
attachmentDeleted, // Always zero attachmentDeleted, // Always zero
sender, sender,
m.User, m.User,
m.ContentType,
m.Encoding, m.Encoding,
published, published,
) )
@ -656,7 +664,7 @@ func readMessages(rows *sql.Rows) ([]*message, error) {
func readMessage(rows *sql.Rows) (*message, error) { func readMessage(rows *sql.Rows) (*message, error) {
var timestamp, expires, attachmentSize, attachmentExpires int64 var timestamp, expires, attachmentSize, attachmentExpires int64
var priority int var priority int
var id, topic, msg, title, tagsStr, click, icon, actionsStr, attachmentName, attachmentType, attachmentURL, sender, user, encoding string var id, topic, msg, title, tagsStr, click, icon, actionsStr, attachmentName, attachmentType, attachmentURL, sender, user, contentType, encoding string
err := rows.Scan( err := rows.Scan(
&id, &id,
&timestamp, &timestamp,
@ -676,6 +684,7 @@ func readMessage(rows *sql.Rows) (*message, error) {
&attachmentURL, &attachmentURL,
&sender, &sender,
&user, &user,
&contentType,
&encoding, &encoding,
) )
if err != nil { if err != nil {
@ -706,22 +715,23 @@ func readMessage(rows *sql.Rows) (*message, error) {
} }
} }
return &message{ return &message{
ID: id, ID: id,
Time: timestamp, Time: timestamp,
Expires: expires, Expires: expires,
Event: messageEvent, Event: messageEvent,
Topic: topic, Topic: topic,
Message: msg, Message: msg,
Title: title, Title: title,
Priority: priority, Priority: priority,
Tags: tags, Tags: tags,
Click: click, Click: click,
Icon: icon, Icon: icon,
Actions: actions, Actions: actions,
Attachment: att, Attachment: att,
Sender: senderIP, // Must parse assuming database must be correct Sender: senderIP, // Must parse assuming database must be correct
User: user, User: user,
Encoding: encoding, ContentType: contentType,
Encoding: encoding,
}, nil }, nil
} }
@ -929,7 +939,7 @@ func migrateFrom9(db *sql.DB, cacheDuration time.Duration) error {
return tx.Commit() return tx.Commit()
} }
func migrateFrom10(db *sql.DB, cacheDuration time.Duration) error { func migrateFrom10(db *sql.DB, _ time.Duration) error {
log.Tag(tagMessageCache).Info("Migrating cache database schema: from 10 to 11") log.Tag(tagMessageCache).Info("Migrating cache database schema: from 10 to 11")
tx, err := db.Begin() tx, err := db.Begin()
if err != nil { if err != nil {
@ -944,3 +954,19 @@ func migrateFrom10(db *sql.DB, cacheDuration time.Duration) error {
} }
return tx.Commit() return tx.Commit()
} }
func migrateFrom11(db *sql.DB, _ time.Duration) error {
log.Tag(tagMessageCache).Info("Migrating cache database schema: from 11 to 12")
tx, err := db.Begin()
if err != nil {
return err
}
defer tx.Rollback()
if _, err := tx.Exec(migrate11To12AlterMessagesTableQuery); err != nil {
return err
}
if _, err := tx.Exec(updateSchemaVersion, 12); err != nil {
return err
}
return tx.Commit()
}

View file

@ -182,6 +182,7 @@ func TestToFirebaseMessage_Message_Normal_Allowed(t *testing.T) {
"title": "some title", "title": "some title",
"message": "this is a message", "message": "this is a message",
"actions": `[{"id":"123","action":"view","label":"Open page","clear":true,"url":"https://ntfy.sh"},{"id":"456","action":"http","label":"Close door","clear":false,"url":"https://door.com/close","method":"PUT","headers":{"really":"yes"}}]`, "actions": `[{"id":"123","action":"view","label":"Open page","clear":true,"url":"https://ntfy.sh"},{"id":"456","action":"http","label":"Close door","clear":false,"url":"https://door.com/close","method":"PUT","headers":{"really":"yes"}}]`,
"content_type": "",
"encoding": "", "encoding": "",
"attachment_name": "some file.jpg", "attachment_name": "some file.jpg",
"attachment_type": "image/jpeg", "attachment_type": "image/jpeg",
@ -203,6 +204,7 @@ func TestToFirebaseMessage_Message_Normal_Allowed(t *testing.T) {
"title": "some title", "title": "some title",
"message": "this is a message", "message": "this is a message",
"actions": `[{"id":"123","action":"view","label":"Open page","clear":true,"url":"https://ntfy.sh"},{"id":"456","action":"http","label":"Close door","clear":false,"url":"https://door.com/close","method":"PUT","headers":{"really":"yes"}}]`, "actions": `[{"id":"123","action":"view","label":"Open page","clear":true,"url":"https://ntfy.sh"},{"id":"456","action":"http","label":"Close door","clear":false,"url":"https://door.com/close","method":"PUT","headers":{"really":"yes"}}]`,
"content_type": "",
"encoding": "", "encoding": "",
"attachment_name": "some file.jpg", "attachment_name": "some file.jpg",
"attachment_type": "image/jpeg", "attachment_type": "image/jpeg",

View file

@ -359,9 +359,6 @@
"prefs_appearance_theme_system": "System (default)", "prefs_appearance_theme_system": "System (default)",
"prefs_appearance_theme_dark": "Dark mode", "prefs_appearance_theme_dark": "Dark mode",
"prefs_appearance_theme_light": "Light mode", "prefs_appearance_theme_light": "Light mode",
"prefs_appearance_markdown_always_enabled_title": "Format all messages as Markdown",
"prefs_appearance_markdown_always_enabled_on": "Enabled",
"prefs_appearance_markdown_always_enabled_off": "Disabled",
"prefs_reservations_title": "Reserved topics", "prefs_reservations_title": "Reserved topics",
"prefs_reservations_description": "You can reserve topic names for personal use here. Reserving a topic gives you ownership over the topic, and allows you to define access permissions for other users over the topic.", "prefs_reservations_description": "You can reserve topic names for personal use here. Reserving a topic gives you ownership over the topic, and allows you to define access permissions for other users over the topic.",
"prefs_reservations_limit_reached": "You reached your reserved topics limit.", "prefs_reservations_limit_reached": "You reached your reserved topics limit.",

View file

@ -55,15 +55,6 @@ class Prefs {
async setTheme(mode) { async setTheme(mode) {
await this.db.prefs.put({ key: "theme", value: mode }); await this.db.prefs.put({ key: "theme", value: mode });
} }
async markdownAlwaysEnabled() {
const record = await this.db.prefs.get("markdownAlwaysEnabled");
return record?.value ?? false;
}
async setMarkdownAlwaysEnabled(enabled) {
await this.db.prefs.put({ key: "markdownAlwaysEnabled", value: enabled });
}
} }
const prefs = new Prefs(db()); const prefs = new Prefs(db());

View file

@ -192,6 +192,10 @@ const MarkdownContainer = styled("div")`
ol { ol {
padding-inline: 1rem; padding-inline: 1rem;
} }
img {
max-width: 100%;
}
`; `;
const MarkdownContent = ({ content }) => { const MarkdownContent = ({ content }) => {
@ -205,17 +209,11 @@ const MarkdownContent = ({ content }) => {
}; };
const NotificationBody = ({ notification }) => { const NotificationBody = ({ notification }) => {
const markdownAlwaysEnabled = useLiveQuery(async () => prefs.markdownAlwaysEnabled()); const displayAsMarkdown = notification.content_type === "text/markdown";
// TODO: check notification content-type when implemented on the server
const displayAsMarkdown = markdownAlwaysEnabled;
const formatted = formatMessage(notification); const formatted = formatMessage(notification);
if (displayAsMarkdown) { if (displayAsMarkdown) {
return <MarkdownContent content={formatted} />; return <MarkdownContent content={formatted} />;
} }
return autolink(formatted); return autolink(formatted);
}; };

View file

@ -259,26 +259,6 @@ const Theme = () => {
); );
}; };
const MarkdownAlwaysEnabled = () => {
const { t } = useTranslation();
const labelId = "prefMarkdown";
const enabled = useLiveQuery(async () => prefs.markdownAlwaysEnabled());
const handleChange = async (ev) => {
await prefs.setMarkdownAlwaysEnabled(ev.target.value);
};
return (
<Pref labelId={labelId} title={t("prefs_appearance_markdown_always_enabled_title")}>
<FormControl fullWidth variant="standard" sx={{ m: 1 }}>
<Select value={enabled ?? false} onChange={handleChange} aria-labelledby={labelId}>
<MenuItem value>{t("prefs_appearance_markdown_always_enabled_on")}</MenuItem>
<MenuItem value={false}>{t("prefs_appearance_markdown_always_enabled_off")}</MenuItem>
</Select>
</FormControl>
</Pref>
);
};
const WebPushEnabled = () => { const WebPushEnabled = () => {
const { t } = useTranslation(); const { t } = useTranslation();
const labelId = "prefWebPushEnabled"; const labelId = "prefWebPushEnabled";
@ -533,7 +513,6 @@ const Appearance = () => {
<PrefGroup> <PrefGroup>
<Theme /> <Theme />
<Language /> <Language />
<MarkdownAlwaysEnabled />
</PrefGroup> </PrefGroup>
</Card> </Card>
); );