2026-03-27 13:58:48 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
|
|
import (
|
2026-08-01 22:06:14 +02:00
|
|
|
"context"
|
2026-03-27 13:58:48 +01:00
|
|
|
"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))
|
2026-08-01 22:06:14 +02:00
|
|
|
r.Post("/group", handleCreateGroupDM(svc, broadcaster))
|
2026-04-05 21:01:29 +00:00
|
|
|
r.Get("/", handleListDMs(svc))
|
2026-08-01 22:06:14 +02:00
|
|
|
r.Patch("/{channelId}", handleRenameGroupDM(svc, broadcaster))
|
2026-04-05 21:01:29 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2026-08-01 22:06:14 +02:00
|
|
|
// createGroupDMRequest is the JSON body for POST /api/v1/dms/group.
|
|
|
|
|
type createGroupDMRequest struct {
|
|
|
|
|
RecipientIDs []int64 `json:"recipient_ids"`
|
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// renameDMRequest is the JSON body for PATCH /api/v1/dms/{channelId}.
|
|
|
|
|
type renameDMRequest struct {
|
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
}
|
|
|
|
|
|
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-07-24 11:06:59 +02:00
|
|
|
writeServiceError(r.Context(), 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
|
|
|
}
|
2026-08-01 22:06:14 +02:00
|
|
|
displayName := ""
|
|
|
|
|
if result.Recipient.DisplayName != nil {
|
|
|
|
|
displayName = *result.Recipient.DisplayName
|
|
|
|
|
}
|
2026-03-27 13:58:48 +01:00
|
|
|
dmUser := db.DMUser{
|
2026-08-01 22:06:14 +02:00
|
|
|
ID: result.Recipient.ID,
|
|
|
|
|
Username: result.Recipient.Username,
|
|
|
|
|
Avatar: avatarStr,
|
|
|
|
|
Status: db.StatusForViewer(result.Recipient.Status, result.Recipient.ID, user.ID),
|
|
|
|
|
DisplayName: displayName,
|
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-07-23 17:03:52 +02:00
|
|
|
channels, err := svc.DMs.ListDMs(r.Context(), user.ID)
|
2026-03-27 13:58:48 +01:00
|
|
|
if err != nil {
|
2026-07-24 11:06:59 +02:00
|
|
|
writeServiceError(r.Context(), 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-08-01 22:06:14 +02:00
|
|
|
result, err := svc.DMs.CloseDM(r.Context(), user.ID, channelID)
|
|
|
|
|
if err != nil {
|
2026-07-24 11:06:59 +02:00
|
|
|
writeServiceError(r.Context(), 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 {
|
2026-07-29 13:25:46 +02:00
|
|
|
closeMsg := fmt.Appendf(nil, `{"type":"dm_channel_close","payload":{"channel_id":%d}}`, channelID)
|
2026-03-27 16:14:54 +01:00
|
|
|
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-08-01 22:06:14 +02:00
|
|
|
// A group leave changes the membership everyone else renders, so
|
|
|
|
|
// the survivors get a refreshed dm_channel_open rather than being
|
|
|
|
|
// left showing a member who has gone.
|
|
|
|
|
if result.Left && !result.ChannelDeleted {
|
|
|
|
|
broadcastDMOpen(r.Context(), svc, broadcaster, channelID, result.RemainingParticipantIDs)
|
|
|
|
|
}
|
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
|
|
|
|
2026-08-01 22:06:14 +02:00
|
|
|
// broadcastDMOpen sends a per-viewer dm_channel_open for channelID to each of
|
|
|
|
|
// targetIDs. The payload differs per addressee (`recipient`/`recipients` are
|
|
|
|
|
// relative to who is reading), so it is rebuilt inside the loop.
|
|
|
|
|
//
|
|
|
|
|
// Failures are logged and skipped, never surfaced: the mutation that prompted
|
|
|
|
|
// this has already committed, and a client that misses the event re-derives
|
|
|
|
|
// the same state from its next `ready`.
|
|
|
|
|
func broadcastDMOpen(ctx context.Context, svc *service.Services, broadcaster DMBroadcaster, channelID int64, targetIDs []int64) {
|
|
|
|
|
if broadcaster == nil || len(targetIDs) == 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
for _, pid := range targetIDs {
|
|
|
|
|
summary, pErr := svc.DMs.DMSummaryFor(ctx, pid, channelID)
|
|
|
|
|
if pErr != nil {
|
|
|
|
|
slog.Debug("broadcastDMOpen: summary unavailable", "user_id", pid, "channel_id", channelID, "err", pErr)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
msg, mErr := json.Marshal(map[string]any{
|
|
|
|
|
"type": "dm_channel_open",
|
|
|
|
|
"payload": summary,
|
|
|
|
|
})
|
|
|
|
|
if mErr != nil {
|
|
|
|
|
slog.Warn("broadcastDMOpen: marshal failed", "err", mErr, "channel_id", channelID)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if ok := broadcaster.SendToUser(pid, msg); !ok {
|
|
|
|
|
slog.Debug("broadcastDMOpen: user not connected", "user_id", pid, "channel_id", channelID)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// handleCreateGroupDM creates a group DM between the caller and 2..8 others.
|
|
|
|
|
func handleCreateGroupDM(svc *service.Services, broadcaster DMBroadcaster) http.HandlerFunc {
|
|
|
|
|
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{
|
|
|
|
|
Error: "UNAUTHORIZED", Message: "authentication required",
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var req createGroupDMRequest
|
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
|
|
|
writeJSON(w, http.StatusBadRequest, errorResponse{
|
|
|
|
|
Error: "BAD_REQUEST", Message: "invalid request body",
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result, err := svc.DMs.CreateGroupDM(r.Context(), user.ID, req.RecipientIDs, req.Name)
|
|
|
|
|
if err != nil {
|
|
|
|
|
writeServiceError(r.Context(), w, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Everyone gets the DM in their sidebar immediately, the creator
|
|
|
|
|
// included — the REST response is only the creator's copy, and a
|
|
|
|
|
// second window of theirs needs the event just as much as the others.
|
|
|
|
|
broadcastDMOpen(r.Context(), svc, broadcaster, result.Channel.ID, result.ParticipantIDs)
|
|
|
|
|
|
|
|
|
|
writeJSON(w, http.StatusCreated,
|
|
|
|
|
db.NewDMChannelInfo(result.Channel.ID, result.Channel.Name, true, result.Participants, user.ID))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// handleRenameGroupDM sets or clears a group DM's name. Participants only —
|
|
|
|
|
// there is no owner, so every member holds the same authority over it.
|
|
|
|
|
func handleRenameGroupDM(svc *service.Services, broadcaster DMBroadcaster) http.HandlerFunc {
|
|
|
|
|
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{
|
|
|
|
|
Error: "UNAUTHORIZED", Message: "authentication required",
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
channelID, ok := parseIDParam(w, r, "channelId")
|
|
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var req renameDMRequest
|
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
|
|
|
writeJSON(w, http.StatusBadRequest, errorResponse{
|
|
|
|
|
Error: "BAD_REQUEST", Message: "invalid request body",
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if _, err := svc.DMs.RenameGroupDM(r.Context(), user.ID, channelID, req.Name); err != nil {
|
|
|
|
|
writeServiceError(r.Context(), w, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
participantIDs, pErr := svc.Channels.GetDMParticipantIDs(r.Context(), channelID)
|
|
|
|
|
if pErr == nil {
|
|
|
|
|
broadcastDMOpen(r.Context(), svc, broadcaster, channelID, participantIDs)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
summary, sErr := svc.DMs.DMSummaryFor(r.Context(), user.ID, channelID)
|
|
|
|
|
if sErr != nil {
|
|
|
|
|
writeServiceError(r.Context(), w, sErr)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
writeJSON(w, http.StatusOK, summary)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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-07-24 11:06:59 +02:00
|
|
|
writeServiceError(r.Context(), w, err)
|
2026-04-05 21:01:29 +00:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-23 17:03:52 +02:00
|
|
|
if err := svc.Blocks.UnblockUser(r.Context(), user.ID, targetID); err != nil {
|
2026-07-24 11:06:59 +02:00
|
|
|
writeServiceError(r.Context(), w, err)
|
2026-04-05 21:01:29 +00:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-23 17:03:52 +02:00
|
|
|
ids, err := svc.Blocks.ListBlocked(r.Context(), user.ID)
|
2026-04-05 21:01:29 +00:00
|
|
|
if err != nil {
|
2026-07-24 11:06:59 +02:00
|
|
|
writeServiceError(r.Context(), w, err)
|
2026-04-05 21:01:29 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
writeJSON(w, http.StatusOK, map[string]any{"blocked_user_ids": ids})
|
|
|
|
|
}
|
|
|
|
|
}
|