Skip to content

Commit

Permalink
fix api problems
Browse files Browse the repository at this point in the history
  • Loading branch information
zyxkad committed Mar 27, 2024
1 parent 1b827a0 commit 38499b7
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 67 deletions.
6 changes: 4 additions & 2 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ func (cr *Cluster) apiV0SubscribeEmailGET(rw http.ResponseWriter, req *http.Requ
writeJson(rw, http.StatusOK, record)
return
}
var records []database.EmailSubscriptionRecord
records := make([]database.EmailSubscriptionRecord, 0, 4)
if err := cr.database.ForEachUsersEmailSubscription(user, func(rec *database.EmailSubscriptionRecord) error {
records = append(records, *rec)
return nil
Expand All @@ -729,6 +729,7 @@ func (cr *Cluster) apiV0SubscribeEmailPOST(rw http.ResponseWriter, req *http.Req
return
}

data.User = user
if err := cr.database.AddEmailSubscription(data); err != nil {
writeJson(rw, http.StatusInternalServerError, Map{
"error": "Database update failed",
Expand Down Expand Up @@ -833,7 +834,7 @@ func (cr *Cluster) apiV0WebhookGET(rw http.ResponseWriter, req *http.Request, us
writeJson(rw, http.StatusOK, record)
return
}
var records []database.WebhookRecord
records := make([]database.WebhookRecord, 0, 4)
if err := cr.database.ForEachUsersWebhook(user, func(rec *database.WebhookRecord) error {
records = append(records, *rec)
return nil
Expand All @@ -853,6 +854,7 @@ func (cr *Cluster) apiV0WebhookPOST(rw http.ResponseWriter, req *http.Request, u
return
}

data.User = user
if err := cr.database.AddWebhook(data); err != nil {
writeJson(rw, http.StatusInternalServerError, Map{
"error": "Database update failed",
Expand Down
5 changes: 4 additions & 1 deletion cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,10 @@ func (cr *Cluster) Init(ctx context.Context) (err error) {
webpushPlg := new(webpush.Plugin)
cr.notifyManager.AddPlugin(webpushPlg)
if config.Notification.EnableEmail {
emailPlg := new(email.Plugin)
emailPlg, err := email.NewSMTP(config.Notification.EmailSMTP, config.Notification.EmailSender, config.Notification.EmailSenderPassword)
if err != nil {
return err
}
cr.notifyManager.AddPlugin(emailPlg)
}

Expand Down
24 changes: 15 additions & 9 deletions dashboard/src/api/v0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,19 @@ async function requestToken(

export async function ping(token?: string): Promise<PingRes> {
const res = await axios.get<PingRes>(`/api/v0/ping`, {
headers: token
? {
Authorization: `Bearer ${token}`,
}
: undefined,
headers: {
Authorization: token ? `Bearer ${token}` : undefined,
},
})
return res.data
}

export async function getStatus(): Promise<StatusRes> {
const res = await axios.get<StatusRes>(`/api/v0/status`)
export async function getStatus(token?: string | null): Promise<StatusRes> {
const res = await axios.get<StatusRes>(`/api/v0/status`, {
headers: {
Authorization: token ? `Bearer ${token}` : undefined,
},
})
return res.data
}

Expand Down Expand Up @@ -136,8 +138,12 @@ interface SubscribeKey {
publicKey: string
}

export async function getSubscribePublicKey(): Promise<string> {
const res = await axios.get<SubscribeKey>(`/api/v0/subscribeKey`)
export async function getSubscribePublicKey(token?: string): Promise<string> {
const res = await axios.get<SubscribeKey>(`/api/v0/subscribeKey`, {
headers: {
Authorization: token ? `Bearer ${token}` : undefined,
},
})
return res.data.publicKey
}

Expand Down
10 changes: 8 additions & 2 deletions dashboard/src/views/HomeView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ import HitsChart from '@/components/HitsChart.vue'
import UAChart from '@/components/UAChart.vue'
import LogBlock from '@/components/LogBlock.vue'
import StatusButton from '@/components/StatusButton.vue'
import { getStatus, getPprofURL, type StatInstData, type PprofLookups } from '@/api/v0'
import {
getStatus,
getPprofURL,
type StatInstData,
type StatusRes,
type PprofLookups,
} from '@/api/v0'
import { LogIO, type LogMsg } from '@/api/log.io'
import { bindRefToLocalStorage } from '@/cookies'
import { tr } from '@/lang'
Expand All @@ -32,7 +38,7 @@ setInterval(() => {
now.value = new Date()
}, 1000)
const { data, error, loading } = useRequest(getStatus, {
const { data, error, loading } = useRequest((): Promise<StatusRes> => getStatus(token.value), {
pollingInterval: 10000,
loadingDelay: 500,
loadingKeep: 2000,
Expand Down
15 changes: 15 additions & 0 deletions dashboard/src/views/settings/NotificationsView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@ async function addEmail(item: EmailItemPayload): Promise<void> {
return
}
await addEmailSubscription(token.value, item)
emails.value?.push({
user: '',
...item,
})
} catch (err) {
toast.add({
severity: 'error',
summary: 'Error when subscribing email',
detail: String(err),
life: 3000,
})
} finally {
newEmailItemSaving.value = false
}
Expand Down Expand Up @@ -261,6 +272,9 @@ async function webhookEditSave(): Promise<void> {
enabled: enabled,
})
}
getWebhooks(token.value).then((res) => {
webhooks.value = res
})
webhookEditingItem.value = null
} catch (err) {
toast.add({
Expand Down Expand Up @@ -320,6 +334,7 @@ onBeforeMount(() => {
<template #footer>
<MultiSelect
class="flex-auto width-full"
style="max-width: 12rem"
:options="ALL_SUBSCRIBE_SCOPES"
v-model="newEmailItem.scopes"
placeholder="Select Scopes"
Expand Down
14 changes: 7 additions & 7 deletions database/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ func (db *SqlDB) setupEmailSubscriptionsQuestionMark(ctx context.Context) (err e

const addInsertCmd = "INSERT INTO " + tableName +
" (`user`,`addr`,`scopes`,`enabled`) VALUES" +
" (?,?,?,TRUE)"
" (?,?,?,?)"
if db.emailSubscriptionStmts.add, err = db.db.PrepareContext(ctx, addInsertCmd); err != nil {
return
}
Expand Down Expand Up @@ -808,7 +808,7 @@ func (db *SqlDB) setupEmailSubscriptionsDollarMark(ctx context.Context) (err err

const addInsertCmd = "INSERT INTO " + tableName +
` ("user",addr,scopes,enabled) VALUES` +
" ($1,$2,$3,TRUE)"
" ($1,$2,$3,$4)"
if db.emailSubscriptionStmts.add, err = db.db.PrepareContext(ctx, addInsertCmd); err != nil {
return
}
Expand Down Expand Up @@ -938,14 +938,14 @@ func (db *SqlDB) ForEachUsersEmailSubscription(user string, cb func(*EmailSubscr
defer cancel()

var rows *sql.Rows
if rows, err = db.emailSubscriptionStmts.forEach.QueryContext(ctx); err != nil {
if rows, err = db.emailSubscriptionStmts.forEachUsers.QueryContext(ctx, user); err != nil {
return
}
defer rows.Close()
var rec EmailSubscriptionRecord
rec.User = user
for rows.Next() {
if err = rows.Scan(&rec.Addr, &rec.Scopes, &rec.Enabled, &rec.User); err != nil {
if err = rows.Scan(&rec.Addr, &rec.Scopes, &rec.Enabled); err != nil {
return
}
cb(&rec)
Expand Down Expand Up @@ -1014,7 +1014,7 @@ func (db *SqlDB) setupWebhooksQuestionMark(ctx context.Context) (err error) {

const addInsertCmd = "INSERT INTO " + tableName +
" (`user`,`id`,`endpoint`,`auth`,`scopes`,`enabled`) VALUES" +
" (?,?,?,?,?,TRUE)"
" (?,?,?,?,?,?)"
if db.webhookStmts.add, err = db.db.PrepareContext(ctx, addInsertCmd); err != nil {
return
}
Expand Down Expand Up @@ -1089,7 +1089,7 @@ func (db *SqlDB) setupWebhooksDollarMark(ctx context.Context) (err error) {

const addInsertCmd = "INSERT INTO " + tableName +
` ("user",id,endpoint,auth,scopes,enabled) VALUES` +
" ($1,$2,$3,$4,$5,TRUE)"
" ($1,$2,$3,$4,$5,$6)"
if db.webhookStmts.add, err = db.db.PrepareContext(ctx, addInsertCmd); err != nil {
return
}
Expand Down Expand Up @@ -1234,7 +1234,7 @@ func (db *SqlDB) ForEachUsersWebhook(user string, cb func(*WebhookRecord) error)
defer cancel()

var rows *sql.Rows
if rows, err = db.webhookStmts.forEachUsers.QueryContext(ctx); err != nil {
if rows, err = db.webhookStmts.forEachUsers.QueryContext(ctx, user); err != nil {
return
}
defer rows.Close()
Expand Down
Loading

0 comments on commit 38499b7

Please sign in to comment.