-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #150 from systemli/add-urls-for-bridges-to-message…
…-response ✨ Add URLs for Bridges to Message Response
- Loading branch information
Showing
7 changed files
with
206 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |