Skip to content

Commit

Permalink
Merge pull request #4 from ErikKalkoken/fix-issue-3
Browse files Browse the repository at this point in the history
Fix issue 3
  • Loading branch information
ErikKalkoken authored Oct 5, 2024
2 parents bd14f18 + 39bf44f commit 70423c8
Show file tree
Hide file tree
Showing 11 changed files with 229 additions and 50 deletions.
4 changes: 2 additions & 2 deletions FyneApp.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ Website = "https://github.com/ErikKalkoken/evebuddy"
Icon = "icon.png"
Name = "EVE Buddy"
ID = "io.github.erikkalkoken.evebuddy"
Version = "0.2.3"
Build = 2
Version = "0.2.4"
Build = 1

[Release]
BuildName = "evebuddy"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# EVE Buddy

A companion app for Eve Online players
A companion app for Eve Online players available on Windows, Linux and macOS.

[![GitHub Release](https://img.shields.io/github/v/release/ErikKalkoken/evebuddy)](https://github.com/ErikKalkoken/evebuddy/releases)
[![build status](https://github.com/ErikKalkoken/evebuddy/actions/workflows/ci-cd.yml/badge.svg)](https://github.com/ErikKalkoken/evebuddy/actions/workflows/ci-cd.yml)
Expand Down
4 changes: 2 additions & 2 deletions internal/app/evenotification/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ func (s *EveNotificationService) renderBilling(ctx context.Context, type_ Type,
return title, body, err
}
var external1 string
if x, ok := entities[int32(data.ExternalID)]; ok {
if x, ok := entities[int32(data.ExternalID)]; ok && x.Name != "" {
external1 = x.Name
} else {
external1 = "?"
}
var external2 string
if x, ok := entities[int32(data.ExternalID2)]; ok {
if x, ok := entities[int32(data.ExternalID2)]; ok && x.Name != "" {
external2 = x.Name
} else {
external2 = "?"
Expand Down
74 changes: 74 additions & 0 deletions internal/app/evenotification/billing_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package evenotification_test

import (
"context"
"testing"
"time"

"github.com/ErikKalkoken/evebuddy/internal/app"
"github.com/ErikKalkoken/evebuddy/internal/app/evenotification"
"github.com/ErikKalkoken/evebuddy/internal/app/eveuniverse"
"github.com/ErikKalkoken/evebuddy/internal/app/storage/testutil"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
)

func TestBilling(t *testing.T) {
db, st, factory := testutil.New()
defer db.Close()
httpmock.Activate()
defer httpmock.DeactivateAndReset()
eu := eveuniverse.New(st, nil)
en := evenotification.New()
en.EveUniverseService = eu
ctx := context.Background()
t.Run("CorpAllBillMsg full data", func(t *testing.T) {
// given
testutil.TruncateTables(db)
httpmock.Reset()
creditor := factory.CreateEveEntityCorporation(app.EveEntity{ID: 1000023})
debtor := factory.CreateEveEntityCorporation(app.EveEntity{ID: 98267621})
office := factory.CreateEveEntityInventoryType(app.EveEntity{ID: 27})
station := factory.CreateEveEntity(app.EveEntity{ID: 60003760, Category: app.EveEntityStation})
text := `
amount: 10000
billTypeID: 2
creditorID: 1000023
currentDate: 133678830021821155
debtorID: 98267621
dueDate: 133704743590000000
externalID: 27
externalID2: 60003760`
title, body, err := en.RenderESI(ctx, "CorpAllBillMsg", text, time.Now())
if assert.NoError(t, err) {
assert.Equal(t, "Bill issued for lease", title.ValueOrZero())
assert.Contains(t, body.ValueOrZero(), creditor.Name)
assert.Contains(t, body.ValueOrZero(), debtor.Name)
assert.Contains(t, body.ValueOrZero(), office.Name)
assert.Contains(t, body.ValueOrZero(), station.Name)
}
})
t.Run("CorpAllBillMsg partial data", func(t *testing.T) {
// given
testutil.TruncateTables(db)
httpmock.Reset()
creditor := factory.CreateEveEntityCorporation(app.EveEntity{ID: 1000023})
debtor := factory.CreateEveEntityCorporation(app.EveEntity{ID: 98267621})
text := `
amount: 10000
billTypeID: 2
creditorID: 1000023
currentDate: 133678830021821155
debtorID: 98267621
dueDate: 133704743590000000
externalID: 0
externalID2: 0`
title, body, err := en.RenderESI(ctx, "CorpAllBillMsg", text, time.Now())
if assert.NoError(t, err) {
assert.Equal(t, "Bill issued for lease", title.ValueOrZero())
assert.Contains(t, body.ValueOrZero(), creditor.Name)
assert.Contains(t, body.ValueOrZero(), debtor.Name)
assert.Contains(t, body.ValueOrZero(), "?")
}
})
}
39 changes: 1 addition & 38 deletions internal/app/evenotification/evenotification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,51 +18,14 @@ import (
"github.com/stretchr/testify/assert"
)

func TestRenderCharacterNotification(t *testing.T) {
db, st, factory := testutil.New()
defer db.Close()
httpmock.Activate()
defer httpmock.DeactivateAndReset()
eu := eveuniverse.New(st, nil)
en := evenotification.New()
en.EveUniverseService = eu
ctx := context.Background()
t.Run("should render notification", func(t *testing.T) {
// given
testutil.TruncateTables(db)
httpmock.Reset()
creditor := factory.CreateEveEntityCorporation(app.EveEntity{ID: 1000023})
debtor := factory.CreateEveEntityCorporation(app.EveEntity{ID: 98267621})
office := factory.CreateEveEntityInventoryType(app.EveEntity{ID: 27})
station := factory.CreateEveEntity(app.EveEntity{ID: 60003760, Category: app.EveEntityStation})
text := `
amount: 10000
billTypeID: 2
creditorID: 1000023
currentDate: 133678830021821155
debtorID: 98267621
dueDate: 133704743590000000
externalID: 27
externalID2: 60003760`
title, body, err := en.RenderESI(ctx, "CorpAllBillMsg", text, time.Now())
if assert.NoError(t, err) {
assert.Equal(t, "Bill issued for lease", title.ValueOrZero())
assert.Contains(t, body.ValueOrZero(), creditor.Name)
assert.Contains(t, body.ValueOrZero(), debtor.Name)
assert.Contains(t, body.ValueOrZero(), office.Name)
assert.Contains(t, body.ValueOrZero(), station.Name)
}
})
}

type notification struct {
NotificationID int `json:"notification_id"`
Type string `json:"type"`
Text string `json:"text"`
Timestamp time.Time `json:"timestamp"`
}

func TestRenderAllCharacterNotifications(t *testing.T) {
func TestShouldRenderAllNotifications(t *testing.T) {
data, err := os.ReadFile("testdata/notifications.json")
if err != nil {
panic(err)
Expand Down
3 changes: 3 additions & 0 deletions internal/app/evenotification/links.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ func makeEveWhoCharacterURL(id int32) string {
}

func makeEveEntityProfileLink(e *app.EveEntity) string {
if e == nil {
return ""
}
var url string
switch e.Category {
case app.EveEntityAlliance:
Expand Down
107 changes: 107 additions & 0 deletions internal/app/evenotification/tower_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package evenotification_test

import (
"context"
"testing"
"time"

"github.com/ErikKalkoken/evebuddy/internal/app"
"github.com/ErikKalkoken/evebuddy/internal/app/evenotification"
"github.com/ErikKalkoken/evebuddy/internal/app/eveuniverse"
"github.com/ErikKalkoken/evebuddy/internal/app/storage"
"github.com/ErikKalkoken/evebuddy/internal/app/storage/testutil"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
)

func TestTowerNotification(t *testing.T) {
db, st, factory := testutil.New()
defer db.Close()
httpmock.Activate()
defer httpmock.DeactivateAndReset()
eu := eveuniverse.New(st, nil)
en := evenotification.New()
en.EveUniverseService = eu
ctx := context.Background()
t.Run("TowerAlertMsg full data", func(t *testing.T) {
// given
testutil.TruncateTables(db)
httpmock.Reset()
aggressorAlliance := factory.CreateEveEntityAlliance(app.EveEntity{ID: 3011})
factory.CreateEveEntityCorporation(app.EveEntity{ID: 2011})
factory.CreateEveEntityCharacter(app.EveEntity{ID: 1011})
type_ := factory.CreateEveType(storage.CreateEveTypeParams{ID: 16213})
moon := factory.CreateEveMoon(storage.CreateEveMoonParams{ID: 40161465})
text := `
aggressorAllianceID: 3011
aggressorCorpID: 2011
aggressorID: 1011
armorValue: 0.6950949076033535
hullValue: 1.0
moonID: 40161465
shieldValue: 0.3950949076033535
solarSystemID: 30002537
typeID: 16213`
title, body, err := en.RenderESI(ctx, "TowerAlertMsg", text, time.Now())
if assert.NoError(t, err) {
assert.Contains(t, title.ValueOrZero(), "is under attack")
assert.Contains(t, body.ValueOrZero(), aggressorAlliance.Name)
assert.Contains(t, body.ValueOrZero(), moon.Name)
assert.Contains(t, body.ValueOrZero(), type_.Name)
}
})
t.Run("TowerAlertMsg partial data 1", func(t *testing.T) {
// given
testutil.TruncateTables(db)
httpmock.Reset()
aggressorAlliance := factory.CreateEveEntityAlliance(app.EveEntity{ID: 3011})
factory.CreateEveEntityCorporation(app.EveEntity{ID: 2011})
factory.CreateEveEntityCharacter(app.EveEntity{ID: 1011})
type_ := factory.CreateEveType(storage.CreateEveTypeParams{ID: 16213})
moon := factory.CreateEveMoon(storage.CreateEveMoonParams{ID: 40161465})
text := `
aggressorAllianceID: 3011
aggressorCorpID: 2011
aggressorID: 0
armorValue: 0.6950949076033535
hullValue: 1.0
moonID: 40161465
shieldValue: 0.3950949076033535
solarSystemID: 30002537
typeID: 16213`
title, body, err := en.RenderESI(ctx, "TowerAlertMsg", text, time.Now())
if assert.NoError(t, err) {
assert.Contains(t, title.ValueOrZero(), "is under attack")
assert.Contains(t, body.ValueOrZero(), aggressorAlliance.Name)
assert.Contains(t, body.ValueOrZero(), moon.Name)
assert.Contains(t, body.ValueOrZero(), type_.Name)
}
})
t.Run("TowerAlertMsg partial data 1", func(t *testing.T) {
// given
testutil.TruncateTables(db)
httpmock.Reset()
aggressorAlliance := factory.CreateEveEntityAlliance(app.EveEntity{ID: 3011})
factory.CreateEveEntityCorporation(app.EveEntity{ID: 2011})
factory.CreateEveEntityCharacter(app.EveEntity{ID: 1011})
type_ := factory.CreateEveType(storage.CreateEveTypeParams{ID: 16213})
moon := factory.CreateEveMoon(storage.CreateEveMoonParams{ID: 40161465})
text := `
aggressorAllianceID: 3011
aggressorCorpID: 2011
aggressorID: 0
armorValue: 0.6950949076033535
hullValue: 1.0
moonID: 40161465
shieldValue: 0.3950949076033535
solarSystemID: 30002537
typeID: 16213`
title, body, err := en.RenderESI(ctx, "TowerAlertMsg", text, time.Now())
if assert.NoError(t, err) {
assert.Contains(t, title.ValueOrZero(), "is under attack")
assert.Contains(t, body.ValueOrZero(), aggressorAlliance.Name)
assert.Contains(t, body.ValueOrZero(), moon.Name)
assert.Contains(t, body.ValueOrZero(), type_.Name)
}
})
}
11 changes: 7 additions & 4 deletions internal/app/eveuniverse/eveentity.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,18 @@ func (eu *EveUniverseService) GetOrCreateEveEntityESI(ctx context.Context, id in
// TODO: Reduce DB calls with AddMissingEveEntities

// ToEveEntities returns the resolved EveEntities for a list of valid entity IDs.
// IDs with the value 0 are ignored.
// Will return an error an ID can not be resolved.
// ID 0 will be resolved to a zero value EveEntity object
// Will return an error if any ID can not be resolved.
func (eu *EveUniverseService) ToEveEntities(ctx context.Context, ids []int32) (map[int32]*app.EveEntity, error) {
r := make(map[int32]*app.EveEntity)
ids2 := set.NewFromSlice(ids)
ids2.Remove(0)
if ids2.Contains(0) {
r[0] = &app.EveEntity{}
ids2.Remove(0)
}
if _, err := eu.AddMissingEveEntities(ctx, ids2.ToSlice()); err != nil {
return nil, err
}
r := make(map[int32]*app.EveEntity)
for id := range ids2.All() {
x, err := eu.GetOrCreateEveEntityESI(ctx, id)
if err != nil {
Expand Down
14 changes: 13 additions & 1 deletion internal/app/eveuniverse/eveentity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,11 +327,23 @@ func TestResolveEveEntities(t *testing.T) {
httpmock.Reset()
e1 := factory.CreateEveEntity()
// when
r, err := s.ToEveEntities(ctx, []int32{e1.ID, 0})
r, err := s.ToEveEntities(ctx, []int32{e1.ID})
// then
if assert.NoError(t, err) {
assert.Len(t, r, 1)
assert.Equal(t, *e1, *r[e1.ID])
}
})
t.Run("should return zero value for ID 0", func(t *testing.T) {
// given
testutil.TruncateTables(db)
httpmock.Reset()
// when
r, err := s.ToEveEntities(ctx, []int32{0})
// then
if assert.NoError(t, err) {
assert.Len(t, r, 1)
assert.Equal(t, app.EveEntity{}, *r[0])
}
})
}
20 changes: 18 additions & 2 deletions internal/app/ui/updateticker.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,21 @@ func (u *ui) processNotifications(ctx context.Context, characterID int32) {
slog.Error("Failed to fetch notifications for processing", "characterID", characterID, "error", err)
return
}
var characterName string
character, err := u.CharacterService.GetCharacter(ctx, characterID)
if err != nil {
slog.Error("Failed to fetch character", "characterID", characterID, "error", err)
characterName = "?"
} else {
characterName = character.EveCharacter.Name
}
typesEnabled := set.NewFromSlice(u.fyneApp.Preferences().StringList(settingNotificationsTypesEnabled))
oldest := time.Now().UTC().Add(time.Second * time.Duration(maxAge) * -1)
for _, n := range nn {
if !typesEnabled.Contains(n.Type) || n.Timestamp.Before(oldest) {
continue
}
title := fmt.Sprintf("New Communication from %s", n.Sender.Name)
title := fmt.Sprintf("%s: New Communication from %s", characterName, n.Sender.Name)
x := fyne.NewNotification(title, n.Title.ValueOrZero())
u.fyneApp.SendNotification(x)
if err := u.CharacterService.UpdateCharacterNotificationSetProcessed(ctx, n); err != nil {
Expand All @@ -213,12 +221,20 @@ func (u *ui) processMails(ctx context.Context, characterID int32) {
slog.Error("Failed to fetch mails for processing", "characterID", characterID, "error", err)
return
}
var characterName string
character, err := u.CharacterService.GetCharacter(ctx, characterID)
if err != nil {
slog.Error("Failed to fetch character", "characterID", characterID, "error", err)
characterName = "?"
} else {
characterName = character.EveCharacter.Name
}
oldest := time.Now().UTC().Add(time.Second * time.Duration(maxAge) * -1)
for _, m := range mm {
if m.Timestamp.Before(oldest) {
continue
}
title := fmt.Sprintf("New Mail from %s", m.From)
title := fmt.Sprintf("%s: New Mail from %s", characterName, m.From)
body := m.Subject
x := fyne.NewNotification(title, body)
u.fyneApp.SendNotification(x)
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Evebuddy is a companion app for Eve Online players.
package main

import (
Expand Down

0 comments on commit 70423c8

Please sign in to comment.