create seprate pocketbase client
This commit is contained in:
parent
654df98711
commit
2837293474
6 changed files with 245 additions and 43 deletions
|
|
@ -1,6 +1,7 @@
|
|||
package microblog
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
|
@ -9,15 +10,42 @@ import (
|
|||
gemini "github.com/kulak/gemini"
|
||||
)
|
||||
|
||||
type source string
|
||||
|
||||
const (
|
||||
sourcePleroma source = "pleroma"
|
||||
sourceBlueSky source = "blue_sky"
|
||||
sourceMastodon source = "mastodon"
|
||||
sourcePixelfed source = "pixelfed"
|
||||
sourceNostr source = "nostr"
|
||||
)
|
||||
|
||||
var supportedSources = []source{
|
||||
sourceNostr,
|
||||
// TODO: Add support for BlueSky and Mastodon
|
||||
// SourceBlueSky,
|
||||
// SourceMastodon,
|
||||
}
|
||||
|
||||
// Post represents a single blog post
|
||||
type Post struct {
|
||||
ID string
|
||||
Title string
|
||||
Content string
|
||||
Author string
|
||||
type post struct {
|
||||
ID string
|
||||
RemoteID string
|
||||
Content string
|
||||
// TODO: add support for images, must extend the pocketbase query
|
||||
// Images []string
|
||||
Timestamp time.Time
|
||||
}
|
||||
|
||||
// PBPost represents a microblog post from PocketBase
|
||||
type pbPost struct {
|
||||
ID string `json:"id"`
|
||||
RemoteID string `json:"remoteId"`
|
||||
Source Source `json:"source"`
|
||||
FullPost json.RawMessage `json:"fullPost"`
|
||||
Posted string `json:"posted"`
|
||||
}
|
||||
|
||||
// MicroBlog manages blog posts
|
||||
type MicroBlog struct {
|
||||
posts []Post
|
||||
|
|
@ -104,13 +132,11 @@ func (mb *MicroBlog) HandleBlogRequest(w gemini.ResponseWriter, req *gemini.Requ
|
|||
path := req.URL.Path
|
||||
|
||||
switch {
|
||||
case path == "/blog" || path == "/blog/":
|
||||
case path == "/microblog" || path == "/microblog/":
|
||||
mb.serveBlogIndex(w, req)
|
||||
case strings.HasPrefix(path, "/blog/post/"):
|
||||
postID := strings.TrimPrefix(path, "/blog/post/")
|
||||
case strings.HasPrefix(path, "/microblog/post/"):
|
||||
postID := strings.TrimPrefix(path, "/microblog/post/")
|
||||
mb.servePost(w, req, postID)
|
||||
case path == "/blog/new":
|
||||
mb.serveNewPostForm(w, req)
|
||||
default:
|
||||
w.WriteStatusMsg(gemini.StatusNotFound, "Blog page not found")
|
||||
}
|
||||
|
|
@ -168,25 +194,3 @@ func (mb *MicroBlog) servePost(w gemini.ResponseWriter, req *gemini.Request, pos
|
|||
|
||||
w.WriteBody([]byte(content.String()))
|
||||
}
|
||||
|
||||
// serveNewPostForm serves the form for creating a new post
|
||||
func (mb *MicroBlog) serveNewPostForm(w gemini.ResponseWriter, req *gemini.Request) {
|
||||
// Check if user is authenticated
|
||||
if req.Certificate() == nil {
|
||||
w.WriteStatusMsg(gemini.StatusCertRequired, "Authentication required to create posts")
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteStatusMsg(gemini.StatusSuccess, "text/gemini")
|
||||
|
||||
var content strings.Builder
|
||||
content.WriteString("# Write a New Post\n\n")
|
||||
content.WriteString("To create a new post, use the Titan protocol:\n\n")
|
||||
content.WriteString("titan://your-server/blog/create;mime=text/plain;token=your-auth-token\n\n")
|
||||
content.WriteString("Format your post as:\n")
|
||||
content.WriteString("Title: Your Post Title\n")
|
||||
content.WriteString("Content: Your post content goes here...\n\n")
|
||||
content.WriteString("=> /blog Back to blog\n")
|
||||
|
||||
w.WriteBody([]byte(content.String()))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -221,7 +221,7 @@ func (c *PocketBaseClient) GetPosts(page int) ([]PBPost, error) {
|
|||
if err := json.NewDecoder(resp.Body).Decode(&postsResp); err != nil {
|
||||
return nil, fmt.Errorf("failed to decode response: %w", err)
|
||||
}
|
||||
slog.Info("Pocketbase Res", "res", postsResp)
|
||||
// slog.Info("Pocketbase Res", "res", postsResp)
|
||||
|
||||
return postsResp.Items, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue