/
uzer_007
/
Rixea
Обзор
Документация
Войти
/
uzer_007
/
Rixea
Код
Запросы
0
Задачи
Вики
Пакеты
5
Релизы
2
CI/CD
Аналитика
Безопасность
master
models/webhook/hooktask.go
149 строк
5 KB
uzer_007
feat: первая версия Rixea
02 авг 2026, 22:16
02 авг 2026, 22:16
d4f7504
Код
Авторство
О чём код?
// Copyright 2026 Uzer_007. All rights reserved. // SPDX-License-Identifier: MIT package webhook import ( "context" "errors" "time" "gitverse.ru/uzer_007/Rixea/modules/json" "gitverse.ru/uzer_007/Rixea/modules/log" "gitverse.ru/uzer_007/Rixea/modules/timeutil" webhook_module "gitverse.ru/uzer_007/Rixea/modules/webhook" gouuid "github.com/google/uuid" ) // ___ ___ __ ___________ __ // / | \ ____ ____ | | _\__ ___/____ _____| | __ // / ~ \/ _ \ / _ \| |/ / | | \__ \ / ___/ |/ / // \ Y ( <_> | <_> ) < | | / __ \_\___ \| < // \___|_ / \____/ \____/|__|_ \ |____| (____ /____ >__|_ \ // \/ \/ \/ \/ \/ // HookRequest represents hook task request information. type HookRequest struct { URL string `json:"url"` HTTPMethod string `json:"http_method"` Headers map[string]string `json:"headers"` Body string `json:"body"` } // HookResponse represents hook task response information. type HookResponse struct { Status int `json:"status"` Headers map[string]string `json:"headers"` Body string `json:"body"` } // HookTask represents a hook task. type HookTask struct { ID int64 HookID int64 UUID string PayloadContent string // PayloadVersion number to allow for smooth version upgrades: // - PayloadVersion 1: PayloadContent contains the JSON as sent to the URL // - PayloadVersion 2: PayloadContent contains the original event PayloadVersion int EventType webhook_module.HookEventType IsDelivered bool Delivered timeutil.TimeStampNano // History info. IsSucceed bool RequestContent string RequestInfo *HookRequest ResponseContent string ResponseInfo *HookResponse } // BeforeUpdate will be invoked by XORM before updating a record // representing this object func (t *HookTask) BeforeUpdate() { if t.RequestInfo != nil { t.RequestContent = t.simpleMarshalJSON(t.RequestInfo) } if t.ResponseInfo != nil { t.ResponseContent = t.simpleMarshalJSON(t.ResponseInfo) } } // AfterLoad updates the webhook object upon setting a column func (t *HookTask) AfterLoad() { if len(t.RequestContent) == 0 { return } t.RequestInfo = &HookRequest{} if err := json.Unmarshal([]byte(t.RequestContent), t.RequestInfo); err != nil { log.Error("Unmarshal RequestContent[%d]: %v", t.ID, err) } if len(t.ResponseContent) > 0 { t.ResponseInfo = &HookResponse{} if err := json.Unmarshal([]byte(t.ResponseContent), t.ResponseInfo); err != nil { log.Error("Unmarshal ResponseContent[%d]: %v", t.ID, err) } } } func (t *HookTask) simpleMarshalJSON(v any) string { p, err := json.Marshal(v) if err != nil { log.Error("Marshal [%d]: %v", t.ID, err) } return string(p) } // HookTasks returns a list of hook tasks by given conditions. func HookTasks(ctx context.Context, hookID int64, page int) ([]*HookTask, error) { return hookTasksReindexer(ctx, hookID, page) } // CreateHookTask creates a new hook task, // it handles conversion from Payload to PayloadContent. func CreateHookTask(ctx context.Context, t *HookTask) (*HookTask, error) { t.UUID = gouuid.New().String() if t.Delivered == 0 { t.Delivered = timeutil.TimeStampNanoNow() } if t.PayloadVersion == 0 { return nil, errors.New("missing HookTask.PayloadVersion") } return t, createHookTaskReindexer(ctx, t) } func GetHookTaskByID(ctx context.Context, id int64) (*HookTask, error) { return getHookTaskReindexer(ctx, id) } // UpdateHookTask updates information of hook task. func UpdateHookTask(ctx context.Context, t *HookTask) error { return updateHookTaskReindexer(ctx, t) } // ReplayHookTask copies a hook task to get re-delivered func ReplayHookTask(ctx context.Context, hookID int64, uuid string) (*HookTask, error) { return replayHookTaskReindexer(ctx, hookID, uuid) } // FindUndeliveredHookTaskIDs will find the next 100 undelivered hook tasks with ID greater than the provided lowerID func FindUndeliveredHookTaskIDs(ctx context.Context, lowerID int64) ([]int64, error) { return findUndeliveredHookTaskIDsReindexer(ctx, lowerID) } func MarkTaskDelivered(ctx context.Context, task *HookTask) (bool, error) { return markTaskDeliveredReindexer(ctx, task.ID) } // CleanupHookTaskTable deletes rows from hook_task as needed. func CleanupHookTaskTable(ctx context.Context, cleanupType HookTaskCleanupType, olderThan time.Duration, numberToKeep int) error { log.Trace("Doing: CleanupHookTaskTable") err := cleanupHookTaskTableReindexer(ctx, cleanupType, olderThan, numberToKeep) log.Trace("Finished: CleanupHookTaskTable") return err }