SendServerNotice sends a message to a specific user. It can only be invoked by an admin.
( req *http.Request, cfgNotices *config.ServerNotices, cfgClient *config.ClientAPI, userAPI userapi.ClientUserAPI, rsAPI api.ClientRoomserverAPI, asAPI appserviceAPI.AppServiceInternalAPI, device *userapi.Device, senderDevice *userapi.Device, txnID *string, txnCache *transactions.Cache, )
| 54 | |
| 55 | // SendServerNotice sends a message to a specific user. It can only be invoked by an admin. |
| 56 | func SendServerNotice( |
| 57 | req *http.Request, |
| 58 | cfgNotices *config.ServerNotices, |
| 59 | cfgClient *config.ClientAPI, |
| 60 | userAPI userapi.ClientUserAPI, |
| 61 | rsAPI api.ClientRoomserverAPI, |
| 62 | asAPI appserviceAPI.AppServiceInternalAPI, |
| 63 | device *userapi.Device, |
| 64 | senderDevice *userapi.Device, |
| 65 | txnID *string, |
| 66 | txnCache *transactions.Cache, |
| 67 | ) util.JSONResponse { |
| 68 | if device.AccountType != userapi.AccountTypeAdmin && device.DisplayName != cfgNotices.LocalPart { // ,admin |
| 69 | return util.JSONResponse{ |
| 70 | Code: http.StatusForbidden, |
| 71 | JSON: jsonerror.Forbidden("This API can only be used by admin users."), |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | if txnID != nil { |
| 76 | // Try to fetch response from transactionsCache |
| 77 | if res, ok := txnCache.FetchTransaction(device.AccessToken, *txnID); ok { |
| 78 | return *res |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | ctx := req.Context() |
| 83 | var r sendServerNoticeRequest |
| 84 | resErr := httputil.UnmarshalJSONRequest(req, &r) // req |
| 85 | if resErr != nil { |
| 86 | return *resErr |
| 87 | } |
| 88 | |
| 89 | // check that all required fields are set |
| 90 | if !r.valid() { |
| 91 | return util.JSONResponse{ |
| 92 | Code: http.StatusBadRequest, |
| 93 | JSON: jsonerror.BadJSON("Invalid request"), |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // get rooms for specified user |
| 98 | allUserRooms := []string{} |
| 99 | userRooms := api.QueryRoomsForUserResponse{} |
| 100 | // Get rooms the user is either joined, invited or has left. |
| 101 | for _, membership := range []string{"join", "invite", "leave"} { |
| 102 | if err := rsAPI.QueryRoomsForUser(ctx, &api.QueryRoomsForUserRequest{ |
| 103 | UserID: r.UserID, |
| 104 | WantMembership: membership, |
| 105 | }, &userRooms); err != nil { |
| 106 | return util.ErrorResponse(err) |
| 107 | } |
| 108 | allUserRooms = append(allUserRooms, userRooms.RoomIDs...) |
| 109 | } |
| 110 | |
| 111 | // get rooms of the sender _server |
| 112 | senderUserID := fmt.Sprintf("@%s:%s", cfgNotices.LocalPart, cfgClient.Matrix.ServerName) |
| 113 | senderRooms := api.QueryRoomsForUserResponse{} |
no test coverage detected