28 Commits

Author SHA1 Message Date
67b6f30c62 Add IDs to metrics 2024-03-25 16:41:49 +00:00
8ae311190f Fix panic????????? 2024-03-25 06:07:30 +00:00
016164affb Remove unused javascript/css 2024-03-24 21:00:27 +00:00
2c0aeef700 Fix build. Bump version 2024-03-24 20:55:07 +00:00
ecd75d6f55 Add metrics 2024-03-24 20:38:19 +00:00
4a80d42f86 Move postman and web inside src 2024-03-24 20:23:16 +00:00
075fde71df Bump version 2024-03-11 18:17:22 +00:00
55136aecdc Set default embed color correctly 2024-03-11 18:14:27 +00:00
63fc2cdcbc Block editing username and avatar on DMs 2024-03-10 19:43:57 +00:00
3190738fc5 Extend user reminder API endpoints 2024-03-09 16:17:55 +00:00
8f4810b532 Convert to/from timezone 2024-03-08 16:36:24 +00:00
a5e6c41fa5 Bump ver
Update build file
2024-03-05 20:55:20 +00:00
5f0aa0f834 Add routes for getting/posting user reminders 2024-03-05 20:36:38 +00:00
dbe8e8e358 Add mentioning for channels 2024-03-04 20:36:37 +00:00
85a114e55c Start adding stuff for user reminders 2024-03-03 21:58:48 +00:00
329492b244 Add mention support
Allow vertical resizing of inputs
2024-03-03 21:44:35 +00:00
66135ecd08 Show time until on collapsed reminders 2024-03-03 20:38:17 +00:00
382c2a5a1e Stick options 2024-03-03 19:43:02 +00:00
b91245a3f7 Build dashboard with bot 2024-03-03 13:21:06 +00:00
6f0bdf9852 Support sending reminders to threads 2024-03-03 13:04:50 +00:00
dcee9e0d2a Begin to work on thread support 2024-03-03 11:58:22 +00:00
8e6e1a18b7 Bump ver 2024-03-01 18:04:34 +00:00
72af0532fa Fix timezones 2024-03-01 17:54:05 +00:00
e83b643d86 Show error for files that are too large 2024-03-01 16:56:31 +00:00
0e0ab053f3 Fix time inputs 2024-03-01 16:54:56 +00:00
8c2296b9c8 Bump versions 2024-02-28 21:37:10 +00:00
1c6103142f Fix color picker not working 2024-02-28 21:30:53 +00:00
328127c55e Fix images not setting properly 2024-02-28 21:30:49 +00:00
198 changed files with 2458 additions and 3800 deletions

