Skip to content

Commit

Permalink
Merge pull request #150 from systemli/add-urls-for-bridges-to-message…
Browse files Browse the repository at this point in the history
…-response

✨ Add URLs for Bridges to Message Response
  • Loading branch information
0x46616c6b authored Oct 16, 2022
2 parents 6ae8695 + 0f268a1 commit c952ec7
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 20 deletions.
37 changes: 18 additions & 19 deletions internal/api/response/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@ import (
"fmt"
"time"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/systemli/ticker/internal/config"
"github.com/systemli/ticker/internal/storage"
)

type Message struct {
ID int `json:"id"`
CreationDate time.Time `json:"creation_date"`
Text string `json:"text"`
Ticker int `json:"ticker"`
TweetID string `json:"tweet_id"`
TweetUser string `json:"tweet_user"`
TelegramMessages []tgbotapi.Message `json:"telegram_messages"`
GeoInformation string `json:"geo_information"`
Attachments []MessageAttachment `json:"attachments"`
ID int `json:"id"`
CreationDate time.Time `json:"creation_date"`
Text string `json:"text"`
Ticker int `json:"ticker"`
TwitterURL string `json:"twitter_url,omitempty"`
TelegramURL string `json:"telegram_url,omitempty"`
MastodonURL string `json:"mastodon_url,omitempty"`
GeoInformation string `json:"geo_information"`
Attachments []MessageAttachment `json:"attachments"`
}

type MessageAttachment struct {
Expand All @@ -36,15 +35,15 @@ func MessageResponse(message storage.Message, config config.Config) Message {
}

return Message{
ID: message.ID,
CreationDate: message.CreationDate,
Text: message.Text,
Ticker: message.Ticker,
TweetID: message.Tweet.ID,
TweetUser: message.Tweet.UserName,
TelegramMessages: message.Telegram.Messages,
GeoInformation: string(m),
Attachments: attachments,
ID: message.ID,
CreationDate: message.CreationDate,
Text: message.Text,
Ticker: message.Ticker,
TwitterURL: message.TwitterURL(),
TelegramURL: message.TelegramURL(),
MastodonURL: message.MastodonURL(),
GeoInformation: string(m),
Attachments: attachments,
}
}

Expand Down
28 changes: 28 additions & 0 deletions internal/api/response/message_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package response

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/systemli/ticker/internal/config"
"github.com/systemli/ticker/internal/storage"
)

func TestMessagesResponse(t *testing.T) {
config := config.Config{UploadURL: "https://upload.example.com"}
message := storage.NewMessage()
message.Attachments = []storage.Attachment{{UUID: "uuid", Extension: "jpg"}}

response := MessagesResponse([]storage.Message{message}, config)

assert.Equal(t, 1, len(response))
assert.Empty(t, response[0].TwitterURL)
assert.Empty(t, response[0].TelegramURL)
assert.Empty(t, response[0].MastodonURL)
assert.Equal(t, `{"type":"FeatureCollection","features":[]}`, response[0].GeoInformation)
assert.Equal(t, 1, len(response[0].Attachments))

attachments := response[0].Attachments

assert.Equal(t, "https://upload.example.com/media/uuid.jpg", attachments[0].URL)
}
47 changes: 47 additions & 0 deletions internal/api/response/timeline.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package response

import (
"fmt"
"time"

"github.com/systemli/ticker/internal/config"
"github.com/systemli/ticker/internal/storage"
)

type Timeline []TimelineEntry

type TimelineEntry struct {
ID int `json:"id"`
CreationDate time.Time `json:"creation_date"`
Text string `json:"text"`
GeoInformation string `json:"geo_information"`
Attachments []MessageAttachment `json:"attachments"`
}

type Attachment struct {
URL string `json:"url"`
ContentType string `json:"content_type"`
}

