-
-
Notifications
You must be signed in to change notification settings - Fork 432
Expand file tree
/
Copy pathhandlers_grouprequests.go
More file actions
213 lines (172 loc) · 6 KB
/
Copy pathhandlers_grouprequests.go
File metadata and controls
213 lines (172 loc) · 6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package main
// Group join-request handlers (membership approval queue).
//
// WhatsApp groups can require admin approval for new members. whatsmeow exposes
// the underlying calls but upstream wuzapi did not surface them, so these three
// handlers wrap them:
// GET /group/requestparticipants -> list pending join requests
// POST /group/updaterequestparticipants -> approve/reject pending requests
// POST /group/joinapprovalmode -> toggle the approval requirement
//
// Kept in a dedicated file to minimise rebase conflicts against upstream.
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"github.com/rs/zerolog/log"
"go.mau.fi/whatsmeow"
"go.mau.fi/whatsmeow/types"
)
// GetGroupRequestParticipants lists the participants who have requested to join
// the group. Only meaningful when join approval mode is enabled for the group.
func (s *server) GetGroupRequestParticipants() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
txtid := r.Context().Value("userinfo").(Values).Get("Id")
client := clientManager.GetWhatsmeowClient(txtid)
if client == nil {
s.Respond(w, r, http.StatusInternalServerError, errors.New("no session"))
return
}
// Get GroupJID from query parameter
groupJID := r.URL.Query().Get("groupJID")
if groupJID == "" {
s.Respond(w, r, http.StatusBadRequest, errors.New("missing groupJID parameter"))
return
}
group, ok := parseJID(groupJID)
if !ok {
s.Respond(w, r, http.StatusBadRequest, errors.New("could not parse Group JID"))
return
}
resp, err := client.GetGroupRequestParticipants(r.Context(), group)
if err != nil {
msg := fmt.Sprintf("Failed to get group request participants: %v", err)
log.Error().Msg(msg)
s.Respond(w, r, http.StatusInternalServerError, errors.New(msg))
return
}
responseJson, err := json.Marshal(resp)
if err != nil {
s.Respond(w, r, http.StatusInternalServerError, err)
} else {
s.Respond(w, r, http.StatusOK, string(responseJson))
}
return
}
}
// UpdateGroupRequestParticipants approves or rejects pending requests to join
// the group. Action must be "approve" or "reject".
func (s *server) UpdateGroupRequestParticipants() http.HandlerFunc {
type updateGroupRequestParticipantsStruct struct {
GroupJID string
Phone []string
Action string // approve, reject
}
return func(w http.ResponseWriter, r *http.Request) {
txtid := r.Context().Value("userinfo").(Values).Get("Id")
client := clientManager.GetWhatsmeowClient(txtid)
if client == nil {
s.Respond(w, r, http.StatusInternalServerError, errors.New("no session"))
return
}
decoder := json.NewDecoder(r.Body)
var t updateGroupRequestParticipantsStruct
err := decoder.Decode(&t)
if err != nil {
s.Respond(w, r, http.StatusBadRequest, errors.New("could not decode Payload"))
return
}
group, ok := parseJID(t.GroupJID)
if !ok {
s.Respond(w, r, http.StatusBadRequest, errors.New("could not parse Group JID"))
return
}
if len(t.Phone) < 1 {
s.Respond(w, r, http.StatusBadRequest, errors.New("missing Phone in Payload"))
return
}
// parse phone numbers
phoneParsed := make([]types.JID, len(t.Phone))
for i, phone := range t.Phone {
phoneParsed[i], ok = parseJID(phone)
if !ok {
s.Respond(w, r, http.StatusBadRequest, errors.New("could not parse Phone"))
return
}
}
if t.Action == "" {
s.Respond(w, r, http.StatusBadRequest, errors.New("missing Action in Payload"))
return
}
// parse action
var action whatsmeow.ParticipantRequestChange
switch t.Action {
case "approve":
action = whatsmeow.ParticipantChangeApprove
case "reject":
action = whatsmeow.ParticipantChangeReject
default:
s.Respond(w, r, http.StatusBadRequest, errors.New("invalid Action in Payload (must be approve or reject)"))
return
}
_, err = client.UpdateGroupRequestParticipants(r.Context(), group, phoneParsed, action)
if err != nil {
log.Error().Str("error", fmt.Sprintf("%v", err)).Msg("failed to update group request participants")
msg := fmt.Sprintf("failed to update group request participants: %v", err)
s.Respond(w, r, http.StatusInternalServerError, errors.New(msg))
return
}
response := map[string]interface{}{"Details": "Group request participants updated successfully"}
responseJson, err := json.Marshal(response)
if err != nil {
s.Respond(w, r, http.StatusInternalServerError, err)
} else {
s.Respond(w, r, http.StatusOK, string(responseJson))
}
return
}
}
// SetGroupJoinApprovalMode toggles whether new members must be approved by an
// admin before joining the group.
func (s *server) SetGroupJoinApprovalMode() http.HandlerFunc {
type setGroupJoinApprovalModeStruct struct {
GroupJID string `json:"groupjid"`
Mode bool `json:"mode"`
}
return func(w http.ResponseWriter, r *http.Request) {
txtid := r.Context().Value("userinfo").(Values).Get("Id")
client := clientManager.GetWhatsmeowClient(txtid)
if client == nil {
s.Respond(w, r, http.StatusInternalServerError, errors.New("no session"))
return
}
decoder := json.NewDecoder(r.Body)
var t setGroupJoinApprovalModeStruct
err := decoder.Decode(&t)
if err != nil {
s.Respond(w, r, http.StatusBadRequest, errors.New("could not decode Payload"))
return
}
group, ok := parseJID(t.GroupJID)
if !ok {
s.Respond(w, r, http.StatusBadRequest, errors.New("could not parse Group JID"))
return
}
err = client.SetGroupJoinApprovalMode(r.Context(), group, t.Mode)
if err != nil {
log.Error().Str("error", fmt.Sprintf("%v", err)).Msg("failed to set group join approval mode")
msg := fmt.Sprintf("failed to set group join approval mode: %v", err)
s.Respond(w, r, http.StatusInternalServerError, errors.New(msg))
return
}
response := map[string]interface{}{"Details": "Group join approval mode updated successfully"}
responseJson, err := json.Marshal(response)
if err != nil {
s.Respond(w, r, http.StatusInternalServerError, err)
} else {
s.Respond(w, r, http.StatusOK, string(responseJson))
}
return
}
}