Skip to content

Commit

Permalink
Use deleted by sender visibity for message unsent on IG channels
Browse files Browse the repository at this point in the history
  • Loading branch information
norkans7 committed Feb 11, 2022
1 parent 952c845 commit 7d54d0f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
10 changes: 6 additions & 4 deletions backends/rapidpro/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,22 @@ func (b *backend) RemoveURNfromContact(ctx context.Context, c courier.Channel, c
return urn, nil
}

const updateMsgVisibilityDeleted = `
const updateMsgVisibilityDeletedBySender = `
UPDATE
msgs_msg
SET
visibility = 'D'
visibility = 'X',
text = '',
attachments = '{}'
WHERE
msgs_msg.id = (SELECT m."id" FROM "msgs_msg" m INNER JOIN "channels_channel" c ON (m."channel_id" = c."id") WHERE (c."uuid" = $1 AND m."external_id" = $2 AND m."direction" = 'I')
msgs_msg.id = (SELECT m."id" FROM "msgs_msg" m INNER JOIN "channels_channel" c ON (m."channel_id" = c."id") WHERE (c."uuid" = $1 AND m."external_id" = $2 AND m."direction" = 'I'))
RETURNING
msgs_msg.id
`

// DeleteMsgWithExternalID delete a message we receive an event that it should be deleted
func (b *backend) DeleteMsgWithExternalID(ctx context.Context, channel courier.Channel, externalID string) error {
_, err := b.db.ExecContext(ctx, updateMsgVisibilityDeleted, channel.UUID().String(), externalID)
_, err := b.db.ExecContext(ctx, updateMsgVisibilityDeletedBySender, string(channel.UUID().String()), externalID)
if err != nil {
return err
}
Expand Down
29 changes: 29 additions & 0 deletions backends/rapidpro/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,35 @@ func (ts *BackendTestSuite) TestCheckMsgExists() {
ts.Nil(err)
}

func (ts *BackendTestSuite) TestDeleteMsgWithExternalID() {
knChannel := ts.getChannel("KN", "dbc126ed-66bc-4e28-b67b-81dc3327c95d")

ctx := context.Background()

// no error for invalid external ID
err := ts.b.DeleteMsgWithExternalID(ctx, knChannel, "ext-invalid")
ts.Nil(err)

// cannot change out going messages
err = ts.b.DeleteMsgWithExternalID(ctx, knChannel, "ext1")
ts.Nil(err)

m := readMsgFromDB(ts.b, courier.NewMsgID(10000))
ts.Equal(m.Text_, "test message")
ts.Equal(len(m.Attachments()), 0)
ts.Equal(m.Visibility_, MsgVisibility("V"))

// for incoming messages mark them deleted by sender and readact their text and clear their attachments
err = ts.b.DeleteMsgWithExternalID(ctx, knChannel, "ext2")
ts.Nil(err)

m = readMsgFromDB(ts.b, courier.NewMsgID(10002))
ts.Equal(m.Text_, "")
ts.Equal(len(m.Attachments()), 0)
ts.Equal(m.Visibility_, MsgVisibility("X"))

}

func (ts *BackendTestSuite) TestContact() {
knChannel := ts.getChannel("KN", "dbc126ed-66bc-4e28-b67b-81dc3327c95d")
urn, _ := urns.NewTelURNForCountry("12065551518", "US")
Expand Down

0 comments on commit 7d54d0f

Please sign in to comment.