Skip to content
This repository has been archived by the owner on Mar 28, 2023. It is now read-only.

Manually send order messages and trigger offline message scan #1584

Merged
merged 27 commits into from
Jul 29, 2019
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
b5b179d
first pass at manually sending order messages and triggering offline …
amangale May 17, 2019
9cdef62
remove reduntant return statement
amangale May 17, 2019
02279da
lint fixes
amangale May 17, 2019
7235b68
rename order_messages to messages and in general make this pr inclusi…
amangale May 21, 2019
ee1b999
add repo.Message as a wrapper for pb.Message
amangale May 22, 2019
1ef4342
add repo.Message as a wrapper for pb.Message
amangale May 22, 2019
bb5f5fe
use repo.Message in the messages datastore
amangale May 22, 2019
d18ec20
use repo.Message in message handler and the resend api endpoint
amangale May 22, 2019
75bdee7
lint fixes
amangale Jun 24, 2019
26428f1
Merge remote-tracking branch 'origin/master' into manual_order_messaging
amangale Jul 10, 2019
63624ff
add messages composite index for peerID messageType tuple
amangale Jul 10, 2019
d4d9305
handle errors in core/net put messages
amangale Jul 10, 2019
cae2cde
fix repo messages
amangale Jul 10, 2019
1366f14
fix migration 24 and add unit test
amangale Jul 10, 2019
bcca56d
lint fix
amangale Jul 10, 2019
35fddca
lint fix
amangale Jul 10, 2019
db438f9
fix panic on pre initialization invoking of offline message scan
amangale Jul 11, 2019
a6e1da5
decrease the manual scan interval to 2 minutes
amangale Jul 12, 2019
9c860ba
reduce retry interval to 1 minute
amangale Jul 16, 2019
26c47e8
update the utxo wallets to invoke the transaction listeners for ORDER…
amangale Jul 17, 2019
4ce3238
fix reviewed issues
amangale Jul 18, 2019
3b17209
remove GetMessageByID
amangale Jul 19, 2019
81187cd
Merge branch 'master' into manual_order_messaging
amangale Jul 23, 2019
57a6bfd
fix error logging and add offline message send after unsuccessful mes…
amangale Jul 24, 2019
4b37451
Merge branch 'manual_order_messaging' of https://github.com/OpenBazaa…
amangale Jul 24, 2019
b948af3
merge master
amangale Jul 24, 2019
0ee517e
fix indentation and modify the offline scan conditional
amangale Jul 26, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions api/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ func post(i *jsonAPIHandler, path string, w http.ResponseWriter, r *http.Request
i.POSTPost(w, r)
case strings.HasPrefix(path, "/ob/bulkupdatecurrency"):
i.POSTBulkUpdateCurrency(w, r)
case strings.HasPrefix(path, "/ob/resendordermessage"):
i.POSTResendOrderMessage(w, r)
default:
ErrorResponse(w, http.StatusNotFound, "Not Found")
}
Expand Down Expand Up @@ -203,6 +205,8 @@ func get(i *jsonAPIHandler, path string, w http.ResponseWriter, r *http.Request)
i.GETPosts(w, r)
case strings.HasPrefix(path, "/ob/post"):
i.GETPost(w, r)
case strings.HasPrefix(path, "/ob/scanofflinemessages"):
i.GETScanOfflineMessages(w, r)
default:
ErrorResponse(w, http.StatusNotFound, "Not Found")
}
Expand Down
89 changes: 82 additions & 7 deletions api/jsonapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ type jsonAPIHandler struct {
node *core.OpenBazaarNode
}

var lastManualScan time.Time

