Skip to content

Commit

Permalink
update subscribers
Browse files Browse the repository at this point in the history
  • Loading branch information
kdudkov committed Feb 7, 2024
1 parent b30b1eb commit adad102
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 70 deletions.
33 changes: 18 additions & 15 deletions cmd/goatak_server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"software.sslmate.com/src/go-pkcs12"

"github.com/kdudkov/goatak/internal/client"
im "github.com/kdudkov/goatak/internal/model"
"github.com/kdudkov/goatak/internal/pm"
"github.com/kdudkov/goatak/internal/repository"
"github.com/kdudkov/goatak/pkg/cot"
Expand Down Expand Up @@ -386,27 +387,29 @@ func (app *App) processMissionPoint(missionName string, msg *cot.CotMessage) {
return
}

var send bool
var ackMsg *cotproto.TakMessage
var change *im.Change

if msg.GetType() == "t-x-d-d" {
uid := msg.GetFirstLink("p-p").GetAttr("uid")
if uid == "" {
return
if uid := msg.GetFirstLink("p-p").GetAttr("uid"); uid != "" {
change = app.missions.DeleteMissionPoint(m.ID, uid, "")
}

send = app.missions.DeleteMissionPoint(m.ID, uid, "")
ackMsg = cot.MissionPointDelMsg(missionName, uid)
} else {
send = true
app.missions.AddPoint(m, msg)
ackMsg = cot.MissionPointAddMsg(missionName, msg)
change = app.missions.AddPoint(m, msg)
}

if send && ackMsg != nil {
for _, uid := range app.missions.GetSubscribers(m.ID) {
app.SendToUID(uid, ackMsg)
}
if change != nil {
app.NotifyMissionSubscribers(m, change)
}
}

func (app *App) NotifyMissionSubscribers(mission *im.Mission, c *im.Change) {
if mission == nil || c == nil {
return
}

msg := im.MissionChangePountMsg(mission.Name, c)
for _, uid := range app.missions.GetSubscribers(mission.ID) {
app.SendToUID(uid, msg)
}
}

Expand Down
16 changes: 8 additions & 8 deletions cmd/goatak_server/mission.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ func (mm *MissionManager) GetPoint(uid string) *model.DataItem {
return d
}

func (mm *MissionManager) AddPoint(mission *model.Mission, msg *cot.CotMessage) bool {
func (mm *MissionManager) AddPoint(mission *model.Mission, msg *cot.CotMessage) *model.Change {
if mission == nil {
return false
return nil
}

now := time.Now()
Expand All @@ -182,7 +182,7 @@ func (mm *MissionManager) AddPoint(mission *model.Mission, msg *cot.CotMessage)
dp.UpdateFromMsg(msg)
mm.db.Save(dp)

return false
return nil
}
}

Expand Down Expand Up @@ -216,20 +216,20 @@ func (mm *MissionManager) AddPoint(mission *model.Mission, msg *cot.CotMessage)

mm.db.Create(c)

return true
return c
}

