From ffa33ea299c53d430402ce1cf45678717d4a6f4f Mon Sep 17 00:00:00 2001 From: bel Date: Mon, 17 Jan 2022 21:57:33 -0700 Subject: [PATCH] when a job dies, delete images associated with it --- broker/testdata/ntgvision_response.json | 34 ++++++++++++++++++++ main.go | 41 +++++++++++++++++-------- message/matrix.go | 32 ++++++++++++++----- message/matrix_test.go | 6 ++-- 4 files changed, 91 insertions(+), 22 deletions(-) diff --git a/broker/testdata/ntgvision_response.json b/broker/testdata/ntgvision_response.json index 0d4f101..0422e0e 100644 --- a/broker/testdata/ntgvision_response.json +++ b/broker/testdata/ntgvision_response.json @@ -1,2 +1,36 @@ [ + { + "id": 4650337, + "sDate": "01/12/22", + "sCity": "Advance", + "sState": "NC", + "sdh": null, + "cDate": "01/13/22", + "cCity": "Sacramento", + "cState": "CA", + "cdh": null, + "stopCnt": 2, + "miles": 578, + "weight": 5000, + "equip": "Str Truck W/ Lift Gate", + "temp": "", + "alertReasons": [] + }, + { + "id": 4650338, + "sDate": "01/12/22", + "sCity": "Advance", + "sState": "NC", + "sdh": null, + "cDate": "01/13/22", + "cCity": "Winston-Salem", + "cState": "NC", + "cdh": null, + "stopCnt": 2, + "miles": 378, + "weight": 2500, + "equip": "107 tall", + "temp": "", + "alertReasons": [] + } ] diff --git a/main.go b/main.go index d5348b5..d9f831c 100644 --- a/main.go +++ b/main.go @@ -321,9 +321,10 @@ func getJobs() ([]broker.Job, error) { } type recordedJob struct { - Job broker.Job - SentNS int64 - MatrixID string + Job broker.Job + SentNS int64 + MatrixID string + MatrixImageIDs []string } func updateDeadJobs(jobs []broker.Job) error { @@ -353,7 +354,14 @@ func updateDeadJobs(jobs []broker.Job) error { if err := message.NewMatrix().Update(recorded.MatrixID, recorded.Job.FormatMultilineTextDead()); err != nil { return err } - db.Set(listEntry, nil) + if err := db.Set(listEntry, nil); err != nil { + return err + } + for _, imageid := range recorded.MatrixImageIDs { + if err := message.NewMatrix().Remove(imageid); err != nil { + return err + } + } } } return nil @@ -388,13 +396,14 @@ func sendJob(job broker.Job) (bool, error) { if err != nil { return false, err } - func() { + recordedJob := recordedJob{ + Job: job, + SentNS: time.Now().UnixNano(), + MatrixID: id, + } + defer func() { db := config.Get().DB() - b, err := json.Marshal(recordedJob{ - Job: job, - SentNS: time.Now().UnixNano(), - MatrixID: id, - }) + b, err := json.Marshal(recordedJob) if err != nil { logtr.Errorf("failed to marshal recorded job: %v", err) return @@ -461,24 +470,30 @@ func sendJob(job broker.Job) (bool, error) { uri = fmt.Sprintf("%s&zoom=%d", uri, maps.Pathed.Zoom.Override) } logtr.Debugf("sending pathed image: %s", uri) - if err := sender.SendImage(uri); err != nil { + pathedid, err := sender.SendImageTracked(uri) + if err != nil { return true, err } + recordedJob.MatrixImageIDs = append(recordedJob.MatrixImageIDs, pathedid) } } if maps.Pickup { uri := fmt.Sprintf(maps.URIFormat, pickup, pickup) logtr.Debugf("sending pickup image: %s", uri) - if err := sender.SendImage(uri); err != nil { + pickupid, err := sender.SendImageTracked(uri) + if err != nil { return true, err } + recordedJob.MatrixImageIDs = append(recordedJob.MatrixImageIDs, pickupid) } if maps.Dropoff { uri := fmt.Sprintf(maps.URIFormat, dropoff, dropoff) logtr.Debugf("sending dropoff image: %s", uri) - if err := sender.SendImage(uri); err != nil { + dropid, err := sender.SendImageTracked(uri) + if err != nil { return true, err } + recordedJob.MatrixImageIDs = append(recordedJob.MatrixImageIDs, dropid) } return true, nil } diff --git a/message/matrix.go b/message/matrix.go index ef26306..42062ee 100644 --- a/message/matrix.go +++ b/message/matrix.go @@ -125,6 +125,19 @@ func (m *Matrix) Receive() ([]Message, error) { return messages, nil } +func (m Matrix) Remove(id string) error { + if m.mock { + logtr.Infof("matrix.Remove(%s)", id) + return nil + } + c, err := m.getclient() + if err != nil { + return err + } + _, err = c.RedactEvent(m.room, id, &gomatrix.ReqRedact{Reason: "stale"}) + return err +} + func (m Matrix) Update(id, text string) error { if m.mock { logtr.Infof("matrix.Update(%s)", text) @@ -185,36 +198,41 @@ func (m Matrix) SendTracked(text string) (string, error) { } func (m Matrix) SendImage(uri string) error { + _, err := m.SendImageTracked(uri) + return err +} + +func (m Matrix) SendImageTracked(uri string) (string, error) { if m.mock { logtr.Infof("matrix.SendImage(%s)", uri) - return nil + return "", nil } response, err := http.Get(uri) if err != nil { - return err + return "", err } if response.StatusCode != http.StatusOK { b, _ := ioutil.ReadAll(response.Body) response.Body.Close() - return fmt.Errorf("failed to get %s: (%d) %s", uri, response.StatusCode, b) + return "", fmt.Errorf("failed to get %s: (%d) %s", uri, response.StatusCode, b) } b, err := ioutil.ReadAll(response.Body) response.Body.Close() if err != nil { - return err + return "", err } c, err := m.getclient() if err != nil { - return err + return "", err } mediaUpload, err := c.UploadToContentRepo(bytes.NewReader(b), "image/jpeg", int64(len(b))) if err != nil { - return err + return "", err } publicURI := mediaUpload.ContentURI resp, err := c.SendImage(m.room, "img", publicURI) logtr.Debugf("sent image %s => %s: %+v", uri, publicURI, resp) - return err + return resp.EventID, err } func SetMatrixContinuation(continuation string) { diff --git a/message/matrix_test.go b/message/matrix_test.go index f6818dc..4f4c687 100644 --- a/message/matrix_test.go +++ b/message/matrix_test.go @@ -10,9 +10,11 @@ import ( "github.com/google/uuid" ) -func TestMatrixSend(t *testing.T) { +func TestMatrixSendDel(t *testing.T) { sender := testMatrix(t) - if err := sender.Send("hello world from unittest"); err != nil { + if id, err := sender.SendTracked("hello world from unittest"); err != nil { + t.Fatal(err) + } else if err := sender.Remove(id); err != nil { t.Fatal(err) } }