databag/net/server/internal/api_setChannelSubject.go

92 lines
2.5 KiB
Go
Raw Normal View History

2022-02-17 08:30:33 +00:00
package databag
import (
2022-07-22 19:28:14 +00:00
"databag/internal/store"
"errors"
"github.com/gorilla/mux"
"gorm.io/gorm"
"net/http"
2022-02-17 08:30:33 +00:00
)
2022-07-29 21:57:25 +00:00
//SetChannelSubject updates channel subject for account holder
2022-02-17 08:30:33 +00:00
func SetChannelSubject(w http.ResponseWriter, r *http.Request) {
2022-07-22 19:28:14 +00:00
account, code, err := ParamAgentToken(r, false)
if err != nil {
ErrResponse(w, code, err)
return
}
2022-02-17 08:30:33 +00:00
2022-07-22 19:28:14 +00:00
// scan parameters
params := mux.Vars(r)
channelID := params["channelID"]
2022-02-17 08:30:33 +00:00
2022-07-22 19:28:14 +00:00
var subject Subject
if err := ParseRequest(r, w, &subject); err != nil {
ErrResponse(w, http.StatusBadRequest, err)
return
}
2022-02-17 08:30:33 +00:00
2022-07-22 19:28:14 +00:00
// load referenced channel
var slot store.ChannelSlot
2022-11-15 18:04:45 +00:00
if err := store.DB.Preload("Channel.Members.Card").Preload("Channel.Groups.Cards").Where("account_id = ? AND channel_slot_id = ?", account.ID, channelID).First(&slot).Error; err != nil {
2022-07-22 19:28:14 +00:00
if !errors.Is(err, gorm.ErrRecordNotFound) {
ErrResponse(w, http.StatusInternalServerError, err)
} else {
ErrResponse(w, http.StatusNotFound, err)
}
return
}
if slot.Channel == nil {
ErrResponse(w, http.StatusNotFound, errors.New("channel has been deleted"))
return
}
2022-02-17 08:30:33 +00:00
2022-07-22 19:28:14 +00:00
// determine affected contact list
cards := make(map[string]store.Card)
2022-11-11 23:26:43 +00:00
for _, member := range slot.Channel.Members {
cards[member.Card.GUID] = member.Card
2022-07-22 19:28:14 +00:00
}
for _, group := range slot.Channel.Groups {
for _, card := range group.Cards {
cards[card.GUID] = card
}
}
2022-02-17 08:30:33 +00:00
2022-07-22 19:28:14 +00:00
// save and update contact revision
err = store.DB.Transaction(func(tx *gorm.DB) error {
if res := tx.Model(&slot.Channel).Update("data", subject.Data).Error; res != nil {
return res
}
if res := tx.Model(&slot.Channel).Update("data_type", subject.DataType).Error; res != nil {
return res
}
if res := tx.Model(&slot.Channel).Update("detail_revision", account.ChannelRevision+1).Error; res != nil {
return res
}
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
}
2022-02-17 08:30:33 +00:00
2022-07-22 19:28:14 +00:00
// notify contacts of content change
SetStatus(account)
for _, card := range cards {
SetContactChannelNotification(account, &card)
}
video := getBoolConfigValue(CNFEnableVideo, true);
audio := getBoolConfigValue(CNFEnableAudio, true);
image := getBoolConfigValue(CNFEnableImage, true);
binary := getBoolConfigValue(CNFEnableBinary, true);
WriteResponse(w, getChannelModel(&slot, true, true, image, audio, video, binary))
2022-02-17 08:30:33 +00:00
}