func (mm *MissionManager) DeleteMissionPoint(missionId uint, uid string, authorUID string) bool {
func (mm *MissionManager) DeleteMissionPoint(missionId uint, uid string, authorUID string) *model.Change {
if mm == nil || mm.db == nil || uid == "" {
return false
return nil
}

var mp *model.DataItem

res := mm.db.Where("mission_id = ? AND uid = ?", missionId, uid).Delete(&mp)

if res.RowsAffected == 0 {
return false
return nil
}

c := &model.Change{
Expand All @@ -248,7 +248,7 @@ func (mm *MissionManager) DeleteMissionPoint(missionId uint, uid string, authorU

mm.db.Create(c)

return true
return c
}

func (mm *MissionManager) DeleteMissionContent(missionId uint, hash string, authorUID string) bool {
Expand Down
4 changes: 3 additions & 1 deletion cmd/goatak_server/mission_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,9 @@ func getMissionContentDeleteHandler(app *App) func(req *air.Request, res *air.Re
author := getStringParam(req, "creatorUid")

if uid := getStringParam(req, "uid"); uid != "" {
app.missions.DeleteMissionPoint(mission.ID, uid, author)
change := app.missions.DeleteMissionPoint(mission.ID, uid, author)

app.NotifyMissionSubscribers(mission, change)
}

if hash := getStringParam(req, "hash"); hash != "" {
Expand Down
12 changes: 6 additions & 6 deletions cmd/goatak_server/mission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,16 @@ func TestAddPoint(t *testing.T) {
require.NoError(t, m.PutMission(m1))
require.NoError(t, m.PutMission(m2))

assert.True(t, m.AddPoint(m1, newCotMessage("scope1", "uid1", 10, 20)))
assert.True(t, m.AddPoint(m1, newCotMessage("scope1", "uid2", 10, 20)))
assert.False(t, m.AddPoint(m1, newCotMessage("scope1", "uid1", 15, 20)))
assert.True(t, m.AddPoint(m2, newCotMessage("scope1", "uid1", 15, 20)))
assert.NotNil(t, m.AddPoint(m1, newCotMessage("scope1", "uid1", 10, 20)))
assert.NotNil(t, m.AddPoint(m1, newCotMessage("scope1", "uid2", 10, 20)))
assert.Nil(t, m.AddPoint(m1, newCotMessage("scope1", "uid1", 15, 20)))
assert.NotNil(t, m.AddPoint(m2, newCotMessage("scope1", "uid1", 15, 20)))

assert.Len(t, m.GetMission("scope1", m1.Name).Items, 2)
assert.Len(t, m.GetMission("scope1", m2.Name).Items, 1)

assert.True(t, m.DeleteMissionPoint(m1.ID, "uid1", ""))
assert.False(t, m.DeleteMissionPoint(m1.ID, "uid1", ""))
assert.NotNil(t, m.DeleteMissionPoint(m1.ID, "uid1", ""))
assert.Nil(t, m.DeleteMissionPoint(m1.ID, "uid1", ""))

assert.Len(t, m.GetMission("scope1", m1.Name).Items, 1)
assert.Len(t, m.GetMission("scope1", m2.Name).Items, 1)
Expand Down
31 changes: 31 additions & 0 deletions internal/model/messages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package model

import (
"strconv"
"time"

"github.com/google/uuid"

"github.com/kdudkov/goatak/pkg/cot"
"github.com/kdudkov/goatak/pkg/cotproto"
)

func MissionChangePountMsg(missionName string, c *Change) *cotproto.TakMessage {
msg := cot.BasicMsg("t-x-m-c", uuid.NewString(), time.Second*5)
msg.CotEvent.How = "h-g-i-g-o"

xd := cot.NewXMLDetails()

ch := xd.AddChild("mission", map[string]string{"type": "CHANGE", "name": missionName}, "").
AddChild("MissionChanges", nil, "").AddChild("MissionChange", nil, "")

ch.AddChild("contentUid", nil, c.ContentUID)
ch.AddChild("type", nil, c.Type)
ch.AddChild("isFederatedChange", nil, "false")
ch.AddChild("missionName", nil, missionName)
ch.AddChild("timestamp", nil, strconv.Itoa(int(c.CreateTime.Unix())))

msg.CotEvent.Detail = &cotproto.Detail{XmlDetail: xd.AsXMLString()}

return msg
}
40 changes: 0 additions & 40 deletions pkg/cot/util.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cot

import (
"strconv"
"time"

"github.com/google/uuid"
Expand Down Expand Up @@ -87,42 +86,3 @@ func CloneMessageNoCoords(msg *cotproto.TakMessage) *cotproto.TakMessage {

return msg1
}

func MissionPointAddMsg(missionName string, m *CotMessage) *cotproto.TakMessage {
msg := BasicMsg("t-x-m-c", uuid.NewString(), time.Second*5)
msg.CotEvent.How = "h-g-i-g-o"

xd := NewXMLDetails()

ch := xd.AddChild("mission", map[string]string{"type": "CHANGE", "name": missionName}, "").
AddChild("MissionChanges", nil, "").AddChild("MissionChange", nil, "")

ch.AddChild("contentUid", nil, m.GetUID())
ch.AddChild("type", nil, "ADD_CONTENT")
ch.AddChild("isFederatedChange", nil, "false")
ch.AddChild("missionName", nil, missionName)
ch.AddChild("timestamp", nil, strconv.Itoa(int(m.GetTakMessage().GetCotEvent().GetStartTime())))

msg.CotEvent.Detail = &cotproto.Detail{XmlDetail: xd.AsXMLString()}

return msg
}

func MissionPointDelMsg(missionName string, uid string) *cotproto.TakMessage {
msg := BasicMsg("t-x-m-c", uuid.NewString(), time.Second*5)
msg.CotEvent.How = "h-g-i-g-o"

xd := NewXMLDetails()

ch := xd.AddChild("mission", map[string]string{"type": "CHANGE", "name": missionName}, "").
AddChild("MissionChanges", nil, "").AddChild("MissionChange", nil, "")

ch.AddChild("contentUid", nil, uid)
ch.AddChild("type", nil, "REMOVE_CONTENT")
ch.AddChild("isFederatedChange", nil, "false")
ch.AddChild("missionName", nil, missionName)

msg.CotEvent.Detail = &cotproto.Detail{XmlDetail: xd.AsXMLString()}

return msg
}

0 comments on commit adad102

Please sign in to comment.