func TimelineResponse(messages []storage.Message, config config.Config) []TimelineEntry {
timeline := make([]TimelineEntry, 0)
for _, message := range messages {
m, _ := message.GeoInformation.MarshalJSON()

var attachments []MessageAttachment
for _, attachment := range message.Attachments {
name := fmt.Sprintf("%s.%s", attachment.UUID, attachment.Extension)
attachments = append(attachments, MessageAttachment{URL: MediaURL(config.UploadURL, name), ContentType: attachment.ContentType})
}

timeline = append(timeline, TimelineEntry{
ID: message.ID,
CreationDate: message.CreationDate,
Text: message.Text,
GeoInformation: string(m),
Attachments: attachments,
})

}
return timeline
}
25 changes: 25 additions & 0 deletions internal/api/response/timeline_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package response

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/systemli/ticker/internal/config"
"github.com/systemli/ticker/internal/storage"
)

func TestTimelineResponse(t *testing.T) {
config := config.Config{UploadURL: "https://upload.example.com"}
message := storage.NewMessage()
message.Attachments = []storage.Attachment{{UUID: "uuid", Extension: "jpg"}}

response := TimelineResponse([]storage.Message{message}, config)

assert.Equal(t, 1, len(response))
assert.Equal(t, `{"type":"FeatureCollection","features":[]}`, response[0].GeoInformation)
assert.Equal(t, 1, len(response[0].Attachments))

attachments := response[0].Attachments

assert.Equal(t, "https://upload.example.com/media/uuid.jpg", attachments[0].URL)
}
2 changes: 1 addition & 1 deletion internal/api/timeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ func (h *handler) GetTimeline(c *gin.Context) {
return
}

c.JSON(http.StatusOK, response.SuccessResponse(map[string]interface{}{"messages": response.MessagesResponse(messages, h.config)}))
c.JSON(http.StatusOK, response.SuccessResponse(map[string]interface{}{"messages": response.TimelineResponse(messages, h.config)}))
}
26 changes: 26 additions & 0 deletions internal/storage/message.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package storage

import (
"fmt"
"time"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
Expand Down Expand Up @@ -56,3 +57,28 @@ func (m *Message) AddAttachments(uploads []Upload) {
m.AddAttachment(upload)
}
}

func (m *Message) TwitterURL() string {
if m.Tweet.ID == "" {
return ""
}

return fmt.Sprintf("https://twitter.com/%s/status/%s", m.Tweet.UserName, m.Tweet.ID)
}

func (m *Message) TelegramURL() string {
if len(m.Telegram.Messages) == 0 {
return ""
}

message := m.Telegram.Messages[0]
return fmt.Sprintf("https://t.me/%s/%d", message.Chat.UserName, message.MessageID)
}

func (m *Message) MastodonURL() string {
if m.Mastodon.ID == "" {
return ""
}

return m.Mastodon.URL
}
61 changes: 61 additions & 0 deletions internal/storage/message_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package storage

import (
"testing"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/mattn/go-mastodon"
"github.com/stretchr/testify/assert"
)

func TestAddAttachments(t *testing.T) {
upload := NewUpload("image.jpg", "image/jped", 1)
message := NewMessage()
message.AddAttachments([]Upload{upload})

assert.Equal(t, 1, len(message.Attachments))
}

func TestTwitterURL(t *testing.T) {
message := NewMessage()

assert.Empty(t, message.TwitterURL())

message.Tweet.ID = "1"
message.Tweet.UserName = "systemli"

assert.Equal(t, "https://twitter.com/systemli/status/1", message.TwitterURL())
}

func TestTelegramURL(t *testing.T) {
message := NewMessage()

assert.Empty(t, message.TelegramURL())

message.Telegram = TelegramMeta{
Messages: []tgbotapi.Message{
{
MessageID: 1,
Chat: &tgbotapi.Chat{
UserName: "systemli",
}},
},
}

assert.Equal(t, "https://t.me/systemli/1", message.TelegramURL())

}

func TestMastodonURL(t *testing.T) {
message := NewMessage()

assert.Empty(t, message.MastodonURL())

url := "https://mastodon.social/web/@systemli/1"
message.Mastodon = mastodon.Status{
ID: "1",
URL: url,
}

assert.Equal(t, url, message.MastodonURL())
}

0 comments on commit c952ec7

Please sign in to comment.