testing channel remove

This commit is contained in:
Roland Osborne 2022-03-03 00:46:32 -08:00
parent 1355d51dfa
commit 26f4f2d458
6 changed files with 224 additions and 26 deletions

View File

@ -51,6 +51,7 @@ func AddChannelTopicTag(w http.ResponseWriter, r *http.Request) {
tagSlot.TagSlotId = uuid.New().String()
tagSlot.AccountID = act.ID
tagSlot.Revision = act.ChannelRevision + 1
tagSlot.ChannelID = channelSlot.Channel.ID
tagSlot.TopicID = topicSlot.Topic.ID
if res := tx.Save(tagSlot).Error; res != nil {
return res

View File

@ -1,26 +0,0 @@
/*
* DataBag
*
* DataBag provides storage for decentralized identity based self-hosting apps. It is intended to support sharing of personal data and hosting group conversations.
*
* API version: 0.0.1
* Contact: roland.osborne@gmail.com
* Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git)
*/
package databag
import (
"net/http"
)
func RemoveChannel(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
}
func RemoveChannelTopic(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
}

View File

@ -0,0 +1,102 @@
package databag
import (
"errors"
"net/http"
"gorm.io/gorm"
"github.com/gorilla/mux"
"databag/internal/store"
)
func RemoveChannel(w http.ResponseWriter, r *http.Request) {
// scan parameters
params := mux.Vars(r)
channelId := params["channelId"]
// validate contact access
account, code, err := BearerAppToken(r, false)
if err != nil {
ErrResponse(w, code, err)
return
}
// load channel
var slot store.ChannelSlot
if err = store.DB.Preload("Channel.Cards").Preload("Channel.Groups.Cards").Where("account_id = ? AND channel_slot_id = ?", account.ID, channelId).First(&slot).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
ErrResponse(w, http.StatusNotFound, err)
} else {
ErrResponse(w, http.StatusInternalServerError, err)
}
return
}
if slot.Channel == nil {
ErrResponse(w, http.StatusNotFound, errors.New("referenced empty channel"))
return
}
// determine affected contact list
cards := make(map[string]store.Card)
for _, card := range slot.Channel.Cards {
cards[card.Guid] = card
}
for _, group := range slot.Channel.Groups {
for _, card := range group.Cards {
cards[card.Guid] = card
}
}
err = store.DB.Transaction(func(tx *gorm.DB) error {
if res := tx.Model(&slot.Channel).Association("Groups").Clear(); res != nil {
return res
}
slot.Channel.Groups = []store.Group{}
if res := tx.Model(&slot.Channel).Association("Cards").Clear(); res != nil {
return res
}
slot.Channel.Cards = []store.Card{}
if res := tx.Where("channel_id = ?", slot.Channel.ID).Delete(&store.Tag{}).Error; res != nil {
return res
}
if res := tx.Where("channel_id = ?", slot.Channel.ID).Delete(&store.TagSlot{}).Error; res != nil {
return res
}
if res := tx.Where("channel_id = ?", slot.Channel.ID).Delete(&store.Asset{}).Error; res != nil {
return res
}
if res := tx.Where("channel_id = ?", slot.Channel.ID).Delete(&store.Topic{}).Error; res != nil {
return res
}
if res := tx.Where("channel_id = ?", slot.Channel.ID).Delete(&store.TopicSlot{}).Error; res != nil {
return res
}
slot.Channel.Topics = []store.Topic{}
if res := tx.Delete(&slot.Channel).Error; res != nil {
return res
}
slot.Channel = nil
if res := tx.Model(&slot).Update("revision", account.ChannelRevision + 1).Error; res != nil {
return res
}
if res := tx.Model(account).Update("channel_revision", account.ChannelRevision + 1).Error; res != nil {
return res
}
return nil
})
if err != nil {
ErrResponse(w, http.StatusInternalServerError, err)
return
}
// cleanup file assets
go garbageCollect(account)
SetStatus(account)
for _, card := range cards {
SetContactChannelNotification(account, &card)
}
WriteResponse(w, nil)
}

