Skip to content

Commit

Permalink
Apply review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
0x46616c6b committed May 21, 2024
1 parent abc34d5 commit 8bb04b5
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 216 deletions.
8 changes: 4 additions & 4 deletions internal/api/tickers.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,8 @@ func (h *handler) PutTickerSignalGroup(c *gin.Context) {
if body.GroupName != "" && body.GroupDescription != "" {
ticker.SignalGroup.GroupName = body.GroupName
ticker.SignalGroup.GroupDescription = body.GroupDescription
err = signal.CreateOrUpdateGroup(&ticker.SignalGroup, h.config)
groupClient := signal.NewGroupClient(h.config)
err = groupClient.CreateOrUpdateGroup(&ticker.SignalGroup)
if err != nil {
log.WithError(err).Error("failed to create or update group")
c.JSON(http.StatusBadRequest, response.ErrorResponse(response.CodeDefault, response.SignalGroupError))
Expand All @@ -332,11 +333,10 @@ func (h *handler) DeleteTickerSignalGroup(c *gin.Context) {
return
}

err = signal.QuitGroup(h.config, ticker.SignalGroup.GroupID)
groupClient := signal.NewGroupClient(h.config)
err = groupClient.QuitGroup(ticker.SignalGroup.GroupID)
if err != nil {
log.WithError(err).Error("failed to quit group")
// c.JSON(http.StatusNotFound, response.ErrorResponse(response.CodeDefault, response.SignalGroupError))
// return
}

ticker.SignalGroup.Reset()
Expand Down
72 changes: 68 additions & 4 deletions internal/bridge/signal_group.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package bridge

import (
"context"
"encoding/base64"
"errors"
"fmt"
"os"

"github.com/systemli/ticker/internal/config"
"github.com/systemli/ticker/internal/signal"
"github.com/systemli/ticker/internal/storage"
Expand All @@ -11,29 +17,87 @@ type SignalGroupBridge struct {
storage storage.Storage
}

type SignalGroupResponse struct {
Timestamp int `json:"timestamp"`
}

func (sb *SignalGroupBridge) Send(ticker storage.Ticker, message *storage.Message) error {
if !sb.config.SignalGroup.Enabled() || !ticker.SignalGroup.Connected() || !ticker.SignalGroup.Active {
return nil
}

err := signal.SendGroupMessage(sb.config, sb.storage, ticker.SignalGroup.GroupID, message)
ctx := context.Background()
client := signal.Client(sb.config)

var attachments []string
if len(message.Attachments) > 0 {
for _, attachment := range message.Attachments {
upload, err := sb.storage.FindUploadByUUID(attachment.UUID)
if err != nil {
log.WithError(err).Error("failed to find upload")
continue
}

fileContent, err := os.ReadFile(upload.FullPath(sb.config.Upload.Path))
if err != nil {
log.WithError(err).Error("failed to read file")
continue
}
fileBase64 := base64.StdEncoding.EncodeToString(fileContent)
aString := fmt.Sprintf("data:%s;filename=%s;base64,%s", upload.ContentType, upload.FileName, fileBase64)

Check failure on line 47 in internal/bridge/signal_group.go

View workflow job for this annotation

GitHub Actions / GolangCI

printf: fmt.Sprintf format %s arg upload.FileName is a func value, not called (govet)

Check failure on line 47 in internal/bridge/signal_group.go

View workflow job for this annotation

GitHub Actions / Test

fmt.Sprintf format %s arg upload.FileName is a func value, not called
attachments = append(attachments, aString)
}
}

params := struct {
Account string `json:"account"`
GroupID string `json:"group-id"`
Message string `json:"message"`
Attachment []string `json:"attachment"`
}{
Account: sb.config.SignalGroup.Account,
GroupID: ticker.SignalGroup.GroupID,
Message: message.Text,
Attachment: attachments,
}

var response SignalGroupResponse
err := client.CallFor(ctx, &response, "send", &params)
if err != nil {
return err
}
if response.Timestamp == 0 {
return errors.New("SignalGroup Bridge: No timestamp in send response")
}

message.SignalGroup = storage.SignalGroupMeta{
Timestamp: response.Timestamp,
}

return nil
}

func (sb *SignalGroupBridge) Delete(ticker storage.Ticker, message *storage.Message) error {
if !sb.config.SignalGroup.Enabled() || !ticker.SignalGroup.Connected() || !ticker.SignalGroup.Active || message.SignalGroup.Timestamp == nil {
if !sb.config.SignalGroup.Enabled() || !ticker.SignalGroup.Connected() || !ticker.SignalGroup.Active || message.SignalGroup.Timestamp != 0 {
return nil
}

err := signal.DeleteMessage(sb.config, ticker.SignalGroup.GroupID, message)
client := signal.Client(sb.config)
params := struct {
Account string `json:"account"`
GroupID string `json:"group-id"`
TargetTimestamp int `json:"target-timestamp"`
}{
Account: sb.config.SignalGroup.Account,
GroupID: ticker.SignalGroup.GroupID,
TargetTimestamp: message.SignalGroup.Timestamp,
}

var response SignalGroupResponse
err := client.CallFor(context.Background(), &response, "remoteDelete", &params)
if err != nil {
return err
}

return nil

}
3 changes: 1 addition & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ type Telegram struct {

type SignalGroup struct {
ApiUrl string `yaml:"api_url"`
ApiUser string `yaml:"api_user"`
ApiPass string `yaml:"api_pass"`
Avatar string `yaml:"avatar"`
Account string
}

Expand Down
Loading

0 comments on commit 8bb04b5

Please sign in to comment.