From 11483178a18cc09244c942d05718a0e036bd0399 Mon Sep 17 00:00:00 2001 From: Roland Osborne Date: Wed, 2 Mar 2022 21:46:21 -0800 Subject: [PATCH] adding tag subject update --- net/server/internal/api_addChannelTopic.go | 1 + net/server/internal/api_content.go | 5 - .../internal/api_setChannelTopicTagSubject.go | 111 ++++++++++++++++++ net/server/internal/store/schema.go | 3 + 4 files changed, 115 insertions(+), 5 deletions(-) create mode 100644 net/server/internal/api_setChannelTopicTagSubject.go diff --git a/net/server/internal/api_addChannelTopic.go b/net/server/internal/api_addChannelTopic.go index a8a21881..375c756f 100644 --- a/net/server/internal/api_addChannelTopic.go +++ b/net/server/internal/api_addChannelTopic.go @@ -29,6 +29,7 @@ func AddChannelTopic(w http.ResponseWriter, r *http.Request) { // add new record topic := &store.Topic{} + topic.ChannelID = channelSlot.Channel.ID topic.Data = subject.Data topic.DataType = subject.DataType topic.TagCount = 0 diff --git a/net/server/internal/api_content.go b/net/server/internal/api_content.go index 66b4f5e7..868b89c9 100644 --- a/net/server/internal/api_content.go +++ b/net/server/internal/api_content.go @@ -33,8 +33,3 @@ func RemoveChannelTopicTag(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) } -func SetChannelTopicTagSubject(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json; charset=UTF-8") - w.WriteHeader(http.StatusOK) -} - diff --git a/net/server/internal/api_setChannelTopicTagSubject.go b/net/server/internal/api_setChannelTopicTagSubject.go new file mode 100644 index 00000000..d9400702 --- /dev/null +++ b/net/server/internal/api_setChannelTopicTagSubject.go @@ -0,0 +1,111 @@ +package databag + +import ( + "time" + "errors" + "net/http" + "gorm.io/gorm" + "github.com/gorilla/mux" + "databag/internal/store" +) + +func SetChannelTopicTagSubject(w http.ResponseWriter, r *http.Request) { + + // scan parameters + params := mux.Vars(r) + channelId := params["channelId"] + topicId := params["topicId"] + tagId := params["tagId"] + + var subject Subject + if err := ParseRequest(r, w, &subject); err != nil { + ErrResponse(w, http.StatusBadRequest, err) + return + } + + channelSlot, guid, err, code := getChannelSlot(r, false) + if err != nil { + ErrResponse(w, code, err) + return + } + act := &channelSlot.Account + + // load topic + var tagSlot store.TagSlot + if err = store.DB.Preload("Tag.Channel.ChannelSlot").Preload("Tag.Topic.TopicSlot").Where("account_id = ? AND tag_slot_id = ?", act.ID, tagId).First(&tagSlot).Error; err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + ErrResponse(w, http.StatusNotFound, err) + } else { + ErrResponse(w, http.StatusInternalServerError, err) + } + return + } + if tagSlot.Tag == nil { + ErrResponse(w, http.StatusNotFound, errors.New("referenced empty tag")) + return + } + if tagSlot.Tag.Channel.ChannelSlot.ChannelSlotId != channelId { + ErrResponse(w, http.StatusNotFound, errors.New("channel tag not found")) + return + } + if tagSlot.Tag.Topic.TopicSlot.TopicSlotId != topicId { + ErrResponse(w, http.StatusNotFound, errors.New("topic tag not found")) + return + } + if tagSlot.Tag.Guid != guid { + ErrResponse(w, http.StatusUnauthorized, errors.New("not creator of tag")) + return + } + + err = store.DB.Transaction(func(tx *gorm.DB) error { + + if res := tx.Model(tagSlot.Tag).Update("data", subject.Data).Error; res != nil { + return res + } + if res := tx.Model(tagSlot.Tag).Update("data_type", subject.DataType).Error; res != nil { + return res + } + if res := tx.Model(&tagSlot).Update("revision", act.ChannelRevision + 1).Error; res != nil { + return res + } + if res := tx.Model(&tagSlot.Tag.Topic).Update("tag_revision", act.ChannelRevision + 1).Error; res != nil { + return res + } + if res := tx.Model(&tagSlot.Tag.Topic).Update("tag_updated", time.Now().Unix()).Error; res != nil { + return res + } + if res := tx.Model(&tagSlot.Tag.Topic.TopicSlot).Update("revision", act.ChannelRevision + 1).Error; res != nil { + return res + } + if res := tx.Model(&channelSlot).Update("revision", act.ChannelRevision + 1).Error; res != nil { + return res + } + if res := tx.Model(act).Update("channel_revision", act.ChannelRevision + 1).Error; res != nil { + return res + } + return nil + }) + if err != nil { + ErrResponse(w, http.StatusInternalServerError, err) + return + } + + // determine affected contact list + cards := make(map[string]store.Card) + for _, card := range channelSlot.Channel.Cards { + cards[card.Guid] = card + } + for _, group := range channelSlot.Channel.Groups { + for _, card := range group.Cards { + cards[card.Guid] = card + } + } + + SetStatus(act) + for _, card := range cards { + SetContactChannelNotification(act, &card) + } + WriteResponse(w, getTagModel(&tagSlot)) +} + + diff --git a/net/server/internal/store/schema.go b/net/server/internal/store/schema.go index 33a7f23d..18b3bf94 100644 --- a/net/server/internal/store/schema.go +++ b/net/server/internal/store/schema.go @@ -202,6 +202,7 @@ type Channel struct { Updated int64 `gorm:"autoUpdateTime"` Groups []Group `gorm:"many2many:channel_groups;"` Cards []Card `gorm:"many2many:channel_cards;"` + Topics []Topic ChannelSlot ChannelSlot } @@ -220,6 +221,7 @@ type TopicSlot struct { type Topic struct { ID uint `gorm:"primaryKey;not null;unique;autoIncrement"` DetailRevision int64 `gorm:"not null"` + ChannelID uint Guid string DataType string `gorm:"index"` Data string @@ -229,6 +231,7 @@ type Topic struct { TagCount int32 `gorm:"not null"` TagUpdated int64 TagRevision int64 `gorm:"not null"` + Channel *Channel Assets []Asset Tags []Tag TopicSlot TopicSlot