More work on the API. Primarely defining jsonapi types

This commit is contained in:
Melody Becker 2024-10-30 16:05:20 +01:00
parent 4f4d05a335
commit 0ed50aca60
13 changed files with 165 additions and 48 deletions

View file

@ -31,6 +31,14 @@ func setupLinstromApiV1Router() http.Handler {
// Pinning
router.HandleFunc("POST /note/{noteId}/pin", linstromPinNote)
router.HandleFunc("DELETE /note/{noteId}/pin", linstromUnpinNote)
// Reports
router.HandleFunc("POST /note/{noteId}/report", linstromReportNote)
router.HandleFunc("DELETE /note/{noteId}/report", linstromRetractReportNote)
// Admin
router.HandleFunc("POST /note/{noteId}/admin/cw", linstromForceCWNote)
// Event streams
router.HandleFunc("/streams", linstromEventStream)
return router
}

View file

@ -1,9 +1,15 @@
package server
import "net/http"
import (
"net/http"
)
// Notes
func linstromGetNote(w http.ResponseWriter, r *http.Request) {}
func linstromGetNote(w http.ResponseWriter, r *http.Request) {
store := StorageFromRequest(r)
noteId := NoteIdFromRequest(r)
note, err := store.FindNoteById(noteId)
}
func linstromUpdateNote(w http.ResponseWriter, r *http.Request) {}
func linstromNewNote(w http.ResponseWriter, r *http.Request) {}
func linstromDeleteNote(w http.ResponseWriter, r *http.Request) {}
@ -25,5 +31,15 @@ func linstromAddQuote(w http.ResponseWriter, r *http.Request) {}
// No delete quote since quotes are their own notes with an extra attribute
// Pinning
func linstromPinNote(w http.ResponseWriter, r *http.Request) {}
func linstromUnpinNote(w http.ResponseWriter, r *http.Request) {}
// Reporting
func linstromReportNote(w http.ResponseWriter, r *http.Request) {}
func linstromRetractReportNote(w http.ResponseWriter, r *http.Request) {}
// Admin tools
// TODO: Figure out more admin tools for managing notes
// Delete can be done via normal note delete, common permission check
func linstromForceCWNote(w http.ResponseWriter, r *http.Request) {}

View file

@ -1,5 +1,24 @@
package server
import (
"net/http"
"github.com/gorilla/websocket"
"github.com/rs/zerolog/hlog"
)
// TODO: Decide where to put data stream handlers
var websocketUpgrader = websocket.Upgrader{}
// Entrypoint for a new stream will be in here at least
func linstromEventStream(w http.ResponseWriter, r *http.Request) {
log := hlog.FromRequest(r)
conn, err := websocketUpgrader.Upgrade(w, r, nil)
if err != nil {
log.Warn().Err(err).Msg("Failed to upgrade connection to websocket")
}
defer conn.Close()
// TODO: Handle initial request for what events to receive
// TODO: Stream all requested events until connection closes (due to bad data from client or disconnect)
}

View file

