MCPcopy Create free account
hub / github.com/GoMudEngine/GoMud / apiAdminSendMudmail

Method apiAdminSendMudmail

modules/mudmail/admin.go:107–176  ·  view source on GitHub ↗

apiAdminSendMudmail handles POST /admin/api/v1/mudmail Body: { "from_name": "...", "body": "...", "gold": 0, "user_id": 0 } If user_id == 0, sends to everyone.

(r *http.Request)

Source from the content-addressed store, hash-verified

105// Body: { "from_name": "...", "body": "...", "gold": 0, "user_id": 0 }
106// If user_id == 0, sends to everyone.
107func (m *MudmailModule) apiAdminSendMudmail(r *http.Request) (int, bool, any) {
108 var req adminSendRequest
109 if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
110 return http.StatusBadRequest, false, map[string]string{"error": "invalid request body"}
111 }
112 if req.FromName == "" {
113 return http.StatusBadRequest, false, map[string]string{"error": "from_name is required"}
114 }
115 if req.Body == "" {
116 return http.StatusBadRequest, false, map[string]string{"error": "body is required"}
117 }
118 if req.Gold < 0 {
119 return http.StatusBadRequest, false, map[string]string{"error": "gold must be non-negative"}
120 }
121
122 if req.UserId != 0 {
123 var itm *items.Item
124 if req.ItemId > 0 {
125 newItem := items.New(req.ItemId)
126 if newItem.ItemId == 0 {
127 return http.StatusBadRequest, false, map[string]string{"error": "item_id not found"}
128 }
129 itm = &newItem
130 }
131 m.SendMudMail(req.UserId, req.FromName, req.Body, req.Gold, itm)
132 return http.StatusOK, true, map[string]any{"sent_to": req.UserId}
133 }
134
135 // Broadcast to everyone.
136 var broadcastItem *items.Item
137 if req.ItemId > 0 {
138 newItem := items.New(req.ItemId)
139 if newItem.ItemId == 0 {
140 return http.StatusBadRequest, false, map[string]string{"error": "item_id not found"}
141 }
142 broadcastItem = &newItem
143 }
144 msg := Message{
145 FromName: req.FromName,
146 Body: req.Body,
147 Gold: req.Gold,
148 Item: broadcastItem,
149 DateSent: time.Now(),
150 }
151
152 onlineIds := map[int]struct{}{}
153 for _, u := range users.GetAllActiveUsers() {
154 onlineIds[u.UserId] = struct{}{}
155 inbox := m.inboxes[u.UserId]
156 inbox = append(Inbox{msg}, inbox...)
157 m.inboxes[u.UserId] = inbox
158 m.save(u.UserId, inbox)
159 u.Command(`inbox check`)
160 }
161
162 sentCount := len(onlineIds)
163
164 users.SearchOfflineUsers(func(u *users.UserRecord) bool {

Callers

nothing calls this directly

Calls 7

SendMudMailMethod · 0.95
saveMethod · 0.95
loadMethod · 0.95
NewFunction · 0.92
GetAllActiveUsersFunction · 0.92
SearchOfflineUsersFunction · 0.92
CommandMethod · 0.45

Tested by

no test coverage detected