2024-10-29 15:09:21 +00:00
|
|
|
package server
|
|
|
|
|
2024-11-15 15:15:07 +00:00
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"gitlab.com/mstarongitlab/goutils/other"
|
|
|
|
"gitlab.com/mstarongitlab/linstrom/storage"
|
|
|
|
)
|
2024-10-29 15:09:21 +00:00
|
|
|
|
|
|
|
func setupLinstromApiRouter() http.Handler {
|
|
|
|
router := http.NewServeMux()
|
|
|
|
router.Handle("/v1/", http.StripPrefix("/v1", setupLinstromApiV1Router()))
|
2024-11-06 15:58:57 +00:00
|
|
|
router.Handle("/s2s/v1/", http.StripPrefix("/s2s/v1", setupLinstromS2SApiV1Router()))
|
2024-10-29 15:09:21 +00:00
|
|
|
|
|
|
|
return router
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupLinstromApiV1Router() http.Handler {
|
|
|
|
router := http.NewServeMux()
|
2024-11-15 15:15:07 +00:00
|
|
|
|
2024-10-29 15:09:21 +00:00
|
|
|
// Notes
|
2024-11-15 15:15:07 +00:00
|
|
|
|
|
|
|
// Get a note
|
2024-10-31 15:53:42 +00:00
|
|
|
router.HandleFunc("GET /notes/{noteId}", linstromGetNote)
|
2024-11-15 15:15:07 +00:00
|
|
|
// Send a new note
|
2024-10-31 15:53:42 +00:00
|
|
|
router.HandleFunc("POST /notes", linstromNewNote)
|
2024-11-15 15:15:07 +00:00
|
|
|
// Update a note
|
2024-10-31 15:53:42 +00:00
|
|
|
router.HandleFunc("PATCH /notes/{noteId}", linstromUpdateNote)
|
2024-11-15 15:15:07 +00:00
|
|
|
// Delete a note
|
2024-10-31 15:53:42 +00:00
|
|
|
router.HandleFunc("DELETE /notes/{noteId}", linstromDeleteNote)
|
2024-11-15 15:15:07 +00:00
|
|
|
|
2024-10-29 15:09:21 +00:00
|
|
|
// Reactions
|
2024-11-15 15:15:07 +00:00
|
|
|
|
|
|
|
// Get all reactions for a note
|
2024-10-31 15:53:42 +00:00
|
|
|
router.HandleFunc("GET /notes/{noteId}/reactions", linstromGetReactions)
|
2024-11-15 15:15:07 +00:00
|
|
|
// Send a new reaction to a note
|
2024-10-31 15:53:42 +00:00
|
|
|
router.HandleFunc("POST /notes/{noteId}/reactions", linstromAddReaction)
|
2024-11-15 15:15:07 +00:00
|
|
|
// Update own reaction on a note
|
2024-10-31 15:53:42 +00:00
|
|
|
router.HandleFunc("PATCH /notes/{noteId}/reactions", linstromUpdateReaction)
|
2024-11-15 15:15:07 +00:00
|
|
|
// Remove own reaction on a note
|
2024-10-31 15:53:42 +00:00
|
|
|
router.HandleFunc("DELETE /notes/{noteId}/reactions", linstromDeleteReaction)
|
2024-11-15 15:15:07 +00:00
|
|
|
|
2024-10-29 15:09:21 +00:00
|
|
|
// Boosts
|
2024-11-15 15:15:07 +00:00
|
|
|
|
|
|
|
// Get all boosters of a note
|
2024-10-31 15:53:42 +00:00
|
|
|
router.HandleFunc("GET /notes/{noteId}/boosts", linstromGetBoosts)
|
2024-11-15 15:15:07 +00:00
|
|
|
// Boost a note
|
2024-10-31 15:53:42 +00:00
|
|
|
router.HandleFunc("POST /notes/{noteId}/boosts", linstromAddBoost)
|
2024-11-15 15:15:07 +00:00
|
|
|
// Unboost a note
|
2024-10-31 15:53:42 +00:00
|
|
|
router.HandleFunc("DELETE /notes/{noteId}/boosts", linstromRemoveBoost)
|
2024-11-15 15:15:07 +00:00
|
|
|
|
2024-10-29 15:09:21 +00:00
|
|
|
// Quotes
|
2024-11-15 15:15:07 +00:00
|
|
|
|
|
|
|
// Get all quotes of a note
|
2024-10-31 15:53:42 +00:00
|
|
|
router.HandleFunc("GET /notes/{noteId}/quotes", linstromGetQuotes)
|
2024-11-15 15:15:07 +00:00
|
|
|
// Create a new quote message of a given note
|
2024-10-31 15:53:42 +00:00
|
|
|
router.HandleFunc("POST /notes/{noteId}/quotes", linstromAddQuote)
|
2024-11-15 15:15:07 +00:00
|
|
|
|
2024-10-29 15:09:21 +00:00
|
|
|
// Pinning
|
2024-11-15 15:15:07 +00:00
|
|
|
|
|
|
|
// Pin a note to account profile
|
2024-10-31 15:53:42 +00:00
|
|
|
router.HandleFunc("POST /notes/{noteId}/pin", linstromPinNote)
|
2024-11-15 15:15:07 +00:00
|
|
|
// Unpin a note from account profile
|
2024-10-31 15:53:42 +00:00
|
|
|
router.HandleFunc("DELETE /notes/{noteId}/pin", linstromUnpinNote)
|
2024-10-30 15:05:20 +00:00
|
|
|
// Reports
|
2024-10-31 15:53:42 +00:00
|
|
|
router.HandleFunc("POST /notes/{noteId}/report", linstromReportNote)
|
|
|
|
router.HandleFunc("DELETE /notes/{noteId}/report", linstromRetractReportNote)
|
2024-10-30 15:05:20 +00:00
|
|
|
// Admin
|
2024-10-31 15:53:42 +00:00
|
|
|
router.HandleFunc("POST /notes/{noteId}/admin/cw", linstromForceCWNote)
|
|
|
|
|
|
|
|
// Accounts
|
|
|
|
// Creating a new account happens either during fetch of a remote one or during registration with a passkey
|
2024-11-15 15:15:07 +00:00
|
|
|
|
|
|
|
// Get an account
|
2024-10-31 15:53:42 +00:00
|
|
|
router.HandleFunc("GET /accounts/{accountId}", linstromGetAccount)
|
2024-11-15 15:15:07 +00:00
|
|
|
// Update own account
|
2024-11-04 15:25:39 +00:00
|
|
|
// Technically also requires authenticated account to also be owner or correct admin perms,
|
|
|
|
// but that's annoying to handle in a general sense. So leaving that to the function
|
2024-11-05 15:29:01 +00:00
|
|
|
// though figuring out a nice generic-ish way to handle those checks would be nice too
|
2024-11-04 15:25:39 +00:00
|
|
|
router.HandleFunc(
|
|
|
|
"PATCH /accounts/{accountId}",
|
|
|
|
requireValidSessionMiddleware(linstromUpdateAccount),
|
|
|
|
)
|
2024-11-15 15:15:07 +00:00
|
|
|
// Delete own account
|
2024-11-04 15:25:39 +00:00
|
|
|
// Technically also requires authenticated account to also be owner or correct admin perms,
|
|
|
|
// but that's annoying to handle in a general sense. So leaving that to the function
|
|
|
|
router.HandleFunc(
|
|
|
|
"DELETE /accounts/{accountId}",
|
|
|
|
requireValidSessionMiddleware(linstromDeleteAccount),
|
|
|
|
)
|
2024-10-31 15:53:42 +00:00
|
|
|
// Follow
|
2024-11-15 15:15:07 +00:00
|
|
|
// Is logged in following accountId
|
|
|
|
router.HandleFunc(
|
|
|
|
"GET /accounts/{accountId}/follow/to",
|
|
|
|
requireValidSessionMiddleware(linstromIsFollowingToAccount),
|
|
|
|
)
|
|
|
|
// Is accountId following logged in
|
|
|
|
router.HandleFunc(
|
|
|
|
"GET /accounts/{accountId}/follow/from",
|
|
|
|
requireValidSessionMiddleware(linstromIsFollowingFromAccount),
|
|
|
|
)
|
|
|
|
// Send follow request to accountId
|
2024-10-31 15:53:42 +00:00
|
|
|
router.HandleFunc("POST /accounts/{accountId}/follow", linstromFollowAccount)
|
2024-11-15 15:15:07 +00:00
|
|
|
// Undo follow request to accountId
|
2024-10-31 15:53:42 +00:00
|
|
|
router.HandleFunc("DELETE /accounts/{accountId}/follow", linstromUnfollowAccount)
|
2024-11-15 15:15:07 +00:00
|
|
|
|
2024-10-31 15:53:42 +00:00
|
|
|
// Block
|
2024-11-15 15:15:07 +00:00
|
|
|
|
|
|
|
// Is logged in account blocking target account
|
2024-10-31 15:53:42 +00:00
|
|
|
router.HandleFunc("GET /accounts/{accountId}/block", linstromIsBlockingAccount)
|
2024-11-15 15:15:07 +00:00
|
|
|
// Block target account
|
2024-10-31 15:53:42 +00:00
|
|
|
router.HandleFunc("POST /accounts/{accountId}/block", linstromBlockAccount)
|
2024-11-15 15:15:07 +00:00
|
|
|
// Unblock target account
|
2024-10-31 15:53:42 +00:00
|
|
|
router.HandleFunc("DELETE /accounts/{accountId}/block", linstromUnblockAccount)
|
2024-11-15 15:15:07 +00:00
|
|
|
|
2024-10-31 15:53:42 +00:00
|
|
|
// Mute
|
2024-11-15 15:15:07 +00:00
|
|
|
|
|
|
|
// Has logged in account muted target account?
|
2024-10-31 15:53:42 +00:00
|
|
|
router.HandleFunc("GET /accounts/{accountId}/mute", linstromIsMutedAccount)
|
2024-11-15 15:15:07 +00:00
|
|
|
// Mute target account
|
2024-10-31 15:53:42 +00:00
|
|
|
router.HandleFunc("POST /accounts/{accountId}/mute", linstromMuteAccount)
|
2024-11-15 15:15:07 +00:00
|
|
|
// Unmute target account
|
2024-10-31 15:53:42 +00:00
|
|
|
router.HandleFunc("DELETE /accounts/{accountId}/mute", linstromUnmuteAccount)
|
2024-11-15 15:15:07 +00:00
|
|
|
|
2024-10-31 15:53:42 +00:00
|
|
|
// Report
|
2024-11-15 15:15:07 +00:00
|
|
|
|
|
|
|
// Report a target account
|
2024-10-31 15:53:42 +00:00
|
|
|
router.HandleFunc("POST /accounts/{accountId}/reports", linstromReportAccount)
|
2024-11-15 15:15:07 +00:00
|
|
|
// Undo report on target account
|
2024-10-31 15:53:42 +00:00
|
|
|
router.HandleFunc("DELETE /accounts/{accountId}/reports", linstromRetractReportAccount)
|
2024-11-15 15:15:07 +00:00
|
|
|
|
2024-10-31 15:53:42 +00:00
|
|
|
// Admin
|
2024-11-15 15:15:07 +00:00
|
|
|
|
|
|
|
// Add new role to account
|
|
|
|
router.Handle(
|
|
|
|
"POST /accounts/{accountId}/admin/roles",
|
|
|
|
buildRequirePermissionsMiddleware(
|
|
|
|
&storage.Role{CanAssignRoles: other.IntoPointer(true)},
|
|
|
|
)(
|
|
|
|
http.HandlerFunc(linstromAdminAddRoleAccount),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
// Remove role from account
|
|
|
|
router.Handle(
|
2024-10-31 15:53:42 +00:00
|
|
|
"DELETE /accounts/{accountId}/admin/roles/{roleName}",
|
2024-11-15 15:15:07 +00:00
|
|
|
buildRequirePermissionsMiddleware(&storage.Role{CanAssignRoles: other.IntoPointer(true)})(
|
|
|
|
http.HandlerFunc(linstromAdminRemoveRoleAccount),
|
|
|
|
),
|
2024-10-31 15:53:42 +00:00
|
|
|
)
|
2024-11-15 15:15:07 +00:00
|
|
|
// Send a warning to account
|
2024-10-31 15:53:42 +00:00
|
|
|
router.HandleFunc("POST /accounts/{accountId}/admin/warn", linstromAdminWarnAccount)
|
|
|
|
|
|
|
|
// Roles
|
2024-11-15 15:15:07 +00:00
|
|
|
|
|
|
|
// Get a role
|
2024-10-31 15:53:42 +00:00
|
|
|
router.HandleFunc("GET /roles/{roleId}", linstromGetRole)
|
2024-11-15 15:15:07 +00:00
|
|
|
// Create a new role
|
2024-10-31 15:53:42 +00:00
|
|
|
router.HandleFunc("POST /roles", linstromCreateRole)
|
2024-11-15 15:15:07 +00:00
|
|
|
// Update a role. Builtin roles cannot be edited
|
2024-10-31 15:53:42 +00:00
|
|
|
router.HandleFunc("PATCH /roles/{roleId}", linstromUpdateRole)
|
2024-11-15 15:15:07 +00:00
|
|
|
// Delete a role. Builtin roles cannot be deleted
|
2024-10-31 15:53:42 +00:00
|
|
|
router.HandleFunc("DELETE /roles/{roleId}", linstromDeleteRole)
|
|
|
|
|
|
|
|
// Media metadata
|
2024-11-15 15:15:07 +00:00
|
|
|
|
|
|
|
// Get the metadata for some media
|
2024-10-31 15:53:42 +00:00
|
|
|
router.HandleFunc("GET /media/{mediaId}", linstromGetMediaMetadata)
|
2024-11-15 15:15:07 +00:00
|
|
|
// Upload new media
|
2024-10-31 15:53:42 +00:00
|
|
|
router.HandleFunc("POST /media", linstromNewMediaMetadata)
|
2024-11-15 15:15:07 +00:00
|
|
|
// Update the metadata for some media
|
2024-10-31 15:53:42 +00:00
|
|
|
router.HandleFunc("PATCH /media/{mediaId}", linstromUpdateMediaMetadata)
|
2024-11-15 15:15:07 +00:00
|
|
|
// Delete a media entry
|
2024-10-31 15:53:42 +00:00
|
|
|
router.HandleFunc("DELETE /media/{mediaId}", linstromDeleteMediaMetadata)
|
2024-10-30 15:05:20 +00:00
|
|
|
|
|
|
|
// Event streams
|
|
|
|
router.HandleFunc("/streams", linstromEventStream)
|
2024-10-29 15:09:21 +00:00
|
|
|
|
|
|
|
return router
|
|
|
|
}
|
2024-11-06 15:58:57 +00:00
|
|
|
|
|
|
|
func setupLinstromS2SApiV1Router() http.Handler {
|
|
|
|
router := http.NewServeMux()
|
|
|
|
// TODO: Figure out a decent server to server API definition
|
|
|
|
router.HandleFunc("/", placeholderEndpoint)
|
|
|
|
return router
|
|
|
|
}
|