@ -0,0 +1,81 @@
package server
// Contains types used by the Linstrom API. Types comply with the jsonapi spec
import "time"
type linstromNote struct {
Id string `jsonapi:"primary,notes"`
RawContent string `jsonapi:"attr,content"`
OriginServer *linstromOriginServer `jsonapi:"relation,origin_server"`
OriginServerId int `jsonapi:"attr,origin_server_id"`
ReactionCount string `jsonapi:"attr,reaction_count"`
CreatedAt time.Time `jsonapi:"attr,created_at"`
UpdatedAt *time.Time `jsonapi:"attr,updated_at,omitempty"`
Author *linstromAccount `jsonapi:"relation,author"`
AuthorId string `jsonapi:"attr,author_id"`
ContentWarning *string `jsonapi:"attr,content_warning,omitempty"`
InReplyToId *string `jsonapi:"attr,in_reply_to_id,omitempty"`
QuotesId *string `jsonapi:"attr,quotes_id,omitempty"`
EmoteIds []string `jsonapi:"attr,emotes,omitempty"`
Attachments []*linstromMediaMetadata `jsonapi:"relation,attachments,omitempty"`
AttachmentIds []string `jsonapi:"attr,attachment_ids"`
AccessLevel uint8 `jsonapi:"attr,access_level"`
Pings []*linstromAccount `jsonapi:"relation,pings,omitempty"`
PingIds []string `jsonapi:"attr,ping_ids,omitempty"`
}
type linstromOriginServer struct {
Id int `jsonapi:"primary,origins"`
CreatedAt time.Time `jsonapi:"attr,created_at"`
UpdatedAt *time.Time `jsonapi:"attr,updated_at,omitempty"`
ServerType string `jsonapi:"attr,server_type"` // one of "Linstrom", ""
Domain string `jsonapi:"attr,domain"`
DisplayName string `jsonapi:"attr,display_name"`
Icon *linstromMediaMetadata `jsonapi:"relation,icon"`
IsSelf bool `jsonapi:"attr,is_self"`
}
type linstromMediaMetadata struct {
Id string `jsonapi:"primary,medias"`
CreatedAt time.Time `jsonapi:"attr,created_at"`
UpdatedAt *time.Time `jsonapi:"attr,updated_at,omitempty"`
IsRemote bool `jsonapi:"attr,is_remote"`
Url string `jsonapi:"attr,url"`
MimeType string `jsonapi:"attr,mime_type"`
Name string `jsonapi:"attr,name"`
AltText string `jsonapi:"attr,alt_text"`
Blurred bool `jsonapi:"attr,blurred"`
}
type linstromAccount struct {
Id string `jsonapi:"primary,accounts"`
CreatedAt time.Time `jsonapi:"attr,created_at"`
UpdatedAt *time.Time `jsonapi:"attr,updated_at,omitempty"`
Username string `jsonapi:"attr,username"`
OriginServer *linstromOriginServer `jsonapi:"relation,origin_server"`
OriginServerId int `jsonapi:"attr,origin_server_id"`
DisplayName string `jsonapi:"attr,display_name"`
CustomFields []*linstromCustomAccountField `jsonapi:"relation,custom_fields"`
CustomFieldIds []uint `jsonapi:"attr,custom_field_ids"`
IsBot bool `jsonapi:"attr,is_bot"`
Description string `jsonapi:"attr,description"`
Icon *linstromMediaMetadata `jsonapi:"relation,icon"`
Banner *linstromMediaMetadata `jsonapi:"relation,banner"`
FollowerIds []string `jsonapi:"attr,follows_ids"`
FollowingIds []string `jsonapi:"attr,following_ids"`
Indexable bool `jsonapi:"attr,indexable"`
RestrictedFollow bool `jsonapi:"attr,restricted_follow"`
IdentifiesAs []string `jsonapi:"attr,identifies_as"`
Pronouns []string `jsonapi:"attr,pronouns"`
}
type linstromCustomAccountField struct {
Id uint
CreatedAt time.Time `jsonapi:"attr,created_at"`
UpdatedAt *time.Time `jsonapi:"attr,updated_at,omitempty"`
Key string `jsonapi:"attr,key"`
Value string `jsonapi:"attr,value"`
Verified *bool `jsonapi:"attr,verified,omitempty"`
BelongsToId string `jsonapi:"attr,belongs_to_id"`
}

View file

@ -1,22 +0,0 @@
package server
import (
"net/http"
"gitlab.com/mstarongitlab/goutils/other"
"gitlab.com/mstarongitlab/linstrom/storage"
)
func StorageFromRequest(w http.ResponseWriter, r *http.Request) *storage.Storage {
store, ok := r.Context().Value(ContextKeyStorage).(*storage.Storage)
if !ok {
other.HttpErr(
w,
HttpErrIdMissingContextValue,
"Missing storage reference",
http.StatusInternalServerError,
)
return nil
}
return store
}

View file

@ -18,16 +18,15 @@ func placeholderEndpoint(w http.ResponseWriter, r *http.Request) {
)
}
func getStorageFromRequest(w http.ResponseWriter, r *http.Request) *storage.Storage {
func StorageFromRequest(r *http.Request) *storage.Storage {
store, ok := r.Context().Value(ContextKeyStorage).(*storage.Storage)
if !ok {
other.HttpErr(
w,
HttpErrIdMissingContextValue,
"Missing storage in context",
http.StatusInternalServerError,
)
hlog.FromRequest(r).Fatal().Msg("Failed to get storage reference from context")
return nil
}
return store
}
func NoteIdFromRequest(r *http.Request) string {
return r.PathValue("noteId")
}