func newJSONAPIHandler(node *core.OpenBazaarNode, authCookie http.Cookie, config schema.APIConfig) *jsonAPIHandler {
allowedIPs := make(map[string]bool)
for _, ip := range config.AllowedIPs {
Expand Down Expand Up @@ -1480,7 +1482,7 @@ func (i *jsonAPIHandler) GETProfile(w http.ResponseWriter, r *http.Request) {
return
}
if profile.PeerID != peerID {
ErrorResponse(w, http.StatusNotFound, "invalid profile: peer id mismatch on found profile")
ErrorResponse(w, http.StatusNotFound, "invalid profile: peer id mismatch on found profile")
placer14 marked this conversation as resolved.
Show resolved Hide resolved
return
}
w.Header().Set("Cache-Control", "public, max-age=600, immutable")
Expand Down Expand Up @@ -1889,7 +1891,7 @@ func (i *jsonAPIHandler) GETModerators(w http.ResponseWriter, r *http.Request) {
if err != nil {
return
}
i.node.Broadcast <- repo.PremarshalledNotifier{b}
i.node.Broadcast <- repo.PremarshalledNotifier{Payload: b}
} else {
type wsResp struct {
ID string `json:"id"`
Expand All @@ -1900,7 +1902,7 @@ func (i *jsonAPIHandler) GETModerators(w http.ResponseWriter, r *http.Request) {
if err != nil {
return
}
i.node.Broadcast <- repo.PremarshalledNotifier{data}
i.node.Broadcast <- repo.PremarshalledNotifier{Payload: data}
}
}(p)
}
Expand Down Expand Up @@ -2824,7 +2826,7 @@ func (i *jsonAPIHandler) POSTFetchProfiles(w http.ResponseWriter, r *http.Reques
if err != nil {
return
}
i.node.Broadcast <- repo.PremarshalledNotifier{ret}
i.node.Broadcast <- repo.PremarshalledNotifier{Payload: ret}
}

pro, err := i.node.FetchProfile(pid, useCache)
Expand All @@ -2849,7 +2851,7 @@ func (i *jsonAPIHandler) POSTFetchProfiles(w http.ResponseWriter, r *http.Reques
respondWithError("error Marshalling to JSON")
return
}
i.node.Broadcast <- repo.PremarshalledNotifier{b}
i.node.Broadcast <- repo.PremarshalledNotifier{Payload: b}
}(p)
}
}()
Expand Down Expand Up @@ -3601,7 +3603,7 @@ func (i *jsonAPIHandler) POSTFetchRatings(w http.ResponseWriter, r *http.Request
if err != nil {
return
}
i.node.Broadcast <- repo.PremarshalledNotifier{ret}
i.node.Broadcast <- repo.PremarshalledNotifier{Payload: ret}
}
ratingBytes, err := ipfs.Cat(i.node.IpfsNode, rid, time.Minute)
if err != nil {
Expand Down Expand Up @@ -3640,7 +3642,7 @@ func (i *jsonAPIHandler) POSTFetchRatings(w http.ResponseWriter, r *http.Request
respondWithError("error marshalling rating")
return
}
i.node.Broadcast <- repo.PremarshalledNotifier{b}
i.node.Broadcast <- repo.PremarshalledNotifier{Payload: b}
}(r)
}
}
Expand Down Expand Up @@ -4220,3 +4222,76 @@ func (i *jsonAPIHandler) GETPost(w http.ResponseWriter, r *http.Request) {
}
SanitizedResponseM(w, out, new(pb.SignedPost))
}

// POSTSendOrderMessage - used to manually send an order message
func (i *jsonAPIHandler) POSTResendOrderMessage(w http.ResponseWriter, r *http.Request) {
type sendRequest struct {
OrderID string `json:"orderID"`
MessageType string `json:"messageType"`
}

var args sendRequest
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&args)
if err != nil {
ErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

if args.OrderID == "" {
ErrorResponse(w, http.StatusBadRequest, core.ErrOrderNotFound.Error())
return
}

var msgType int32
var ok bool

if msgType, ok = pb.Message_MessageType_value[args.MessageType]; !ok {
ErrorResponse(w, http.StatusBadRequest, "invalid order message type")
return
}

msg, peerID, err := i.node.Datastore.Messages().
GetByOrderIDType(args.OrderID, pb.Message_MessageType(msgType))
if err != nil || msg == nil || msg.Msg.GetPayload() == nil {
ErrorResponse(w, http.StatusBadRequest, "order message not found")
return
}

p, err := peer.IDB58Decode(peerID)
if err != nil {
ErrorResponse(w, http.StatusBadRequest, "invalid peer id")
return
}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

err = i.node.Service.SendMessage(ctx, p, &msg.Msg)
if err != nil {
// If send message failed, try sending offline message
log.Errorf("resending message failed: %v", err)
placer14 marked this conversation as resolved.
Show resolved Hide resolved
err = i.node.SendOfflineMessage(p, nil, &msg.Msg)
if err != nil {
log.Errorf("resending offline message failed: %v", err)
ErrorResponse(w, http.StatusBadRequest, "order message not sent")
return
}
}

SanitizedResponse(w, `{}`)
}

// GETScanOfflineMessages - used to manually trigger offline message scan
func (i *jsonAPIHandler) GETScanOfflineMessages(w http.ResponseWriter, r *http.Request) {
t := time.Since(lastManualScan).Minutes()
if t < 100000000 {
placer14 marked this conversation as resolved.
Show resolved Hide resolved
if t >= 1 {
i.node.MessageRetriever.RunOnce()
lastManualScan = time.Now()
}
} else {
lastManualScan = time.Now()
}
SanitizedResponse(w, `{}`)
}
Loading