Allow client to request a channel refresh
This commit is contained in:
parent
9dcf498dc5
commit
c675d0f655
@ -1,29 +1,58 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"github.com/TicketsBot/GoPanel/botcontext"
|
||||||
"errors"
|
"github.com/TicketsBot/GoPanel/redis"
|
||||||
"github.com/TicketsBot/GoPanel/rpc/cache"
|
"github.com/TicketsBot/GoPanel/rpc/cache"
|
||||||
"github.com/TicketsBot/GoPanel/utils"
|
"github.com/TicketsBot/GoPanel/utils"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
cache2 "github.com/rxdn/gdl/cache"
|
|
||||||
"github.com/rxdn/gdl/objects/channel"
|
"github.com/rxdn/gdl/objects/channel"
|
||||||
|
"github.com/rxdn/gdl/rest"
|
||||||
"sort"
|
"sort"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ChannelsHandler(ctx *gin.Context) {
|
func ChannelsHandler(ctx *gin.Context) {
|
||||||
guildId := ctx.Keys["guildid"].(uint64)
|
guildId := ctx.Keys["guildid"].(uint64)
|
||||||
|
|
||||||
// TODO: Use proper context
|
botContext, err := botcontext.ContextForGuild(guildId)
|
||||||
channels, err := cache.Instance.GetGuildChannels(context.Background(), guildId)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, cache2.ErrNotFound) {
|
ctx.JSON(500, utils.ErrorJson(err))
|
||||||
ctx.JSON(200, make([]channel.Channel, 0))
|
return
|
||||||
} else {
|
}
|
||||||
|
|
||||||
|
var channels []channel.Channel
|
||||||
|
if ctx.Query("refresh") == "true" {
|
||||||
|
hasToken, err := redis.Client.TakeChannelRefreshToken(ctx, guildId)
|
||||||
|
if err != nil {
|
||||||
ctx.JSON(500, utils.ErrorJson(err))
|
ctx.JSON(500, utils.ErrorJson(err))
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
if hasToken {
|
||||||
|
channels, err = rest.GetGuildChannels(ctx, botContext.Token, botContext.RateLimiter, guildId)
|
||||||
|
if err != nil {
|
||||||
|
ctx.JSON(500, utils.ErrorJson(err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := cache.Instance.StoreChannels(ctx, channels); err != nil {
|
||||||
|
ctx.JSON(500, utils.ErrorJson(err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
channels, err = cache.Instance.GetGuildChannels(ctx, guildId)
|
||||||
|
if err != nil {
|
||||||
|
ctx.JSON(500, utils.ErrorJson(err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
var err error
|
||||||
|
channels, err = botContext.GetGuildChannels(ctx, guildId)
|
||||||
|
if err != nil {
|
||||||
|
ctx.JSON(500, utils.ErrorJson(err))
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
filtered := make([]channel.Channel, 0, len(channels))
|
filtered := make([]channel.Channel, 0, len(channels))
|
||||||
|
@ -81,7 +81,7 @@ func main() {
|
|||||||
app.StartServer(socketManager)
|
app.StartServer(socketManager)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ListenChat(client redis.RedisClient, sm *livechat.SocketManager) {
|
func ListenChat(client *redis.RedisClient, sm *livechat.SocketManager) {
|
||||||
ch := make(chan chatrelay.MessageData)
|
ch := make(chan chatrelay.MessageData)
|
||||||
go chatrelay.Listen(client.Client, ch)
|
go chatrelay.Listen(client.Client, ch)
|
||||||
|
|
||||||
|
@ -245,8 +245,13 @@
|
|||||||
panels = res.data;
|
panels = res.data;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function loadChannels() {
|
async function loadChannels(refresh) {
|
||||||
const res = await axios.get(`${API_URL}/api/${guildId}/channels`);
|
let uri = `${API_URL}/api/${guildId}/channels`;
|
||||||
|
if (refresh === true) {
|
||||||
|
uri += "?refresh=true";
|
||||||
|
}
|
||||||
|
|
||||||
|
const res = await axios.get(uri);
|
||||||
if (res.status !== 200) {
|
if (res.status !== 200) {
|
||||||
notifyError(res.data.error);
|
notifyError(res.data.error);
|
||||||
return;
|
return;
|
||||||
@ -404,6 +409,17 @@
|
|||||||
loadSettings()
|
loadSettings()
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
if (data.archive_channel && !channels.some(c => c.id === data.archive_channel)) {
|
||||||
|
await loadChannels(true);
|
||||||
|
|
||||||
|
const tmp = data.archive_channel;
|
||||||
|
data.archive_channel = null;
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
data.archive_channel = tmp;
|
||||||
|
}, 100);
|
||||||
|
}
|
||||||
|
|
||||||
doOverrides(); // Depends on channels
|
doOverrides(); // Depends on channels
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
20
redis/channelrefreshcooldown.go
Normal file
20
redis/channelrefreshcooldown.go
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package redis
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const ChannelRefreshCooldown = 60 * time.Second
|
||||||
|
|
||||||
|
func (c *RedisClient) TakeChannelRefreshToken(ctx context.Context, guildId uint64) (bool, error) {
|
||||||
|
key := fmt.Sprintf("tickets:channelrefershcooldown:%d", guildId)
|
||||||
|
|
||||||
|
res, err := c.SetNX(ctx, key, "1", ChannelRefreshCooldown).Result()
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return res, nil
|
||||||
|
}
|
@ -12,16 +12,16 @@ type RedisClient struct {
|
|||||||
*redis.Client
|
*redis.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
var Client RedisClient
|
var Client *RedisClient
|
||||||
|
|
||||||
func NewRedisClient() RedisClient {
|
func NewRedisClient() *RedisClient {
|
||||||
client := redis.NewClient(&redis.Options{
|
client := redis.NewClient(&redis.Options{
|
||||||
Addr: fmt.Sprintf("%s:%d", config.Conf.Redis.Host, config.Conf.Redis.Port),
|
Addr: fmt.Sprintf("%s:%d", config.Conf.Redis.Host, config.Conf.Redis.Port),
|
||||||
Password: config.Conf.Redis.Password,
|
Password: config.Conf.Redis.Password,
|
||||||
PoolSize: config.Conf.Redis.Threads,
|
PoolSize: config.Conf.Redis.Threads,
|
||||||
})
|
})
|
||||||
|
|
||||||
return RedisClient{
|
return &RedisClient{
|
||||||
client,
|
client,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user