Skip to content

Commit

Permalink
new: add a delete content button to empty feed
Browse files Browse the repository at this point in the history
  • Loading branch information
ybizeul committed Sep 17, 2024
1 parent 0e4bb28 commit 7ef5166
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 14 deletions.
Binary file added cmd/ybfeed/__debug_bin2036534813
Binary file not shown.
14 changes: 14 additions & 0 deletions internal/feed/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,20 @@ func (feed *Feed) publicItems() ([]PublicFeedItem, error) {
return items, nil
}

func (feed *Feed) Empty() error {
items, err := feed.publicItems()
if err != nil {
return err
}
for _, item := range items {
err := feed.RemoveItem(item.Name)
if err != nil {
return err
}
}
return nil
}

// GetPublicItem returns a marshable struct for a specific feed item
func (feed *Feed) GetPublicItem(i string) (*PublicFeedItem, error) {
if i == "secret" || i == "pin" || i == "config.json" {
Expand Down
40 changes: 34 additions & 6 deletions internal/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,16 +193,15 @@ func (api *ApiHandler) GetServer() *chi.Mux {
}
})

r.Route("/api/feed", func(r chi.Router) {
r.Route("/api/feeds", func(r chi.Router) {
r.Get("/{feedName}", api.feedHandlerFunc)
r.Post("/{feedName}", api.feedPostHandlerFunc)
r.Patch("/{feedName}", api.feedPatchHandlerFunc)

r.Get("/{feedName}/{itemName}", api.feedItemHandlerFunc)
r.Delete("/{feedName}/{itemName}", api.feedItemDeleteHandlerFunc)

r.Post("/{feedName}/subscription", api.feedSubscriptionHandlerFunc)
r.Delete("/{feedName}/subscription", api.feedUnsubscribeHandlerFunc)
r.Delete("/{feedName}/items", api.feedItemEmptyHandlerFunc)
r.Get("/{feedName}/items/{itemName}", api.feedItemHandlerFunc)
r.Delete("/{feedName}/items/{itemName}", api.feedItemDeleteHandlerFunc)
})
r.Get("/*", RootHandlerFunc)

Expand Down Expand Up @@ -291,7 +290,7 @@ func (api *ApiHandler) feedHandlerFunc(w http.ResponseWriter, r *http.Request) {
http.SetCookie(w, &http.Cookie{
Name: "Secret",
Value: publicFeed.Secret,
Path: fmt.Sprintf("/api/feed/%s", feedName),
Path: fmt.Sprintf("/api/feeds/%s", feedName),
Expires: time.Now().Add(time.Hour * 24 * 365 * 10),
})

Expand Down Expand Up @@ -345,6 +344,35 @@ func (api *ApiHandler) feedPatchHandlerFunc(w http.ResponseWriter, r *http.Reque
return
}
}
func (api *ApiHandler) feedItemEmptyHandlerFunc(w http.ResponseWriter, r *http.Request) {
logger.Debug("Item API EMPTY request", slog.Any("request", r))

secret, _ := utils.GetSecret(r)

feedName, _ := url.QueryUnescape(chi.URLParam(r, "feedName"))
if feedName == "" {
utils.CloseWithCodeAndMessage(w, 500, "Unable to obtain feed name")
}

f, err := api.FeedManager.GetFeedWithAuth(feedName, secret)
if err != nil {
switch {
case errors.Is(err, feed.FeedErrorNotFound):
utils.CloseWithCodeAndMessage(w, 404, fmt.Sprintf("feed '%s' not found", feedName))
case errors.Is(err, feed.FeedErrorInvalidSecret):
utils.CloseWithCodeAndMessage(w, 401, "Unauthorized")
default:
utils.CloseWithCodeAndMessage(w, 500, fmt.Sprintf("Error while getting feed: %s", err.Error()))
}
return
}

err = f.Empty()
if err != nil {
utils.CloseWithCodeAndMessage(w, 500, fmt.Sprintf("Error while getting feed: %s", err.Error()))
return
}
}

func (api *ApiHandler) feedItemHandlerFunc(w http.ResponseWriter, r *http.Request) {
logger.Debug("Item API GET request", slog.Any("request", r))
Expand Down
2 changes: 1 addition & 1 deletion web/ui/src/YBFeed/Components/YBFeedItemImageComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function YBFeedItemImageComponent() {

return(
<Card.Section mt="sm">
<Image src={"/api/feed/"+encodeURIComponent(item!.feed.name)+"/"+item!.name} />
<Image src={"/api/feeds/"+encodeURIComponent(item!.feed.name)+"/items/"+item!.name} />
</Card.Section>
)
}
4 changes: 2 additions & 2 deletions web/ui/src/YBFeed/Components/YBPasteCardComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export function YBPasteCardComponent(props:YBPasteCardComponentProps) {

const requestHeaders: HeadersInit = new Headers();
requestHeaders.set("Content-Type", type)
fetch("/api/feed/" + encodeURIComponent(feed!),{
fetch("/api/feeds/" + encodeURIComponent(feed!),{
method: "POST",
body: data,
headers: requestHeaders,
Expand All @@ -71,7 +71,7 @@ export function YBPasteCardComponent(props:YBPasteCardComponentProps) {
const handleFinish = (text:string) => {
const requestHeaders: HeadersInit = new Headers();
requestHeaders.set("Content-Type", "text/plain")
fetch("/api/feed/" + encodeURIComponent(feed!),{
fetch("/api/feeds/" + encodeURIComponent(feed!),{
method: "POST",
body: text,
headers: requestHeaders,
Expand Down
2 changes: 1 addition & 1 deletion web/ui/src/YBFeed/Components/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const copyImageItem = (item:YBFeedItem) => {
img.onload = imageLoaded

})
img.src = "/api/feed/"+encodeURIComponent(item.feed.name)+"/"+item.name
img.src = "/api/feeds/"+encodeURIComponent(item.feed.name)+"/items/"+item.name

const mime = 'image/png'
navigator.clipboard.write([new ClipboardItem({[mime]:imageDataPromise})])
Expand Down
29 changes: 26 additions & 3 deletions web/ui/src/YBFeed/YBFeedConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { YBFeed, YBFeedItem, YBFeedError } from '.'

export class YBFeedConnector {
feedUrl(feedName: string): string {
return "/api/feed/"+encodeURIComponent(feedName)
return "/api/feeds/"+encodeURIComponent(feedName)
}
async Ping(): Promise<Headers> {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -68,7 +68,7 @@ export class YBFeedConnector {
}
async GetItem(item: YBFeedItem): Promise<string> {
return new Promise((resolve, reject) => {
fetch(this.feedUrl(item.feed.name)+"/"+item.name,{
fetch(this.feedUrl(item.feed.name)+"/items/"+item.name,{
credentials: "include"
})
.then(r => {
Expand All @@ -87,7 +87,7 @@ export class YBFeedConnector {
}
async DeleteItem(item: YBFeedItem) {
return new Promise((resolve, reject) => {
fetch(this.feedUrl(item.feed.name)+"/"+encodeURIComponent(item.name),{
fetch(this.feedUrl(item.feed.name)+"/items/"+encodeURIComponent(item.name),{
method: "DELETE",
credentials: "include"
})
Expand All @@ -106,6 +106,29 @@ export class YBFeedConnector {
})
})
}
async EmptyFeed(feedName: string): Promise<boolean> {
return new Promise((resolve, reject) => {
fetch(this.feedUrl(feedName)+
"/items",{
method: "DELETE",
credentials: "include"
})
.then((f) => {
if (f.status !== 200) {
f.text()
.then(text => {
reject(new YBFeedError(f.status, text))
})
.catch(() => {
reject(new YBFeedError(f.status, "Server Unavailable"))
})
} else {
resolve(true)
}
})
})
}

async SetPIN(feedName: string, pin: string): Promise<boolean> {
return new Promise((resolve, reject) => {
fetch(this.feedUrl(feedName),{
Expand Down
6 changes: 5 additions & 1 deletion web/ui/src/YBFeed/YBFeedFeed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import useWebSocket, {ReadyState} from 'react-use-websocket';

import { notifications } from '@mantine/notifications';

import { Menu, ActionIcon, PinInput, Text, Modal, Center, Group, rem} from '@mantine/core';
import { Menu, ActionIcon, PinInput, Text, Modal, Center, Group, rem, Button} from '@mantine/core';

import { YBFeed, YBFeedConnector, YBFeedItem } from '.'

Expand Down Expand Up @@ -136,13 +136,17 @@ export function YBFeedFeed() {
})
}

const deleteAll = () => {
connection.EmptyFeed(feedParam)
}
return (
<>
{goTo?
<Navigate to={goTo} />
:""}
{authenticated===true?
<Group gap="xs" justify="flex-end" style={{float: 'right'}}>
<Button size="xs" variant="outline" color="red" onClick={deleteAll}>Delete Content</Button>
{vapid?
<YBNotificationToggleComponent vapid={vapid} feedName={feedParam}/>
:""}
Expand Down

0 comments on commit 7ef5166

Please sign in to comment.