2026-03-27 13:58:48 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
2026-03-27 16:14:54 +01:00
|
|
|
"fmt"
|
2026-03-27 13:58:48 +01:00
|
|
|
"log/slog"
|
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
|
"github.com/owncord/server/db"
|
2026-04-05 21:01:29 +00:00
|
|
|
"github.com/owncord/server/service"
|
2026-03-27 13:58:48 +01:00
|
|
|
)
|
|
|
|
|
|
2026-03-27 16:14:54 +01:00
|
|
|
// DMBroadcaster is the interface needed to send WebSocket events from REST
|
|
|
|
|
// handlers. Satisfied by *ws.Hub.
|
|
|
|
|
type DMBroadcaster interface {
|
|
|
|
|
SendToUser(userID int64, msg []byte) bool
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-27 13:58:48 +01:00
|
|
|
// MountDMRoutes registers DM-related routes onto r.
|
|
|
|
|
// All routes require authentication.
|
2026-03-27 16:14:54 +01:00
|
|
|
// hub is used to send real-time WebSocket events on DM close.
|
2026-04-05 21:01:29 +00:00
|
|
|
func MountDMRoutes(r chi.Router, database *db.DB, svc *service.Services, broadcaster DMBroadcaster) {
|
2026-03-27 13:58:48 +01:00
|
|
|
r.Route("/api/v1/dms", func(r chi.Router) {
|
|
|
|
|
r.Use(AuthMiddleware(database))
|
2026-04-05 21:01:29 +00:00
|
|
|
r.Post("/", handleCreateDM(svc))
|
|
|
|
|
r.Get("/", handleListDMs(svc))
|
|
|
|
|
r.Delete("/{channelId}", handleCloseDM(svc, broadcaster))
|
2026-03-27 13:58:48 +01:00
|
|
|
})
|
2026-04-04 16:48:57 +00:00
|
|
|
|
|
|
|
|
// User blocking routes — prevent DM creation and messaging.
|
|
|
|
|
r.Route("/api/v1/blocks", func(r chi.Router) {
|
|
|
|
|
r.Use(AuthMiddleware(database))
|
2026-04-05 21:01:29 +00:00
|
|
|
r.Get("/", handleListBlocks(svc))
|
|
|
|
|
r.Put("/{userId}", handleBlockUser(svc))
|
|
|
|
|
r.Delete("/{userId}", handleUnblockUser(svc))
|
2026-04-04 16:48:57 +00:00
|
|
|
})
|
2026-03-27 13:58:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// createDMRequest is the JSON body for POST /api/v1/dms.
|
|
|
|
|
type createDMRequest struct {
|
|
|
|
|
RecipientID int64 `json:"recipient_id"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// createDMResponse is the JSON response for POST /api/v1/dms.
|
|
|
|
|
type createDMResponse struct {
|
2026-04-01 11:38:33 +02:00
|
|
|
ChannelID int64 `json:"channel_id"`
|
2026-03-27 13:58:48 +01:00
|
|
|
Recipient db.DMUser `json:"recipient"`
|
2026-04-01 11:38:33 +02:00
|
|
|
Created bool `json:"created"`
|
2026-03-27 13:58:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// listDMsResponse is the JSON response for GET /api/v1/dms.
|
|
|
|
|
type listDMsResponse struct {
|
|
|
|
|
DMChannels []db.DMChannelInfo `json:"dm_channels"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// handleCreateDM creates or retrieves a DM channel with a recipient.
|
2026-04-05 21:01:29 +00:00
|
|
|
func handleCreateDM(svc *service.Services) http.HandlerFunc {
|
2026-03-27 13:58:48 +01:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
user, ok := r.Context().Value(UserKey).(*db.User)
|
|
|
|
|
if !ok || user == nil {
|
|
|
|
|
writeJSON(w, http.StatusUnauthorized, errorResponse{
|
2026-04-05 21:01:29 +00:00
|
|
|
Error: "UNAUTHORIZED", Message: "authentication required",
|
2026-03-27 13:58:48 +01:00
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var req createDMRequest
|
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
|
|
|
writeJSON(w, http.StatusBadRequest, errorResponse{
|
2026-04-05 21:01:29 +00:00
|
|
|
Error: "BAD_REQUEST", Message: "invalid request body",
|
2026-03-27 13:58:48 +01:00
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-07 09:10:14 +02:00
|
|
|
result, err := svc.DMs.CreateDM(r.Context(), user.ID, req.RecipientID)
|
2026-03-27 13:58:48 +01:00
|
|
|
if err != nil {
|
2026-04-05 21:01:29 +00:00
|
|
|
writeServiceError(w, err)
|
2026-03-27 13:58:48 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
avatarStr := ""
|
2026-04-05 21:01:29 +00:00
|
|
|
if result.Recipient.Avatar != nil {
|
|
|
|
|
avatarStr = *result.Recipient.Avatar
|
2026-03-27 13:58:48 +01:00
|
|
|
}
|
|
|
|
|
dmUser := db.DMUser{
|
2026-04-05 21:01:29 +00:00
|
|
|
ID: result.Recipient.ID,
|
|
|
|
|
Username: result.Recipient.Username,
|
2026-03-27 13:58:48 +01:00
|
|
|
Avatar: avatarStr,
|
2026-04-05 21:01:29 +00:00
|
|
|
Status: result.Recipient.Status,
|
2026-03-27 13:58:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
status := http.StatusOK
|
2026-04-05 21:01:29 +00:00
|
|
|
if result.Created {
|
2026-03-27 13:58:48 +01:00
|
|
|
status = http.StatusCreated
|
|
|
|
|
}
|
|
|
|
|
writeJSON(w, status, createDMResponse{
|
2026-04-05 21:01:29 +00:00
|
|
|
ChannelID: result.Channel.ID,
|
2026-03-27 13:58:48 +01:00
|
|
|
Recipient: dmUser,
|
2026-04-05 21:01:29 +00:00
|
|
|
Created: result.Created,
|
2026-03-27 13:58:48 +01:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// handleListDMs returns all open DM channels for the authenticated user.
|
2026-04-05 21:01:29 +00:00
|
|
|
func handleListDMs(svc *service.Services) http.HandlerFunc {
|
2026-03-27 13:58:48 +01:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
user, ok := r.Context().Value(UserKey).(*db.User)
|
|
|
|
|
if !ok || user == nil {
|
|
|
|
|
writeJSON(w, http.StatusUnauthorized, errorResponse{
|
2026-04-05 21:01:29 +00:00
|
|
|
Error: "UNAUTHORIZED", Message: "authentication required",
|
2026-03-27 13:58:48 +01:00
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 21:01:29 +00:00
|
|
|
channels, err := svc.DMs.ListDMs(user.ID)
|
2026-03-27 13:58:48 +01:00
|
|
|
if err != nil {
|
2026-04-05 21:01:29 +00:00
|
|
|
writeServiceError(w, err)
|
2026-03-27 13:58:48 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
writeJSON(w, http.StatusOK, listDMsResponse{DMChannels: channels})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// handleCloseDM removes a DM channel from the authenticated user's open list.
|
2026-04-05 21:01:29 +00:00
|
|
|
func handleCloseDM(svc *service.Services, broadcaster DMBroadcaster) http.HandlerFunc {
|
2026-03-27 13:58:48 +01:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
user, ok := r.Context().Value(UserKey).(*db.User)
|
|
|
|
|
if !ok || user == nil {
|
|
|
|
|
writeJSON(w, http.StatusUnauthorized, errorResponse{
|
2026-04-05 21:01:29 +00:00
|
|
|
Error: "UNAUTHORIZED", Message: "authentication required",
|
2026-03-27 13:58:48 +01:00
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
channelID, ok := parseIDParam(w, r, "channelId")
|
|
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 21:01:29 +00:00
|
|
|
if err := svc.DMs.CloseDM(user.ID, channelID); err != nil {
|
|
|
|
|
writeServiceError(w, err)
|
2026-03-27 13:58:48 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 21:01:29 +00:00
|
|
|
// Notify via WebSocket so sidebar updates immediately.
|
2026-03-27 16:14:54 +01:00
|
|
|
if broadcaster != nil {
|
|
|
|
|
closeMsg := []byte(fmt.Sprintf(`{"type":"dm_channel_close","payload":{"channel_id":%d}}`, channelID))
|
|
|
|
|
if ok := broadcaster.SendToUser(user.ID, closeMsg); !ok {
|
2026-04-05 21:01:29 +00:00
|
|
|
slog.Debug("handleCloseDM: user not connected", "user_id", user.ID, "channel_id", channelID)
|
2026-03-27 16:14:54 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-27 13:58:48 +01:00
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-05 21:01:29 +00:00
|
|
|
|
|
|
|
|
// handleBlockUser blocks a user.
|
|
|
|
|
func handleBlockUser(svc *service.Services) http.HandlerFunc {
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
user, _ := r.Context().Value(UserKey).(*db.User)
|
|
|
|
|
if user == nil {
|
|
|
|
|
writeJSON(w, http.StatusUnauthorized, errorResponse{Error: "UNAUTHORIZED", Message: "authentication required"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
targetID, ok := parseIDParam(w, r, "userId")
|
|
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-07 09:10:14 +02:00
|
|
|
if err := svc.Blocks.BlockUser(r.Context(), user.ID, targetID); err != nil {
|
2026-04-05 21:01:29 +00:00
|
|
|
writeServiceError(w, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
writeJSON(w, http.StatusOK, map[string]string{"message": "user blocked"})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// handleUnblockUser unblocks a user.
|
|
|
|
|
func handleUnblockUser(svc *service.Services) http.HandlerFunc {
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
user, _ := r.Context().Value(UserKey).(*db.User)
|
|
|
|
|
if user == nil {
|
|
|
|
|
writeJSON(w, http.StatusUnauthorized, errorResponse{Error: "UNAUTHORIZED", Message: "authentication required"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
targetID, ok := parseIDParam(w, r, "userId")
|
|
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := svc.Blocks.UnblockUser(user.ID, targetID); err != nil {
|
|
|
|
|
writeServiceError(w, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
writeJSON(w, http.StatusOK, map[string]string{"message": "user unblocked"})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// handleListBlocks returns all blocked user IDs.
|
|
|
|
|
func handleListBlocks(svc *service.Services) http.HandlerFunc {
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
user, _ := r.Context().Value(UserKey).(*db.User)
|
|
|
|
|
if user == nil {
|
|
|
|
|
writeJSON(w, http.StatusUnauthorized, errorResponse{Error: "UNAUTHORIZED", Message: "authentication required"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ids, err := svc.Blocks.ListBlocked(user.ID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
writeServiceError(w, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
writeJSON(w, http.StatusOK, map[string]any{"blocked_user_ids": ids})
|
|
|
|
|
}
|
|
|
|
|
}
|