View File

@ -0,0 +1,101 @@
package databag
import (
"errors"
"net/http"
"gorm.io/gorm"
"github.com/gorilla/mux"
"databag/internal/store"
)
func RemoveChannelTopic(w http.ResponseWriter, r *http.Request) {
// scan parameters
params := mux.Vars(r)
channelId := params["channelId"]
topicId := params["topicId"]
channelSlot, guid, err, code := getChannelSlot(r, true)
if err != nil {
ErrResponse(w, code, err)
return
}
act := &channelSlot.Account
// load topic
var topicSlot store.TopicSlot
if ret := store.DB.Preload("Topic.Channel.ChannelSlot").Where("account_id = ? AND topic_slot_id = ?", act.ID, topicId).First(&topicSlot).Error; ret != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
ErrResponse(w, http.StatusNotFound, ret)
} else {
ErrResponse(w, http.StatusInternalServerError, ret)
}
}
if topicSlot.Topic == nil {
ErrResponse(w, http.StatusNotFound, errors.New("referenced empty topic"))
return
}
if topicSlot.Topic.Channel.ChannelSlot.ChannelSlotId != channelId {
ErrResponse(w, http.StatusNotFound, errors.New("channel topic not found"))
return
}
// check permission
if topicSlot.Topic.Guid != guid {
ErrResponse(w, http.StatusUnauthorized, errors.New("not creator of topic"))
return
}
err = store.DB.Transaction(func(tx *gorm.DB) error {
if res := tx.Where("topic_id = ?", topicSlot.Topic.ID).Delete(&store.Tag{}).Error; res != nil {
return res
}
if res := tx.Where("topic_id = ?", topicSlot.Topic.ID).Delete(&store.TagSlot{}).Error; res != nil {
return res
}
if res := tx.Where("topic_id = ?", topicSlot.Topic.ID).Delete(&store.Asset{}).Error; res != nil {
return res
}
if res := tx.Delete(&topicSlot.Topic).Error; res != nil {
return res
}
topicSlot.Topic = nil
if res := tx.Model(&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
}
// cleanup file assets
go garbageCollect(act)
// 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, nil)
}

View File

@ -261,10 +261,12 @@ type TagSlot struct {
ID uint
TagSlotId string `gorm:"not null;index:tagslot,unique"`
AccountID uint `gorm:"not null;index:tagslot,unique"`
ChannelID uint `gorm:"not null"`
TopicID uint `gorm:"not null;index:tagtopic"`
Revision int64 `gorm:"not null"`
Tag *Tag
Account Account
Channel *Channel
Topic *Topic
}

View File

@ -150,5 +150,23 @@ func TestTopicShare(t *testing.T) {
assert.NoError(t, ApiTestMsg(GetChannelTopicAssets, "GET", "/content/channels/{channelId}/topics/{topicId}",
&params, nil, APP_TOKENCONTACT, set.C.A.Token, &assets, nil))
assert.Equal(t, 0, len(assets))
// add asset to topic
assets = []Asset{}
params["topicId"] = topic.Id
transforms, err = json.Marshal([]string{ "copy;photo" })
assert.NoError(t, err)
assert.NoError(t, ApiTestUpload(AddChannelTopicAsset, "POST", "/content/channels/{channelId}/topics/{topicId}/assets?transforms=" + url.QueryEscape(string(transforms)),
&params, img, APP_TOKENCONTACT, set.C.A.Token, &assets, nil))
// add a tag to topic
tag = Tag{}
subject = &Subject{ DataType: "tagdatatype", Data: "subjectfromA" }
assert.NoError(t, ApiTestMsg(AddChannelTopicTag, "POST", "/content/channels/{channelId}/topics/{topicId}",
&params, subject, APP_TOKENAPP, set.A.Token, &tag, nil))
// remove channel
assert.NoError(t, ApiTestMsg(RemoveChannel, "DELETE", "/content/channels/{channelId}",
&params, nil, APP_TOKENAPP, set.A.Token, nil, nil))
}