2022-02-19 06:33:13 +00:00
package databag
import (
"strconv"
"net/http"
"databag/internal/store"
)
2022-07-04 04:11:30 +00:00
func reverseTopics ( input [ ] store . TopicSlot ) [ ] store . TopicSlot {
2022-07-03 07:18:57 +00:00
var output [ ] store . TopicSlot
for i := len ( input ) - 1 ; i >= 0 ; i -- {
output = append ( output , input [ i ] )
}
return output
}
2022-02-19 06:33:13 +00:00
func GetChannelTopics ( w http . ResponseWriter , r * http . Request ) {
var revisionSet bool
var revision int64
2022-07-03 07:18:57 +00:00
var beginSet bool
var begin int64
var endSet bool
var end int64
var countSet bool
var count int
2022-02-19 06:33:13 +00:00
channelSlot , _ , err , code := getChannelSlot ( r , false )
if err != nil {
ErrResponse ( w , code , err )
return
}
rev := r . FormValue ( "revision" )
if rev != "" {
revisionSet = true
if revision , err = strconv . ParseInt ( rev , 10 , 64 ) ; err != nil {
ErrResponse ( w , http . StatusBadRequest , err )
return
}
}
2022-07-03 07:18:57 +00:00
cnt := r . FormValue ( "count" )
if cnt != "" {
countSet = true
if count , err = strconv . Atoi ( cnt ) ; err != nil {
ErrResponse ( w , http . StatusBadRequest , err )
return
}
}
bn := r . FormValue ( "begin" )
if bn != "" {
beginSet = true
if begin , err = strconv . ParseInt ( bn , 10 , 64 ) ; err != nil {
ErrResponse ( w , http . StatusBadRequest , err )
return
}
}
en := r . FormValue ( "end" )
if en != "" {
endSet = true
if end , err = strconv . ParseInt ( en , 10 , 64 ) ; err != nil {
ErrResponse ( w , http . StatusBadRequest , err )
return
}
}
2022-04-15 08:49:08 +00:00
response := [ ] * Topic { }
2022-02-19 06:33:13 +00:00
if revisionSet {
var slots [ ] store . TopicSlot
2022-07-03 07:18:57 +00:00
if beginSet && ! endSet {
if err := store . DB . Preload ( "Topic" ) . Where ( "channel_id = ? AND revision > ? AND id >= ?" , channelSlot . Channel . ID , revision , begin ) . Find ( & slots ) . Error ; err != nil {
ErrResponse ( w , http . StatusInternalServerError , err )
return
}
} else if ! beginSet && endSet {
if err := store . DB . Preload ( "Topic" ) . Where ( "channel_id = ? AND revision > ? AND id < ?" , channelSlot . Channel . ID , revision , end ) . Find ( & slots ) . Error ; err != nil {
ErrResponse ( w , http . StatusInternalServerError , err )
return
}
} else if beginSet && endSet {
if err := store . DB . Preload ( "Topic" ) . Where ( "channel_id = ? AND revision > ? AND id >= ? AND id < ?" , channelSlot . Channel . ID , revision , begin , end ) . Find ( & slots ) . Error ; err != nil {
ErrResponse ( w , http . StatusInternalServerError , err )
return
}
} else {
if err := store . DB . Preload ( "Topic" ) . Where ( "channel_id = ? AND revision > ?" , channelSlot . Channel . ID , revision ) . Find ( & slots ) . Error ; err != nil {
ErrResponse ( w , http . StatusInternalServerError , err )
return
}
2022-02-19 06:33:13 +00:00
}
for _ , slot := range slots {
response = append ( response , getTopicRevisionModel ( & slot ) )
}
} else {
var slots [ ] store . TopicSlot
2022-07-03 07:18:57 +00:00
if countSet {
if ! endSet {
if err := store . DB . Preload ( "Topic.Assets" ) . Where ( "channel_id = ?" , channelSlot . Channel . ID ) . Order ( "id desc" ) . Limit ( count ) . Find ( & slots ) . Error ; err != nil {
ErrResponse ( w , http . StatusInternalServerError , err )
return
}
} else {
if err := store . DB . Preload ( "Topic.Assets" ) . Where ( "channel_id = ? AND id < ?" , channelSlot . Channel . ID , end ) . Order ( "id desc" ) . Limit ( count ) . Find ( & slots ) . Error ; err != nil {
ErrResponse ( w , http . StatusInternalServerError , err )
return
}
}
2022-07-04 04:11:30 +00:00
slots = reverseTopics ( slots )
2022-07-03 07:18:57 +00:00
} else if beginSet && ! endSet {
if err := store . DB . Preload ( "Topic.Assets" ) . Where ( "channel_id = ? AND id >= ?" , channelSlot . Channel . ID , begin ) . Find ( & slots ) . Error ; err != nil {
ErrResponse ( w , http . StatusInternalServerError , err )
return
}
} else if ! beginSet && endSet {
if err := store . DB . Preload ( "Topic.Assets" ) . Where ( "channel_id = ? AND id < ?" , channelSlot . Channel . ID , end ) . Find ( & slots ) . Error ; err != nil {
ErrResponse ( w , http . StatusInternalServerError , err )
return
}
} else if beginSet && endSet {
if err := store . DB . Preload ( "Topic.Assets" ) . Where ( "channel_id = ? AND id >= ? AND id < ?" , channelSlot . Channel . ID , begin , end ) . Find ( & slots ) . Error ; err != nil {
ErrResponse ( w , http . StatusInternalServerError , err )
return
}
} else {
if err := store . DB . Preload ( "Topic.Assets" ) . Where ( "channel_id = ?" , channelSlot . Channel . ID ) . Find ( & slots ) . Error ; err != nil {
ErrResponse ( w , http . StatusInternalServerError , err )
return
}
2022-02-19 06:33:13 +00:00
}
for _ , slot := range slots {
2022-03-03 08:03:32 +00:00
if slot . Topic != nil {
2022-07-03 07:18:57 +00:00
if countSet {
2022-07-10 07:33:50 +00:00
w . Header ( ) . Set ( "topic-marker" , strconv . FormatUint ( uint64 ( slot . ID ) , 10 ) )
2022-07-03 07:18:57 +00:00
countSet = false
}
2022-03-03 08:03:32 +00:00
response = append ( response , getTopicModel ( & slot ) )
}
2022-02-19 06:33:13 +00:00
}
}
2022-07-10 07:33:50 +00:00
w . Header ( ) . Set ( "topic-revision" , strconv . FormatInt ( channelSlot . Revision , 10 ) )
2022-07-18 21:06:57 +00:00
w . Header ( ) . Set ( "Access-Control-Expose-Headers" , "*" ) ;
2022-02-19 06:33:13 +00:00
WriteResponse ( w , response )
}