922
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
[package]
name = "reminder-rs"
version = "1.7.0-rc5"
version = "1.7.4-2"
authors = ["Jude Southworth <judesouthworth@pm.me>"]
edition = "2021"
license = "AGPL-3.0 only"
@ -10,8 +10,7 @@ description = "Reminder Bot for Discord, now in Rust"
poise = "0.6.1"
dotenv = "0.15"
tokio = { version = "1", features = ["process", "full"] }
reqwest = "0.11"
lazy-regex = "3.1"
reqwest = { version = "0.12", features = ["json"] }
regex = "1.10"
log = "0.4"
env_logger = "0.11"
@ -28,12 +27,14 @@ levenshtein = "1.0"
sqlx = { version = "0.7", features = ["runtime-tokio-rustls", "macros", "mysql", "bigdecimal", "chrono", "migrate"] }
base64 = "0.21"
secrecy = "0.8.0"
[dependencies.postman]
path = "postman"
[dependencies.reminder_web]
path = "web"
futures = "0.3.30"
prometheus = "0.13.3"
rocket = { version = "0.5.0", features = ["tls", "secrets", "json"] }
rocket_dyn_templates = { version = "0.1.0", features = ["tera"] }
serenity = { version = "0.12", default-features = false, features = ["builder", "cache", "client", "gateway", "http", "model", "utils", "rustls_backend"] }
oauth2 = "4"
csv = "1.2"
axum = "0.7"
[dependencies.extract_derive]
path = "extract_derive"
@ -47,19 +48,17 @@ suggests = "mysql-server-8.0, nginx"
maintainer-scripts = "debian"
assets = [
["target/release/reminder-rs", "usr/bin/reminder-rs", "755"],
["web/static/css/*", "lib/reminder-rs/static/css", "644"],
["web/static/favicon/*", "lib/reminder-rs/static/favicon", "644"],
["web/static/img/*", "lib/reminder-rs/static/img", "644"],
["web/static/js/*", "lib/reminder-rs/static/js", "644"],
["web/static/webfonts/*", "lib/reminder-rs/static/webfonts", "644"],
["web/static/site.webmanifest", "lib/reminder-rs/static/site.webmanifest", "644"],
["web/templates/**/*", "lib/reminder-rs/templates", "644"],
["static/css/*", "lib/reminder-rs/static/css", "644"],
["static/favicon/*", "lib/reminder-rs/static/favicon", "644"],
["static/img/*", "lib/reminder-rs/static/img", "644"],
["static/js/*", "lib/reminder-rs/static/js", "644"],
["static/webfonts/*", "lib/reminder-rs/static/webfonts", "644"],
["static/site.webmanifest", "lib/reminder-rs/static/site.webmanifest", "644"],
["templates/**/*", "lib/reminder-rs/templates", "644"],
["reminder-dashboard/dist/static/assets/*", "lib/reminder-rs/static/assets", "644"],
["reminder-dashboard/dist/index.html", "lib/reminder-rs/static/index.html", "644"],
["conf/default.env", "etc/reminder-rs/config.env", "600"],
["conf/Rocket.toml", "etc/reminder-rs/Rocket.toml", "600"],
["healthcheck", "lib/reminder-rs/healthcheck", "755"],
["cron.d/reminder_health", "etc/cron.d/reminder_health", "644"],
# ["nginx/reminder-rs", "etc/nginx/sites-available/reminder-rs", "755"]
]
conf-files = [

View File

@ -4,6 +4,6 @@ ENV RUSTUP_HOME=/usr/local/rustup \
CARGO_HOME=/usr/local/cargo \
PATH=/usr/local/cargo/bin:$PATH
RUN apt update && DEBIAN_FRONTEND=noninteractive TZ=Etc/UTC apt install -y gcc gcc-multilib cmake pkg-config libssl-dev curl mysql-client-8.0
RUN apt update && DEBIAN_FRONTEND=noninteractive TZ=Etc/UTC apt install -y gcc gcc-multilib cmake pkg-config libssl-dev curl mysql-client-8.0 npm
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --no-modify-path --profile minimal --default-toolchain nightly
RUN cargo install cargo-deb

View File

@ -1,28 +1,28 @@
[default]
address = "0.0.0.0"
port = 18920
template_dir = "web/templates"
template_dir = "templates"
limits = { json = "10MiB" }
[debug]
secret_key = "tR8krio5FXTnnyIZNiJDXPondz0kI1v6X6BXZcBGIRY="
[debug.tls]
certs = "web/private/rsa_sha256_cert.pem"
key = "web/private/rsa_sha256_key.pem"
certs = "private/rsa_sha256_cert.pem"
key = "private/rsa_sha256_key.pem"
[debug.rsa_sha256.tls]
certs = "web/private/rsa_sha256_cert.pem"
key = "web/private/rsa_sha256_key.pem"
certs = "private/rsa_sha256_cert.pem"
key = "private/rsa_sha256_key.pem"
[debug.ecdsa_nistp256_sha256.tls]
certs = "web/private/ecdsa_nistp256_sha256_cert.pem"
key = "web/private/ecdsa_nistp256_sha256_key_pkcs8.pem"
certs = "private/ecdsa_nistp256_sha256_cert.pem"
key = "private/ecdsa_nistp256_sha256_key_pkcs8.pem"
[debug.ecdsa_nistp384_sha384.tls]
certs = "web/private/ecdsa_nistp384_sha384_cert.pem"
key = "web/private/ecdsa_nistp384_sha384_key_pkcs8.pem"
certs = "private/ecdsa_nistp384_sha384_cert.pem"
key = "private/ecdsa_nistp384_sha384_key_pkcs8.pem"
[debug.ed25519.tls]
certs = "web/private/ed25519_cert.pem"
key = "eb/private/ed25519_key.pem"
certs = "private/ed25519_cert.pem"
key = "private/ed25519_key.pem"

View File

@ -1,3 +1,13 @@
use std::{path::Path, process::Command};
fn main() {
println!("cargo:rerun-if-changed=migrations");
println!("cargo:rerun-if-changed=reminder-dashboard");
Command::new("npm")
.arg("run")
.arg("build")
.current_dir(Path::new("reminder-dashboard"))
.spawn()
.expect("Failed to build NPM");
}

View File

@ -1,13 +0,0 @@
#!/bin/bash
export $(grep -v '^#' /etc/reminder-rs/config.env | xargs -d '\n')
REGEX='mysql://([A-Za-z]+)@(.+)/(.+)'
[[ $DATABASE_URL =~ $REGEX ]]
VAR=$(mysql -u "${BASH_REMATCH[1]}" -h "${BASH_REMATCH[2]}" -N -D "${BASH_REMATCH[3]}" -e "SELECT COUNT(1) FROM reminders WHERE utc_time < NOW() - INTERVAL 10 MINUTE AND enabled = 1 AND status = 'pending'")
if [ "$VAR" -gt 0 ]
then
echo "This is to inform that there is a reminder backlog which must be resolved." | mail -s "Backlog: $VAR" "$REPORT_EMAIL"
fi

View File

@ -0,0 +1,5 @@
-- Add migration script here
ALTER TABLE reminders
ADD INDEX `utc_time_index` (`utc_time`);
ALTER TABLE reminders
ADD INDEX `status_index` (`status`);

View File

@ -1,41 +1,41 @@
server {
server_name www.reminder-bot.com;
server_name www.reminder-bot.com;
return 301 $scheme://reminder-bot.com$request_uri;
return 301 $scheme://reminder-bot.com$request_uri;
}
server {
listen 80;
server_name reminder-bot.com;
listen 80;
server_name reminder-bot.com;
return 301 https://reminder-bot.com$request_uri;
return 301 https://reminder-bot.com$request_uri;
}
server {
listen 443 ssl;
server_name reminder-bot.com;
listen 443 ssl;
server_name reminder-bot.com;
ssl_certificate /etc/letsencrypt/live/reminder-bot.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/reminder-bot.com/privkey.pem;
ssl_certificate /etc/letsencrypt/live/reminder-bot.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/reminder-bot.com/privkey.pem;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
location / {
proxy_pass http://localhost:18920;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location / {
proxy_pass http://localhost:18920;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /static {
alias /var/www/reminder-rs/static;
expires 30d;
}
location /static {
alias /var/www/reminder-rs/static;
expires 30d;
}
}

View File

@ -1,16 +0,0 @@
[package]
name = "postman"
version = "0.1.0"
edition = "2021"
[dependencies]
tokio = { version = "1", features = ["process", "full"] }
regex = "1.9"
log = "0.4"
chrono = "0.4"
chrono-tz = { version = "0.8", features = ["serde"] }
lazy_static = "1.4"
num-integer = "0.1"
serde = "1.0"
sqlx = { version = "0.7", features = ["runtime-tokio-rustls", "macros", "mysql", "bigdecimal", "chrono", "json"]}
serenity = { version = "0.12", default-features = false, features = ["builder", "cache", "client", "gateway", "http", "model", "utils", "rustls_backend"] }

View File

@ -11,8 +11,10 @@
"luxon": "^3.4.3",
"preact": "^10.13.1",
"react-colorful": "^5.6.1",
"react-mentions": "^4.4.10",
"react-query": "^3.39.3",
"tributejs": "^5.1.3",
"use-debounce": "^10.0.0",
"wouter": "^3.0"
},
"devDependencies": {
@ -3388,6 +3390,14 @@
"node": ">= 0.4"
}
},
"node_modules/invariant": {
"version": "2.2.4",
"resolved": "https://registry.npmjs.org/invariant/-/invariant-2.2.4.tgz",
"integrity": "sha512-phJfQVBuaJM5raOpJjSfkiD6BpbCE4Ns//LaXl6wGYtUBY83nWS6Rf9tXm2e8VaK60JEjYldbPif/A2B1C2gNA==",
"dependencies": {
"loose-envify": "^1.0.0"
}
},
"node_modules/is-array-buffer": {
"version": "3.0.4",
"resolved": "https://registry.npmjs.org/is-array-buffer/-/is-array-buffer-3.0.4.tgz",
@ -4068,7 +4078,6 @@
"version": "4.1.1",
"resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz",
"integrity": "sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==",
"dev": true,
"engines": {
"node": ">=0.10.0"
}
@ -4375,7 +4384,6 @@
"version": "15.8.1",
"resolved": "https://registry.npmjs.org/prop-types/-/prop-types-15.8.1.tgz",
"integrity": "sha512-oj87CgZICdulUohogVAR7AjlC0327U4el4L6eAvOqCeudMDVU0NThNaV+b9Df4dXgSP1gXMTnPdhfe/2qDH5cg==",
"dev": true,
"dependencies": {
"loose-envify": "^1.4.0",
"object-assign": "^4.1.1",
@ -4477,8 +4485,35 @@
"node_modules/react-is": {
"version": "16.13.1",
"resolved": "https://registry.npmjs.org/react-is/-/react-is-16.13.1.tgz",
"integrity": "sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==",
"dev": true
"integrity": "sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ=="
},
"node_modules/react-mentions": {
"version": "4.4.10",
"resolved": "https://registry.npmjs.org/react-mentions/-/react-mentions-4.4.10.tgz",
"integrity": "sha512-JHiQlgF1oSZR7VYPjq32wy97z1w1oE4x10EuhKjPr4WUKhVzG1uFQhQjKqjQkbVqJrmahf+ldgBTv36NrkpKpA==",
"dependencies": {
"@babel/runtime": "7.4.5",
"invariant": "^2.2.4",
"prop-types": "^15.5.8",
"substyle": "^9.1.0"
},
"peerDependencies": {
"react": ">=16.8.3",
"react-dom": ">=16.8.3"
}
},
"node_modules/react-mentions/node_modules/@babel/runtime": {
"version": "7.4.5",
"resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.4.5.tgz",
"integrity": "sha512-TuI4qpWZP6lGOGIuGWtp9sPluqYICmbk8T/1vpSysqJxRPkudh/ofFWyqdcMsDf2s7KvDL4/YHgKyvcS3g9CJQ==",
"dependencies": {
"regenerator-runtime": "^0.13.2"
}
},
"node_modules/react-mentions/node_modules/regenerator-runtime": {
"version": "0.13.11",
"resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.13.11.tgz",
"integrity": "sha512-kY1AZVr2Ra+t+piVaJ4gxaFaReZVH40AKNo7UCX6W+dEwBo/2oZJzqfuN1qLq1oL45o56cPaTXELwrTh8Fpggg=="
},
"node_modules/react-onclickoutside": {
"version": "6.13.0",
@ -4956,6 +4991,18 @@
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/substyle": {
"version": "9.4.1",
"resolved": "https://registry.npmjs.org/substyle/-/substyle-9.4.1.tgz",
"integrity": "sha512-VOngeq/W1/UkxiGzeqVvDbGDPM8XgUyJVWjrqeh+GgKqspEPiLYndK+XRcsKUHM5Muz/++1ctJ1QCF/OqRiKWA==",
"dependencies": {
"@babel/runtime": "^7.3.4",
"invariant": "^2.2.4"
},
"peerDependencies": {
"react": ">=16.8.3"
}
},
"node_modules/supports-color": {
"version": "5.5.0",
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz",
@ -5206,6 +5253,17 @@
"punycode": "^2.1.0"
}
},
"node_modules/use-debounce": {
"version": "10.0.0",
"resolved": "https://registry.npmjs.org/use-debounce/-/use-debounce-10.0.0.tgz",
"integrity": "sha512-XRjvlvCB46bah9IBXVnq/ACP2lxqXyZj0D9hj4K5OzNroMDpTEBg8Anuh1/UfRTRs7pLhQ+RiNxxwZu9+MVl1A==",
"engines": {
"node": ">= 16.0.0"
},
"peerDependencies": {
"react": ">=16.8.0"
}
},
"node_modules/use-sync-external-store": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/use-sync-external-store/-/use-sync-external-store-1.2.0.tgz",

View File

@ -5,8 +5,7 @@
"scripts": {
"dev": "vite build --watch --mode development",
"build": "vite build",
"preview": "vite preview",
"package": "mkdir -p reminder-dashboard/usr/share/reminder-dashboard && vite build --mode production --outDir reminder-dashboard/usr/share/reminder-dashboard && dpkg-deb --build reminder-dashboard"
"preview": "vite preview"
},
"dependencies": {
"axios": "^1.5.1",
@ -16,6 +15,7 @@
"react-colorful": "^5.6.1",
"react-query": "^3.39.3",
"tributejs": "^5.1.3",
"use-debounce": "^10.0.0",
"wouter": "^3.0"
},
"devDependencies": {

View File

@ -37,7 +37,7 @@ export type Reminder = {
embed_title: string;
embed_fields: EmbedField[] | null;
enabled: boolean;
expires: DateTime | null;
expires: string | null;
interval_seconds: number | null;
interval_days: number | null;
interval_months: number | null;
@ -46,7 +46,7 @@ export type Reminder = {
tts: boolean;
uid: string;
username: string;
utc_time: DateTime;
utc_time: string;
};
export type ChannelInfo = {
@ -128,43 +128,25 @@ export const fetchGuildRoles = (guild: string) => ({
export const fetchGuildReminders = (guild: string) => ({
queryKey: ["GUILD_REMINDERS", guild],
queryFn: () =>
axios
.get(`/dashboard/api/guild/${guild}/reminders`)
.then((resp) => resp.data)
.then((value) =>
value.map((reminder) => ({
...reminder,
utc_time: DateTime.fromISO(reminder.utc_time, { zone: "UTC" }),
expires:
reminder.expires === null
? null
: DateTime.fromISO(reminder.expires, { zone: "UTC" }),
})),
) as Promise<Reminder[]>,
axios.get(`/dashboard/api/guild/${guild}/reminders`).then((resp) => resp.data) as Promise<
Reminder[]
>,
staleTime: OTHER_STALE_TIME,
});
export const patchGuildReminder = (guild: string) => ({
mutationFn: (reminder: Reminder) =>
axios.patch(`/dashboard/api/guild/${guild}/reminders`, {
...reminder,
utc_time: reminder.utc_time.toFormat("yyyy-LL-dd'T'HH:mm:ss"),
}),
axios.patch(`/dashboard/api/guild/${guild}/reminders`, reminder),
});
export const postGuildReminder = (guild: string) => ({
mutationFn: (reminder: Reminder) =>
axios
.post(`/dashboard/api/guild/${guild}/reminders`, {
...reminder,
utc_time: reminder.utc_time.toFormat("yyyy-LL-dd'T'HH:mm:ss"),
})
.then((resp) => resp.data),
axios.post(`/dashboard/api/guild/${guild}/reminders`, reminder).then((resp) => resp.data),
});
export const deleteGuildReminder = (guild: string) => ({
export const deleteReminder = () => ({
mutationFn: (reminder: Reminder) =>
axios.delete(`/dashboard/api/guild/${guild}/reminders`, {
axios.delete(`/dashboard/api/reminders`, {
data: {
uid: reminder.uid,
},
@ -182,12 +164,7 @@ export const fetchGuildTemplates = (guild: string) => ({
export const postGuildTemplate = (guild: string) => ({
mutationFn: (reminder: Reminder) =>
axios
.post(`/dashboard/api/guild/${guild}/templates`, {
...reminder,
utc_time: reminder.utc_time.toFormat("yyyy-LL-dd'T'HH:mm:ss"),
})
.then((resp) => resp.data),
axios.post(`/dashboard/api/guild/${guild}/templates`, reminder).then((resp) => resp.data),
});
export const deleteGuildTemplate = (guild: string) => ({
@ -198,3 +175,19 @@ export const deleteGuildTemplate = (guild: string) => ({
},
}),
});
export const fetchUserReminders = () => ({
queryKey: ["USER_REMINDERS"],
queryFn: () =>
axios.get(`/dashboard/api/user/reminders`).then((resp) => resp.data) as Promise<Reminder[]>,
staleTime: OTHER_STALE_TIME,
});
export const postUserReminder = () => ({
mutationFn: (reminder: Reminder) =>
axios.post(`/dashboard/api/user/reminders`, reminder).then((resp) => resp.data),
});
export const patchUserReminder = () => ({
mutationFn: (reminder: Reminder) => axios.patch(`/dashboard/api/user/reminders`, reminder),
});

View File

@ -0,0 +1,42 @@
import { useEffect, useMemo } from "preact/hooks";
import { useQuery } from "react-query";
import { fetchGuildChannels, fetchGuildRoles } from "../../api";
import Tribute from "tributejs";
import { useGuild } from "./useGuild";
export const Mentions = ({ input }) => {
const guild = useGuild();
const { data: roles } = useQuery(fetchGuildRoles(guild));
const { data: channels } = useQuery(fetchGuildChannels(guild));
const tribute = useMemo(() => {
return new Tribute({
collection: [
{
trigger: "@",
values: (roles || []).map(({ id, name }) => ({ key: name, value: id })),
allowSpaces: true,
selectTemplate: (item) => `<@&${item.original.value}>`,
menuItemTemplate: (item) => `@${item.original.key}`,
},
{
trigger: "#",
values: (channels || []).map(({ id, name }) => ({ key: name, value: id })),
allowSpaces: true,
selectTemplate: (item) => `<#${item.original.value}>`,
menuItemTemplate: (item) => `#${item.original.key}`,
},
],
});
}, [roles, channels]);
useEffect(() => {
tribute.detach(input.current);
if (input.current !== null) {
tribute.attach(input.current);
}
}, [tribute]);
return <></>;
};

View File

@ -5,6 +5,7 @@ import { Welcome } from "../Welcome";
import { Guild } from "../Guild";
import { FlashProvider } from "./FlashProvider";
import { TimezoneProvider } from "./TimezoneProvider";
import { User } from "../User";
export function App() {
const queryClient = new QueryClient();
@ -18,6 +19,7 @@ export function App() {
<Sidebar />
<div class="column is-main-content">
<Switch>
<Route path={"/@me/reminders"} component={User}></Route>
<Route path={"/:guild/reminders"} component={Guild}></Route>
<Route>
<Welcome />

View File

@ -0,0 +1,6 @@
import { useParams } from "wouter";
export const useGuild = () => {
const { guild } = useParams() as { guild?: string };
return guild || null;
};

View File

@ -1,10 +1,10 @@
import { useParams } from "wouter";
import { useQuery } from "react-query";
import { fetchGuildChannels, fetchGuildReminders } from "../../api";
import { EditReminder } from "../Reminder/EditReminder";
import { CreateReminder } from "../Reminder/CreateReminder";
import { useState } from "preact/hooks";
import { Loader } from "../Loader";
import { useGuild } from "../App/useGuild";
enum Sort {
Time = "time",
@ -13,7 +13,7 @@ enum Sort {
}
export const GuildReminders = () => {
const { guild } = useParams();
const guild = useGuild();
const {
isSuccess,

View File

@ -1,13 +1,13 @@
import { useQuery } from "react-query";
import { fetchGuildInfo } from "../../api";
import { useParams } from "wouter";
import { GuildReminders } from "./GuildReminders";
import { GuildError } from "./GuildError";
import { createPortal } from "preact/compat";
import { Import } from "../Import";
import { useGuild } from "../App/useGuild";
export const Guild = () => {
const { guild } = useParams();
const guild = useGuild();
const { isSuccess, data: guildInfo } = useQuery(fetchGuildInfo(guild));
if (!isSuccess) {

View File

@ -1,5 +1,5 @@
import { JSX } from "preact";
import { createPortal } from "preact/compat";
import {JSX} from "preact";
import {createPortal} from "preact/compat";
type Props = {
setModalOpen: (open: boolean) => never;
@ -9,7 +9,7 @@ type Props = {
children: string | JSX.Element | JSX.Element[] | (() => JSX.Element);
};
export const Modal = ({ setModalOpen, title, onSubmit, onSubmitText, children }: Props) => {
export const Modal = ({setModalOpen, title, onSubmit, onSubmitText, children}: Props) => {
const body = document.querySelector("body");
return createPortal(
@ -34,7 +34,7 @@ export const Modal = ({ setModalOpen, title, onSubmit, onSubmitText, children }:
<section class="modal-card-body">{children}</section>
{onSubmit && (
<footer class="modal-card-foot">
<button class="button is-success" onInput={onSubmit}>
<button class="button is-success" onClick={onSubmit}>
{onSubmitText || "Save"}
</button>
<button

View File

@ -1,8 +1,11 @@
import { useReminder } from "./ReminderContext";
import { useFlash } from "../App/FlashContext";
export const Attachment = () => {
const [{ attachment_name }, setReminder] = useReminder();
const flash = useFlash();
return (
<div class="file is-small is-boxed">
<label class="file-label">
@ -16,7 +19,8 @@ export const Attachment = () => {
let file = input.files[0];
if (file.size >= 8 * 1024 * 1024) {
return { error: "File too large." };
flash({ message: "File too large (max. 8MB).", type: "error" });
return;
}
let attachment: string = await new Promise((resolve) => {

View File

@ -0,0 +1,28 @@
import { ImagePicker } from "./ImagePicker";
import { useReminder } from "./ReminderContext";
import { useGuild } from "../App/useGuild";
export const Avatar = () => {
const guild = useGuild();
const [reminder, setReminder] = useReminder();
return guild ? (
<ImagePicker
class="is-rounded avatar"
url={reminder.avatar || "/static/img/icon.png"}
alt="Image for discord avatar"
setImage={(url: string) => {
setReminder((reminder) => ({
...reminder,
avatar: url,
}));
}}
></ImagePicker>
) : (
<img
class="is-rounded avatar"
alt="Image for discord avatar"
src={"/static/img/icon.png"}
></img>
);
};

View File

@ -1,14 +1,14 @@
import { LoadTemplate } from "../LoadTemplate";
import { useReminder } from "../ReminderContext";
import { useMutation, useQueryClient } from "react-query";
import { postGuildReminder, postGuildTemplate } from "../../../api";
import { useParams } from "wouter";
import { postGuildReminder, postGuildTemplate, postUserReminder } from "../../../api";
import { useState } from "preact/hooks";
import { ICON_FLASH_TIME } from "../../../consts";
import { useFlash } from "../../App/FlashContext";
import { useGuild } from "../../App/useGuild";
export const CreateButtonRow = () => {
const { guild } = useParams();
const guild = useGuild();
const [reminder] = useReminder();
const [recentlyCreated, setRecentlyCreated] = useState(false);
@ -17,7 +17,7 @@ export const CreateButtonRow = () => {
const flash = useFlash();
const queryClient = useQueryClient();
const mutation = useMutation({
...postGuildReminder(guild),
...(guild ? postGuildReminder(guild) : postUserReminder()),
onSuccess: (data) => {
if (data.error) {
flash({
@ -29,9 +29,15 @@ export const CreateButtonRow = () => {
message: "Reminder created",
type: "success",
});
queryClient.invalidateQueries({
queryKey: ["GUILD_REMINDERS", guild],
});
if (guild) {
queryClient.invalidateQueries({
queryKey: ["GUILD_REMINDERS", guild],
});
} else {
queryClient.invalidateQueries({
queryKey: ["USER_REMINDERS"],
});
}
setRecentlyCreated(true);
setTimeout(() => {
setRecentlyCreated(false);
@ -89,34 +95,36 @@ export const CreateButtonRow = () => {
)}
</button>
</div>
<div class="button-row-template">
<div>
<button
class="button is-success is-outlined"
onClick={() => {
templateMutation.mutate(reminder);
}}
>
<span>Create Template</span>{" "}
{templateMutation.isLoading ? (
<span class="icon">
<i class="fas fa-spin fa-cog"></i>
</span>
) : templateRecentlyCreated ? (
<span class="icon">
<i class="fas fa-check"></i>
</span>
) : (
<span class="icon">
<i class="fas fa-file-spreadsheet"></i>
</span>
)}
</button>
{guild && (
<div class="button-row-template">
<div>
<button
class="button is-success is-outlined"
onClick={() => {
templateMutation.mutate(reminder);
}}
>
<span>Create Template</span>{" "}
{templateMutation.isLoading ? (
<span class="icon">
<i class="fas fa-spin fa-cog"></i>
</span>
) : templateRecentlyCreated ? (
<span class="icon">
<i class="fas fa-check"></i>
</span>
) : (
<span class="icon">
<i class="fas fa-file-spreadsheet"></i>
</span>
)}
</button>
</div>
<div>
<LoadTemplate />
</div>
</div>
<div>
<LoadTemplate />
</div>
</div>
)}
</div>
);
};

View File

@ -2,9 +2,10 @@ import { useState } from "preact/hooks";
import { Modal } from "../../Modal";
import { useMutation, useQueryClient } from "react-query";
import { useReminder } from "../ReminderContext";
import { deleteGuildReminder } from "../../../api";
import { deleteReminder } from "../../../api";
import { useParams } from "wouter";
import { useFlash } from "../../App/FlashContext";
import { useGuild } from "../../App/useGuild";
export const DeleteButton = () => {
const [modalOpen, setModalOpen] = useState(false);
@ -26,20 +27,26 @@ export const DeleteButton = () => {
const DeleteModal = ({ setModalOpen }) => {
const [reminder] = useReminder();
const { guild } = useParams();
const guild = useGuild();
const flash = useFlash();
const queryClient = useQueryClient();
const mutation = useMutation({
...deleteGuildReminder(guild),
...deleteReminder(),
onSuccess: () => {
flash({
message: "Reminder deleted",
type: "success",
});
queryClient.invalidateQueries({
queryKey: ["GUILD_REMINDERS", guild],
});
if (guild) {
queryClient.invalidateQueries({
queryKey: ["GUILD_REMINDERS", guild],
});
} else {
queryClient.invalidateQueries({
queryKey: ["USER_REMINDERS"],
});
}
setModalOpen(false);
},
});

View File

@ -1,14 +1,14 @@
import { useRef, useState } from "preact/hooks";
import { useMutation, useQueryClient } from "react-query";
import { patchGuildReminder } from "../../../api";
import { useParams } from "wouter";
import { patchGuildReminder, patchUserReminder } from "../../../api";
import { ICON_FLASH_TIME } from "../../../consts";
import { DeleteButton } from "./DeleteButton";
import { useReminder } from "../ReminderContext";
import { useFlash } from "../../App/FlashContext";
import { useGuild } from "../../App/useGuild";
export const EditButtonRow = () => {
const { guild } = useParams();
const guild = useGuild();
const [reminder, setReminder] = useReminder();
const [recentlySaved, setRecentlySaved] = useState(false);
@ -18,11 +18,17 @@ export const EditButtonRow = () => {
const flash = useFlash();
const mutation = useMutation({
...patchGuildReminder(guild),
...(guild ? patchGuildReminder(guild) : patchUserReminder()),
onSuccess: (response) => {
queryClient.invalidateQueries({
queryKey: ["GUILD_REMINDERS", guild],
});
if (guild) {
queryClient.invalidateQueries({
queryKey: ["GUILD_REMINDERS", guild],
});
} else {
queryClient.invalidateQueries({
queryKey: ["USER_REMINDERS"],
});
}
if (iconFlashTimeout.current !== null) {
clearTimeout(iconFlashTimeout.current);

View File

@ -1,10 +1,16 @@
import { useReminder } from "./ReminderContext";
import { useRef } from "preact/hooks";
import { Mentions } from "../App/Mentions";
import { useGuild } from "../App/useGuild";
export const Content = () => {
const guild = useGuild();
const [reminder, setReminder] = useReminder();
const input = useRef(null);
return (
<>
{guild && <Mentions input={input} />}
<label class="is-sr-only">Content</label>
<textarea
class="message-input autoresize discord-content"
@ -12,6 +18,7 @@ export const Content = () => {
maxlength={2000}
name="content"
rows={1}
ref={input}
value={reminder.content}
onInput={(ev) => {
setReminder((reminder) => ({

View File

@ -7,7 +7,9 @@ import { Message } from "./Message";
import { Settings } from "./Settings";
import { ReminderContext } from "./ReminderContext";
import { useQuery } from "react-query";
import { useParams } from "wouter";
import "./styles.scss";
import { useGuild } from "../App/useGuild";
import { DEFAULT_COLOR } from "./Embed";
function defaultReminder(): Reminder {
return {
@ -18,7 +20,7 @@ function defaultReminder(): Reminder {
content: "",
embed_author: "",
embed_author_url: null,
embed_color: 0,
embed_color: DEFAULT_COLOR,
embed_description: "",
embed_fields: [],
embed_footer: "",
@ -36,12 +38,12 @@ function defaultReminder(): Reminder {
tts: false,
uid: "",
username: "",
utc_time: DateTime.now(),
utc_time: DateTime.now().toFormat("yyyy-LL-dd'T'HH:mm:ss"),
};
}
export const CreateReminder = () => {
const { guild } = useParams();
const guild = useGuild();
const [reminder, setReminder] = useState(defaultReminder());
const [collapsed, setCollapsed] = useState(false);

View File

@ -5,6 +5,7 @@ import { Message } from "./Message";
import { Settings } from "./Settings";
import { ReminderContext } from "./ReminderContext";
import { TopBar } from "./TopBar";
import "./styles.scss";
type Props = {
reminder: Reminder;

View File

@ -1,5 +1,8 @@
import { ImagePicker } from "../ImagePicker";
import { Reminder } from "../../../api";
import { Mentions } from "../../App/Mentions";
import { useRef } from "preact/hooks";
import { useGuild } from "../../App/useGuild";
type Props = {
name: string;
@ -8,6 +11,9 @@ type Props = {
};
export const Author = ({ name, icon, setReminder }: Props) => {
const guild = useGuild();
const input = useRef(null);
return (
<div class="embed-author-box">
<div class="a">
@ -16,7 +22,7 @@ export const Author = ({ name, icon, setReminder }: Props) => {
class="is-rounded embed_author_url"
url={icon}
alt="Image for embed author"
setImage={(url) => {
setImage={(url: string) => {
setReminder((reminder) => ({
...reminder,
embed_author_url: url,
@ -27,6 +33,7 @@ export const Author = ({ name, icon, setReminder }: Props) => {
</div>
<div class="b">
{guild && <Mentions input={input} />}
<label class="is-sr-only" for="embedAuthor">
Embed Author
</label>
@ -34,6 +41,7 @@ export const Author = ({ name, icon, setReminder }: Props) => {
class="discord-embed-author message-input autoresize"
placeholder="Embed Author..."
rows={1}
ref={input}
maxlength={256}
name="embed_author"
value={name}

View File

@ -2,6 +2,7 @@ import { useState } from "preact/hooks";
import { HexColorPicker } from "react-colorful";
import { Modal } from "../../Modal";
import { Reminder } from "../../../api";
import { useDebouncedCallback } from "use-debounce";
type Props = {
color: string;
@ -38,16 +39,20 @@ export const Color = ({ color, setReminder }: Props) => {
};
const ColorModal = ({ setModalOpen, color, setReminder }) => {
const setDebounced = useDebouncedCallback((color) => {
setReminder((reminder: Reminder) => ({
...reminder,
embed_color: colorToInt(color),
}));
}, 100);
return (
<Modal setModalOpen={setModalOpen} title={"Select Color"}>
<div class="colorpicker-container">
<HexColorPicker
color={color}
onInput={(color) => {
setReminder((reminder) => ({
...reminder,
embed_color: colorToInt(color),
}));
onChange={(color: string) => {
setDebounced(color);
}}
></HexColorPicker>
</div>
@ -57,10 +62,7 @@ const ColorModal = ({ setModalOpen, color, setReminder }) => {
id="colorInput"
value={color}
onInput={(ev) => {
setReminder((reminder) => ({
...reminder,
embed_color: colorToInt(ev.currentTarget.value),
}));
setDebounced(ev.currentTarget.value);
}}
></input>
</Modal>

View File

@ -1,18 +1,29 @@
export const Description = ({ description, onInput }) => (
<>
<label class="is-sr-only" for="embedDescription">
Embed Description
</label>
<textarea
class="discord-description message-input autoresize "
placeholder="Embed Description..."
maxlength={4096}
name="embed_description"
rows={1}
value={description}
onInput={(ev) => {
onInput(ev.currentTarget.value);
}}
></textarea>
</>
);
import { Mentions } from "../../App/Mentions";
import { useRef } from "preact/hooks";
import { useGuild } from "../../App/useGuild";
export const Description = ({ description, onInput }) => {
const guild = useGuild();
const input = useRef(null);
return (
<>
{guild && <Mentions input={input} />}
<label class="is-sr-only" for="embedDescription">
Embed Description
</label>
<textarea
class="discord-description message-input autoresize "
placeholder="Embed Description..."
maxlength={4096}
name="embed_description"
rows={1}
ref={input}
value={description}
onInput={(ev) => {
onInput(ev.currentTarget.value);
}}
></textarea>
</>
);
};

View File

@ -1,4 +1,11 @@
import { useRef } from "preact/hooks";
import { Mentions } from "../../../App/Mentions";
import { useGuild } from "../../../App/useGuild";
export const Field = ({ title, value, inline, index, onUpdate }) => {
const guild = useGuild();
const input = useRef(null);
return (
<div data-inlined={inline ? "1" : "0"} class="embed-field-box" key={index}>
<label class="is-sr-only" for="embedFieldTitle">
@ -35,6 +42,7 @@ export const Field = ({ title, value, inline, index, onUpdate }) => {
)}
</div>
{guild && <Mentions input={input} />}
<label class="is-sr-only" for="embedFieldValue">
Field Value
</label>
@ -44,6 +52,7 @@ export const Field = ({ title, value, inline, index, onUpdate }) => {
maxlength={1024}
name="embed_field_value[]"
rows={1}
ref={input}
value={value}
onInput={(ev) =>
onUpdate({

View File

@ -1,5 +1,8 @@
import { Reminder } from "../../../api";
import { ImagePicker } from "../ImagePicker";
import { Mentions } from "../../App/Mentions";
import { useRef } from "preact/hooks";
import { useGuild } from "../../App/useGuild";
type Props = {
footer: string;
@ -7,37 +10,44 @@ type Props = {
setReminder: (r: (reminder: Reminder) => Reminder) => void;
};
export const Footer = ({ footer, icon, setReminder }: Props) => (
<div class="embed-footer-box">
<p class="image is-20x20 customizable">
<ImagePicker
class="is-rounded embed_footer_url"
url={icon}
alt="Footer profile-like image"
setImage={(url: string) => {
export const Footer = ({ footer, icon, setReminder }: Props) => {
const guild = useGuild();
const input = useRef(null);
return (
<div class="embed-footer-box">
<p class="image is-20x20 customizable">
<ImagePicker
class="is-rounded embed_footer_url"
url={icon}
alt="Footer profile-like image"
setImage={(url: string) => {
setReminder((reminder) => ({
...reminder,
embed_footer_url: url,
}));
}}
></ImagePicker>
</p>
<label class="is-sr-only" for="embedFooter">
Embed Footer text
</label>
{guild && <Mentions input={input} />}
<textarea
class="discord-embed-footer message-input autoresize "
placeholder="Embed Footer..."
maxlength={2048}
name="embed_footer"
rows={1}
ref={input}
value={footer}
onInput={(ev) => {
setReminder((reminder) => ({
...reminder,
embed_footer_url: url,
embed_footer: ev.currentTarget.value,
}));
}}
></ImagePicker>
</p>
<label class="is-sr-only" for="embedFooter">
Embed Footer text
</label>
<textarea
class="discord-embed-footer message-input autoresize "
placeholder="Embed Footer..."
maxlength={2048}
name="embed_footer"
rows={1}
value={footer}
onInput={(ev) => {
setReminder((reminder) => ({
...reminder,
embed_footer: ev.currentTarget.value,
}));
}}
></textarea>
</div>
);
></textarea>
</div>
);
};

View File

@ -1,18 +1,29 @@
export const Title = ({ title, onInput }) => (
<>
<label class="is-sr-only" for="embedTitle">
Embed Title
</label>
<textarea
class="discord-title message-input autoresize"
placeholder="Embed Title..."
maxlength={256}
rows={1}
name="embed_title"
value={title}
onInput={(ev) => {
onInput(ev.currentTarget.value);
}}
></textarea>
</>
);
import { useRef } from "preact/hooks";
import { useGuild } from "../../App/useGuild";
import { Mentions } from "../../App/Mentions";
export const Title = ({ title, onInput }) => {
const guild = useGuild();
const input = useRef(null);
return (
<>
{guild && <Mentions input={input} />}
<label class="is-sr-only" for="embedTitle">
Embed Title
</label>
<textarea
class="discord-title message-input autoresize"
placeholder="Embed Title..."
maxlength={256}
rows={1}
ref={input}
name="embed_title"
value={title}
onInput={(ev) => {
onInput(ev.currentTarget.value);
}}
></textarea>
</>
);
};

View File

@ -12,7 +12,7 @@ function intToColor(num: number) {
return `#${num.toString(16).padStart(6, "0")}`;
}
const DEFAULT_COLOR = 9418359;
export const DEFAULT_COLOR = 9418359;
export const Embed = () => {
const [reminder, setReminder] = useReminder();
@ -65,7 +65,12 @@ export const Embed = () => {
class="embed_thumbnail_url"
url={reminder.embed_thumbnail_url}
alt="Square thumbnail embedded image"
setImage={() => {}}
setImage={(url: string) =>
setReminder((reminder: Reminder) => ({
...reminder,
embed_thumbnail_url: url || null,
}))
}
/>
</p>
</div>
@ -76,7 +81,12 @@ export const Embed = () => {
class="embed_image_url"
url={reminder.embed_image_url}
alt="Large embedded image"
setImage={() => {}}
setImage={(url: string) =>
setReminder((reminder: Reminder) => ({
...reminder,
embed_image_url: url || null,
}))
}
/>
</p>

View File

@ -33,6 +33,7 @@ const ImagePickerModal = ({ setModalOpen, setImage }) => {
title={"Enter Image URL"}
onSubmit={() => {
setImage(value);
setModalOpen(false);
}}
onSubmitText={"Save"}
>

View File

@ -1,38 +1,23 @@
import { ImagePicker } from "./ImagePicker";
import { Username } from "./Username";
import { Content } from "./Content";
import { Embed } from "./Embed";
import { useReminder } from "./ReminderContext";
import { Avatar } from "./Avatar";
export const Message = () => {
const [reminder, setReminder] = useReminder();
return (
<div class="column discord-frame">
<article class="media">
<figure class="media-left">
<p class="image is-32x32 customizable">
<ImagePicker
class="is-rounded avatar"
url={reminder.avatar || "/static/img/icon.png"}
alt="Image for discord avatar"
setImage={(url: string) => {
setReminder((reminder) => ({
...reminder,
avatar: url,
}));
}}
></ImagePicker>
</p>
</figure>
<div class="media-content">
<div class="content">
<Username />
<Content />
<Embed />
</div>
export const Message = () => (
<div class="column discord-frame">
<article class="media">
<figure class="media-left">
<p class="image is-32x32 customizable">
<Avatar />
</p>
</figure>
<div class="media-content">
<div class="content">
<Username />
<Content />
<Embed />
</div>
</article>
</div>
);
};
</div>
</article>
</div>
);

View File

@ -7,13 +7,13 @@ import { useReminder } from "./ReminderContext";
import { Attachment } from "./Attachment";
import { TTS } from "./TTS";
import { TimeInput } from "./TimeInput";
import { useTimezone } from "../App/TimezoneProvider";
import { useGuild } from "../App/useGuild";
export const Settings = () => {
const guild = useGuild();
const { isSuccess: userFetched, data: userInfo } = useQuery(fetchUserInfo());
const [reminder, setReminder] = useReminder();
const [timezone] = useTimezone();
if (!userFetched) {
return <></>;
@ -21,33 +21,35 @@ export const Settings = () => {
return (
<div class="column settings">
<div class="field channel-field">
<div class="collapses">
<label class="label" for="channelOption">
Channel*
</label>
{guild && (
<div class="field channel-field">
<div class="collapses">
<label class="label" for="channelOption">
Channel*
</label>
</div>
<ChannelSelector
channel={reminder.channel}
setChannel={(channel: string) => {
setReminder((reminder) => ({
...reminder,
channel: channel,
}));
}}
/>
</div>
<ChannelSelector
channel={reminder.channel}
setChannel={(channel: string) => {
setReminder((reminder) => ({
...reminder,
channel: channel,
}));
}}
/>
</div>
)}
<div class="field">
<div class="control">
<label class="label collapses">
Time*
<TimeInput
defaultValue={reminder.utc_time.setZone(timezone)}
onInput={(time: DateTime) => {
defaultValue={reminder.utc_time}
onInput={(time: string) => {
setReminder((reminder) => ({
...reminder,
utc_time: time.toUTC(),
utc_time: time,
}));
}}
/>
@ -98,11 +100,11 @@ export const Settings = () => {
<label class="label">
Expiration
<TimeInput
defaultValue={reminder.expires?.setZone(timezone)}
onInput={(time: DateTime) => {
defaultValue={reminder.expires}
onInput={(time: string) => {
setReminder((reminder) => ({
...reminder,
expires: time?.toUTC(),
expires: time,
}));
}}
/>

View File

@ -1,14 +1,42 @@
import { useEffect, useRef, useState } from "preact/hooks";
import { useCallback, useEffect, useRef, useState } from "preact/hooks";
import { DateTime } from "luxon";
import { useFlash } from "../App/FlashContext";
import { useTimezone } from "../App/TimezoneProvider";
type TimeUpdate = {
year?: number | null;
month?: number;
day?: number;
hour?: number;
minute?: number;
second?: number;
};
export const TimeInput = ({ defaultValue, onInput }) => {
const ref = useRef(null);
const [time, setTime] = useState(defaultValue);
const [timezone] = useTimezone();
const [time, setTime] = useState(
defaultValue ? DateTime.fromISO(defaultValue, { zone: "UTC" }) : null,
);
const updateTime = useCallback(
(upd: TimeUpdate) => {
if (upd === null) {
setTime(null);
}
let newTime = time;
if (newTime === null) {
newTime = DateTime.now().setZone("UTC");
}
setTime(newTime.setZone(timezone).set(upd).setZone("UTC"));
},
[time, timezone],
);
useEffect(() => {
onInput(time);
onInput(time?.setZone("UTC").toFormat("yyyy-LL-dd'T'HH:mm:ss"));
}, [time]);
const flash = useFlash();
@ -20,7 +48,7 @@ export const TimeInput = ({ defaultValue, onInput }) => {
onPaste={(ev) => {
ev.preventDefault();
const pasteValue = ev.clipboardData.getData("text/plain");
let dt = DateTime.fromISO(pasteValue);
let dt = DateTime.fromISO(pasteValue, { zone: timezone });
if (dt.isValid) {
setTime(dt);
@ -54,12 +82,20 @@ export const TimeInput = ({ defaultValue, onInput }) => {
pattern="\d*"
maxlength={4}
placeholder="YYYY"
value={time?.year.toLocaleString("en-US", {
minimumIntegerDigits: 4,
useGrouping: false,
})}
value={
time
? time.setZone(timezone).year.toLocaleString("en-US", {
minimumIntegerDigits: 4,
useGrouping: false,
})
: ""
}
onBlur={(ev) => {
setTime(time.set({ year: ev.currentTarget.value }));
ev.currentTarget.value
? updateTime({
year: parseInt(ev.currentTarget.value),
})
: updateTime(null);
}}
></input>{" "}
</label>
@ -77,9 +113,19 @@ export const TimeInput = ({ defaultValue, onInput }) => {
pattern="\d*"
maxlength={2}
placeholder="MM"
value={time?.month.toLocaleString("en-US", { minimumIntegerDigits: 2 })}
value={
time
? time.setZone(timezone).month.toLocaleString("en-US", {
minimumIntegerDigits: 2,
})
: ""
}
onBlur={(ev) => {
setTime(time.set({ month: ev.currentTarget.value }));
ev.currentTarget.value
? updateTime({
month: parseInt(ev.currentTarget.value),
})
: updateTime(null);
}}
></input>{" "}
</label>
@ -97,9 +143,19 @@ export const TimeInput = ({ defaultValue, onInput }) => {
pattern="\d*"
maxlength={2}
placeholder="DD"
value={time?.day.toLocaleString("en-US", { minimumIntegerDigits: 2 })}
value={
time
? time
.setZone(timezone)
.day.toLocaleString("en-US", { minimumIntegerDigits: 2 })
: ""
}
onBlur={(ev) => {
setTime(time.set({ day: ev.currentTarget.value }));
ev.currentTarget.value
? updateTime({
day: parseInt(ev.currentTarget.value),
})
: updateTime(null);
}}
></input>{" "}
</label>
@ -116,9 +172,19 @@ export const TimeInput = ({ defaultValue, onInput }) => {
pattern="\d*"
maxlength={2}
placeholder="hh"
value={time?.hour.toLocaleString("en-US", { minimumIntegerDigits: 2 })}
value={
time
? time
.setZone(timezone)
.hour.toLocaleString("en-US", { minimumIntegerDigits: 2 })
: ""
}
onBlur={(ev) => {
setTime(time.set({ hour: ev.currentTarget.value }));
ev.currentTarget.value
? updateTime({
hour: parseInt(ev.currentTarget.value),
})
: updateTime(null);
}}
></input>{" "}
</label>
@ -136,11 +202,19 @@ export const TimeInput = ({ defaultValue, onInput }) => {
pattern="\d*"
maxlength={2}
placeholder="mm"
value={time?.minute.toLocaleString("en-US", {
minimumIntegerDigits: 2,
})}
value={
time
? time.setZone(timezone).minute.toLocaleString("en-US", {
minimumIntegerDigits: 2,
})
: ""
}
onBlur={(ev) => {
setTime(time.set({ minute: ev.currentTarget.value }));
ev.currentTarget.value
? updateTime({
minute: parseInt(ev.currentTarget.value),
})
: updateTime(null);
}}
></input>{" "}
</label>
@ -158,11 +232,19 @@ export const TimeInput = ({ defaultValue, onInput }) => {
pattern="\d*"
maxlength={2}
placeholder="ss"
value={time?.second.toLocaleString("en-US", {
minimumIntegerDigits: 2,
})}
value={
time
? time.setZone(timezone).second.toLocaleString("en-US", {
minimumIntegerDigits: 2,
})
: ""
}
onBlur={(ev) => {
setTime(time.set({ second: ev.currentTarget.value }));
ev.currentTarget.value
? updateTime({
second: parseInt(ev.currentTarget.value),
})
: updateTime(null);
}}
></input>{" "}
</label>
@ -200,7 +282,13 @@ export const TimeInput = ({ defaultValue, onInput }) => {
}
ref={ref}
onInput={(ev) => {
setTime(DateTime.fromISO(ev.currentTarget.value));
ev.currentTarget.value === ""
? updateTime(null)
: setTime(
DateTime.fromISO(ev.currentTarget.value, { zone: timezone }).setZone(
"UTC",
),
);
}}
></input>
</>

View File

@ -1,30 +0,0 @@
import { useReminder } from "./ReminderContext";
import { Name } from "./Name";
import { fetchGuildChannels, Reminder } from "../../api";
import { useQuery } from "react-query";
import { useParams } from "wouter";
export const TopBar = ({ toggleCollapsed }) => {
const { guild } = useParams();
const [reminder] = useReminder();
const { isSuccess, data: guildChannels } = useQuery(fetchGuildChannels(guild));
const channelName = (reminder: Reminder) => {
const channel = guildChannels.find((c) => c.id === reminder.channel);
return channel === undefined ? "" : channel.name;
};
return (
<div class="columns is-mobile column reminder-topbar">
{isSuccess && <div class="invert-collapses channel-bar">#{channelName(reminder)}</div>}
<Name />
<div class="hide-button-bar">
<button class="button hide-box" onClick={toggleCollapsed}>
<span class="is-sr-only">Hide reminder</span>
<i class="fas fa-chevron-down"></i>
</button>
</div>
</div>
);
};

View File

@ -0,0 +1,67 @@
import { useGuild } from "../../App/useGuild";
import { useReminder } from "../ReminderContext";
import { useQuery } from "react-query";
import { fetchGuildChannels, Reminder } from "../../../api";
import { useCallback } from "preact/hooks";
import { DateTime } from "luxon";
import { Name } from "../Name";
export const Guild = ({ toggleCollapsed }) => {
const guild = useGuild();
const [reminder] = useReminder();
const { isSuccess, data: guildChannels } = useQuery(fetchGuildChannels(guild));
const channelName = useCallback(
(reminder: Reminder) => {
const channel = guildChannels.find((c) => c.id === reminder.channel);
return channel === undefined ? "" : channel.name;
},
[guildChannels],
);
let days, hours, minutes, seconds;
seconds = Math.floor(
DateTime.fromISO(reminder.utc_time, { zone: "UTC" }).diffNow("seconds").seconds,
);
[days, seconds] = [Math.floor(seconds / 86400), seconds % 86400];
[hours, seconds] = [Math.floor(seconds / 3600), seconds % 3600];
[minutes, seconds] = [Math.floor(seconds / 60), seconds % 60];
let string;
if (days !== 0) {
if (hours !== 0) {
string = `${days} days, ${hours} hours`;
} else {
string = `${days} days`;
}
} else if (hours !== 0) {
if (minutes !== 0) {
string = `${hours} hours, ${minutes} minutes`;
} else {
string = `${hours} hours`;
}
} else if (minutes !== 0) {
if (seconds !== 0) {
string = `${minutes} minutes, ${seconds} seconds`;
} else {
string = `${minutes} minutes`;
}
} else {
string = `${seconds} seconds`;
}
return (
<div class="columns is-mobile column reminder-topbar">
{isSuccess && <div class="invert-collapses channel-bar">#{channelName(reminder)}</div>}
<Name />
<div class="invert-collapses time-bar">in {string}</div>
<div class="hide-button-bar">
<button class="button hide-box" onClick={toggleCollapsed}>
<span class="is-sr-only">Hide reminder</span>
<i class="fas fa-chevron-down"></i>
</button>
</div>
</div>
);
};

View File

@ -0,0 +1,51 @@
import { Name } from "../Name";
import { DateTime } from "luxon";
import { useReminder } from "../ReminderContext";
export const User = ({ toggleCollapsed }) => {
const [reminder] = useReminder();
let days, hours, minutes, seconds;
seconds = Math.floor(
DateTime.fromISO(reminder.utc_time, { zone: "UTC" }).diffNow("seconds").seconds,
);
[days, seconds] = [Math.floor(seconds / 86400), seconds % 86400];
[hours, seconds] = [Math.floor(seconds / 3600), seconds % 3600];
[minutes, seconds] = [Math.floor(seconds / 60), seconds % 60];
let string;
if (days !== 0) {
if (hours !== 0) {
string = `${days} days, ${hours} hours`;
} else {
string = `${days} days`;
}
} else if (hours !== 0) {
if (minutes !== 0) {
string = `${hours} hours, ${minutes} minutes`;
} else {
string = `${hours} hours`;
}
} else if (minutes !== 0) {
if (seconds !== 0) {
string = `${minutes} minutes, ${seconds} seconds`;
} else {
string = `${minutes} minutes`;
}
} else {
string = `${seconds} seconds`;
}
return (
<div class="columns is-mobile column reminder-topbar">
<Name />
<div class="invert-collapses time-bar">in {string}</div>
<div class="hide-button-bar">
<button class="button hide-box" onClick={toggleCollapsed}>
<span class="is-sr-only">Hide reminder</span>
<i class="fas fa-chevron-down"></i>
</button>
</div>
</div>
);
};

View File

@ -0,0 +1,13 @@
import { useGuild } from "../../App/useGuild";
import { Guild } from "./Guild";
import { User } from "./User";
export const TopBar = ({ toggleCollapsed }) => {
const guild = useGuild();
if (guild) {
return <Guild toggleCollapsed={toggleCollapsed} />;
} else {
return <User toggleCollapsed={toggleCollapsed} />;
}
};

View File

@ -1,9 +1,11 @@
import { useReminder } from "./ReminderContext";
import { useGuild } from "../App/useGuild";
export const Username = () => {
const guild = useGuild();
const [reminder, setReminder] = useReminder();
return (
return guild ? (
<div class="discord-message-header">
<label class="is-sr-only">Username Override</label>
<input
@ -20,5 +22,9 @@ export const Username = () => {
}}
></input>
</div>
) : (
<div class="discord-message-header">
<span class="discord-username">Reminder Bot</span>
</div>
);
};

View File

@ -0,0 +1,28 @@
.time-bar {
display: flex;
justify-content: center;
flex-direction: column;
font-style: italic;
}
.tribute-container {
background-color: #2b2d31;
color: #fff;
border-radius: 8px;
margin: 4px;
padding: 4px;
box-shadow: 0 0 5px 0 rgba(0,0,0,0.75);
.highlight {
background-color: #35373c;
}
li {
padding: 8px 12px;
border-radius: 8px;
}
}
textarea.autoresize {
resize: vertical !important;
}

View File

@ -4,16 +4,19 @@ import { MobileSidebar } from "./MobileSidebar";
import { Brand } from "./Brand";
import { Wave } from "./Wave";
import { GuildEntry } from "./GuildEntry";
import { fetchUserGuilds, GuildInfo } from "../../api";
import { fetchUserGuilds, fetchUserInfo, GuildInfo } from "../../api";
import { TimezonePicker } from "../TimezonePicker";
import "./style.scss";
import "./styles.scss";
import { Link, useLocation } from "wouter";
type ContentProps = {
guilds: GuildInfo[];
};
const SidebarContent = ({ guilds }: ContentProps) => {
const guildEntries = guilds.map((guild) => <GuildEntry guild={guild}></GuildEntry>);
const guildEntries = guilds.map((guild) => <GuildEntry guild={guild} />);
const [loc] = useLocation();
const { data: userInfo } = useQuery({ ...fetchUserInfo() });
return (
<>
@ -22,9 +25,29 @@ const SidebarContent = ({ guilds }: ContentProps) => {
</a>
<Wave />
<aside class="menu">
<ul class="menu-list">
<li>
<Link
class={loc.startsWith("/@me") ? "is-active switch-pane" : "switch-pane"}
data-pane="guild"
href={"/@me/reminders"}
>
<>
<span class="guild-name">@{userInfo?.name || "unknown"}</span>
</>
</Link>
</li>
</ul>
<p class="menu-label">Servers</p>
<ul class="menu-list guildList">{guildEntries}</ul>
<div class="aside-footer">
<div
class="aside-footer"
style={{
position: "sticky",
bottom: "0px",
backgroundColor: "rgb(54, 54, 54)",
}}
>
<p class="menu-label">Options</p>
<ul class="menu-list">
<li>

View File

@ -0,0 +1,107 @@
import { useQuery } from "react-query";
import { fetchUserReminders } from "../../api";
import { EditReminder } from "../Reminder/EditReminder";
import { CreateReminder } from "../Reminder/CreateReminder";
import { useState } from "preact/hooks";
import { Loader } from "../Loader";
enum Sort {
Time = "time",
Name = "name",
}
export const UserReminders = () => {
const {
isSuccess,
isFetching,
isFetched,
data: guildReminders,
} = useQuery(fetchUserReminders());
const [collapsed, setCollapsed] = useState(false);
const [sort, setSort] = useState(Sort.Time);
return (
<>
{!isFetched && <Loader />}
<div style={{ margin: "0 12px 12px 12px" }}>
<strong>Create Reminder</strong>
<div id={"reminderCreator"}>
<CreateReminder />
</div>
<br></br>
<div class={"field"}>
<div class={"columns is-mobile"}>
<div class={"column"}>
<strong>Reminders</strong>
</div>
<div class={"column is-narrow"}>
<div class="control has-icons-left">
<div class="select is-small">
<select
id="orderBy"
onInput={(ev) => {
setSort(ev.currentTarget.value as Sort);
}}
>
<option value={Sort.Time} selected={sort == Sort.Time}>
Time
</option>
<option value={Sort.Name} selected={sort == Sort.Name}>
Name
</option>
</select>
</div>
<div class="icon is-small is-left">
<i class="fas fa-sort-amount-down"></i>
</div>
</div>
</div>
<div class={"column is-narrow"}>
<div class="control has-icons-left">
<div class="select is-small">
<select
id="expandAll"
onInput={(ev) => {
if (ev.currentTarget.value === "expand") {
setCollapsed(false);
} else if (ev.currentTarget.value === "collapse") {
setCollapsed(true);
}
}}
>
<option value="" selected></option>
<option value="expand">Expand All</option>
<option value="collapse">Collapse All</option>
</select>
</div>
<div class="icon is-small is-left">
<i class="fas fa-expand-arrows"></i>
</div>
</div>
</div>
</div>
</div>
<div id={"guildReminders"} className={isFetching ? "loading" : ""}>
{isSuccess &&
guildReminders
.sort((r1, r2) => {
if (sort === Sort.Time) {
return r1.utc_time > r2.utc_time ? 1 : -1;
} else {
return r1.name > r2.name ? 1 : -1;
}
})
.map((reminder) => (
<EditReminder
key={reminder.uid}
reminder={reminder}
globalCollapse={collapsed}
/>
))}
</div>
</div>
</>
);
};

View File

@ -0,0 +1,9 @@
import { UserReminders } from "./UserReminders";
export const User = () => {
return (
<>
<UserReminders />
</>
);
};

View File

@ -1,4 +1,4 @@
use chrono::NaiveDateTime;
use chrono::DateTime;
use serde::{Deserialize, Serialize};
use crate::{
@ -24,10 +24,10 @@ impl Recordable for Options {
let parsed = natural_parser(&until, &timezone.to_string()).await;
if let Some(timestamp) = parsed {
match NaiveDateTime::from_timestamp_opt(timestamp, 0) {
match DateTime::from_timestamp(timestamp, 0) {
Some(dt) => {
channel.paused = true;
channel.paused_until = Some(dt);
channel.paused_until = Some(dt.naive_utc());
channel.commit_changes(&ctx.data().database).await;

View File

@ -1,10 +1,11 @@
use poise::serenity_prelude::ActivityData;
use poise::{
serenity_prelude as serenity,
serenity_prelude::{CreateEmbed, CreateMessage, FullEvent},
serenity_prelude::{ActivityData, CreateEmbed, CreateMessage, FullEvent},
};
use crate::{component_models::ComponentDataModel, Data, Error, THEME_COLOR};
use crate::{
component_models::ComponentDataModel, metrics::COMMAND_COUNTER, Data, Error, THEME_COLOR,
};
pub async fn listener(
ctx: &serenity::Context,
@ -67,6 +68,10 @@ To stay up to date on the latest features and fixes, join our [Discord](https://
component_model.act(ctx, data, &component).await;
}
if let Some(command) = interaction.clone().command() {
COMMAND_COUNTER.with_label_values(&[command.data.name.as_str()]).inc();
}
}
_ => {}
}

View File

@ -12,12 +12,15 @@ mod event_handlers;
#[cfg(not(test))]
mod hooks;
mod interval_parser;
mod metrics;
#[cfg(not(test))]
mod models;
mod postman;
#[cfg(test)]
mod test;
mod time_parser;
mod utils;
mod web;
use std::{
collections::HashMap,
@ -28,7 +31,7 @@ use std::{
};
use chrono_tz::Tz;
use log::{error, warn};
use log::warn;
use poise::serenity_prelude::{
model::{
gateway::GatewayIntents,
@ -39,6 +42,7 @@ use poise::serenity_prelude::{
use sqlx::{MySql, Pool};
use tokio::sync::{broadcast, broadcast::Sender, RwLock};
use crate::metrics::init_metrics;
#[cfg(test)]
use crate::test::TestContext;
#[cfg(not(test))]
@ -206,6 +210,10 @@ async fn _main(tx: Sender<()>) -> Result<(), Box<dyn StdError + Send + Sync>> {
..Default::default()
};
// Start metrics
init_metrics();
tokio::spawn(async { metrics::serve().await });
let database =
Pool::connect(&env::var("DATABASE_URL").expect("No database URL provided")).await.unwrap();
@ -249,7 +257,7 @@ async fn _main(tx: Sender<()>) -> Result<(), Box<dyn StdError + Send + Sync>> {
match postman::initialize(kill_recv, ctx1, &pool1).await {
Ok(_) => {}
Err(e) => {
error!("postman exiting: {}", e);
panic!("postman exiting: {}", e);
}
};
});
@ -259,7 +267,7 @@ async fn _main(tx: Sender<()>) -> Result<(), Box<dyn StdError + Send + Sync>> {
if !run_settings.contains("web") {
tokio::spawn(async move {
reminder_web::initialize(kill_tx, ctx2, pool2).await.unwrap();
web::initialize(kill_tx, ctx2, pool2).await.unwrap();
});
} else {
warn!("Not running web");

46
src/metrics.rs Normal file
View File

@ -0,0 +1,46 @@
use axum::{routing::get, Router};
use lazy_static::lazy_static;
use log::warn;
use prometheus::{IntCounterVec, Opts, Registry};
lazy_static! {
pub static ref REGISTRY: Registry = Registry::new();
pub static ref REQUEST_COUNTER: IntCounterVec =
IntCounterVec::new(Opts::new("requests", "Web requests"), &["method", "status", "route"])
.unwrap();
pub static ref REMINDER_COUNTER: IntCounterVec =
IntCounterVec::new(Opts::new("reminders_sent", "Reminders sent"), &["id", "channel"])
.unwrap();
pub static ref REMINDER_FAIL_COUNTER: IntCounterVec = IntCounterVec::new(
Opts::new("reminders_failed", "Reminders failed"),
&["id", "channel", "error"]
)
.unwrap();
pub static ref COMMAND_COUNTER: IntCounterVec =
IntCounterVec::new(Opts::new("commands", "Commands used"), &["command"]).unwrap();
}
pub fn init_metrics() {
REGISTRY.register(Box::new(REQUEST_COUNTER.clone())).unwrap();
REGISTRY.register(Box::new(REMINDER_COUNTER.clone())).unwrap();
REGISTRY.register(Box::new(REMINDER_FAIL_COUNTER.clone())).unwrap();
REGISTRY.register(Box::new(COMMAND_COUNTER.clone())).unwrap();
}
pub async fn serve() {
let app = Router::new().route("/metrics", get(metrics));
let listener = tokio::net::TcpListener::bind("localhost:31756").await.unwrap();
axum::serve(listener, app).await.unwrap();
}
async fn metrics() -> String {
let encoder = prometheus::TextEncoder::new();
let res_custom = encoder.encode_to_string(&REGISTRY.gather());
res_custom.unwrap_or_else(|e| {
warn!("Error encoding metrics: {:?}", e);
String::new()
})
}

View File

@ -1,9 +1,13 @@
use chrono::NaiveDateTime;
use poise::serenity_prelude::model::channel::Channel;
use poise::serenity_prelude::{model::channel::Channel, CacheHttp, ChannelId, CreateWebhook};
use secrecy::ExposeSecret;
use sqlx::MySqlPool;
use crate::{consts::DEFAULT_AVATAR, Error};
pub struct ChannelData {
pub id: u32,
pub channel: u64,
pub name: Option<String>,
pub nudge: i16,
pub blacklisted: bool,
@ -22,7 +26,12 @@ impl ChannelData {
if let Ok(c) = sqlx::query_as_unchecked!(
Self,
"SELECT id, name, nudge, blacklisted, webhook_id, webhook_token, paused, paused_until FROM channels WHERE channel = ?",
"
SELECT id, channel, name, nudge, blacklisted, webhook_id, webhook_token, paused,
paused_until
FROM channels
WHERE channel = ?
",
channel_id
)
.fetch_one(pool)
@ -32,7 +41,8 @@ impl ChannelData {
} else {
let props = channel.to_owned().guild().map(|g| (g.guild_id.get().to_owned(), g.name));
let (guild_id, channel_name) = if let Some((a, b)) = props { (Some(a), Some(b)) } else { (None, None) };
let (guild_id, channel_name) =
if let Some((a, b)) = props { (Some(a), Some(b)) } else { (None, None) };
sqlx::query!(
"INSERT IGNORE INTO channels (channel, name, guild_id) VALUES (?, ?, (SELECT id FROM guilds WHERE guild = ?))",
@ -46,7 +56,9 @@ impl ChannelData {
Ok(sqlx::query_as_unchecked!(
Self,
"
SELECT id, name, nudge, blacklisted, webhook_id, webhook_token, paused, paused_until FROM channels WHERE channel = ?
SELECT id, channel, name, nudge, blacklisted, webhook_id, webhook_token, paused, paused_until
FROM channels
WHERE channel = ?
",
channel_id
)
@ -58,8 +70,16 @@ SELECT id, name, nudge, blacklisted, webhook_id, webhook_token, paused, paused_u
pub async fn commit_changes(&self, pool: &MySqlPool) {
sqlx::query!(
"
UPDATE channels SET name = ?, nudge = ?, blacklisted = ?, webhook_id = ?, webhook_token = ?, paused = ?, paused_until \
= ? WHERE id = ?
UPDATE channels
SET
name = ?,
nudge = ?,
blacklisted = ?,
webhook_id = ?,
webhook_token = ?,
paused = ?,
paused_until = ?
WHERE id = ?
",
self.name,
self.nudge,
@ -74,4 +94,24 @@ UPDATE channels SET name = ?, nudge = ?, blacklisted = ?, webhook_id = ?, webhoo
.await
.unwrap();
}
pub async fn ensure_webhook(
&mut self,
ctx: impl CacheHttp,
pool: &MySqlPool,
) -> Result<(), Error> {
if self.webhook_id.is_none() || self.webhook_token.is_none() {
let guild_channel = ChannelId::new(self.channel);
let webhook = guild_channel
.create_webhook(ctx.http(), CreateWebhook::new("Reminder").avatar(&*DEFAULT_AVATAR))
.await?;
self.webhook_id = Some(webhook.id.get().to_owned());
self.webhook_token = webhook.token.map(|s| s.expose_secret().clone());
self.commit_changes(pool).await;
}
Ok(())
}
}

View File

@ -1,21 +1,15 @@
use std::collections::HashSet;
use chrono::{Duration, NaiveDateTime, Utc};
use chrono::{DateTime, NaiveDateTime, TimeDelta, Utc};
use chrono_tz::Tz;
use poise::serenity_prelude::{
http::CacheHttp,
model::{
channel::GuildChannel,
id::{ChannelId, GuildId, UserId},
webhook::Webhook,
},
ChannelType, CreateWebhook, Result as SerenityResult,
model::id::{ChannelId, GuildId, UserId},
ChannelType,
};
use secrecy::ExposeSecret;
use sqlx::MySqlPool;
use crate::{
consts::{DAY, DEFAULT_AVATAR, MAX_TIME, MIN_INTERVAL},
consts::{DAY, MAX_TIME, MIN_INTERVAL},
interval_parser::Interval,
models::{
channel_data::ChannelData,
@ -25,25 +19,23 @@ use crate::{
Context,
};
async fn create_webhook(
ctx: impl CacheHttp,
channel: GuildChannel,
name: impl Into<String>,
) -> SerenityResult<Webhook> {
channel.create_webhook(ctx.http(), CreateWebhook::new(name).avatar(&*DEFAULT_AVATAR)).await
#[derive(Hash, PartialEq, Eq, Copy, Clone)]
pub struct ChannelWithThread {
pub channel_id: u64,
pub thread_id: Option<u64>,
}
#[derive(Hash, PartialEq, Eq)]
pub enum ReminderScope {
User(u64),
Channel(u64),
Channel(ChannelWithThread),
}
impl ReminderScope {
pub fn mention(&self) -> String {
match self {
Self::User(id) => format!("<@{}>", id),
Self::Channel(id) => format!("<#{}>", id),
Self::Channel(c) => format!("<#{}>", c.channel_id),
}
}
}
@ -81,7 +73,7 @@ impl ReminderBuilder {
match queried_time.utc_time {
Some(utc_time) => {
if utc_time < (Utc::now() - Duration::seconds(60)).naive_local() {
if utc_time < (Utc::now() - TimeDelta::try_minutes(1).unwrap()).naive_local() {
Err(ReminderError::PastTime)
} else {
sqlx::query!(
@ -89,6 +81,7 @@ impl ReminderBuilder {
INSERT INTO reminders (
`uid`,
`channel_id`,
`thread_id`,
`utc_time`,
`timezone`,
`interval_seconds`,
@ -101,11 +94,12 @@ impl ReminderBuilder {
`attachment`,
`set_by`
) VALUES (
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
)
",
self.uid,
self.channel,
self.thread_id,
utc_time,
self.timezone,
self.interval_seconds,
@ -171,7 +165,7 @@ impl<'a> MultiReminderBuilder<'a> {
}
pub fn time<T: Into<i64>>(mut self, time: T) -> Self {
if let Some(utc_time) = NaiveDateTime::from_timestamp_opt(time.into(), 0) {
if let Some(utc_time) = DateTime::from_timestamp(time.into(), 0).map(|d| d.naive_utc()) {
self.utc_time = utc_time;
}
@ -179,7 +173,8 @@ impl<'a> MultiReminderBuilder<'a> {
}
pub fn expires<T: Into<i64>>(mut self, time: Option<T>) -> Self {
self.expires = time.map(|t| NaiveDateTime::from_timestamp_opt(t.into(), 0)).flatten();
self.expires =
time.map(|t| DateTime::from_timestamp(t.into(), 0)).flatten().map(|d| d.naive_utc());
self
}
@ -218,7 +213,6 @@ impl<'a> MultiReminderBuilder<'a> {
errors.insert(ReminderError::LongInterval);
} else {
for scope in self.scopes {
let thread_id = None;
let db_channel_id = match scope {
ReminderScope::User(user_id) => {
if let Ok(user) = UserId::new(user_id).to_user(&self.ctx).await {
@ -238,34 +232,34 @@ impl<'a> MultiReminderBuilder<'a> {
{
Err(ReminderError::UserBlockedDm)
} else {
Ok(user_data.dm_channel)
Ok((user_data.dm_channel, None))
}
} else {
Ok(user_data.dm_channel)
Ok((user_data.dm_channel, None))
}
} else {
Err(ReminderError::InvalidTag)
}
}
ReminderScope::Channel(channel_id) => {
let channel =
ChannelId::new(channel_id).to_channel(&self.ctx).await.unwrap();
ReminderScope::Channel(channel_with_thread) => {
let channel = ChannelId::new(channel_with_thread.channel_id)
.to_channel(&self.ctx)
.await
.unwrap();
if let Some(mut guild_channel) = channel.clone().guild() {
if let Some(guild_channel) = channel.clone().guild() {
if Some(guild_channel.guild_id) != self.guild_id {
Err(ReminderError::InvalidTag)
} else {
let mut channel_data = if guild_channel.kind
== ChannelType::PublicThread
{
// fixme jesus christ
let parent = guild_channel
.parent_id
.unwrap()
.to_channel(&self.ctx)
.await
.unwrap();
guild_channel = parent.clone().guild().unwrap();
ChannelData::from_channel(&parent, &self.ctx.data().database)
.await
.unwrap()
@ -275,28 +269,13 @@ impl<'a> MultiReminderBuilder<'a> {
.unwrap()
};
if channel_data.webhook_id.is_none()
|| channel_data.webhook_token.is_none()
match channel_data
.ensure_webhook(&self.ctx, &self.ctx.data().database)
.await
.map_err(|e| ReminderError::DiscordError(e.to_string()))
{
match create_webhook(&self.ctx, guild_channel, "Reminder").await
{
Ok(webhook) => {
channel_data.webhook_id =
Some(webhook.id.get().to_owned());
channel_data.webhook_token =
webhook.token.map(|s| s.expose_secret().clone());
channel_data
.commit_changes(&self.ctx.data().database)
.await;
Ok(channel_data.id)
}
Err(e) => Err(ReminderError::DiscordError(e.to_string())),
}
} else {
Ok(channel_data.id)
Ok(()) => Ok((channel_data.id, channel_with_thread.thread_id)),
Err(e) => Err(e),
}
}
} else {
@ -306,11 +285,11 @@ impl<'a> MultiReminderBuilder<'a> {
};
match db_channel_id {
Ok(c) => {
Ok((channel, thread_id)) => {
let builder = ReminderBuilder {
pool: self.ctx.data().database.clone(),
uid: generate_uid(),
channel: c,
channel,
thread_id,
utc_time: self.utc_time,
timezone: self.timezone.to_string(),

View File

@ -13,7 +13,7 @@ use chrono_tz::Tz;
use poise::{
serenity_prelude::{
model::id::{ChannelId, GuildId, UserId},
ButtonStyle, Cache, CreateActionRow, CreateButton, CreateEmbed, ReactionType,
ButtonStyle, Cache, ChannelType, CreateActionRow, CreateButton, CreateEmbed, ReactionType,
},
CreateReply,
};
@ -26,7 +26,7 @@ use crate::{
interval_parser::parse_duration,
models::{
reminder::{
builder::{MultiReminderBuilder, ReminderScope},
builder::{ChannelWithThread, MultiReminderBuilder, ReminderScope},
content::Content,
errors::ReminderError,
},
@ -38,6 +38,7 @@ use crate::{
};
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct Reminder {
pub id: u32,
pub uid: String,
@ -406,7 +407,8 @@ pub async fn create_reminder(
let id = i.get(2).unwrap().as_str().parse::<u64>().unwrap();
if pref == "#" {
ReminderScope::Channel(id)
let channel_with_thread = ChannelWithThread { channel_id: id, thread_id: None };
ReminderScope::Channel(channel_with_thread)
} else {
ReminderScope::User(id)
}
@ -481,8 +483,23 @@ pub async fn create_reminder(
let list = channels.map(|arg| parse_mention_list(&arg)).unwrap_or_default();
if list.is_empty() {
if ctx.guild_id().is_some() {
vec![ReminderScope::Channel(ctx.channel_id().get())]
if let Some(channel) = ctx.guild_channel().await {
if channel.kind == ChannelType::PublicThread
|| channel.kind == ChannelType::PrivateThread
{
let parent = channel.parent_id.unwrap();
let channel_with_threads = ChannelWithThread {
channel_id: parent.get(),
thread_id: Some(ctx.channel_id().get()),
};
vec![ReminderScope::Channel(channel_with_threads)]
} else {
let channel_with_threads = ChannelWithThread {
channel_id: ctx.channel_id().get(),
thread_id: None,
};
vec![ReminderScope::Channel(channel_with_threads)]
}
} else {
vec![ReminderScope::User(ctx.author().id.get())]
}

View File

@ -4,6 +4,7 @@ use sqlx::MySqlPool;
pub struct Timer {
pub name: String,
pub start_time: DateTime<Utc>,
#[allow(dead_code)]
pub owner: u64,
}

View File

@ -7,6 +7,7 @@ use crate::consts::LOCAL_TIMEZONE;
pub struct UserData {
pub id: u32,
#[allow(dead_code)]
pub user: u64,
pub dm_channel: u32,
pub timezone: String,
@ -22,7 +23,7 @@ impl UserData {
match sqlx::query!(
"
SELECT IFNULL(timezone, 'UTC') AS timezone FROM users WHERE user = ?
SELECT IFNULL(timezone, 'UTC') AS timezone FROM users WHERE user = ?
",
user_id
)

View File

@ -3,7 +3,7 @@ mod sender;
use std::env;
use log::{info, warn};
use serenity::client::Context;
use poise::serenity_prelude::client::Context;
use sqlx::{Executor, MySql};
use tokio::{
sync::broadcast::Receiver,

View File

@ -1,13 +1,11 @@
use std::env;
use chrono::{DateTime, Days, Duration, Months};
use chrono::{DateTime, Days, Months, TimeDelta};
use chrono_tz::Tz;
use lazy_static::lazy_static;
use log::{error, info, warn};
use num_integer::Integer;
use regex::{Captures, Regex};
use serde::Deserialize;
use serenity::{
use poise::serenity_prelude::{
all::{CreateAttachment, CreateEmbedFooter},
builder::{CreateEmbed, CreateEmbedAuthor, CreateMessage, ExecuteWebhook},
http::{CacheHttp, Http, HttpError},
@ -18,6 +16,8 @@ use serenity::{
},
Error, Result,
};
use regex::{Captures, Regex};
use serde::Deserialize;
use sqlx::{
types::{
chrono::{NaiveDateTime, Utc},
@ -26,7 +26,10 @@ use sqlx::{
Executor,
};
use crate::Database;
use crate::{
metrics::{REMINDER_COUNTER, REMINDER_FAIL_COUNTER},
Database,
};
lazy_static! {
pub static ref TIMEFROM_REGEX: Regex =
@ -66,15 +69,15 @@ pub fn substitute(string: &str) -> String {
let format = caps.name("format").map(|m| m.as_str());
if let (Some(final_time), Some(format)) = (final_time, format) {
match NaiveDateTime::from_timestamp_opt(final_time, 0) {
match DateTime::from_timestamp(final_time, 0) {
Some(dt) => {
let now = Utc::now().naive_utc();
let now = Utc::now();
let difference = {
if now < dt {
dt - Utc::now().naive_utc()
dt - Utc::now()
} else {
Utc::now().naive_utc() - dt
Utc::now() - dt
}
};
@ -232,6 +235,7 @@ pub struct Reminder {
id: u32,
channel_id: u64,
thread_id: Option<u64>,
webhook_id: Option<u64>,
webhook_token: Option<String>,
@ -262,58 +266,59 @@ impl Reminder {
match sqlx::query_as_unchecked!(
Reminder,
r#"
SELECT
reminders.`id` AS id,
SELECT
reminders.`id` AS id,
channels.`channel` AS channel_id,
channels.`webhook_id` AS webhook_id,
channels.`webhook_token` AS webhook_token,
channels.`channel` AS channel_id,
reminders.`thread_id` AS thread_id,
channels.`webhook_id` AS webhook_id,
channels.`webhook_token` AS webhook_token,
channels.`paused` AS 'channel_paused',
channels.`paused_until` AS 'channel_paused_until',
reminders.`enabled` AS 'enabled',
channels.`paused` AS 'channel_paused',
channels.`paused_until` AS 'channel_paused_until',
reminders.`enabled` AS 'enabled',
reminders.`tts` AS tts,
reminders.`pin` AS pin,
reminders.`content` AS content,
reminders.`attachment` AS attachment,
reminders.`attachment_name` AS attachment_name,
reminders.`tts` AS tts,
reminders.`pin` AS pin,
reminders.`content` AS content,
reminders.`attachment` AS attachment,
reminders.`attachment_name` AS attachment_name,
reminders.`utc_time` AS 'utc_time',
reminders.`timezone` AS timezone,
reminders.`restartable` AS restartable,
reminders.`expires` AS 'expires',
reminders.`interval_seconds` AS 'interval_seconds',
reminders.`interval_days` AS 'interval_days',
reminders.`interval_months` AS 'interval_months',
reminders.`utc_time` AS 'utc_time',
reminders.`timezone` AS timezone,
reminders.`restartable` AS restartable,
reminders.`expires` AS 'expires',
reminders.`interval_seconds` AS 'interval_seconds',
reminders.`interval_days` AS 'interval_days',
reminders.`interval_months` AS 'interval_months',
reminders.`avatar` AS avatar,
reminders.`username` AS username
FROM
reminders
INNER JOIN
channels
ON
reminders.channel_id = channels.id
WHERE
reminders.`status` = 'pending' AND
reminders.`id` IN (
SELECT
MIN(id)
FROM
reminders
WHERE
reminders.`utc_time` <= NOW() AND
`status` = 'pending' AND
(
reminders.`interval_seconds` IS NOT NULL
OR reminders.`interval_months` IS NOT NULL
OR reminders.`interval_days` IS NOT NULL
OR reminders.enabled
)
GROUP BY channel_id
)
"#,
reminders.`avatar` AS avatar,
reminders.`username` AS username
FROM
reminders
INNER JOIN
channels
ON
reminders.channel_id = channels.id
WHERE
reminders.`status` = 'pending' AND
reminders.`id` IN (
SELECT
MIN(id)
FROM
reminders
WHERE
reminders.`utc_time` <= NOW() AND
`status` = 'pending' AND
(
reminders.`interval_seconds` IS NOT NULL
OR reminders.`interval_months` IS NOT NULL
OR reminders.`interval_days` IS NOT NULL
OR reminders.enabled
)
GROUP BY channel_id
)
"#,
)
.fetch_all(pool)
.await
@ -337,7 +342,9 @@ WHERE
async fn reset_webhook(&self, pool: impl Executor<'_, Database = Database> + Copy) {
let _ = sqlx::query!(
"UPDATE channels SET webhook_id = NULL, webhook_token = NULL WHERE channel = ?",
"
UPDATE channels SET webhook_id = NULL, webhook_token = NULL WHERE channel = ?
",
self.channel_id
)
.execute(pool)
@ -393,7 +400,13 @@ WHERE
}
if let Some(interval) = self.interval_seconds {
updated_reminder_time += Duration::seconds(interval as i64);
updated_reminder_time += TimeDelta::try_seconds(interval as i64)
.unwrap_or_else(|| {
warn!("{}: Could not add {} seconds to a reminder", self.id, interval);
fail_count += 1;
TimeDelta::zero()
});
}
}
@ -432,10 +445,24 @@ WHERE
None => error.to_string(),
};
REMINDER_FAIL_COUNTER
.get_metric_with_label_values(&[
self.id.to_string().as_str(),
self.channel_id.to_string().as_str(),
&message,
])
.map_or_else(|e| warn!("Couldn't count failed reminder: {:?}", e), |c| c.inc());
error!("[Reminder {}] {}", self.id, message);
}
async fn log_success(&self) {}
async fn log_success(&self) {
REMINDER_COUNTER
.get_metric_with_label_values(&[
self.id.to_string().as_str(),
self.channel_id.to_string().as_str(),
])
.map_or_else(|e| warn!("Couldn't count sent reminder: {:?}", e), |c| c.inc());
}
async fn set_sent(&self, pool: impl Executor<'_, Database = Database> + Copy) {
sqlx::query!("UPDATE reminders SET `status` = 'sent' WHERE `id` = ?", self.id)
@ -473,7 +500,11 @@ WHERE
reminder: &Reminder,
embed: Option<CreateEmbed>,
) -> Result<()> {
let channel = ChannelId::new(reminder.channel_id).to_channel(&cache_http).await;
let channel = if let Some(thread_id) = reminder.thread_id {
ChannelId::new(thread_id).to_channel(&cache_http).await
} else {
ChannelId::new(reminder.channel_id).to_channel(&cache_http).await
};
let mut message = CreateMessage::new().content(&reminder.content).tts(reminder.tts);
@ -524,7 +555,14 @@ WHERE
webhook: Webhook,
embed: Option<CreateEmbed>,
) -> Result<()> {
let mut builder = ExecuteWebhook::new().content(&reminder.content).tts(reminder.tts);
let mut builder = if let Some(thread_id) = reminder.thread_id {
ExecuteWebhook::new()
.content(&reminder.content)
.tts(reminder.tts)
.in_thread(thread_id)
} else {
ExecuteWebhook::new().content(&reminder.content).tts(reminder.tts)
};
if let Some(username) = &reminder.username {
if !username.is_empty() {
@ -571,7 +609,9 @@ WHERE
.map_or(true, |inner| inner >= Utc::now().naive_local()))
{
let _ = sqlx::query!(
"UPDATE `channels` SET paused = 0, paused_until = NULL WHERE `channel` = ?",
"
UPDATE `channels` SET paused = 0, paused_until = NULL WHERE `channel` = ?
",
self.channel_id
)
.execute(pool)

View File

@ -1,9 +1,9 @@
use std::collections::HashMap;
use rocket::serde::json::json;
use rocket::{catch, serde::json::json};
use rocket_dyn_templates::Template;
use crate::JsonValue;
use crate::web::JsonValue;
#[catch(403)]
pub(crate) async fn forbidden() -> Template {

View File

@ -20,14 +20,14 @@ pub const DAY: usize = 24 * HOUR;
pub const CHARACTERS: &str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
use std::{collections::HashSet, env, iter::FromIterator};
use std::{collections::HashSet, env};
use lazy_static::lazy_static;
use serenity::builder::CreateAttachment;
lazy_static! {
pub static ref DEFAULT_AVATAR: CreateAttachment = CreateAttachment::bytes(
include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/../assets/webhook.jpg")) as &[u8],
include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/assets/webhook.jpg")) as &[u8],
"webhook.jpg",
);
pub static ref SUBSCRIPTION_ROLES: HashSet<u64> = HashSet::from_iter(

View File

@ -20,6 +20,7 @@ impl Transaction<'_> {
}
#[derive(Debug)]
#[allow(dead_code)]
pub enum TransactionError {
Error(sqlx::Error),
Missing,

27
src/web/metrics.rs Normal file
View File

@ -0,0 +1,27 @@
use rocket::{
fairing::{Fairing, Info, Kind},
Request, Response,
};
use crate::metrics::REQUEST_COUNTER;
pub struct MetricProducer;
#[rocket::async_trait]
impl Fairing for MetricProducer {
fn info(&self) -> Info {
Info { name: "Metrics fairing", kind: Kind::Response }
}
async fn on_response<'r>(&self, req: &'r Request<'_>, resp: &mut Response<'r>) {
if let Some(route) = req.route() {
REQUEST_COUNTER
.with_label_values(&[
req.method().as_str(),
&resp.status().code.to_string(),
&route.uri.to_string(),
])
.inc();
}
}
}

View File

@ -1,6 +1,3 @@
#[macro_use]
extern crate rocket;
mod consts;
#[macro_use]
mod macros;
@ -11,32 +8,37 @@ mod routes;
use std::{env, path::Path};
use log::{error, info, warn};
use oauth2::{basic::BasicClient, AuthUrl, ClientId, ClientSecret, RedirectUrl, TokenUrl};
use rocket::{
fs::FileServer,
http::CookieJar,
serde::json::{json, Value as JsonValue},
tokio::sync::broadcast::Sender,
};
use rocket_dyn_templates::Template;
use serenity::{
use poise::serenity_prelude::{
client::Context,
http::CacheHttp,
model::id::{GuildId, UserId},
};
use rocket::{
catchers,
fs::FileServer,
http::CookieJar,
routes,
serde::json::{json, Value as JsonValue},
tokio::sync::broadcast::Sender,
};
use rocket_dyn_templates::Template;
use sqlx::{MySql, Pool};
use crate::{
use crate::web::{
consts::{CNC_GUILD, DISCORD_OAUTH_AUTHORIZE, DISCORD_OAUTH_TOKEN, SUBSCRIPTION_ROLES},
metrics::{init_metrics, MetricProducer},
metrics::MetricProducer,
};
type Database = MySql;
#[derive(Debug)]
enum Error {
SQLx,
Serenity,
#[allow(unused)]
SQLx(sqlx::Error),
#[allow(unused)]
Serenity(serenity::Error),
}
pub async fn initialize(
@ -66,9 +68,7 @@ pub async fn initialize(
let reqwest_client = reqwest::Client::new();
let static_path =
if Path::new("web/static").exists() { "web/static" } else { "/lib/reminder-rs/static" };
init_metrics();
if Path::new("static").exists() { "static" } else { "/lib/reminder-rs/static" };
rocket::build()
.attach(MetricProducer)
@ -94,7 +94,6 @@ pub async fn initialize(
routes![
routes::cookies,
routes::index,
routes::metrics::metrics,
routes::privacy,
routes::report::report_error,
routes::return_to_same_site,
@ -129,9 +128,13 @@ pub async fn initialize(
routes![
routes::dashboard::dashboard,
routes::dashboard::dashboard_home,
routes::dashboard::api::delete_reminder,
routes::dashboard::api::user::get_user_info,
routes::dashboard::api::user::update_user_info,
routes::dashboard::api::user::get_user_guilds,
routes::dashboard::api::user::get_reminders,
routes::dashboard::api::user::edit_reminder,
routes::dashboard::api::user::create_user_reminder,
routes::dashboard::api::guild::get_guild_info,
routes::dashboard::api::guild::get_guild_channels,
routes::dashboard::api::guild::get_guild_roles,
@ -141,7 +144,6 @@ pub async fn initialize(
routes::dashboard::api::guild::create_guild_reminder,
routes::dashboard::api::guild::get_reminders,
routes::dashboard::api::guild::edit_reminder,
routes::dashboard::api::guild::delete_reminder,
routes::dashboard::export::export_reminders,
routes::dashboard::export::export_reminder_templates,
routes::dashboard::export::export_todos,
@ -149,7 +151,6 @@ pub async fn initialize(
routes::dashboard::export::import_todos,
],
)
.mount("/admin", routes![routes::admin::admin_dashboard_home, routes::admin::bot_data])
.launch()
.await?;

View File

@ -1,4 +1,4 @@
use rocket::{http::CookieJar, serde::json::json, State};
use rocket::{get, http::CookieJar, serde::json::json, State};
use serde::Serialize;
use serenity::{
client::Context,
@ -8,7 +8,7 @@ use serenity::{
},
};
use crate::{check_authorization, routes::JsonResult};
use crate::web::{check_authorization, routes::JsonResult};
#[derive(Serialize)]
struct ChannelInfo {

View File

@ -7,7 +7,7 @@ use std::env;
pub use channels::*;
pub use reminders::*;
use rocket::{http::CookieJar, serde::json::json, State};
use rocket::{get, http::CookieJar, serde::json::json, State};
pub use roles::*;
use serenity::{
client::Context,
@ -15,7 +15,7 @@ use serenity::{
};
pub use templates::*;
use crate::{check_authorization, routes::JsonResult};
use crate::web::{check_authorization, routes::JsonResult};
#[get("/api/guild/<id>")]
pub async fn get_guild_info(id: u64, cookies: &CookieJar<'_>, ctx: &State<Context>) -> JsonResult {

View File

@ -1,5 +1,8 @@
use log::warn;
use rocket::{
get,
http::CookieJar,
patch, post,
serde::json::{json, Json},
State,
};
@ -9,14 +12,12 @@ use serenity::{
};
use sqlx::{MySql, Pool};
use crate::{
use crate::web::{
check_authorization, check_guild_subscription, check_subscription,
consts::MIN_INTERVAL,
guards::transaction::Transaction,
routes::{
dashboard::{
create_database_channel, create_reminder, DeleteReminder, PatchReminder, Reminder,
},
dashboard::{create_database_channel, create_reminder, PatchReminder, Reminder},
JsonResult,
},
Database,
@ -106,7 +107,7 @@ pub async fn get_reminders(
reminders.username,
reminders.utc_time
FROM reminders
LEFT JOIN channels ON channels.id = reminders.channel_id
INNER JOIN channels ON channels.id = reminders.channel_id
WHERE `status` = 'pending' AND FIND_IN_SET(channels.channel, ?)",
channels
)
@ -364,27 +365,3 @@ pub async fn edit_reminder(
}
}
}
#[delete("/api/guild/<id>/reminders", data = "<reminder>")]
pub async fn delete_reminder(
cookies: &CookieJar<'_>,
id: u64,
reminder: Json<DeleteReminder>,
ctx: &State<Context>,
pool: &State<Pool<MySql>>,
) -> JsonResult {
check_authorization(cookies, ctx.inner(), id).await?;
match sqlx::query!("UPDATE reminders SET `status` = 'deleted' WHERE uid = ?", reminder.uid)
.execute(pool.inner())
.await
{
Ok(_) => Ok(json!({})),
Err(e) => {
warn!("Error in `delete_reminder`: {:?}", e);
Err(json!({"error": "Could not delete reminder"}))
}
}
}

View File

@ -1,8 +1,9 @@
use rocket::{http::CookieJar, serde::json::json, State};
use log::warn;
use rocket::{get, http::CookieJar, serde::json::json, State};
use serde::Serialize;
use serenity::client::Context;
use crate::{check_authorization, routes::JsonResult};
use crate::web::{check_authorization, routes::JsonResult};
#[derive(Serialize)]
struct RoleInfo {

View File

@ -1,12 +1,15 @@
use log::warn;
use rocket::{
delete, get,
http::CookieJar,
post,
serde::json::{json, Json},
State,
};
use serenity::client::Context;
use sqlx::{MySql, Pool};
use crate::{
use crate::web::{
check_authorization,
consts::{
MAX_CONTENT_LENGTH, MAX_EMBED_AUTHOR_LENGTH, MAX_EMBED_DESCRIPTION_LENGTH,

View File

@ -0,0 +1,42 @@
pub mod guild;
pub mod user;
use log::warn;
use rocket::{
delete,
http::CookieJar,
serde::json::{json, Json},
State,
};
use sqlx::{MySql, Pool};
use crate::web::routes::{dashboard::DeleteReminder, JsonResult};
#[delete("/api/reminders", data = "<reminder>")]
pub async fn delete_reminder(
cookies: &CookieJar<'_>,
reminder: Json<DeleteReminder>,
pool: &State<Pool<MySql>>,
) -> JsonResult {
match cookies.get_private("userid").map(|c| c.value().parse::<u64>().ok()).flatten() {
Some(_) => {
match sqlx::query!(
"UPDATE reminders SET `status` = 'deleted' WHERE uid = ?",
reminder.uid
)
.execute(pool.inner())
.await
{
Ok(_) => Ok(json!({})),
Err(e) => {
warn!("Error in `delete_reminder`: {:?}", e);
Err(json!({"error": "Could not delete reminder"}))
}
}
}
None => Err(json!({"error": "User not authorized"})),
}
}

View File

@ -1,5 +1,7 @@
use log::warn;
use reqwest::Client;
use rocket::{
get,
http::CookieJar,
serde::json::{json, Value as JsonValue},
State,
@ -7,7 +9,7 @@ use rocket::{
use serde::{Deserialize, Serialize};
use serenity::model::{id::GuildId, permissions::Permissions};
use crate::consts::DISCORD_API;
use crate::web::consts::DISCORD_API;
#[derive(Serialize)]
struct GuildInfo {

View File

@ -1,11 +1,16 @@
mod guilds;
mod models;
mod reminders;
use std::env;
use chrono_tz::Tz;
pub use guilds::*;
pub use reminders::*;
use rocket::{
get,
http::CookieJar,
patch,
serde::json::{json, Json, Value as JsonValue},
State,
};
@ -54,7 +59,7 @@ pub async fn get_user_info(
let user_info = UserInfo {
name: cookies
.get_private("username")
.map_or("DiscordUser#0000".to_string(), |c| c.value().to_string()),
.map_or("Discord User".to_string(), |c| c.value().to_string()),
patreon: member_res.map_or(false, |member| {
member
.roles

View File

@ -0,0 +1,231 @@
use chrono::{naive::NaiveDateTime, Utc};
use futures::TryFutureExt;
use log::warn;
use rocket::serde::json::json;
use serde::{Deserialize, Serialize};
use serenity::{client::Context, model::id::UserId};
use sqlx::types::Json;
use crate::web::{
check_subscription,
consts::{
DAY, MAX_CONTENT_LENGTH, MAX_EMBED_AUTHOR_LENGTH, MAX_EMBED_DESCRIPTION_LENGTH,
MAX_EMBED_FIELDS, MAX_EMBED_FIELD_TITLE_LENGTH, MAX_EMBED_FIELD_VALUE_LENGTH,
MAX_EMBED_FOOTER_LENGTH, MAX_EMBED_TITLE_LENGTH, MAX_NAME_LENGTH, MAX_URL_LENGTH,
MIN_INTERVAL,
},
guards::transaction::Transaction,
routes::{
dashboard::{create_database_channel, generate_uid, name_default, Attachment, EmbedField},
JsonResult,
},
Error,
};
#[derive(Serialize, Deserialize)]
pub struct Reminder {
pub attachment: Option<Attachment>,
pub attachment_name: Option<String>,
pub content: String,
pub embed_author: String,
pub embed_author_url: Option<String>,
pub embed_color: u32,
pub embed_description: String,
pub embed_footer: String,
pub embed_footer_url: Option<String>,
pub embed_image_url: Option<String>,
pub embed_thumbnail_url: Option<String>,
pub embed_title: String,
pub embed_fields: Option<Json<Vec<EmbedField>>>,
pub enabled: bool,
pub expires: Option<NaiveDateTime>,
pub interval_seconds: Option<u32>,
pub interval_days: Option<u32>,
pub interval_months: Option<u32>,
#[serde(default = "name_default")]
pub name: String,
pub tts: bool,
#[serde(default)]
pub uid: String,
pub utc_time: NaiveDateTime,
}
pub async fn create_reminder(
ctx: &Context,
transaction: &mut Transaction<'_>,
user_id: UserId,
reminder: Reminder,
) -> JsonResult {
let channel = user_id
.create_dm_channel(&ctx)
.map_err(|e| Error::Serenity(e))
.and_then(|dm_channel| create_database_channel(&ctx, dm_channel.id, transaction))
.await;
if let Err(e) = channel {
warn!("`create_database_channel` returned an error code: {:?}", e);
return Err(json!({"error": "Failed to configure channel for reminders."}));
}
let channel = channel.unwrap();
// validate lengths
check_length!(MAX_NAME_LENGTH, reminder.name);
check_length!(MAX_CONTENT_LENGTH, reminder.content);
check_length!(MAX_EMBED_DESCRIPTION_LENGTH, reminder.embed_description);
check_length!(MAX_EMBED_TITLE_LENGTH, reminder.embed_title);
check_length!(MAX_EMBED_AUTHOR_LENGTH, reminder.embed_author);
check_length!(MAX_EMBED_FOOTER_LENGTH, reminder.embed_footer);
check_length_opt!(MAX_EMBED_FIELDS, reminder.embed_fields);
if let Some(fields) = &reminder.embed_fields {
for field in &fields.0 {
check_length!(MAX_EMBED_FIELD_VALUE_LENGTH, field.value);
check_length!(MAX_EMBED_FIELD_TITLE_LENGTH, field.title);
}
}
check_length_opt!(
MAX_URL_LENGTH,
reminder.embed_footer_url,
reminder.embed_thumbnail_url,
reminder.embed_author_url,
reminder.embed_image_url
);
// validate urls
check_url_opt!(
reminder.embed_footer_url,
reminder.embed_thumbnail_url,
reminder.embed_author_url,
reminder.embed_image_url
);
// validate time and interval
if reminder.utc_time < Utc::now().naive_utc() {
return Err(json!({"error": "Time must be in the future"}));
}
if reminder.interval_seconds.is_some()
|| reminder.interval_days.is_some()
|| reminder.interval_months.is_some()
{
if reminder.interval_months.unwrap_or(0) * 30 * DAY as u32
+ reminder.interval_days.unwrap_or(0) * DAY as u32
+ reminder.interval_seconds.unwrap_or(0)
< *MIN_INTERVAL
{
return Err(json!({"error": "Interval too short"}));
}
}
// check patreon if necessary
if reminder.interval_seconds.is_some()
|| reminder.interval_days.is_some()
|| reminder.interval_months.is_some()
{
if !check_subscription(&ctx, user_id).await {
return Err(json!({"error": "Patreon is required to set intervals"}));
}
}
let name = if reminder.name.is_empty() { name_default() } else { reminder.name.clone() };
let new_uid = generate_uid();
// write to db
match sqlx::query!(
"INSERT INTO reminders (
uid,
attachment,
attachment_name,
channel_id,
content,
embed_author,
embed_author_url,
embed_color,
embed_description,
embed_footer,
embed_footer_url,
embed_image_url,
embed_thumbnail_url,
embed_title,
embed_fields,
enabled,
expires,
interval_seconds,
interval_days,
interval_months,
name,
tts,
`utc_time`
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
new_uid,
reminder.attachment,
reminder.attachment_name,
channel,
reminder.content,
reminder.embed_author,
reminder.embed_author_url,
reminder.embed_color,
reminder.embed_description,
reminder.embed_footer,
reminder.embed_footer_url,
reminder.embed_image_url,
reminder.embed_thumbnail_url,
reminder.embed_title,
reminder.embed_fields,
reminder.enabled,
reminder.expires,
reminder.interval_seconds,
reminder.interval_days,
reminder.interval_months,
name,
reminder.tts,
reminder.utc_time,
)
.execute(transaction.executor())
.await
{
Ok(_) => sqlx::query_as_unchecked!(
Reminder,
"SELECT
reminders.attachment,
reminders.attachment_name,
reminders.content,
reminders.embed_author,
reminders.embed_author_url,
reminders.embed_color,
reminders.embed_description,
reminders.embed_footer,
reminders.embed_footer_url,
reminders.embed_image_url,
reminders.embed_thumbnail_url,
reminders.embed_title,
reminders.embed_fields,
reminders.enabled,
reminders.expires,
reminders.interval_seconds,
reminders.interval_days,
reminders.interval_months,
reminders.name,
reminders.tts,
reminders.uid,
reminders.utc_time
FROM reminders
WHERE uid = ?",
new_uid
)
.fetch_one(transaction.executor())
.await
.map(|r| Ok(json!(r)))
.unwrap_or_else(|e| {
warn!("Failed to complete SQL query: {:?}", e);
Err(json!({"error": "Could not load reminder"}))
}),
Err(e) => {
warn!("Error in `create_reminder`: Could not execute query: {:?}", e);
Err(json!({"error": "Unknown error"}))
}
}
}

View File

@ -0,0 +1,284 @@
use log::warn;
use rocket::{
get,
http::CookieJar,
patch, post,
serde::json::{json, Json},
State,
};
use serenity::{client::Context, model::id::UserId};
use sqlx::{MySql, Pool};
use crate::web::{
check_subscription,
guards::transaction::Transaction,
routes::{
dashboard::{
api::user::models::{create_reminder, Reminder},
PatchReminder, MIN_INTERVAL,
},
JsonResult,
},
Database,
};
#[post("/api/user/reminders", data = "<reminder>")]
pub async fn create_user_reminder(
reminder: Json<Reminder>,
cookies: &CookieJar<'_>,
ctx: &State<Context>,
mut transaction: Transaction<'_>,
) -> JsonResult {
let user_id =
cookies.get_private("userid").map(|c| c.value().parse::<u64>().ok()).flatten().unwrap();
match create_reminder(
ctx.inner(),
&mut transaction,
UserId::new(user_id),
reminder.into_inner(),
)
.await
{
Ok(r) => match transaction.commit().await {
Ok(_) => Ok(r),
Err(e) => {
warn!("Couldn't commit transaction: {:?}", e);
json_err!("Couldn't commit transaction.")
}
},
Err(e) => Err(e),
}
}
#[get("/api/user/reminders")]
pub async fn get_reminders(
cookies: &CookieJar<'_>,
ctx: &State<Context>,
pool: &State<Pool<MySql>>,
) -> JsonResult {
let user_id =
cookies.get_private("userid").map(|c| c.value().parse::<u64>().ok()).flatten().unwrap();
let channel = UserId::new(user_id).create_dm_channel(ctx.inner()).await;
match channel {
Ok(channel) => sqlx::query_as_unchecked!(
Reminder,
"
SELECT
reminders.attachment,
reminders.attachment_name,
reminders.content,
reminders.embed_author,
reminders.embed_author_url,
reminders.embed_color,
reminders.embed_description,
reminders.embed_footer,
reminders.embed_footer_url,
reminders.embed_image_url,
reminders.embed_thumbnail_url,
reminders.embed_title,
IFNULL(reminders.embed_fields, '[]') AS embed_fields,
reminders.enabled,
reminders.expires,
reminders.interval_seconds,
reminders.interval_days,
reminders.interval_months,
reminders.name,
reminders.tts,
reminders.uid,
reminders.utc_time
FROM reminders
INNER JOIN channels ON channels.id = reminders.channel_id
WHERE `status` = 'pending' AND channels.channel = ?
",
channel.id.get()
)
.fetch_all(pool.inner())
.await
.map(|r| Ok(json!(r)))
.unwrap_or_else(|e| {
warn!("Failed to complete SQL query: {:?}", e);
json_err!("Could not load reminders")
}),
Err(e) => {
warn!("Couldn't get DM channel: {:?}", e);
json_err!("Could not find a DM channel")
}
}
}
#[patch("/api/user/reminders", data = "<reminder>")]
pub async fn edit_reminder(
reminder: Json<PatchReminder>,
ctx: &State<Context>,
mut transaction: Transaction<'_>,
pool: &State<Pool<Database>>,
cookies: &CookieJar<'_>,
) -> JsonResult {
let user_id_cookie =
cookies.get_private("userid").map(|c| c.value().parse::<u64>().ok()).flatten();
if user_id_cookie.is_none() {
return Err(json!({"error": "User not authorized"}));
}
let mut error = vec![];
let user_id = user_id_cookie.unwrap();
if reminder.message_ok() {
update_field!(transaction.executor(), error, reminder.[
content,
embed_author,
embed_description,
embed_footer,
embed_title,
embed_fields
]);
} else {
error.push("Message exceeds limits.".to_string());
}
update_field!(transaction.executor(), error, reminder.[
attachment,
attachment_name,
embed_author_url,
embed_color,
embed_footer_url,
embed_image_url,
embed_thumbnail_url,
enabled,
expires,
name,
tts,
utc_time
]);
if reminder.interval_days.flatten().is_some()
|| reminder.interval_months.flatten().is_some()
|| reminder.interval_seconds.flatten().is_some()
{
if check_subscription(&ctx.inner(), user_id).await {
let new_interval_length = match reminder.interval_days {
Some(interval) => interval.unwrap_or(0),
None => sqlx::query!(
"SELECT interval_days AS days FROM reminders WHERE uid = ?",
reminder.uid
)
.fetch_one(transaction.executor())
.await
.map_err(|e| {
warn!("Error updating reminder interval: {:?}", e);
json!({ "reminder": Option::<Reminder>::None, "errors": vec!["Unknown error"] })
})?
.days
.unwrap_or(0),
} * 86400 + match reminder.interval_months {
Some(interval) => interval.unwrap_or(0),
None => sqlx::query!(
"SELECT interval_months AS months FROM reminders WHERE uid = ?",
reminder.uid
)
.fetch_one(transaction.executor())
.await
.map_err(|e| {
warn!("Error updating reminder interval: {:?}", e);
json!({ "reminder": Option::<Reminder>::None, "errors": vec!["Unknown error"] })
})?
.months
.unwrap_or(0),
} * 2592000 + match reminder.interval_seconds {
Some(interval) => interval.unwrap_or(0),
None => sqlx::query!(
"SELECT interval_seconds AS seconds FROM reminders WHERE uid = ?",
reminder.uid
)
.fetch_one(transaction.executor())
.await
.map_err(|e| {
warn!("Error updating reminder interval: {:?}", e);
json!({ "reminder": Option::<Reminder>::None, "errors": vec!["Unknown error"] })
})?
.seconds
.unwrap_or(0),
};
if new_interval_length < *MIN_INTERVAL {
error.push(String::from("New interval is too short."));
} else {
update_field!(transaction.executor(), error, reminder.[
interval_days,
interval_months,
interval_seconds
]);
}
}
} else {
sqlx::query!(
"
UPDATE reminders
SET interval_seconds = NULL, interval_days = NULL, interval_months = NULL
WHERE uid = ?
",
reminder.uid
)
.execute(transaction.executor())
.await
.map_err(|e| {
warn!("Error updating reminder interval: {:?}", e);
json!({ "reminder": Option::<Reminder>::None, "errors": vec!["Unknown error"] })
})?;
}
if let Err(e) = transaction.commit().await {
warn!("Couldn't commit transaction: {:?}", e);
return json_err!("Couldn't commit transaction");
}
match sqlx::query_as_unchecked!(
Reminder,
"
SELECT reminders.attachment,
reminders.attachment_name,
reminders.content,
reminders.embed_author,
reminders.embed_author_url,
reminders.embed_color,
reminders.embed_description,
reminders.embed_footer,
reminders.embed_footer_url,
reminders.embed_image_url,
reminders.embed_thumbnail_url,
reminders.embed_title,
reminders.embed_fields,
reminders.enabled,
reminders.expires,
reminders.interval_seconds,
reminders.interval_days,
reminders.interval_months,
reminders.name,
reminders.tts,
reminders.uid,
reminders.utc_time
FROM reminders
LEFT JOIN channels ON channels.id = reminders.channel_id
WHERE uid = ?
",
reminder.uid
)
.fetch_one(pool.inner())
.await
{
Ok(reminder) => Ok(json!({"reminder": reminder, "errors": error})),
Err(e) => {
warn!("Error exiting `edit_reminder': {:?}", e);
Err(json!({"reminder": Option::<Reminder>::None, "errors": vec!["Unknown error"]}))
}
}
}

View File

@ -1,7 +1,11 @@
use base64::{prelude::BASE64_STANDARD, Engine};
use csv::{QuoteStyle, WriterBuilder};
use log::warn;
use rocket::{
get,
http::CookieJar,
serde::json::{json, serde_json, Json},
put,
serde::json::{json, Json},
State,
};
use serenity::{
@ -10,7 +14,7 @@ use serenity::{
};
use sqlx::{MySql, Pool};
use crate::{
use crate::web::{
check_authorization,
guards::transaction::Transaction,
routes::{
@ -134,7 +138,7 @@ pub(crate) async fn import_reminders(
let user_id =
cookies.get_private("userid").map(|c| c.value().parse::<u64>().ok()).flatten().unwrap();
match base64::decode(&body.body) {
match BASE64_STANDARD.decode(&body.body) {
Ok(body) => {
let mut reader = csv::Reader::from_reader(body.as_slice());
let mut count = 0;
@ -292,7 +296,7 @@ pub async fn import_todos(
let channels_res = GuildId::new(id).channels(&ctx.inner()).await;
match channels_res {
Ok(channels) => match base64::decode(&body.body) {
Ok(channels) => match BASE64_STANDARD.decode(&body.body) {
Ok(body) => {
let mut reader = csv::Reader::from_reader(body.as_slice());

View File

@ -1,8 +1,12 @@
use std::path::Path;
use base64::{prelude::BASE64_STANDARD, Engine};
use chrono::{naive::NaiveDateTime, Utc};
use log::warn;
use rand::{rngs::OsRng, seq::IteratorRandom};
use rocket::{fs::NamedFile, http::CookieJar, response::Redirect, serde::json::json};
use rocket::{
fs::NamedFile, get, http::CookieJar, response::Redirect, serde::json::json, Responder,
};
use rocket_dyn_templates::Template;
use secrecy::ExposeSecret;
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
@ -14,7 +18,7 @@ use serenity::{
};
use sqlx::types::Json;
use crate::{
use crate::web::{
catchers::internal_server_error,
check_guild_subscription, check_subscription,
consts::{
@ -55,7 +59,7 @@ fn interval_default() -> Unset<Option<u32>> {
#[derive(sqlx::Type)]
#[sqlx(transparent)]
struct Attachment(Vec<u8>);
pub struct Attachment(Vec<u8>);
impl<'de> Deserialize<'de> for Attachment {
fn deserialize<D>(deserializer: D) -> Result<Attachment, D::Error>
@ -63,7 +67,7 @@ impl<'de> Deserialize<'de> for Attachment {
D: Deserializer<'de>,
{
let string = String::deserialize(deserializer)?;
Ok(Attachment(base64::decode(string).map_err(de::Error::custom)?))
Ok(Attachment(BASE64_STANDARD.decode(string).map_err(de::Error::custom)?))
}
}
@ -72,7 +76,7 @@ impl Serialize for Attachment {
where
S: Serializer,
{
serializer.collect_str(&base64::encode(&self.0))
serializer.collect_str(&BASE64_STANDARD.encode(&self.0))
}
}
@ -595,7 +599,7 @@ async fn create_database_channel(
ctx: impl CacheHttp,
channel: ChannelId,
transaction: &mut Transaction<'_>,
) -> Result<u32, crate::Error> {
) -> Result<u32, Error> {
let row = sqlx::query!(
"SELECT webhook_token, webhook_id FROM channels WHERE channel = ?",
channel.get()
@ -605,11 +609,13 @@ async fn create_database_channel(
match row {
Ok(row) => {
if row.webhook_token.is_none() || row.webhook_id.is_none() {
let is_dm =
channel.to_channel(&ctx).await.map_err(|e| Error::Serenity(e))?.private().is_some();
if !is_dm && (row.webhook_token.is_none() || row.webhook_id.is_none()) {
let webhook = channel
.create_webhook(&ctx, CreateWebhook::new("Reminder").avatar(&*DEFAULT_AVATAR))
.await
.map_err(|_| Error::Serenity)?;
.map_err(|e| Error::Serenity(e))?;
let token = webhook.token.unwrap();
@ -623,7 +629,7 @@ async fn create_database_channel(
)
.execute(transaction.executor())
.await
.map_err(|_| Error::SQLx)?;
.map_err(|e| Error::SQLx(e))?;
}
Ok(())
@ -634,7 +640,7 @@ async fn create_database_channel(
let webhook = channel
.create_webhook(&ctx, CreateWebhook::new("Reminder").avatar(&*DEFAULT_AVATAR))
.await
.map_err(|_| Error::Serenity)?;
.map_err(|e| Error::Serenity(e))?;
let token = webhook.token.unwrap();
@ -653,18 +659,18 @@ async fn create_database_channel(
)
.execute(transaction.executor())
.await
.map_err(|_| Error::SQLx)?;
.map_err(|e| Error::SQLx(e))?;
Ok(())
}
Err(_) => Err(Error::SQLx),
Err(e) => Err(Error::SQLx(e)),
}?;
let row = sqlx::query!("SELECT id FROM channels WHERE channel = ?", channel.get())
.fetch_one(transaction.executor())
.await
.map_err(|_| Error::SQLx)?;
.map_err(|e| Error::SQLx(e))?;
Ok(row.id)
}

View File

@ -5,13 +5,14 @@ use oauth2::{
};
use reqwest::Client;
use rocket::{
get,
http::{private::cookie::Expiration, Cookie, CookieJar, SameSite},
response::{Flash, Redirect},
uri, State,
};
use serenity::model::user::User;
use crate::{consts::DISCORD_API, routes};
use crate::web::{consts::DISCORD_API, routes};
#[get("/discord")]
pub async fn discord_login(

View File

@ -1,12 +1,10 @@
pub mod admin;
pub mod dashboard;
pub mod login;
pub mod metrics;
pub mod report;
use std::collections::HashMap;
use rocket::{request::FlashMessage, serde::json::Value as JsonValue};
use rocket::{get, request::FlashMessage, serde::json::Value as JsonValue};
use rocket_dyn_templates::Template;
pub type JsonResult = Result<JsonValue, JsonValue>;

View File

@ -1,12 +1,14 @@
use log::error;
use rocket::{
http::CookieJar,
post,
serde::{
json::{json, Json},
Deserialize,
},
};
use crate::routes::JsonResult;
use crate::web::routes::JsonResult;
#[derive(Deserialize)]
pub struct ClientError {

View File

Before

Width:  |  Height:  |  Size: 7.2 KiB

After

Width:  |  Height:  |  Size: 7.2 KiB

View File

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

View File

Before

Width:  |  Height:  |  Size: 6.8 KiB

After

Width:  |  Height:  |  Size: 6.8 KiB

Some files were not shown because too many files have changed in this diff Show More