/
new_func.go
242 lines (227 loc) · 6.57 KB
/
new_func.go
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package routing
import (
"encoding/hex"
"errors"
"freemasonry.cc/chat/clientapi/httputil"
"freemasonry.cc/chat/clientapi/jsonerror"
"freemasonry.cc/chat/internal/eventutil"
"freemasonry.cc/chat/new_feature"
"freemasonry.cc/chat/new_feature/new_db"
roomserverAPI "freemasonry.cc/chat/roomserver/api"
"freemasonry.cc/chat/setup/config"
"freemasonry.cc/chat/syncapi/sync"
userapi "freemasonry.cc/chat/userapi/api"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/util"
"github.com/tharsis/ethermint/crypto/ethsecp256k1"
"net/http"
"strings"
"time"
)
type queryReq struct {
PubKey string `json:"pub_key"`
QuerySign string `json:"query_sign"`
Invitee string `json:"invitee"`
Timestamp string `json:"timestamp"`
Localpart string `json:"localpart"`
}
var errMissingUserID = errors.New("'user_id' must be supplied")
func QueryInviteRes(
httpReq *http.Request,
) util.JSONResponse {
// 1, ,,chat_addr
req := queryReq{}
resErr := httputil.UnmarshalJSONRequest(httpReq, &req)
if resErr != nil {
return *resErr
}
if req.QuerySign == "" || req.Invitee == "" || req.PubKey == "" {
return util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.BadJSON("invalid params"),
}
}
// 1.1,
pubKeyBytes, err := hex.DecodeString(req.PubKey)
if err != nil {
return util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.Unknown("err hex fmt of PubKey"),
}
}
signBytes, err := hex.DecodeString(req.QuerySign)
if err != nil {
return util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.Unknown("err hex fmt of QuerySign"),
}
}
msgBytes := []byte(req.Timestamp)
pubKeyChat := ethsecp256k1.PubKey{Key: pubKeyBytes}
addressHexChat := sdk.AccAddress(pubKeyChat.Address()).String()
//addressHex := pubKey.Address().String()
recoveredChatAddress := strings.ToLower(addressHexChat)
if !pubKeyChat.VerifySignature(msgBytes, signBytes) {
return util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.BadJSON("check sign err"),
}
}
// 2, chat_addrlocalpart :1, 2,,,
localPart, err := new_feature.GetLocalByChatAddr(recoveredChatAddress)
if err != nil {
util.GetLogger(httpReq.Context()).WithError(err).Error("new_feature.GetLocalByChatAddr")
}
if localPart == "" {
localPart = req.Localpart
}
// 3, ,get/user/by/phone
res, err := new_feature.QueryInviteRes(localPart, req.Invitee)
if err != nil {
return util.JSONResponse{
Code: http.StatusInternalServerError,
JSON: jsonerror.Unknown(err.Error()),
}
}
return util.JSONResponse{
Code: http.StatusOK,
JSON: res,
}
}
type inviteAsOwnerReq struct {
RoomId string `json:"room_id"`
TargetUserId string `json:"target_user_id"`
Reason string `json:"reason"`
}
// UserInactive3Days
func UserInactive3Days(httpReq *http.Request) util.JSONResponse {
//ctx := httpReq.Context()
req := []string{}
res := []string{}
resErr := httputil.UnmarshalJSONRequest(httpReq, &req)
if resErr != nil {
return *resErr
}
if len(req) == 0 {
return util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.BadJSON("invalid params"),
}
}
for i, _ := range req {
if value, ok := sync.SoPool.PositiveUsers[req[i]]; ok {
if value>>1|0b000 == 0 {
res = append(res, req[i])
}
}
}
return util.JSONResponse{
Code: http.StatusOK,
JSON: res,
}
}
// UserInactive7Days
func UserInactive7Days(httpReq *http.Request) util.JSONResponse {
//ctx := httpReq.Context()
req := []string{}
res := []string{}
resErr := httputil.UnmarshalJSONRequest(httpReq, &req)
if resErr != nil {
return *resErr
}
if len(req) == 0 {
return util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.BadJSON("invalid params"),
}
}
for i, _ := range req {
if value, ok := sync.SoPool.PositiveUsers[req[i]]; ok {
if value>>1|0b0000000 == 0 {
res = append(res, req[i])
}
}
}
return util.JSONResponse{
Code: http.StatusOK,
JSON: res,
}
}
func SendOwnerInvite(httpReq *http.Request, userAPI userapi.FederationUserAPI, cfg *config.FederationAPI, rsAPI roomserverAPI.FederationRoomserverAPI,
) util.JSONResponse {
ctx := httpReq.Context()
req := inviteAsOwnerReq{}
resErr := httputil.UnmarshalJSONRequest(httpReq, &req)
if resErr != nil {
return *resErr
}
if req.RoomId == "" || req.TargetUserId == "" {
return util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.BadJSON("invalid params"),
}
}
groupOwner, err := new_db.GetGroupOwner(req.RoomId)
if err != nil {
return util.ErrorResponse(err)
}
var userPro userapi.QueryProfileResponse
err = userAPI.QueryProfile(ctx, &userapi.QueryProfileRequest{
req.TargetUserId,
}, &userPro)
if err != nil {
return util.ErrorResponse(err)
}
builder := gomatrixserverlib.EventBuilder{
Sender: groupOwner,
RoomID: req.RoomId,
Type: "m.room.member",
StateKey: &req.TargetUserId,
}
content := gomatrixserverlib.MemberContent{
Membership: gomatrixserverlib.Invite,
DisplayName: userPro.DisplayName,
AvatarURL: userPro.AvatarURL,
Reason: req.Reason,
IsDirect: false,
}
if err = builder.SetContent(content); err != nil {
return util.ErrorResponse(err)
}
event, err := eventutil.QueryAndBuildEvent(ctx, &builder, cfg.Matrix, time.Now(), rsAPI, nil)
if err == errMissingUserID {
return util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.BadJSON(err.Error()),
}
} else if err == eventutil.ErrRoomNoExists {
return util.JSONResponse{
Code: http.StatusNotFound,
JSON: jsonerror.NotFound(err.Error()),
}
} else if err != nil {
util.GetLogger(ctx).WithError(err).Error("buildMembershipEvent failed")
return jsonerror.InternalServerError()
}
var inviteRes roomserverAPI.PerformInviteResponse
if err := rsAPI.PerformInvite(ctx, &roomserverAPI.PerformInviteRequest{
Event: event,
InviteRoomState: nil, // ask the roomserver to draw up invite room state for us
RoomVersion: event.RoomVersion,
SendAsServer: string(cfg.Matrix.ServerName),
}, &inviteRes); err != nil {
util.GetLogger(ctx).WithError(err).Error("PerformInvite failed")
return util.JSONResponse{
Code: http.StatusInternalServerError,
JSON: jsonerror.InternalServerError(),
}
}
if inviteRes.Error != nil {
return inviteRes.Error.JSONResponse()
}
return util.JSONResponse{
Code: http.StatusOK,
JSON: struct{}{},
}
}