Compare commits

..

27 Commits

Author SHA1 Message Date
Abhinav Raut
6bb5728665 check csrf only for state-changing methods 2025-06-19 01:53:19 +05:30
Abhinav Raut
2322ec33b0 update admin role permissions to include webhooks:manage permission 2025-06-19 01:00:36 +05:30
Abhinav Raut
9132e11458 refactor return envelope errors from handlers 2025-06-19 00:14:35 +05:30
Abhinav Raut
e70f92d377 remove validation tags from loginRequest struct 2025-06-18 23:58:07 +05:30
Abhinav Raut
591108f094 fix: reset recipients when no latest message is found 2025-06-18 01:31:15 +05:30
Abhinav Raut
1b2a5e4f36 feat: standardize API requests to use JSON instead of form data 2025-06-18 01:30:38 +05:30
Abhinav Raut
f613cc237b fix schema file 2025-06-16 23:49:41 +05:30
Abhinav Raut
c37258fccb feat: API key management for agents, api keys can now be generated for any agent in libredesk allowing programmatic access.
- Added endpoints to generate and revoke API keys for agents.
- Updated user model to include API key fields.
- Update authentication middleware to support API key validation.
- Modified database schema to accommodate API key fields.
- Updated frontend to manage API keys, including generation and revocation.
- Added localization strings for API key related messages.
2025-06-16 23:45:00 +05:30
Abhinav Raut
1879d9d22b docs: add webhooks feature to README for external system integration 2025-06-15 13:56:48 +05:30
Abhinav Raut
b369e2f56a Merge pull request #104 from abhinavxd/feat/webhooks
Feat: Webhooks
2025-06-15 13:46:48 +05:30
Abhinav Raut
ef56f1a74e feat: add webhooks documentation and navigation entry 2025-06-15 13:42:50 +05:30
Abhinav Raut
d274adb19b lower case webhook data table event label 2025-06-15 13:27:56 +05:30
Abhinav Raut
d31fcb00b6 refactor: simplify checkbox event handling in webhook form 2025-06-15 13:22:27 +05:30
Abhinav Raut
88d719ec4f fix remove unncessary indexes on webhooks table 2025-06-15 13:10:19 +05:30
Abhinav Raut
147180a536 remove unused headers column from webhooks table 2025-06-15 13:05:14 +05:30
Abhinav Raut
faa195f0a6 fix typo in prepared query 2025-06-15 12:43:28 +05:30
Abhinav Raut
4b0422d904 Reduce duplicate database conversation fetches by adding a new method EvaluateConversationUpdateRulesByID that accepts only a conversation ID instead of a conversation object, allowing the caller to control the behavior since the caller might already have the conversation object or might want a fresh fetch.
- Return early if user / team is already assigned to a conversation

- rename query get-pending-messages to get-outgoing-pending-messages to reflect purpose.
2025-06-15 12:43:01 +05:30
Abhinav Raut
9303997cea feat: update sidebar title translation to support pluralization for integration nav item 2025-06-14 22:28:46 +05:30
Abhinav Raut
aba07b3096 fix transalations for webhooks 2025-06-14 22:24:41 +05:30
Abhinav Raut
27aac88f53 update webhook payloads, send reduced payloads showing what changed instead of entire objects 2025-06-14 21:51:11 +05:30
Abhinav Raut
cb6b0e420b adds missing colunms from select query were missing for webhook events 2025-06-14 21:49:31 +05:30
Abhinav Raut
e004afd7d1 fix: remove unused email fields from Conversation model 2025-06-14 21:49:01 +05:30
Abhinav Raut
6a77d346dc feat: add version package to hold application version for build time configuration 2025-06-14 21:48:55 +05:30
Abhinav Raut
60c89cb617 use http client with set timeout for webhook requests
remove db calls from `TriggerEvent`
2025-06-14 21:48:46 +05:30
Abhinav Raut
b7d4b187e8 fix: adds missing conversation update rule evaluation missing for event EventConversationTeamAssigned
refactor code
2025-06-14 19:55:24 +05:30
Abhinav Raut
2bf45f32de fix: remove headers field from webhook model and related queries 2025-06-14 15:03:11 +05:30
Abhinav Raut
981372ab86 wip webhooks 2025-06-13 02:17:00 +05:30
58 changed files with 3147 additions and 537 deletions

View File

@@ -38,7 +38,7 @@ frontend-build: install-deps
.PHONY: run-backend
run-backend:
@echo "→ Running backend..."
CGO_ENABLED=0 go run -ldflags="-s -w -X 'main.buildString=${BUILDSTR}' -X 'main.versionString=${VERSION}' -X 'main.frontendDir=frontend/dist'" cmd/*.go
CGO_ENABLED=0 go run -ldflags="-s -w -X 'main.buildString=${BUILDSTR}' -X 'main.versionString=${VERSION}' -X 'github.com/abhinavxd/libredesk/internal/version.Version=${VERSION}' -X 'main.frontendDir=frontend/dist'" cmd/*.go
# Run the JS frontend server in development mode.
.PHONY: run-frontend
@@ -52,8 +52,8 @@ run-frontend:
.PHONY: build-backend
build-backend: $(STUFFBIN)
@echo "→ Building backend..."
@CGO_ENABLED=0 go build -a\
-ldflags="-X 'main.buildString=${BUILDSTR}' -X 'main.versionString=${VERSION}' -s -w" \
@CGO_ENABLED=0 go build -a \
-ldflags="-X 'main.buildString=${BUILDSTR}' -X 'main.versionString=${VERSION}' -X 'github.com/abhinavxd/libredesk/internal/version.Version=${VERSION}' -s -w" \
-o ${BIN} cmd/*.go
# Main build target: builds both frontend and backend, then stuffs static assets into the binary.

View File

@@ -36,6 +36,8 @@ Visit [libredesk.io](https://libredesk.io) for more info. Check out the [**Live
Instantly rewrite responses with AI to make them more friendly, professional, or polished.
- **Activity logs**
Track all actions performed by agents and admins—updates and key events across the system—for auditing and accountability.
- **Webhooks**
Integrate with external systems using real-time HTTP notifications for conversation and message events.
- **Command Bar**
Opens with a simple shortcut (CTRL+K) and lets you quickly perform actions on conversations.

View File

@@ -5,6 +5,11 @@ import (
"github.com/zerodha/fastglue"
)
type aiCompletionReq struct {
PromptKey string `json:"prompt_key"`
Content string `json:"content"`
}
type providerUpdateReq struct {
Provider string `json:"provider"`
APIKey string `json:"api_key"`
@@ -13,11 +18,15 @@ type providerUpdateReq struct {
// handleAICompletion handles AI completion requests
func handleAICompletion(r *fastglue.Request) error {
var (
app = r.Context.(*App)
promptKey = string(r.RequestCtx.PostArgs().Peek("prompt_key"))
content = string(r.RequestCtx.PostArgs().Peek("content"))
app = r.Context.(*App)
req = aiCompletionReq{}
)
resp, err := app.ai.Completion(promptKey, content)
if err := r.Decode(&req, "json"); err != nil {
return sendErrorEnvelope(r, envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil))
}
resp, err := app.ai.Completion(req.PromptKey, req.Content)
if err != nil {
return sendErrorEnvelope(r, err)
}

View File

@@ -9,6 +9,10 @@ import (
"github.com/zerodha/fastglue"
)
type updateAutomationRuleExecutionModeReq struct {
Mode string `json:"mode"`
}
// handleGetAutomationRules gets all automation rules
func handleGetAutomationRules(r *fastglue.Request) error {
var (
@@ -118,14 +122,20 @@ func handleUpdateAutomationRuleWeights(r *fastglue.Request) error {
// handleUpdateAutomationRuleExecutionMode updates the execution mode of the automation rules for a given type
func handleUpdateAutomationRuleExecutionMode(r *fastglue.Request) error {
var (
app = r.Context.(*App)
mode = string(r.RequestCtx.PostArgs().Peek("mode"))
app = r.Context.(*App)
req = updateAutomationRuleExecutionModeReq{}
)
if mode != amodels.ExecutionModeAll && mode != amodels.ExecutionModeFirstMatch {
if err := r.Decode(&req, "json"); err != nil {
return sendErrorEnvelope(r, envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil))
}
if req.Mode != amodels.ExecutionModeAll && req.Mode != amodels.ExecutionModeFirstMatch {
return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.T("automation.invalidRuleExecutionMode"), nil, envelope.InputError)
}
// Only new conversation rules can be updated as they are the only ones that have execution mode.
if err := app.automation.UpdateRuleExecutionMode(amodels.RuleTypeNewConversation, mode); err != nil {
if err := app.automation.UpdateRuleExecutionMode(amodels.RuleTypeNewConversation, req.Mode); err != nil {
return sendErrorEnvelope(r, err)
}
return r.SendEnvelope(true)

View File

@@ -14,6 +14,14 @@ import (
"github.com/zerodha/fastglue"
)
type createContactNoteReq struct {
Note string `json:"note"`
}
type blockContactReq struct {
Enabled bool `json:"enabled"`
}
// handleGetContacts returns a list of contacts from the database.
func handleGetContacts(r *fastglue.Request) error {
var (
@@ -185,12 +193,17 @@ func handleCreateContactNote(r *fastglue.Request) error {
app = r.Context.(*App)
contactID, _ = strconv.Atoi(r.RequestCtx.UserValue("id").(string))
auser = r.RequestCtx.UserValue("user").(amodels.User)
note = string(r.RequestCtx.PostArgs().Peek("note"))
req = createContactNoteReq{}
)
if len(note) == 0 {
if err := r.Decode(&req, "json"); err != nil {
return sendErrorEnvelope(r, envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil))
}
if len(req.Note) == 0 {
return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.empty", "name", "note"), nil, envelope.InputError)
}
if err := app.user.CreateNote(contactID, auser.ID, note); err != nil {
if err := app.user.CreateNote(contactID, auser.ID, req.Note); err != nil {
return sendErrorEnvelope(r, err)
}
return r.SendEnvelope(true)
@@ -238,12 +251,18 @@ func handleBlockContact(r *fastglue.Request) error {
var (
app = r.Context.(*App)
contactID, _ = strconv.Atoi(r.RequestCtx.UserValue("id").(string))
enabled = r.RequestCtx.PostArgs().GetBool("enabled")
req = blockContactReq{}
)
if contactID <= 0 {
return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError)
}
if err := app.user.ToggleEnabled(contactID, models.UserTypeContact, enabled); err != nil {
if err := r.Decode(&req, "json"); err != nil {
return sendErrorEnvelope(r, envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil))
}
if err := app.user.ToggleEnabled(contactID, models.UserTypeContact, req.Enabled); err != nil {
return sendErrorEnvelope(r, err)
}
return r.SendEnvelope(true)

View File

@@ -1,7 +1,6 @@
package main
import (
"encoding/json"
"strconv"
"time"
@@ -13,21 +12,43 @@ import (
medModels "github.com/abhinavxd/libredesk/internal/media/models"
"github.com/abhinavxd/libredesk/internal/stringutil"
umodels "github.com/abhinavxd/libredesk/internal/user/models"
wmodels "github.com/abhinavxd/libredesk/internal/webhook/models"
"github.com/valyala/fasthttp"
"github.com/volatiletech/null/v9"
"github.com/zerodha/fastglue"
)
type assigneeChangeReq struct {
AssigneeID int `json:"assignee_id"`
}
type teamAssigneeChangeReq struct {
AssigneeID int `json:"assignee_id"`
}
type priorityUpdateReq struct {
Priority string `json:"priority"`
}
type statusUpdateReq struct {
Status string `json:"status"`
SnoozedUntil string `json:"snoozed_until,omitempty"`
}
type tagsUpdateReq struct {
Tags []string `json:"tags"`
}
type createConversationRequest struct {
InboxID int `json:"inbox_id" form:"inbox_id"`
AssignedAgentID int `json:"agent_id" form:"agent_id"`
AssignedTeamID int `json:"team_id" form:"team_id"`
Email string `json:"contact_email" form:"contact_email"`
FirstName string `json:"first_name" form:"first_name"`
LastName string `json:"last_name" form:"last_name"`
Subject string `json:"subject" form:"subject"`
Content string `json:"content" form:"content"`
Attachments []int `json:"attachments" form:"attachments"`
InboxID int `json:"inbox_id"`
AssignedAgentID int `json:"agent_id"`
AssignedTeamID int `json:"team_id"`
Email string `json:"contact_email"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Subject string `json:"subject"`
Content string `json:"content"`
Attachments []int `json:"attachments"`
}
// handleGetAllConversations retrieves all conversations.
@@ -303,13 +324,15 @@ func handleGetConversationParticipants(r *fastglue.Request) error {
// handleUpdateUserAssignee updates the user assigned to a conversation.
func handleUpdateUserAssignee(r *fastglue.Request) error {
var (
app = r.Context.(*App)
uuid = r.RequestCtx.UserValue("uuid").(string)
auser = r.RequestCtx.UserValue("user").(amodels.User)
assigneeID = r.RequestCtx.PostArgs().GetUintOrZero("assignee_id")
app = r.Context.(*App)
uuid = r.RequestCtx.UserValue("uuid").(string)
auser = r.RequestCtx.UserValue("user").(amodels.User)
req = assigneeChangeReq{}
)
if assigneeID == 0 {
return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`assignee_id`"), nil, envelope.InputError)
if err := r.Decode(&req, "json"); err != nil {
app.lo.Error("error decoding assignee change request", "error", err)
return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil, envelope.InputError)
}
user, err := app.user.GetAgent(auser.ID, "")
@@ -317,17 +340,19 @@ func handleUpdateUserAssignee(r *fastglue.Request) error {
return sendErrorEnvelope(r, err)
}
_, err = enforceConversationAccess(app, uuid, user)
conversation, err := enforceConversationAccess(app, uuid, user)
if err != nil {
return sendErrorEnvelope(r, err)
}
if err := app.conversation.UpdateConversationUserAssignee(uuid, assigneeID, user); err != nil {
return sendErrorEnvelope(r, err)
// Already assigned?
if conversation.AssignedUserID.Int == req.AssigneeID {
return r.SendEnvelope(true)
}
// Evaluate automation rules.
app.automation.EvaluateConversationUpdateRules(uuid, models.EventConversationUserAssigned)
if err := app.conversation.UpdateConversationUserAssignee(uuid, req.AssigneeID, user); err != nil {
return sendErrorEnvelope(r, err)
}
return r.SendEnvelope(true)
}
@@ -338,12 +363,16 @@ func handleUpdateTeamAssignee(r *fastglue.Request) error {
app = r.Context.(*App)
uuid = r.RequestCtx.UserValue("uuid").(string)
auser = r.RequestCtx.UserValue("user").(amodels.User)
req = teamAssigneeChangeReq{}
)
assigneeID, err := r.RequestCtx.PostArgs().GetUint("assignee_id")
if err != nil {
return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`assignee_id`"), nil, envelope.InputError)
if err := r.Decode(&req, "json"); err != nil {
app.lo.Error("error decoding team assignee change request", "error", err)
return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil, envelope.InputError)
}
assigneeID := req.AssigneeID
user, err := app.user.GetAgent(auser.ID, "")
if err != nil {
return sendErrorEnvelope(r, err)
@@ -354,28 +383,37 @@ func handleUpdateTeamAssignee(r *fastglue.Request) error {
return sendErrorEnvelope(r, err)
}
_, err = enforceConversationAccess(app, uuid, user)
conversation, err := enforceConversationAccess(app, uuid, user)
if err != nil {
return sendErrorEnvelope(r, err)
}
// Already assigned?
if conversation.AssignedTeamID.Int == assigneeID {
return r.SendEnvelope(true)
}
if err := app.conversation.UpdateConversationTeamAssignee(uuid, assigneeID, user); err != nil {
return sendErrorEnvelope(r, err)
}
// Evaluate automation rules on team assignment.
app.automation.EvaluateConversationUpdateRules(uuid, models.EventConversationTeamAssigned)
return r.SendEnvelope(true)
}
// handleUpdateConversationPriority updates the priority of a conversation.
func handleUpdateConversationPriority(r *fastglue.Request) error {
var (
app = r.Context.(*App)
uuid = r.RequestCtx.UserValue("uuid").(string)
auser = r.RequestCtx.UserValue("user").(amodels.User)
priority = string(r.RequestCtx.PostArgs().Peek("priority"))
app = r.Context.(*App)
uuid = r.RequestCtx.UserValue("uuid").(string)
auser = r.RequestCtx.UserValue("user").(amodels.User)
req = priorityUpdateReq{}
)
if err := r.Decode(&req, "json"); err != nil {
app.lo.Error("error decoding priority update request", "error", err)
return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil, envelope.InputError)
}
priority := req.Priority
if priority == "" {
return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.empty", "name", "`priority`"), nil, envelope.InputError)
}
@@ -392,22 +430,26 @@ func handleUpdateConversationPriority(r *fastglue.Request) error {
return sendErrorEnvelope(r, err)
}
// Evaluate automation rules.
app.automation.EvaluateConversationUpdateRules(uuid, models.EventConversationPriorityChange)
return r.SendEnvelope(true)
}
// handleUpdateConversationStatus updates the status of a conversation.
func handleUpdateConversationStatus(r *fastglue.Request) error {
var (
app = r.Context.(*App)
status = string(r.RequestCtx.PostArgs().Peek("status"))
snoozedUntil = string(r.RequestCtx.PostArgs().Peek("snoozed_until"))
uuid = r.RequestCtx.UserValue("uuid").(string)
auser = r.RequestCtx.UserValue("user").(amodels.User)
app = r.Context.(*App)
uuid = r.RequestCtx.UserValue("uuid").(string)
auser = r.RequestCtx.UserValue("user").(amodels.User)
req = statusUpdateReq{}
)
if err := r.Decode(&req, "json"); err != nil {
app.lo.Error("error decoding status update request", "error", err)
return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil, envelope.InputError)
}
status := req.Status
snoozedUntil := req.SnoozedUntil
// Validate inputs
if status == "" {
return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.empty", "name", "`status`"), nil, envelope.InputError)
@@ -442,9 +484,6 @@ func handleUpdateConversationStatus(r *fastglue.Request) error {
return sendErrorEnvelope(r, err)
}
// Evaluate automation rules.
app.automation.EvaluateConversationUpdateRules(uuid, models.EventConversationStatusChange)
// If status is `Resolved`, send CSAT survey if enabled on inbox.
if status == cmodels.StatusResolved {
// Check if CSAT is enabled on the inbox and send CSAT survey message.
@@ -464,18 +503,19 @@ func handleUpdateConversationStatus(r *fastglue.Request) error {
// handleUpdateConversationtags updates conversation tags.
func handleUpdateConversationtags(r *fastglue.Request) error {
var (
app = r.Context.(*App)
tagNames = []string{}
tagJSON = r.RequestCtx.PostArgs().Peek("tags")
auser = r.RequestCtx.UserValue("user").(amodels.User)
uuid = r.RequestCtx.UserValue("uuid").(string)
app = r.Context.(*App)
auser = r.RequestCtx.UserValue("user").(amodels.User)
uuid = r.RequestCtx.UserValue("uuid").(string)
req = tagsUpdateReq{}
)
if err := json.Unmarshal(tagJSON, &tagNames); err != nil {
app.lo.Error("error unmarshalling tags JSON", "error", err)
return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil, envelope.InputError)
if err := r.Decode(&req, "json"); err != nil {
app.lo.Error("error decoding tags update request", "error", err)
return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil, envelope.InputError)
}
tagNames := req.Tags
user, err := app.user.GetAgent(auser.ID, "")
if err != nil {
return sendErrorEnvelope(r, err)
@@ -582,7 +622,7 @@ func handleRemoveUserAssignee(r *fastglue.Request) error {
if err != nil {
return sendErrorEnvelope(r, err)
}
if err = app.conversation.RemoveConversationAssignee(uuid, "user"); err != nil {
if err = app.conversation.RemoveConversationAssignee(uuid, "user", user); err != nil {
return sendErrorEnvelope(r, err)
}
return r.SendEnvelope(true)
@@ -603,7 +643,7 @@ func handleRemoveTeamAssignee(r *fastglue.Request) error {
if err != nil {
return sendErrorEnvelope(r, err)
}
if err = app.conversation.RemoveConversationAssignee(uuid, "team"); err != nil {
if err = app.conversation.RemoveConversationAssignee(uuid, "team", user); err != nil {
return sendErrorEnvelope(r, err)
}
return r.SendEnvelope(true)
@@ -720,7 +760,11 @@ func handleCreateConversation(r *fastglue.Request) error {
app.conversation.UpdateConversationTeamAssignee(conversationUUID, req.AssignedTeamID, user)
}
// Send the created conversation back to the client.
conversation, _ := app.conversation.GetConversation(conversationID, "")
// Trigger webhook event for conversation created.
conversation, err := app.conversation.GetConversation(conversationID, "")
if err == nil {
app.webhook.TriggerEvent(wmodels.EventConversationCreated, conversation)
}
return r.SendEnvelope(conversation)
}

View File

@@ -15,7 +15,7 @@ import (
// initHandlers initializes the HTTP routes and handlers for the application.
func initHandlers(g *fastglue.Fastglue, hub *ws.Hub) {
// Authentication.
g.POST("/api/v1/login", handleLogin)
g.POST("/api/v1/auth/login", handleLogin)
g.GET("/logout", auth(handleLogout))
g.GET("/api/v1/oidc/{id}/login", handleOIDCLogin)
g.GET("/api/v1/oidc/{id}/finish", handleOIDCCallback)
@@ -110,6 +110,8 @@ func initHandlers(g *fastglue.Fastglue, hub *ws.Hub) {
g.POST("/api/v1/agents", perm(handleCreateAgent, "users:manage"))
g.PUT("/api/v1/agents/{id}", perm(handleUpdateAgent, "users:manage"))
g.DELETE("/api/v1/agents/{id}", perm(handleDeleteAgent, "users:manage"))
g.POST("/api/v1/agents/{id}/api-key", perm(handleGenerateAPIKey, "users:manage"))
g.DELETE("/api/v1/agents/{id}/api-key", perm(handleRevokeAPIKey, "users:manage"))
g.POST("/api/v1/agents/reset-password", tryAuth(handleResetPassword))
g.POST("/api/v1/agents/set-password", tryAuth(handleSetPassword))
@@ -157,6 +159,15 @@ func initHandlers(g *fastglue.Fastglue, hub *ws.Hub) {
g.PUT("/api/v1/roles/{id}", perm(handleUpdateRole, "roles:manage"))
g.DELETE("/api/v1/roles/{id}", perm(handleDeleteRole, "roles:manage"))
// Webhooks.
g.GET("/api/v1/webhooks", perm(handleGetWebhooks, "webhooks:manage"))
g.GET("/api/v1/webhooks/{id}", perm(handleGetWebhook, "webhooks:manage"))
g.POST("/api/v1/webhooks", perm(handleCreateWebhook, "webhooks:manage"))
g.PUT("/api/v1/webhooks/{id}", perm(handleUpdateWebhook, "webhooks:manage"))
g.DELETE("/api/v1/webhooks/{id}", perm(handleDeleteWebhook, "webhooks:manage"))
g.PUT("/api/v1/webhooks/{id}/toggle", perm(handleToggleWebhook, "webhooks:manage"))
g.POST("/api/v1/webhooks/{id}/test", perm(handleTestWebhook, "webhooks:manage"))
// Reports.
g.GET("/api/v1/reports/overview/sla", perm(handleOverviewSLA, "reports:manage"))
g.GET("/api/v1/reports/overview/counts", perm(handleOverviewCounts, "reports:manage"))

View File

@@ -45,6 +45,7 @@ import (
tmpl "github.com/abhinavxd/libredesk/internal/template"
"github.com/abhinavxd/libredesk/internal/user"
"github.com/abhinavxd/libredesk/internal/view"
"github.com/abhinavxd/libredesk/internal/webhook"
"github.com/abhinavxd/libredesk/internal/ws"
"github.com/jmoiron/sqlx"
"github.com/knadh/go-i18n"
@@ -220,8 +221,9 @@ func initConversations(
csat *csat.Manager,
automationEngine *automation.Engine,
template *tmpl.Manager,
webhook *webhook.Manager,
) *conversation.Manager {
c, err := conversation.New(hub, i18n, notif, sla, status, priority, inboxStore, userStore, teamStore, mediaStore, settings, csat, automationEngine, template, conversation.Opts{
c, err := conversation.New(hub, i18n, notif, sla, status, priority, inboxStore, userStore, teamStore, mediaStore, settings, csat, automationEngine, template, webhook, conversation.Opts{
DB: db,
Lo: initLogger("conversation_manager"),
OutgoingMessageQueueSize: ko.MustInt("message.outgoing_queue_size"),
@@ -838,6 +840,23 @@ func initReport(db *sqlx.DB, i18n *i18n.I18n) *report.Manager {
return m
}
// initWebhook inits webhook manager.
func initWebhook(db *sqlx.DB, i18n *i18n.I18n) *webhook.Manager {
var lo = initLogger("webhook")
m, err := webhook.New(webhook.Opts{
DB: db,
Lo: lo,
I18n: i18n,
Workers: ko.MustInt("webhook.workers"),
QueueSize: ko.MustInt("webhook.queue_size"),
Timeout: ko.MustDuration("webhook.timeout"),
})
if err != nil {
log.Fatalf("error initializing webhook manager: %v", err)
}
return m
}
// initLogger initializes a logf logger.
func initLogger(src string) *logf.Logger {
lvl, env := ko.MustString("app.log_level"), ko.MustString("app.env")

View File

@@ -9,17 +9,30 @@ import (
"github.com/zerodha/fastglue"
)
type loginRequest struct {
Email string `json:"email"`
Password string `json:"password"`
}
// handleLogin logs in the user and returns the user.
func handleLogin(r *fastglue.Request) error {
var (
app = r.Context.(*App)
email = string(r.RequestCtx.PostArgs().Peek("email"))
password = r.RequestCtx.PostArgs().Peek("password")
ip = realip.FromRequest(r.RequestCtx)
loginReq loginRequest
)
// Decode JSON request.
if err := r.Decode(&loginReq, "json"); err != nil {
return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil, envelope.InputError)
}
if loginReq.Email == "" || loginReq.Password == "" {
return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.T("globals.messages.badRequest"), nil, envelope.InputError)
}
// Verify email and password.
user, err := app.user.VerifyPassword(email, password)
user, err := app.user.VerifyPassword(loginReq.Email, []byte(loginReq.Password))
if err != nil {
return sendErrorEnvelope(r, err)
}

View File

@@ -41,6 +41,7 @@ import (
"github.com/abhinavxd/libredesk/internal/team"
"github.com/abhinavxd/libredesk/internal/template"
"github.com/abhinavxd/libredesk/internal/user"
"github.com/abhinavxd/libredesk/internal/webhook"
"github.com/knadh/go-i18n"
"github.com/knadh/koanf/v2"
"github.com/knadh/stuffbin"
@@ -92,6 +93,7 @@ type App struct {
notifier *notifier.Service
customAttribute *customAttribute.Manager
report *report.Manager
webhook *webhook.Manager
// Global state that stores data on an available app update.
update *AppUpdate
@@ -191,12 +193,13 @@ func main() {
inbox = initInbox(db, i18n)
team = initTeam(db, i18n)
businessHours = initBusinessHours(db, i18n)
webhook = initWebhook(db, i18n)
user = initUser(i18n, db)
wsHub = initWS(user)
notifier = initNotifier()
automation = initAutomationEngine(db, i18n)
sla = initSLA(db, team, settings, businessHours, notifier, template, user, i18n)
conversation = initConversations(i18n, sla, status, priority, wsHub, notifier, db, inbox, user, team, media, settings, csat, automation, template)
conversation = initConversations(i18n, sla, status, priority, wsHub, notifier, db, inbox, user, team, media, settings, csat, automation, template, webhook)
autoassigner = initAutoAssigner(team, user, conversation)
)
automation.SetConversationStore(conversation)
@@ -206,6 +209,7 @@ func main() {
go autoassigner.Run(ctx, autoAssignInterval)
go conversation.Run(ctx, messageIncomingQWorkers, messageOutgoingQWorkers, messageOutgoingScanInterval)
go conversation.RunUnsnoozer(ctx, unsnoozeInterval)
go webhook.Run(ctx)
go notifier.Run(ctx)
go sla.Run(ctx, slaEvaluationInterval)
go sla.SendNotifications(ctx)
@@ -243,6 +247,7 @@ func main() {
tag: initTag(db, i18n),
macro: initMacro(db, i18n),
ai: initAI(db, i18n),
webhook: webhook,
}
app.consts.Store(constants)
@@ -286,6 +291,8 @@ func main() {
autoassigner.Close()
colorlog.Red("Shutting down notifier...")
notifier.Close()
colorlog.Red("Shutting down webhook...")
webhook.Close()
colorlog.Red("Shutting down conversation...")
conversation.Close()
colorlog.Red("Shutting down SLA...")

View File

@@ -4,7 +4,6 @@ import (
"strconv"
amodels "github.com/abhinavxd/libredesk/internal/auth/models"
"github.com/abhinavxd/libredesk/internal/automation/models"
"github.com/abhinavxd/libredesk/internal/envelope"
medModels "github.com/abhinavxd/libredesk/internal/media/models"
"github.com/valyala/fasthttp"
@@ -170,8 +169,6 @@ func handleSendMessage(r *fastglue.Request) error {
if err := app.conversation.SendReply(media, conv.InboxID, user.ID, cuuid, req.Message, req.To, req.CC, req.BCC, map[string]any{} /**meta**/); err != nil {
return sendErrorEnvelope(r, err)
}
// Evaluate automation rules.
app.automation.EvaluateConversationUpdateRules(cuuid, models.EventConversationMessageOutgoing)
}
return r.SendEnvelope(true)
}

View File

@@ -6,30 +6,80 @@ import (
amodels "github.com/abhinavxd/libredesk/internal/auth/models"
"github.com/abhinavxd/libredesk/internal/envelope"
"github.com/abhinavxd/libredesk/internal/user/models"
"github.com/valyala/fasthttp"
"github.com/zerodha/fastglue"
"github.com/zerodha/simplesessions/v3"
)
// authenticateUser handles both API key and session-based authentication
// Returns the authenticated user or an error
// For session-based auth, CSRF is checked for POST/PUT/DELETE requests
func authenticateUser(r *fastglue.Request, app *App) (models.User, error) {
var user models.User
// Check for Authorization header first (API key authentication)
apiKey, apiSecret, err := r.ParseAuthHeader(fastglue.AuthBasic | fastglue.AuthToken)
if err == nil && len(apiKey) > 0 && len(apiSecret) > 0 {
user, err = app.user.ValidateAPIKey(string(apiKey), string(apiSecret))
if err != nil {
return user, err
}
return user, nil
}
// Session-based authentication - Check CSRF first.
method := string(r.RequestCtx.Method())
if method == "POST" || method == "PUT" || method == "DELETE" {
cookieToken := string(r.RequestCtx.Request.Header.Cookie("csrf_token"))
hdrToken := string(r.RequestCtx.Request.Header.Peek("X-CSRFTOKEN"))
// Match CSRF token from cookie and header.
if cookieToken == "" || hdrToken == "" || cookieToken != hdrToken {
app.lo.Error("csrf token mismatch", "method", method, "cookie_token", cookieToken, "header_token", hdrToken)
return user, envelope.NewError(envelope.PermissionError, app.i18n.T("auth.csrfTokenMismatch"), nil)
}
}
// Validate session and fetch user.
sessUser, err := app.auth.ValidateSession(r)
if err != nil || sessUser.ID <= 0 {
app.lo.Error("error validating session", "error", err)
return user, envelope.NewError(envelope.GeneralError, app.i18n.T("auth.invalidOrExpiredSession"), nil)
}
// Get agent user from cache or load it.
user, err = app.user.GetAgentCachedOrLoad(sessUser.ID)
if err != nil {
return user, err
}
// Destroy session if user is disabled.
if !user.Enabled {
if err := app.auth.DestroySession(r); err != nil {
app.lo.Error("error destroying session", "error", err)
}
return user, envelope.NewError(envelope.PermissionError, app.i18n.T("user.accountDisabled"), nil)
}
return user, nil
}
// tryAuth attempts to authenticate the user and add them to the context but doesn't enforce authentication.
// Handlers can check if user exists in context optionally.
// Supports both API key authentication (Authorization header) and session-based authentication.
func tryAuth(handler fastglue.FastRequestHandler) fastglue.FastRequestHandler {
return func(r *fastglue.Request) error {
app := r.Context.(*App)
// Try to validate session without returning error.
userSession, err := app.auth.ValidateSession(r)
if err != nil || userSession.ID <= 0 {
return handler(r)
}
// Try to get user.
user, err := app.user.GetAgentCachedOrLoad(userSession.ID)
// Try to authenticate user using shared authentication logic, but don't return errors
user, err := authenticateUser(r, app)
if err != nil {
// Authentication failed, but this is optional, so continue without user
return handler(r)
}
// Set user in context if found.
// Set user in context if authentication succeeded.
r.RequestCtx.SetUserValue("user", amodels.User{
ID: user.ID,
Email: user.Email.String,
@@ -41,23 +91,25 @@ func tryAuth(handler fastglue.FastRequestHandler) fastglue.FastRequestHandler {
}
}
// auth validates the session and adds the user to the request context.
// auth validates the session or API key and adds the user to the request context.
// Supports both API key authentication (Authorization header) and session-based authentication.
func auth(handler fastglue.FastRequestHandler) fastglue.FastRequestHandler {
return func(r *fastglue.Request) error {
var app = r.Context.(*App)
// Validate session and fetch user.
userSession, err := app.auth.ValidateSession(r)
if err != nil || userSession.ID <= 0 {
app.lo.Error("error validating session", "error", err)
return r.SendErrorEnvelope(http.StatusUnauthorized, app.i18n.T("auth.invalidOrExpiredSession"), nil, envelope.GeneralError)
// Authenticate user using shared authentication logic
user, err := authenticateUser(r, app)
if err != nil {
if envErr, ok := err.(envelope.Error); ok {
if envErr.ErrorType == envelope.PermissionError {
return r.SendErrorEnvelope(http.StatusForbidden, envErr.Message, nil, envelope.PermissionError)
}
return r.SendErrorEnvelope(http.StatusUnauthorized, envErr.Message, nil, envelope.GeneralError)
}
return sendErrorEnvelope(r, err)
}
// Set user in the request context.
user, err := app.user.GetAgentCachedOrLoad(userSession.ID)
if err != nil {
return sendErrorEnvelope(r, err)
}
r.RequestCtx.SetUserValue("user", amodels.User{
ID: user.ID,
Email: user.Email.String,
@@ -69,41 +121,22 @@ func auth(handler fastglue.FastRequestHandler) fastglue.FastRequestHandler {
}
}
// perm matches the CSRF token and checks if the user has the required permission to access the endpoint.
// and sets the user in the request context.
// perm checks if the user has the required permission to access the endpoint.
// Supports both API key authentication (Authorization header) and session-based authentication.
func perm(handler fastglue.FastRequestHandler, perm string) fastglue.FastRequestHandler {
return func(r *fastglue.Request) error {
var (
app = r.Context.(*App)
cookieToken = string(r.RequestCtx.Request.Header.Cookie("csrf_token"))
hdrToken = string(r.RequestCtx.Request.Header.Peek("X-CSRFTOKEN"))
)
var app = r.Context.(*App)
// Match CSRF token from cookie and header.
if cookieToken == "" || hdrToken == "" || cookieToken != hdrToken {
app.lo.Error("csrf token mismatch", "cookie_token", cookieToken, "header_token", hdrToken)
return r.SendErrorEnvelope(http.StatusForbidden, app.i18n.T("auth.csrfTokenMismatch"), nil, envelope.PermissionError)
}
// Validate session and fetch user.
sessUser, err := app.auth.ValidateSession(r)
if err != nil || sessUser.ID <= 0 {
app.lo.Error("error validating session", "error", err)
return r.SendErrorEnvelope(http.StatusUnauthorized, app.i18n.T("auth.invalidOrExpiredSession"), nil, envelope.GeneralError)
}
// Get agent user from cache or load it.
user, err := app.user.GetAgentCachedOrLoad(sessUser.ID)
// Authenticate user using shared authentication logic
user, err := authenticateUser(r, app)
if err != nil {
return sendErrorEnvelope(r, err)
}
// Destroy session if user is disabled.
if !user.Enabled {
if err := app.auth.DestroySession(r); err != nil {
app.lo.Error("error destroying session", "error", err)
if envErr, ok := err.(envelope.Error); ok {
if envErr.ErrorType == envelope.PermissionError {
return r.SendErrorEnvelope(http.StatusForbidden, envErr.Message, nil, envelope.PermissionError)
}
return r.SendErrorEnvelope(http.StatusUnauthorized, envErr.Message, nil, envelope.GeneralError)
}
return r.SendErrorEnvelope(http.StatusUnauthorized, app.i18n.T("user.accountDisabled"), nil, envelope.PermissionError)
return sendErrorEnvelope(r, err)
}
// Split the permission string into object and action and enforce it.

View File

@@ -4,8 +4,8 @@ import (
"strconv"
"github.com/abhinavxd/libredesk/internal/envelope"
"github.com/abhinavxd/libredesk/internal/team/models"
"github.com/valyala/fasthttp"
"github.com/volatiletech/null/v9"
"github.com/zerodha/fastglue"
)
@@ -52,16 +52,15 @@ func handleGetTeam(r *fastglue.Request) error {
// handleCreateTeam creates a new team.
func handleCreateTeam(r *fastglue.Request) error {
var (
app = r.Context.(*App)
name = string(r.RequestCtx.PostArgs().Peek("name"))
timezone = string(r.RequestCtx.PostArgs().Peek("timezone"))
emoji = string(r.RequestCtx.PostArgs().Peek("emoji"))
conversationAssignmentType = string(r.RequestCtx.PostArgs().Peek("conversation_assignment_type"))
businessHrsID, _ = strconv.Atoi(string(r.RequestCtx.PostArgs().Peek("business_hours_id")))
slaPolicyID, _ = strconv.Atoi(string(r.RequestCtx.PostArgs().Peek("sla_policy_id")))
maxAutoAssignedConversations, _ = strconv.Atoi(string(r.RequestCtx.PostArgs().Peek("max_auto_assigned_conversations")))
app = r.Context.(*App)
req = models.Team{}
)
if err := app.team.Create(name, timezone, conversationAssignmentType, null.NewInt(businessHrsID, businessHrsID != 0), null.NewInt(slaPolicyID, slaPolicyID != 0), emoji, maxAutoAssignedConversations); err != nil {
if err := r.Decode(&req, "json"); err != nil {
return sendErrorEnvelope(r, envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil))
}
if err := app.team.Create(req.Name, req.Timezone, req.ConversationAssignmentType, req.BusinessHoursID, req.SLAPolicyID, req.Emoji.String, req.MaxAutoAssignedConversations); err != nil {
return sendErrorEnvelope(r, err)
}
return r.SendEnvelope(true)
@@ -70,20 +69,20 @@ func handleCreateTeam(r *fastglue.Request) error {
// handleUpdateTeam updates an existing team.
func handleUpdateTeam(r *fastglue.Request) error {
var (
app = r.Context.(*App)
name = string(r.RequestCtx.PostArgs().Peek("name"))
timezone = string(r.RequestCtx.PostArgs().Peek("timezone"))
emoji = string(r.RequestCtx.PostArgs().Peek("emoji"))
conversationAssignmentType = string(r.RequestCtx.PostArgs().Peek("conversation_assignment_type"))
id, _ = strconv.Atoi(r.RequestCtx.UserValue("id").(string))
businessHrsID, _ = strconv.Atoi(string(r.RequestCtx.PostArgs().Peek("business_hours_id")))
slaPolicyID, _ = strconv.Atoi(string(r.RequestCtx.PostArgs().Peek("sla_policy_id")))
maxAutoAssignedConversations, _ = strconv.Atoi(string(r.RequestCtx.PostArgs().Peek("max_auto_assigned_conversations")))
app = r.Context.(*App)
id, _ = strconv.Atoi(r.RequestCtx.UserValue("id").(string))
req = models.Team{}
)
if id < 1 {
return r.SendErrorEnvelope(fasthttp.StatusBadRequest, "Invalid team `id`", nil, envelope.InputError)
return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError)
}
if err := app.team.Update(id, name, timezone, conversationAssignmentType, null.NewInt(businessHrsID, businessHrsID != 0), null.NewInt(slaPolicyID, slaPolicyID != 0), emoji, maxAutoAssignedConversations); err != nil {
if err := r.Decode(&req, "json"); err != nil {
return sendErrorEnvelope(r, envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil))
}
if err := app.team.Update(id, req.Name, req.Timezone, req.ConversationAssignmentType, req.BusinessHoursID, req.SLAPolicyID, req.Emoji.String, req.MaxAutoAssignedConversations); err != nil {
return sendErrorEnvelope(r, err)
}
return r.SendEnvelope(true)

View File

@@ -34,6 +34,7 @@ var migList = []migFunc{
{"v0.4.0", migrations.V0_4_0},
{"v0.5.0", migrations.V0_5_0},
{"v0.6.0", migrations.V0_6_0},
{"v0.7.0", migrations.V0_7_0},
}
// upgrade upgrades the database to the current version by running SQL migration files

View File

@@ -26,6 +26,29 @@ const (
maxAvatarSizeMB = 2
)
// Request structs for user-related endpoints
// UpdateAvailabilityRequest represents the request to update user availability
type UpdateAvailabilityRequest struct {
Status string `json:"status"`
}
// ResetPasswordRequest represents the password reset request
type ResetPasswordRequest struct {
Email string `json:"email"`
}
// SetPasswordRequest represents the set password request
type SetPasswordRequest struct {
Token string `json:"token"`
Password string `json:"password"`
}
// AvailabilityRequest represents the request to update agent availability
type AvailabilityRequest struct {
Status string `json:"status"`
}
// handleGetAgents returns all agents.
func handleGetAgents(r *fastglue.Request) error {
var (
@@ -67,29 +90,35 @@ func handleGetAgent(r *fastglue.Request) error {
// handleUpdateAgentAvailability updates the current agent availability.
func handleUpdateAgentAvailability(r *fastglue.Request) error {
var (
app = r.Context.(*App)
auser = r.RequestCtx.UserValue("user").(amodels.User)
status = string(r.RequestCtx.PostArgs().Peek("status"))
ip = realip.FromRequest(r.RequestCtx)
app = r.Context.(*App)
auser = r.RequestCtx.UserValue("user").(amodels.User)
ip = realip.FromRequest(r.RequestCtx)
availReq AvailabilityRequest
)
// Decode JSON request
if err := r.Decode(&availReq, "json"); err != nil {
return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil, envelope.InputError)
}
agent, err := app.user.GetAgent(auser.ID, "")
if err != nil {
return sendErrorEnvelope(r, err)
}
// Same status?
if agent.AvailabilityStatus == status {
if agent.AvailabilityStatus == availReq.Status {
return r.SendEnvelope(true)
}
// Update availability status.
if err := app.user.UpdateAvailability(auser.ID, status); err != nil {
if err := app.user.UpdateAvailability(auser.ID, availReq.Status); err != nil {
return sendErrorEnvelope(r, err)
}
// Skip activity log if agent returns online from away (to avoid spam).
if !(agent.AvailabilityStatus == models.Away && status == models.Online) {
if err := app.activityLog.UserAvailability(auser.ID, auser.Email, status, ip, "", 0); err != nil {
if !(agent.AvailabilityStatus == models.Away && availReq.Status == models.Online) {
if err := app.activityLog.UserAvailability(auser.ID, auser.Email, availReq.Status, ip, "", 0); err != nil {
app.lo.Error("error creating activity log", "error", err)
}
}
@@ -351,19 +380,23 @@ func handleDeleteCurrentAgentAvatar(r *fastglue.Request) error {
func handleResetPassword(r *fastglue.Request) error {
var (
app = r.Context.(*App)
p = r.RequestCtx.PostArgs()
auser, ok = r.RequestCtx.UserValue("user").(amodels.User)
email = string(p.Peek("email"))
resetReq ResetPasswordRequest
)
if ok && auser.ID > 0 {
return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.T("user.userAlreadyLoggedIn"), nil, envelope.InputError)
}
if email == "" {
// Decode JSON request
if err := r.Decode(&resetReq, "json"); err != nil {
return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil, envelope.InputError)
}
if resetReq.Email == "" {
return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.empty", "name", "`email`"), nil, envelope.InputError)
}
agent, err := app.user.GetAgent(0, email)
agent, err := app.user.GetAgent(0, resetReq.Email)
if err != nil {
// Send 200 even if user not found, to prevent email enumeration.
return r.SendEnvelope("Reset password email sent successfully.")
@@ -401,20 +434,22 @@ func handleSetPassword(r *fastglue.Request) error {
var (
app = r.Context.(*App)
agent, ok = r.RequestCtx.UserValue("user").(amodels.User)
p = r.RequestCtx.PostArgs()
password = string(p.Peek("password"))
token = string(p.Peek("token"))
req = SetPasswordRequest{}
)
if ok && agent.ID > 0 {
return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.T("user.userAlreadyLoggedIn"), nil, envelope.InputError)
}
if password == "" {
if err := r.Decode(&req, "json"); err != nil {
return sendErrorEnvelope(r, envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), nil))
}
if req.Password == "" {
return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.empty", "name", "{globals.terms.password}"), nil, envelope.InputError)
}
if err := app.user.ResetPassword(token, password); err != nil {
if err := app.user.ResetPassword(req.Token, req.Password); err != nil {
return sendErrorEnvelope(r, err)
}
@@ -484,3 +519,61 @@ func uploadUserAvatar(r *fastglue.Request, user *models.User, files []*multipart
}
return nil
}
// handleGenerateAPIKey generates a new API key for a user
func handleGenerateAPIKey(r *fastglue.Request) error {
var (
app = r.Context.(*App)
id, _ = strconv.Atoi(r.RequestCtx.UserValue("id").(string))
)
if id <= 0 {
return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError)
}
// Check if user exists
user, err := app.user.GetAgent(id, "")
if err != nil {
return sendErrorEnvelope(r, err)
}
// Generate API key and secret
apiKey, apiSecret, err := app.user.GenerateAPIKey(user.ID)
if err != nil {
return sendErrorEnvelope(r, err)
}
// Return the API key and secret (only shown once)
response := struct {
APIKey string `json:"api_key"`
APISecret string `json:"api_secret"`
}{
APIKey: apiKey,
APISecret: apiSecret,
}
return r.SendEnvelope(response)
}
// handleRevokeAPIKey revokes a user's API key
func handleRevokeAPIKey(r *fastglue.Request) error {
var (
app = r.Context.(*App)
id, _ = strconv.Atoi(r.RequestCtx.UserValue("id").(string))
)
if id <= 0 {
return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError)
}
// Check if user exists
_, err := app.user.GetAgent(id, "")
if err != nil {
return sendErrorEnvelope(r, err)
}
// Revoke API key
if err := app.user.RevokeAPIKey(id); err != nil {
return sendErrorEnvelope(r, err)
}
return r.SendEnvelope(true)
}

180
cmd/webhooks.go Normal file
View File

@@ -0,0 +1,180 @@
package main
import (
"strconv"
"strings"
"github.com/abhinavxd/libredesk/internal/envelope"
"github.com/abhinavxd/libredesk/internal/stringutil"
"github.com/abhinavxd/libredesk/internal/webhook/models"
"github.com/valyala/fasthttp"
"github.com/zerodha/fastglue"
)
// handleGetWebhooks returns all webhooks from the database.
func handleGetWebhooks(r *fastglue.Request) error {
var (
app = r.Context.(*App)
)
webhooks, err := app.webhook.GetAll()
if err != nil {
return sendErrorEnvelope(r, err)
}
// Hide secrets.
for i := range webhooks {
if webhooks[i].Secret != "" {
webhooks[i].Secret = strings.Repeat(stringutil.PasswordDummy, 10)
}
}
return r.SendEnvelope(webhooks)
}
// handleGetWebhook returns a specific webhook by ID.
func handleGetWebhook(r *fastglue.Request) error {
var (
app = r.Context.(*App)
id, _ = strconv.Atoi(r.RequestCtx.UserValue("id").(string))
)
if id <= 0 {
return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError)
}
webhook, err := app.webhook.Get(id)
if err != nil {
return sendErrorEnvelope(r, err)
}
// Hide secret in the response.
if webhook.Secret != "" {
webhook.Secret = strings.Repeat(stringutil.PasswordDummy, 10)
}
return r.SendEnvelope(webhook)
}
// handleCreateWebhook creates a new webhook in the database.
func handleCreateWebhook(r *fastglue.Request) error {
var (
app = r.Context.(*App)
webhook = models.Webhook{}
)
if err := r.Decode(&webhook, "json"); err != nil {
return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), err.Error(), envelope.InputError)
}
// Validate webhook fields
if err := validateWebhook(app, webhook); err != nil {
return r.SendEnvelope(err)
}
_, err := app.webhook.Create(webhook)
if err != nil {
return sendErrorEnvelope(r, err)
}
return r.SendEnvelope(true)
}
// handleUpdateWebhook updates an existing webhook in the database.
func handleUpdateWebhook(r *fastglue.Request) error {
var (
app = r.Context.(*App)
webhook = models.Webhook{}
id, _ = strconv.Atoi(r.RequestCtx.UserValue("id").(string))
)
if id <= 0 {
return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError)
}
if err := r.Decode(&webhook, "json"); err != nil {
return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), err.Error(), envelope.InputError)
}
// Validate webhook fields
if err := validateWebhook(app, webhook); err != nil {
return r.SendEnvelope(err)
}
// If secret is empty or contains dummy characters, fetch existing webhook and preserve the secret
if webhook.Secret == "" || strings.Contains(webhook.Secret, stringutil.PasswordDummy) {
existingWebhook, err := app.webhook.Get(id)
if err != nil {
return sendErrorEnvelope(r, err)
}
webhook.Secret = existingWebhook.Secret
}
if err := app.webhook.Update(id, webhook); err != nil {
return sendErrorEnvelope(r, err)
}
return r.SendEnvelope(true)
}
// handleDeleteWebhook deletes a webhook from the database.
func handleDeleteWebhook(r *fastglue.Request) error {
var (
app = r.Context.(*App)
id, _ = strconv.Atoi(r.RequestCtx.UserValue("id").(string))
)
if id <= 0 {
return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError)
}
if err := app.webhook.Delete(id); err != nil {
return sendErrorEnvelope(r, err)
}
return r.SendEnvelope(true)
}
// handleToggleWebhook toggles the active status of a webhook.
func handleToggleWebhook(r *fastglue.Request) error {
var (
app = r.Context.(*App)
id, _ = strconv.Atoi(r.RequestCtx.UserValue("id").(string))
)
if id <= 0 {
return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError)
}
if err := app.webhook.Toggle(id); err != nil {
return sendErrorEnvelope(r, err)
}
return r.SendEnvelope(true)
}
// handleTestWebhook sends a test payload to a webhook.
func handleTestWebhook(r *fastglue.Request) error {
var (
app = r.Context.(*App)
id, _ = strconv.Atoi(r.RequestCtx.UserValue("id").(string))
)
if id <= 0 {
return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError)
}
if err := app.webhook.SendTestWebhook(id); err != nil {
return sendErrorEnvelope(r, err)
}
return r.SendEnvelope(true)
}
// validateWebhook validates the webhook data.
func validateWebhook(app *App, webhook models.Webhook) error {
if webhook.Name == "" {
return envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.empty", "name", "`name`"), nil)
}
if webhook.URL == "" {
return envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.empty", "name", "`url`"), nil)
}
if len(webhook.Events) == 0 {
return envelope.NewError(envelope.InputError, app.i18n.Ts("globals.messages.empty", "name", "`events`"), nil)
}
return nil
}

View File

@@ -107,6 +107,14 @@ worker_count = 10
# How often to run automatic conversation assignment
autoassign_interval = "5m"
[webhook]
# Number of webhook delivery workers
workers = 5
# Maximum number of webhook deliveries that can be queued
queue_size = 10000
# HTTP timeout for webhook requests
timeout = "15s"
[conversation]
# How often to check for conversations to unsnooze
unsnooze_interval = "5m"

View File

@@ -1,3 +1,3 @@
# Translations / Internationalization
You can help translate libreDesk into different languages by contributing here: [LibreDesk Translation Project](https://crowdin.com/project/libredesk)
You can help translate libreDesk into different languages by contributing here: [Libredesk Translation Project](https://crowdin.com/project/libredesk)

222
docs/docs/webhooks.md Normal file
View File

@@ -0,0 +1,222 @@
# Webhooks
Webhooks allow you to receive real-time HTTP notifications when specific events occur in your Libredesk instance. This enables you to integrate Libredesk with external systems and automate workflows based on conversation and message events.
## Overview
When a configured event occurs in Libredesk, a HTTP POST request is sent to the webhook URL you specify. The request contains a JSON payload with event details and relevant data.
## Webhook Configuration
1. Navigate to **Admin > Integrations > Webhooks** in your Libredesk dashboard
2. Click **Create Webhook**
3. Configure the following:
- **Name**: A descriptive name for your webhook
- **URL**: The endpoint URL where webhook payloads will be sent
- **Events**: Select which events you want to subscribe to
- **Secret**: Optional secret key for signature verification
- **Status**: Enable or disable the webhook
## Security
### Signature Verification
If you provide a secret key, webhook payloads will be signed using HMAC-SHA256. The signature is included in the `X-Signature-256` header in the format `sha256=<signature>`.
To verify the signature:
```python
import hmac
import hashlib
def verify_signature(payload, signature, secret):
expected_signature = hmac.new(
secret.encode('utf-8'),
payload,
hashlib.sha256
).hexdigest()
return hmac.compare_digest(f"sha256={expected_signature}", signature)
```
### Headers
Each webhook request includes the following headers:
- `Content-Type`: `application/json`
- `User-Agent`: `Libredesk-Webhook/<libredesk_version_here>`
- `X-Signature-256`: HMAC signature (if secret is configured)
## Available Events
### Conversation Events
#### `conversation.created`
Triggered when a new conversation is created.
**Sample Payload:**
```json
{
"event": "conversation.created",
"timestamp": "2025-06-15T10:30:00Z",
"payload": {
"id": 123,
"created_at": "2025-06-15T10:30:00Z",
"updated_at": "2025-06-15T10:30:00Z",
"uuid": "550e8400-e29b-41d4-a716-446655440000",
"contact_id": 456,
"inbox_id": 1,
"reference_number": "100",
"priority": "Medium",
"priority_id": 2,
"status": "Open",
"status_id": 1,
"subject": "Help with account setup",
"inbox_name": "Support",
"inbox_channel": "email",
"contact": {
"id": 456,
"first_name": "John",
"last_name": "Doe",
"email": "john.doe@example.com",
"type": "contact"
},
"custom_attributes": {},
"tags": []
}
}
```
#### `conversation.status_changed`
Triggered when a conversation's status is updated.
**Sample Payload:**
```json
{
"event": "conversation.status_changed",
"timestamp": "2025-06-15T10:35:00Z",
"payload": {
"conversation_uuid": "550e8400-e29b-41d4-a716-446655440000",
"previous_status": "Open",
"new_status": "Resolved",
"snooze_until": "",
"actor_id": 789
}
}
```
#### `conversation.assigned`
Triggered when a conversation is assigned to a user.
**Sample Payload:**
```json
{
"event": "conversation.assigned",
"timestamp": "2025-06-15T10:32:00Z",
"payload": {
"conversation_uuid": "550e8400-e29b-41d4-a716-446655440000",
"assigned_to": 789,
"actor_id": 789
}
}
```
#### `conversation.unassigned`
Triggered when a conversation is unassigned from a user.
**Sample Payload:**
```json
{
"event": "conversation.unassigned",
"timestamp": "2025-06-15T10:40:00Z",
"payload": {
"conversation_uuid": "550e8400-e29b-41d4-a716-446655440000",
"actor_id": 789
}
}
```
#### `conversation.tags_changed`
Triggered when tags are added or removed from a conversation.
**Sample Payload:**
```json
{
"event": "conversation.tags_changed",
"timestamp": "2025-06-15T10:45:00Z",
"payload": {
"conversation_uuid": "550e8400-e29b-41d4-a716-446655440000",
"previous_tags": ["bug", "priority"],
"new_tags": ["bug", "priority", "resolved"],
"actor_id": 789
}
}
```
### Message Events
#### `message.created`
Triggered when a new message is created in a conversation.
**Sample Payload:**
```json
{
"event": "message.created",
"timestamp": "2025-06-15T10:33:00Z",
"payload": {
"id": 987,
"created_at": "2025-06-15T10:33:00Z",
"updated_at": "2025-06-15T10:33:00Z",
"uuid": "123e4567-e89b-12d3-a456-426614174000",
"type": "outgoing",
"status": "sent",
"conversation_id": 123,
"content": "<p>Hello! How can I help you today?</p>",
"text_content": "Hello! How can I help you today?",
"content_type": "html",
"private": false,
"sender_id": 789,
"sender_type": "agent",
"attachments": []
}
}
```
#### `message.updated`
Triggered when an existing message is updated.
**Sample Payload:**
```json
{
"event": "message.updated",
"timestamp": "2025-06-15T10:34:00Z",
"payload": {
"id": 987,
"created_at": "2025-06-15T10:33:00Z",
"updated_at": "2025-06-15T10:34:00Z",
"uuid": "123e4567-e89b-12d3-a456-426614174000",
"type": "outgoing",
"status": "sent",
"conversation_id": 123,
"content": "<p>Hello! How can I help you today? (Updated)</p>",
"text_content": "Hello! How can I help you today? (Updated)",
"content_type": "html",
"private": false,
"sender_id": 789,
"sender_type": "agent",
"attachments": []
}
}
```
## Delivery and Retries
- Webhooks are delivered with a 10-second timeout
- Failed deliveries are not automatically retried
- Webhook delivery runs in a background worker pool for better performance
- If the webhook queue is full (configurable in config.toml file), new events may be dropped
## Testing Webhooks
You can test your webhook configuration using tools like:
- [Webhook.site](https://webhook.site) - Generate a temporary URL to inspect webhook payloads

View File

@@ -1,4 +1,4 @@
site_name: LibreDesk Docs
site_name: Libredesk Docs
theme:
name: material
language: en
@@ -31,6 +31,7 @@ nav:
- Upgrade Guide: upgrade.md
- Email Templates: templating.md
- SSO Setup: sso.md
- Webhooks: webhooks.md
- Contributions:
- Developer Setup: developer-setup.md
- Translate Libredesk: translations.md

View File

@@ -38,7 +38,7 @@ describe('Login Component', () => {
it('should show error for invalid login attempt', () => {
// Mock failed login API call
cy.intercept('POST', '**/api/v1/login', {
cy.intercept('POST', '**/api/v1/auth/login', {
statusCode: 401,
body: {
message: 'Invalid credentials'
@@ -61,7 +61,7 @@ describe('Login Component', () => {
it('should login successfully with correct credentials', () => {
// Mock successful login API call
cy.intercept('POST', '**/api/v1/login', {
cy.intercept('POST', '**/api/v1/auth/login', {
statusCode: 200,
body: {
data: {
@@ -111,7 +111,7 @@ describe('Login Component', () => {
it('should show loading state during login', () => {
// Mock slow API response
cy.intercept('POST', '**/api/v1/login', {
cy.intercept('POST', '**/api/v1/auth/login', {
statusCode: 200,
body: {
data: {

View File

@@ -212,7 +212,6 @@ const deleteView = async (view) => {
})
} catch (err) {
emitter.emit(EMITTER_EVENTS.SHOW_TOAST, {
title: 'Error',
variant: 'destructive',
description: handleHTTPError(err).message
})

View File

@@ -7,15 +7,15 @@ const http = axios.create({
})
function getCSRFToken () {
const name = 'csrf_token=';
const cookies = document.cookie.split(';');
const name = 'csrf_token='
const cookies = document.cookie.split(';')
for (let i = 0; i < cookies.length; i++) {
let c = cookies[i].trim();
let c = cookies[i].trim()
if (c.indexOf(name) === 0) {
return c.substring(name.length, c.length);
return c.substring(name.length, c.length)
}
}
return '';
return ''
}
// Request interceptor.
@@ -27,15 +27,20 @@ http.interceptors.request.use((request) => {
// Set content type for POST/PUT requests if the content type is not set.
if ((request.method === 'post' || request.method === 'put') && !request.headers['Content-Type']) {
request.headers['Content-Type'] = 'application/x-www-form-urlencoded'
request.headers['Content-Type'] = 'application/json'
}
if (request.headers['Content-Type'] === 'application/x-www-form-urlencoded') {
request.data = qs.stringify(request.data)
}
return request
})
const getCustomAttributes = (appliesTo) => http.get('/api/v1/custom-attributes', {
params: { applies_to: appliesTo }
})
const getCustomAttributes = (appliesTo) =>
http.get('/api/v1/custom-attributes', {
params: { applies_to: appliesTo }
})
const createCustomAttribute = (data) =>
http.post('/api/v1/custom-attributes', data, {
headers: {
@@ -54,7 +59,8 @@ const searchConversations = (params) => http.get('/api/v1/conversations/search',
const searchMessages = (params) => http.get('/api/v1/messages/search', { params })
const searchContacts = (params) => http.get('/api/v1/contacts/search', { params })
const getEmailNotificationSettings = () => http.get('/api/v1/settings/notifications/email')
const updateEmailNotificationSettings = (data) => http.put('/api/v1/settings/notifications/email', data)
const updateEmailNotificationSettings = (data) =>
http.put('/api/v1/settings/notifications/email', data)
const getPriorities = () => http.get('/api/v1/priorities')
const getStatuses = () => http.get('/api/v1/statuses')
const createStatus = (data) => http.post('/api/v1/statuses', data)
@@ -81,11 +87,12 @@ const updateTemplate = (id, data) =>
const getAllBusinessHours = () => http.get('/api/v1/business-hours')
const getBusinessHours = (id) => http.get(`/api/v1/business-hours/${id}`)
const createBusinessHours = (data) => http.post('/api/v1/business-hours', data, {
headers: {
'Content-Type': 'application/json'
}
})
const createBusinessHours = (data) =>
http.post('/api/v1/business-hours', data, {
headers: {
'Content-Type': 'application/json'
}
})
const updateBusinessHours = (id, data) =>
http.put(`/api/v1/business-hours/${id}`, data, {
headers: {
@@ -96,16 +103,18 @@ const deleteBusinessHours = (id) => http.delete(`/api/v1/business-hours/${id}`)
const getAllSLAs = () => http.get('/api/v1/sla')
const getSLA = (id) => http.get(`/api/v1/sla/${id}`)
const createSLA = (data) => http.post('/api/v1/sla', data, {
headers: {
'Content-Type': 'application/json'
}
})
const updateSLA = (id, data) => http.put(`/api/v1/sla/${id}`, data, {
headers: {
'Content-Type': 'application/json'
}
})
const createSLA = (data) =>
http.post('/api/v1/sla', data, {
headers: {
'Content-Type': 'application/json'
}
})
const updateSLA = (id, data) =>
http.put(`/api/v1/sla/${id}`, data, {
headers: {
'Content-Type': 'application/json'
}
})
const deleteSLA = (id) => http.delete(`/api/v1/sla/${id}`)
const createOIDC = (data) =>
http.post('/api/v1/oidc', data, {
@@ -130,7 +139,11 @@ const updateSettings = (key, data) =>
}
})
const getSettings = (key) => http.get(`/api/v1/settings/${key}`)
const login = (data) => http.post(`/api/v1/login`, data)
const login = (data) => http.post(`/api/v1/auth/login`, data, {
headers: {
'Content-Type': 'application/json'
}
})
const getAutomationRules = (type) =>
http.get(`/api/v1/automations/rules`, {
params: { type: type }
@@ -156,7 +169,12 @@ const updateAutomationRuleWeights = (data) =>
'Content-Type': 'application/json'
}
})
const updateAutomationRulesExecutionMode = (data) => http.put(`/api/v1/automations/rules/execution-mode`, data)
const updateAutomationRulesExecutionMode = (data) =>
http.put(`/api/v1/automations/rules/execution-mode`, data, {
headers: {
'Content-Type': 'application/json'
}
})
const getRoles = () => http.get('/api/v1/roles')
const getRole = (id) => http.get(`/api/v1/roles/${id}`)
const createRole = (data) =>
@@ -174,16 +192,29 @@ const updateRole = (id, data) =>
const deleteRole = (id) => http.delete(`/api/v1/roles/${id}`)
const getContacts = (params) => http.get('/api/v1/contacts', { params })
const getContact = (id) => http.get(`/api/v1/contacts/${id}`)
const updateContact = (id, data) => http.put(`/api/v1/contacts/${id}`, data, {
const updateContact = (id, data) =>
http.put(`/api/v1/contacts/${id}`, data, {
headers: {
'Content-Type': 'multipart/form-data'
}
})
const blockContact = (id, data) => http.put(`/api/v1/contacts/${id}/block`, data, {
headers: {
'Content-Type': 'multipart/form-data'
'Content-Type': 'application/json'
}
})
const blockContact = (id, data) => http.put(`/api/v1/contacts/${id}/block`, data)
const getTeam = (id) => http.get(`/api/v1/teams/${id}`)
const getTeams = () => http.get('/api/v1/teams')
const updateTeam = (id, data) => http.put(`/api/v1/teams/${id}`, data)
const createTeam = (data) => http.post('/api/v1/teams', data)
const updateTeam = (id, data) => http.put(`/api/v1/teams/${id}`, data, {
headers: {
'Content-Type': 'application/json'
}
})
const createTeam = (data) => http.post('/api/v1/teams', data, {
headers: {
'Content-Type': 'application/json'
}
})
const getTeamsCompact = () => http.get('/api/v1/teams/compact')
const deleteTeam = (id) => http.delete(`/api/v1/teams/${id}`)
const updateUser = (id, data) =>
@@ -204,9 +235,21 @@ const getUser = (id) => http.get(`/api/v1/agents/${id}`)
const deleteUserAvatar = () => http.delete('/api/v1/agents/me/avatar')
const getCurrentUser = () => http.get('/api/v1/agents/me')
const getCurrentUserTeams = () => http.get('/api/v1/agents/me/teams')
const updateCurrentUserAvailability = (data) => http.put('/api/v1/agents/me/availability', data)
const resetPassword = (data) => http.post('/api/v1/agents/reset-password', data)
const setPassword = (data) => http.post('/api/v1/agents/set-password', data)
const updateCurrentUserAvailability = (data) => http.put('/api/v1/agents/me/availability', data, {
headers: {
'Content-Type': 'application/json'
}
})
const resetPassword = (data) => http.post('/api/v1/agents/reset-password', data, {
headers: {
'Content-Type': 'application/json'
}
})
const setPassword = (data) => http.post('/api/v1/agents/set-password', data, {
headers: {
'Content-Type': 'application/json'
}
})
const deleteUser = (id) => http.delete(`/api/v1/agents/${id}`)
const createUser = (data) =>
http.post('/api/v1/agents', data, {
@@ -215,32 +258,56 @@ const createUser = (data) =>
}
})
const getTags = () => http.get('/api/v1/tags')
const upsertTags = (uuid, data) => http.post(`/api/v1/conversations/${uuid}/tags`, data)
const updateAssignee = (uuid, assignee_type, data) => http.put(`/api/v1/conversations/${uuid}/assignee/${assignee_type}`, data)
const removeAssignee = (uuid, assignee_type) => http.put(`/api/v1/conversations/${uuid}/assignee/${assignee_type}/remove`)
const updateContactCustomAttribute = (uuid, data) => http.put(`/api/v1/conversations/${uuid}/contacts/custom-attributes`, data,
{
headers: {
'Content-Type': 'application/json'
}
})
const updateConversationCustomAttribute = (uuid, data) => http.put(`/api/v1/conversations/${uuid}/custom-attributes`, data,
{
headers: {
'Content-Type': 'application/json'
}
})
const createConversation = (data) => http.post('/api/v1/conversations', data, {
const upsertTags = (uuid, data) => http.post(`/api/v1/conversations/${uuid}/tags`, data, {
headers: {
'Content-Type': 'application/json'
}
})
const updateConversationStatus = (uuid, data) => http.put(`/api/v1/conversations/${uuid}/status`, data)
const updateConversationPriority = (uuid, data) => http.put(`/api/v1/conversations/${uuid}/priority`, data)
const updateAssignee = (uuid, assignee_type, data) =>
http.put(`/api/v1/conversations/${uuid}/assignee/${assignee_type}`, data, {
headers: {
'Content-Type': 'application/json'
}
})
const removeAssignee = (uuid, assignee_type) =>
http.put(`/api/v1/conversations/${uuid}/assignee/${assignee_type}/remove`)
const updateContactCustomAttribute = (uuid, data) =>
http.put(`/api/v1/conversations/${uuid}/contacts/custom-attributes`, data, {
headers: {
'Content-Type': 'application/json'
}
})
const updateConversationCustomAttribute = (uuid, data) =>
http.put(`/api/v1/conversations/${uuid}/custom-attributes`, data, {
headers: {
'Content-Type': 'application/json'
}
})
const createConversation = (data) =>
http.post('/api/v1/conversations', data, {
headers: {
'Content-Type': 'application/json'
}
})
const updateConversationStatus = (uuid, data) =>
http.put(`/api/v1/conversations/${uuid}/status`, data, {
headers: {
'Content-Type': 'application/json'
}
})
const updateConversationPriority = (uuid, data) =>
http.put(`/api/v1/conversations/${uuid}/priority`, data, {
headers: {
'Content-Type': 'application/json'
}
})
const updateAssigneeLastSeen = (uuid) => http.put(`/api/v1/conversations/${uuid}/last-seen`)
const getConversationMessage = (cuuid, uuid) => http.get(`/api/v1/conversations/${cuuid}/messages/${uuid}`)
const retryMessage = (cuuid, uuid) => http.put(`/api/v1/conversations/${cuuid}/messages/${uuid}/retry`)
const getConversationMessages = (uuid, params) => http.get(`/api/v1/conversations/${uuid}/messages`, { params })
const getConversationMessage = (cuuid, uuid) =>
http.get(`/api/v1/conversations/${cuuid}/messages/${uuid}`)
const retryMessage = (cuuid, uuid) =>
http.put(`/api/v1/conversations/${cuuid}/messages/${uuid}/retry`)
const getConversationMessages = (uuid, params) =>
http.get(`/api/v1/conversations/${uuid}/messages`, { params })
const sendMessage = (uuid, data) =>
http.post(`/api/v1/conversations/${uuid}/messages`, data, {
headers: {
@@ -251,28 +318,33 @@ const getConversation = (uuid) => http.get(`/api/v1/conversations/${uuid}`)
const getConversationParticipants = (uuid) => http.get(`/api/v1/conversations/${uuid}/participants`)
const getAllMacros = () => http.get('/api/v1/macros')
const getMacro = (id) => http.get(`/api/v1/macros/${id}`)
const createMacro = (data) => http.post('/api/v1/macros', data, {
headers: {
'Content-Type': 'application/json'
}
})
const updateMacro = (id, data) => http.put(`/api/v1/macros/${id}`, data, {
headers: {
'Content-Type': 'application/json'
}
})
const createMacro = (data) =>
http.post('/api/v1/macros', data, {
headers: {
'Content-Type': 'application/json'
}
})
const updateMacro = (id, data) =>
http.put(`/api/v1/macros/${id}`, data, {
headers: {
'Content-Type': 'application/json'
}
})
const deleteMacro = (id) => http.delete(`/api/v1/macros/${id}`)
const applyMacro = (uuid, id, data) => http.post(`/api/v1/conversations/${uuid}/macros/${id}/apply`, data, {
headers: {
'Content-Type': 'application/json'
}
})
const applyMacro = (uuid, id, data) =>
http.post(`/api/v1/conversations/${uuid}/macros/${id}/apply`, data, {
headers: {
'Content-Type': 'application/json'
}
})
const getTeamUnassignedConversations = (teamID, params) =>
http.get(`/api/v1/teams/${teamID}/conversations/unassigned`, { params })
const getAssignedConversations = (params) => http.get('/api/v1/conversations/assigned', { params })
const getUnassignedConversations = (params) => http.get('/api/v1/conversations/unassigned', { params })
const getUnassignedConversations = (params) =>
http.get('/api/v1/conversations/unassigned', { params })
const getAllConversations = (params) => http.get('/api/v1/conversations/all', { params })
const getViewConversations = (id, params) => http.get(`/api/v1/views/${id}/conversations`, { params })
const getViewConversations = (id, params) =>
http.get(`/api/v1/views/${id}/conversations`, { params })
const uploadMedia = (data) =>
http.post('/api/v1/media', data, {
headers: {
@@ -314,12 +386,50 @@ const updateView = (id, data) =>
})
const deleteView = (id) => http.delete(`/api/v1/views/me/${id}`)
const getAiPrompts = () => http.get('/api/v1/ai/prompts')
const aiCompletion = (data) => http.post('/api/v1/ai/completion', data)
const updateAIProvider = (data) => http.put('/api/v1/ai/provider', data)
const aiCompletion = (data) => http.post('/api/v1/ai/completion', data, {
headers: {
'Content-Type': 'application/json'
}
})
const updateAIProvider = (data) => http.put('/api/v1/ai/provider', data, {
headers: {
'Content-Type': 'application/json'
}
})
const getContactNotes = (id) => http.get(`/api/v1/contacts/${id}/notes`)
const createContactNote = (id, data) => http.post(`/api/v1/contacts/${id}/notes`, data)
const createContactNote = (id, data) => http.post(`/api/v1/contacts/${id}/notes`, data, {
headers: {
'Content-Type': 'application/json'
}
})
const deleteContactNote = (id, noteId) => http.delete(`/api/v1/contacts/${id}/notes/${noteId}`)
const getActivityLogs = (params) => http.get('/api/v1/activity-logs', { params })
const getWebhooks = () => http.get('/api/v1/webhooks')
const getWebhook = (id) => http.get(`/api/v1/webhooks/${id}`)
const createWebhook = (data) =>
http.post('/api/v1/webhooks', data, {
headers: {
'Content-Type': 'application/json'
}
})
const updateWebhook = (id, data) =>
http.put(`/api/v1/webhooks/${id}`, data, {
headers: {
'Content-Type': 'application/json'
}
})
const deleteWebhook = (id) => http.delete(`/api/v1/webhooks/${id}`)
const toggleWebhook = (id) => http.put(`/api/v1/webhooks/${id}/toggle`)
const testWebhook = (id) => http.post(`/api/v1/webhooks/${id}/test`)
const generateAPIKey = (id) =>
http.post(`/api/v1/agents/${id}/api-key`, {}, {
headers: {
'Content-Type': 'application/json'
}
})
const revokeAPIKey = (id) => http.delete(`/api/v1/agents/${id}/api-key`)
export default {
login,
@@ -448,5 +558,14 @@ export default {
getContactNotes,
createContactNote,
deleteContactNote,
getActivityLogs
getActivityLogs,
getWebhooks,
getWebhook,
createWebhook,
updateWebhook,
deleteWebhook,
toggleWebhook,
testWebhook,
generateAPIKey,
revokeAPIKey
}

View File

@@ -223,7 +223,7 @@ const viewInboxOpen = useStorage('viewInboxOpen', true)
>
<CollapsibleTrigger as-child>
<SidebarMenuButton :isActive="isActiveParent(item.href)">
<span>{{ t(item.titleKey) }}</span>
<span>{{ t(item.titleKey, item.isTitleKeyPlural === true ? 2 : 1) }}</span>
<ChevronRight
class="ml-auto transition-transform duration-200 group-data-[state=open]/collapsible:rotate-90"
/>

View File

@@ -1,149 +1,160 @@
export const reportsNavItems = [
{
titleKey: 'globals.terms.overview',
href: '/reports/overview',
permission: 'reports:manage'
}
{
titleKey: 'globals.terms.overview',
href: '/reports/overview',
permission: 'reports:manage'
}
]
export const adminNavItems = [
{
titleKey: 'globals.terms.workspace',
children: [
{
titleKey: 'globals.terms.general',
href: '/admin/general',
permission: 'general_settings:manage'
},
{
titleKey: 'globals.terms.businessHour',
href: '/admin/business-hours',
permission: 'business_hours:manage'
},
{
titleKey: 'globals.terms.slaPolicy',
href: '/admin/sla',
permission: 'sla:manage'
}
]
},
{
titleKey: 'globals.terms.conversation',
children: [
{
titleKey: 'globals.terms.tag',
href: '/admin/conversations/tags',
permission: 'tags:manage'
},
{
titleKey: 'globals.terms.macro',
href: '/admin/conversations/macros',
permission: 'macros:manage'
},
{
titleKey: 'globals.terms.status',
href: '/admin/conversations/statuses',
permission: 'status:manage'
}
]
},
{
{
titleKey: 'globals.terms.workspace',
children: [
{
titleKey: 'globals.terms.general',
href: '/admin/general',
permission: 'general_settings:manage'
},
{
titleKey: 'globals.terms.businessHour',
href: '/admin/business-hours',
permission: 'business_hours:manage'
},
{
titleKey: 'globals.terms.slaPolicy',
href: '/admin/sla',
permission: 'sla:manage'
}
]
},
{
titleKey: 'globals.terms.conversation',
children: [
{
titleKey: 'globals.terms.tag',
href: '/admin/conversations/tags',
permission: 'tags:manage'
},
{
titleKey: 'globals.terms.macro',
href: '/admin/conversations/macros',
permission: 'macros:manage'
},
{
titleKey: 'globals.terms.status',
href: '/admin/conversations/statuses',
permission: 'status:manage'
}
]
},
{
titleKey: 'globals.terms.inbox',
children: [
{
titleKey: 'globals.terms.inbox',
children: [
{
titleKey: 'globals.terms.inbox',
href: '/admin/inboxes',
permission: 'inboxes:manage'
}
]
},
{
titleKey: 'globals.terms.teammate',
children: [
{
titleKey: 'globals.terms.agent',
href: '/admin/teams/agents',
permission: 'users:manage'
},
{
titleKey: 'globals.terms.team',
href: '/admin/teams/teams',
permission: 'teams:manage'
},
{
titleKey: 'globals.terms.role',
href: '/admin/teams/roles',
permission: 'roles:manage'
},
{
titleKey: 'globals.terms.activityLog',
href: '/admin/teams/activity-log',
permission: 'activity_logs:manage'
}
]
},
{
href: '/admin/inboxes',
permission: 'inboxes:manage'
}
]
},
{
titleKey: 'globals.terms.teammate',
children: [
{
titleKey: 'globals.terms.agent',
href: '/admin/teams/agents',
permission: 'users:manage'
},
{
titleKey: 'globals.terms.team',
href: '/admin/teams/teams',
permission: 'teams:manage'
},
{
titleKey: 'globals.terms.role',
href: '/admin/teams/roles',
permission: 'roles:manage'
},
{
titleKey: 'globals.terms.activityLog',
href: '/admin/teams/activity-log',
permission: 'activity_logs:manage'
}
]
},
{
titleKey: 'globals.terms.automation',
children: [
{
titleKey: 'globals.terms.automation',
children: [
{
titleKey: 'globals.terms.automation',
href: '/admin/automations',
permission: 'automations:manage'
}
]
},
{
href: '/admin/automations',
permission: 'automations:manage'
}
]
},
{
titleKey: 'globals.terms.customAttribute',
children: [
{
titleKey: 'globals.terms.customAttribute',
children: [
{
titleKey: 'globals.terms.customAttribute',
href: '/admin/custom-attributes',
permission: 'custom_attributes:manage'
}
]
},
{
titleKey: 'globals.terms.notification',
children: [
{
titleKey: 'globals.terms.email',
href: '/admin/notification',
permission: 'notification_settings:manage'
}
]
},
{
href: '/admin/custom-attributes',
permission: 'custom_attributes:manage'
}
]
},
{
titleKey: 'globals.terms.notification',
children: [
{
titleKey: 'globals.terms.email',
href: '/admin/notification',
permission: 'notification_settings:manage'
}
]
},
{
titleKey: 'globals.terms.template',
children: [
{
titleKey: 'globals.terms.template',
children: [
{
titleKey: 'globals.terms.template',
href: '/admin/templates',
permission: 'templates:manage'
}
]
},
{
titleKey: 'globals.terms.security',
children: [
{
titleKey: 'globals.terms.sso',
href: '/admin/sso',
permission: 'oidc:manage'
}
]
},
href: '/admin/templates',
permission: 'templates:manage'
}
]
},
{
titleKey: 'globals.terms.security',
children: [
{
titleKey: 'globals.terms.sso',
href: '/admin/sso',
permission: 'oidc:manage'
}
]
},
{
titleKey: 'globals.terms.integration',
isTitleKeyPlural: true,
children: [
{
titleKey: 'globals.terms.webhook',
href: '/admin/webhooks',
permission: 'webhooks:manage'
}
]
}
]
export const accountNavItems = [
{
titleKey: 'globals.terms.profile',
href: '/account/profile',
},
{
titleKey: 'globals.terms.profile',
href: '/account/profile'
}
]
export const contactNavItems = [
{
titleKey: 'globals.terms.contact',
href: '/contacts',
}
]
{
titleKey: 'globals.terms.contact',
href: '/contacts'
}
]

View File

@@ -1,41 +1,42 @@
export const permissions = {
CONVERSATIONS_READ: 'conversations:read',
CONVERSATIONS_WRITE: 'conversations:write',
CONVERSATIONS_READ_ASSIGNED: 'conversations:read_assigned',
CONVERSATIONS_READ_ALL: 'conversations:read_all',
CONVERSATIONS_READ_UNASSIGNED: 'conversations:read_unassigned',
CONVERSATIONS_READ_TEAM_INBOX: 'conversations:read_team_inbox',
CONVERSATIONS_UPDATE_USER_ASSIGNEE: 'conversations:update_user_assignee',
CONVERSATIONS_UPDATE_TEAM_ASSIGNEE: 'conversations:update_team_assignee',
CONVERSATIONS_UPDATE_PRIORITY: 'conversations:update_priority',
CONVERSATIONS_UPDATE_STATUS: 'conversations:update_status',
CONVERSATIONS_UPDATE_TAGS: 'conversations:update_tags',
MESSAGES_READ: 'messages:read',
MESSAGES_WRITE: 'messages:write',
VIEW_MANAGE: 'view:manage',
GENERAL_SETTINGS_MANAGE: 'general_settings:manage',
NOTIFICATION_SETTINGS_MANAGE: 'notification_settings:manage',
STATUS_MANAGE: 'status:manage',
OIDC_MANAGE: 'oidc:manage',
TAGS_MANAGE: 'tags:manage',
MACROS_MANAGE: 'macros:manage',
USERS_MANAGE: 'users:manage',
TEAMS_MANAGE: 'teams:manage',
AUTOMATIONS_MANAGE: 'automations:manage',
INBOXES_MANAGE: 'inboxes:manage',
ROLES_MANAGE: 'roles:manage',
TEMPLATES_MANAGE: 'templates:manage',
REPORTS_MANAGE: 'reports:manage',
BUSINESS_HOURS_MANAGE: 'business_hours:manage',
SLA_MANAGE: 'sla:manage',
AI_MANAGE: 'ai:manage',
CUSTOM_ATTRIBUTES_MANAGE: 'custom_attributes:manage',
CONTACTS_READ_ALL: 'contacts:read_all',
CONTACTS_READ: 'contacts:read',
CONTACTS_WRITE: 'contacts:write',
CONTACTS_BLOCK: 'contacts:block',
CONTACT_NOTES_READ: 'contact_notes:read',
CONTACT_NOTES_WRITE: 'contact_notes:write',
CONTACT_NOTES_DELETE: 'contact_notes:delete',
ACTIVITY_LOGS_MANAGE: 'activity_logs:manage',
};
CONVERSATIONS_READ: 'conversations:read',
CONVERSATIONS_WRITE: 'conversations:write',
CONVERSATIONS_READ_ASSIGNED: 'conversations:read_assigned',
CONVERSATIONS_READ_ALL: 'conversations:read_all',
CONVERSATIONS_READ_UNASSIGNED: 'conversations:read_unassigned',
CONVERSATIONS_READ_TEAM_INBOX: 'conversations:read_team_inbox',
CONVERSATIONS_UPDATE_USER_ASSIGNEE: 'conversations:update_user_assignee',
CONVERSATIONS_UPDATE_TEAM_ASSIGNEE: 'conversations:update_team_assignee',
CONVERSATIONS_UPDATE_PRIORITY: 'conversations:update_priority',
CONVERSATIONS_UPDATE_STATUS: 'conversations:update_status',
CONVERSATIONS_UPDATE_TAGS: 'conversations:update_tags',
MESSAGES_READ: 'messages:read',
MESSAGES_WRITE: 'messages:write',
VIEW_MANAGE: 'view:manage',
GENERAL_SETTINGS_MANAGE: 'general_settings:manage',
NOTIFICATION_SETTINGS_MANAGE: 'notification_settings:manage',
STATUS_MANAGE: 'status:manage',
OIDC_MANAGE: 'oidc:manage',
TAGS_MANAGE: 'tags:manage',
MACROS_MANAGE: 'macros:manage',
USERS_MANAGE: 'users:manage',
TEAMS_MANAGE: 'teams:manage',
AUTOMATIONS_MANAGE: 'automations:manage',
INBOXES_MANAGE: 'inboxes:manage',
ROLES_MANAGE: 'roles:manage',
TEMPLATES_MANAGE: 'templates:manage',
REPORTS_MANAGE: 'reports:manage',
BUSINESS_HOURS_MANAGE: 'business_hours:manage',
SLA_MANAGE: 'sla:manage',
AI_MANAGE: 'ai:manage',
CUSTOM_ATTRIBUTES_MANAGE: 'custom_attributes:manage',
CONTACTS_READ_ALL: 'contacts:read_all',
CONTACTS_READ: 'contacts:read',
CONTACTS_WRITE: 'contacts:write',
CONTACTS_BLOCK: 'contacts:block',
CONTACT_NOTES_READ: 'contact_notes:read',
CONTACT_NOTES_WRITE: 'contact_notes:write',
CONTACT_NOTES_DELETE: 'contact_notes:delete',
ACTIVITY_LOGS_MANAGE: 'activity_logs:manage',
WEBHOOKS_MANAGE: 'webhooks:manage'
}

View File

@@ -52,6 +52,124 @@
</div>
</div>
<!-- API Key Management Section -->
<div class="bg-muted/30 box p-4 space-y-4" v-if="!isNewForm">
<!-- Header -->
<div class="flex items-center justify-between">
<div>
<p class="text-base font-semibold text-gray-900 dark:text-foreground">
{{ $t('globals.terms.apiKey', 2) }}
</p>
<p class="text-sm text-gray-500">
{{ $t('admin.agent.apiKey.description') }}
</p>
</div>
</div>
<!-- API Key Display -->
<div v-if="apiKeyData.api_key" class="space-y-3">
<div class="flex items-center justify-between p-3 bg-background border rounded-md">
<div class="flex items-center gap-3">
<Key class="w-4 h-4 text-gray-400" />
<div>
<p class="text-sm font-medium">{{ $t('globals.terms.apiKey') }}</p>
<p class="text-xs text-gray-500 font-mono">{{ apiKeyData.api_key }}</p>
</div>
</div>
<div class="flex gap-2">
<Button
type="button"
variant="outline"
size="sm"
@click="regenerateAPIKey"
:disabled="isAPIKeyLoading"
>
<RotateCcw class="w-4 h-4 mr-1" />
{{ $t('globals.messages.regenerate') }}
</Button>
<Button
type="button"
variant="destructive"
size="sm"
@click="revokeAPIKey"
:disabled="isAPIKeyLoading"
>
<Trash2 class="w-4 h-4 mr-1" />
{{ $t('globals.messages.revoke') }}
</Button>
</div>
</div>
<!-- Last Used Info -->
<div v-if="apiKeyLastUsedAt" class="text-xs text-gray-500">
{{ $t('globals.messages.lastUsed') }}:
{{ format(new Date(apiKeyLastUsedAt), 'PPpp') }}
</div>
</div>
<!-- No API Key State -->
<div v-else class="text-center py-6">
<Key class="w-8 h-8 text-gray-400 mx-auto mb-2" />
<p class="text-sm text-gray-500 mb-3">{{ $t('admin.agent.apiKey.noKey') }}</p>
<Button type="button" @click="generateAPIKey" :disabled="isAPIKeyLoading">
<Plus class="w-4 h-4 mr-1" />
{{ $t('globals.messages.generate', { name: $t('globals.terms.apiKey') }) }}
</Button>
</div>
</div>
<!-- API Key Display Dialog -->
<Dialog v-model:open="showAPIKeyDialog">
<DialogContent class="sm:max-w-md">
<DialogHeader>
<DialogTitle>
{{ $t('globals.messages.generated', { name: $t('globals.terms.apiKey') }) }}
</DialogTitle>
<DialogDescription> </DialogDescription>
</DialogHeader>
<div class="space-y-4">
<div>
<Label class="text-sm font-medium">{{ $t('globals.terms.apiKey') }}</Label>
<div class="flex items-center gap-2 mt-1">
<Input v-model="newAPIKeyData.api_key" readonly class="font-mono text-sm" />
<Button
type="button"
variant="outline"
size="sm"
@click="copyToClipboard(newAPIKeyData.api_key)"
>
<Copy class="w-4 h-4" />
</Button>
</div>
</div>
<div>
<Label class="text-sm font-medium">{{ $t('globals.terms.secret') }}</Label>
<div class="flex items-center gap-2 mt-1">
<Input v-model="newAPIKeyData.api_secret" readonly class="font-mono text-sm" />
<Button
type="button"
variant="outline"
size="sm"
@click="copyToClipboard(newAPIKeyData.api_secret)"
>
<Copy class="w-4 h-4" />
</Button>
</div>
</div>
<Alert>
<AlertTriangle class="h-4 w-4" />
<AlertTitle>{{ $t('globals.terms.warning') }}</AlertTitle>
<AlertDescription>
{{ $t('admin.agent.apiKey.warningMessage') }}
</AlertDescription>
</Alert>
</div>
<DialogFooter>
<Button @click="closeAPIKeyModal">{{ $t('globals.messages.close') }}</Button>
</DialogFooter>
</DialogContent>
</Dialog>
<!-- Form Fields -->
<FormField v-slot="{ field }" name="first_name">
<FormItem v-auto-animate>
@@ -194,7 +312,7 @@ import { Checkbox } from '@/components/ui/checkbox'
import { Label } from '@/components/ui/label'
import { vAutoAnimate } from '@formkit/auto-animate/vue'
import { Badge } from '@/components/ui/badge'
import { Clock, LogIn } from 'lucide-vue-next'
import { Clock, LogIn, Key, RotateCcw, Trash2, Plus, Copy, AlertTriangle } from 'lucide-vue-next'
import { FormControl, FormField, FormItem, FormLabel, FormMessage } from '@/components/ui/form'
import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar'
import {
@@ -207,7 +325,18 @@ import {
} from '@/components/ui/select'
import { SelectTag } from '@/components/ui/select'
import { Input } from '@/components/ui/input'
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle
} from '@/components/ui/dialog'
import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert'
import { useI18n } from 'vue-i18n'
import { useEmitter } from '@/composables/useEmitter'
import { EMITTER_EVENTS } from '@/constants/emitterEvents.js'
import { format } from 'date-fns'
import api from '@/api'
@@ -238,6 +367,19 @@ const props = defineProps({
const { t } = useI18n()
const teams = ref([])
const roles = ref([])
const emitter = useEmitter()
const apiKeyData = ref({
api_key: props.initialValues?.api_key || '',
api_secret: ''
})
const apiKeyLastUsedAt = ref(props.initialValues?.api_key_last_used_at || null)
const newAPIKeyData = ref({
api_key: '',
api_secret: ''
})
const showAPIKeyDialog = ref(false)
const isAPIKeyLoading = ref(false)
onMounted(async () => {
try {
@@ -245,7 +387,10 @@ onMounted(async () => {
teams.value = teamsResp.value.data.data
roles.value = rolesResp.value.data.data
} catch (err) {
console.log(err)
emitter.emit(EMITTER_EVENTS.SHOW_TOAST, {
variant: 'destructive',
description: t('globals.messages.errorFetching')
})
}
})
@@ -284,6 +429,87 @@ const getInitials = (firstName, lastName) => {
return `${firstName.charAt(0).toUpperCase()}${lastName.charAt(0).toUpperCase()}`
}
const generateAPIKey = async () => {
if (!props.initialValues?.id) return
try {
isAPIKeyLoading.value = true
const response = await api.generateAPIKey(props.initialValues.id)
if (response.data) {
const responseData = response.data.data
newAPIKeyData.value = {
api_key: responseData.api_key,
api_secret: responseData.api_secret
}
apiKeyData.value.api_key = responseData.api_key
// Clear the last used timestamp since this is a new API key
apiKeyLastUsedAt.value = null
showAPIKeyDialog.value = true
emitter.emit(EMITTER_EVENTS.SHOW_TOAST, {
description: t('globals.messages.generatedSuccessfully', {
name: t('globals.terms.apiKey')
})
})
}
} catch (error) {
emitter.emit(EMITTER_EVENTS.SHOW_TOAST, {
variant: 'destructive',
description: t('globals.messages.errorGenerating', {
name: t('globals.terms.apiKey')
})
})
} finally {
isAPIKeyLoading.value = false
}
}
const regenerateAPIKey = async () => {
await generateAPIKey()
}
const revokeAPIKey = async () => {
if (!props.initialValues?.id) return
try {
isAPIKeyLoading.value = true
await api.revokeAPIKey(props.initialValues.id)
apiKeyData.value.api_key = ''
apiKeyData.value.api_secret = ''
apiKeyLastUsedAt.value = null
emitter.emit(EMITTER_EVENTS.SHOW_TOAST, {
description: t('globals.messages.revokedSuccessfully', {
name: t('globals.terms.apiKey')
})
})
} catch (error) {
emitter.emit(EMITTER_EVENTS.SHOW_TOAST, {
variant: 'destructive',
description: t('globals.messages.errorRevoking', {
name: t('globals.terms.apiKey')
})
})
} finally {
isAPIKeyLoading.value = false
}
}
const copyToClipboard = async (text) => {
try {
await navigator.clipboard.writeText(text)
emitter.emit(EMITTER_EVENTS.SHOW_TOAST, {
description: t('globals.messages.copied')
})
} catch (error) {
console.error('Error copying to clipboard:', error)
}
}
const closeAPIKeyModal = () => {
showAPIKeyDialog.value = false
newAPIKeyData.value = { api_key: '', api_secret: '' }
}
watch(
() => props.initialValues,
(newValues) => {
@@ -302,6 +528,10 @@ watch(
'teams',
newValues.teams.map((team) => team.name)
)
// Update API key data
apiKeyData.value.api_key = newValues.api_key || ''
apiKeyLastUsedAt.value = newValues.api_key_last_used_at || null
}, 0)
}
},

View File

@@ -166,7 +166,8 @@ const permissions = ref([
{ name: perms.SLA_MANAGE, label: t('admin.role.sla.manage') },
{ name: perms.AI_MANAGE, label: t('admin.role.ai.manage') },
{ name: perms.CUSTOM_ATTRIBUTES_MANAGE, label: t('admin.role.customAttributes.manage') },
{ name: perms.ACTIVITY_LOGS_MANAGE, label: t('admin.role.activityLog.manage') }
{ name: perms.ACTIVITY_LOGS_MANAGE, label: t('admin.role.activityLog.manage') },
{ name: perms.WEBHOOKS_MANAGE, label: t('admin.role.webhooks.manage') }
]
},
{

View File

@@ -0,0 +1,168 @@
<template>
<form class="space-y-6 w-full">
<FormField v-slot="{ componentField }" name="name">
<FormItem>
<FormLabel>{{ $t('globals.terms.name') }}</FormLabel>
<FormControl>
<Input type="text" placeholder="My Webhook" v-bind="componentField" />
</FormControl>
<FormMessage />
</FormItem>
</FormField>
<FormField v-slot="{ componentField }" name="url">
<FormItem>
<FormLabel>{{ $t('globals.terms.url') }}</FormLabel>
<FormControl>
<Input type="url" placeholder="https://your-app.com/webhook" v-bind="componentField" />
</FormControl>
<FormMessage />
</FormItem>
</FormField>
<FormField name="events" v-slot="{ componentField, handleChange }">
<FormItem>
<FormLabel>{{ $t('globals.terms.event', 2) }}</FormLabel>
<FormDescription>
{{ $t('admin.webhook.events.description') }}
</FormDescription>
<FormControl>
<div class="space-y-6">
<div
v-for="group in webhookEvents"
:key="group.name"
class="rounded border border-border bg-card"
>
<div class="border-b border-border bg-muted/30 px-5 py-3">
<h4 class="font-medium text-card-foreground">{{ group.name }}</h4>
</div>
<div class="p-5 grid gap-4 sm:grid-cols-2 lg:grid-cols-3">
<div
v-for="event in group.events"
:key="event.value"
class="flex items-start space-x-3"
>
<Checkbox
:checked="componentField.modelValue?.includes(event.value)"
@update:checked="
(checked) =>
handleEventChange(
checked,
event.value,
handleChange,
componentField.modelValue
)
"
/>
<label class="font-normal text-sm">{{ event.label }}</label>
</div>
</div>
</div>
</div>
</FormControl>
<FormMessage />
</FormItem>
</FormField>
<FormField v-slot="{ componentField }" name="secret">
<FormItem>
<FormLabel>{{ $t('globals.terms.secret') }}</FormLabel>
<FormControl>
<Input type="password" v-bind="componentField" />
</FormControl>
<FormDescription>{{ $t('admin.webhook.secret.description') }}</FormDescription>
<FormMessage />
</FormItem>
</FormField>
<FormField name="is_active" v-slot="{ value, handleChange }" v-if="!isNewForm">
<FormItem>
<FormControl>
<div class="flex items-center space-x-2">
<Checkbox :checked="value" @update:checked="handleChange" />
<Label>{{ $t('globals.terms.active') }}</Label>
</div>
</FormControl>
<FormMessage />
</FormItem>
</FormField>
<!-- Form submit button slot -->
<slot name="footer"></slot>
</form>
</template>
<script setup>
import { ref } from 'vue'
import { Checkbox } from '@/components/ui/checkbox'
import { Label } from '@/components/ui/label'
import { useI18n } from 'vue-i18n'
import {
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
FormDescription
} from '@/components/ui/form'
import { Input } from '@/components/ui/input'
defineProps({
form: {
type: Object,
required: true
},
isNewForm: {
type: Boolean
}
})
const { t } = useI18n()
const webhookEvents = ref([
{
name: t('globals.terms.conversation'),
events: [
{
value: 'conversation.created',
label: 'Conversation Created'
},
{
value: 'conversation.status_changed',
label: 'Conversation Status Changed'
},
{
value: 'conversation.tags_changed',
label: 'Conversation Tags Changed'
},
{
value: 'conversation.assigned',
label: 'Conversation Assigned'
},
{
value: 'conversation.unassigned',
label: 'Conversation Unassigned'
}
]
},
{
name: t('globals.terms.message'),
events: [
{
value: 'message.created',
label: 'Message Created'
},
{
value: 'message.updated',
label: 'Message Updated'
}
]
}
])
// If checked add event to the list, if unchecked remove it and call handleChange
const handleEventChange = (checked, eventName, handleChange, currentEvents) => {
const events = currentEvents || []
handleChange(checked ? [...events, eventName] : events.filter((e) => e !== eventName))
}
</script>

View File

@@ -0,0 +1,82 @@
import { h } from 'vue'
import dropdown from './dataTableDropdown.vue'
import { format } from 'date-fns'
import { Badge } from '@/components/ui/badge'
export const createColumns = (t) => [
{
accessorKey: 'name',
header: function () {
return h('div', { class: 'text-center' }, t('globals.terms.name'))
},
cell: function ({ row }) {
return h('div', { class: 'text-center font-medium' }, row.getValue('name'))
}
},
{
accessorKey: 'url',
header: function () {
return h('div', { class: 'text-center' }, t('globals.terms.url'))
},
cell: function ({ row }) {
const url = row.getValue('url')
return h('div', { class: 'text-center font-mono text-sm max-w-sm truncate' }, url)
}
},
{
accessorKey: 'events',
header: function () {
return h('div', { class: 'text-center' }, t('globals.terms.event', 2))
},
cell: function ({ row }) {
const events = row.getValue('events')
return h('div', { class: 'text-center' }, [
h(
Badge,
{ variant: 'secondary', class: 'text-xs' },
() => `${events.length} ${t('globals.terms.event', 2).toLowerCase()}`
)
])
}
},
{
accessorKey: 'is_active',
header: () => h('div', { class: 'text-center' }, t('globals.terms.status')),
cell: ({ row }) => {
const isActive = row.getValue('is_active')
return h('div', { class: 'text-center' }, [
h(
Badge,
{
variant: isActive ? 'default' : 'secondary',
class: 'text-xs'
},
() => isActive ? t('globals.terms.active') : t('globals.terms.inactive')
)
])
}
},
{
accessorKey: 'updated_at',
header: function () {
return h('div', { class: 'text-center' }, t('globals.terms.updatedAt'))
},
cell: function ({ row }) {
return h('div', { class: 'text-center text-sm' }, format(row.getValue('updated_at'), 'PPpp'))
}
},
{
id: 'actions',
enableHiding: false,
cell: ({ row }) => {
const webhook = row.original
return h(
'div',
{ class: 'relative' },
h(dropdown, {
webhook
})
)
}
}
]

View File

@@ -0,0 +1,152 @@
<template>
<DropdownMenu>
<DropdownMenuTrigger as-child>
<Button variant="ghost" class="w-8 h-8 p-0">
<span class="sr-only"></span>
<MoreHorizontal class="w-4 h-4" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent>
<DropdownMenuItem :as-child="true">
<RouterLink :to="{ name: 'edit-webhook', params: { id: props.webhook.id } }">
{{ $t('globals.messages.edit') }}
</RouterLink>
</DropdownMenuItem>
<DropdownMenuItem @click="handleToggle">
{{
props.webhook.is_active ? $t('globals.messages.disable') : $t('globals.messages.enable')
}}
</DropdownMenuItem>
<DropdownMenuItem @click="handleTest">
{{
$t('globals.messages.send', {
name: $t('globals.terms.test').toLowerCase()
})
}}
</DropdownMenuItem>
<DropdownMenuSeparator />
<DropdownMenuItem @click="() => (alertOpen = true)" class="text-destructive">
{{ $t('globals.messages.delete') }}
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
<AlertDialog :open="alertOpen" @update:open="alertOpen = $event">
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>{{ $t('globals.messages.areYouAbsolutelySure') }}</AlertDialogTitle>
<AlertDialogDescription>
{{ $t('globals.messages.deletionConfirmation', { name: $t('globals.terms.webhook') }) }}
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>{{ $t('globals.messages.cancel') }}</AlertDialogCancel>
<AlertDialogAction @click="handleDelete">
{{ $t('globals.messages.delete') }}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</template>
<script setup>
import { ref } from 'vue'
import { MoreHorizontal } from 'lucide-vue-next'
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuSeparator,
DropdownMenuTrigger
} from '@/components/ui/dropdown-menu'
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle
} from '@/components/ui/alert-dialog'
import { Button } from '@/components/ui/button'
import api from '@/api'
import { useEmitter } from '@/composables/useEmitter'
import { EMITTER_EVENTS } from '@/constants/emitterEvents.js'
import { handleHTTPError } from '@/utils/http'
import { useI18n } from 'vue-i18n'
const emit = useEmitter()
const { t } = useI18n()
const alertOpen = ref(false)
const props = defineProps({
webhook: {
type: Object,
required: true,
default: () => ({
id: '',
name: '',
is_active: false
})
}
})
async function handleDelete() {
try {
await api.deleteWebhook(props.webhook.id)
alertOpen.value = false
emit.emit(EMITTER_EVENTS.REFRESH_LIST, {
model: 'webhook'
})
emit.emit(EMITTER_EVENTS.SHOW_TOAST, {
title: 'Success',
description: t('globals.messages.deletedSuccessfully', {
name: t('globals.terms.webhook')
})
})
} catch (error) {
emit.emit(EMITTER_EVENTS.SHOW_TOAST, {
variant: 'destructive',
description: handleHTTPError(error).message
})
}
}
async function handleToggle() {
try {
await api.toggleWebhook(props.webhook.id)
emit.emit(EMITTER_EVENTS.REFRESH_LIST, {
model: 'webhook'
})
emit.emit(EMITTER_EVENTS.SHOW_TOAST, {
variant: 'success',
description: t('globals.messages.updatedSuccessfully', {
name: t('globals.terms.webhook')
})
})
} catch (error) {
emit.emit(EMITTER_EVENTS.SHOW_TOAST, {
variant: 'destructive',
description: handleHTTPError(error).message
})
}
}
async function handleTest() {
try {
await api.testWebhook(props.webhook.id)
emit.emit(EMITTER_EVENTS.SHOW_TOAST, {
variant: 'success',
description: t('globals.messages.sentSuccessfully', {
name: t('globals.terms.webhook')
})
})
} catch (error) {
emit.emit(EMITTER_EVENTS.SHOW_TOAST, {
variant: 'destructive',
description: handleHTTPError(error).message
})
}
}
</script>

View File

@@ -0,0 +1,25 @@
import * as z from 'zod'
export const createFormSchema = (t) =>
z.object({
name: z
.string({
required_error: t('globals.messages.required')
})
.min(1, {
message: t('globals.messages.required')
}),
url: z
.string({
required_error: t('globals.messages.required')
})
.url({
message: t('form.error.validUrl')
}),
events: z.array(z.string()).min(1, {
message: t('globals.messages.required')
}),
secret: z.string().optional(),
is_active: z.boolean().default(true).optional(),
headers: z.string().optional()
})

View File

@@ -162,7 +162,7 @@ watch(
}
conversationStore.upsertTags({
tags: JSON.stringify(newTags)
tags: newTags
})
},
{ immediate: false }
@@ -184,13 +184,13 @@ const fetchTags = async () => {
const handleAssignedUserChange = (id) => {
conversationStore.updateAssignee('user', {
assignee_id: id
assignee_id: parseInt(id)
})
}
const handleAssignedTeamChange = (id) => {
conversationStore.updateAssignee('team', {
assignee_id: id
assignee_id: parseInt(id)
})
}

View File

@@ -45,7 +45,7 @@ const routes = [
path: 'contacts/:id',
name: 'contact-detail',
component: () => import('@/views/contact/ContactDetailView.vue'),
meta: { title: 'Contacts' },
meta: { title: 'Contacts' }
},
{
path: '/reports',
@@ -57,7 +57,7 @@ const routes = [
name: 'overview',
component: () => import('@/views/reports/OverviewView.vue'),
meta: { title: 'Overview' }
},
}
]
},
{
@@ -108,7 +108,7 @@ const routes = [
path: 'inboxes/search',
name: 'search',
component: () => import('@/views/search/SearchView.vue'),
meta: { title: 'Search', hidePageHeader: true },
meta: { title: 'Search', hidePageHeader: true }
},
{
path: '/inboxes/:type(assigned|unassigned|all)?',
@@ -124,7 +124,7 @@ const routes = [
component: () => import('@/views/inbox/InboxView.vue'),
meta: {
title: 'Inbox',
type: route => route.params.type === 'assigned' ? 'My inbox' : route.params.type
type: (route) => (route.params.type === 'assigned' ? 'My inbox' : route.params.type)
},
children: [
{
@@ -134,12 +134,13 @@ const routes = [
props: true,
meta: {
title: 'Inbox',
type: route => route.params.type === 'assigned' ? 'My inbox' : route.params.type,
type: (route) =>
route.params.type === 'assigned' ? 'My inbox' : route.params.type,
hidePageHeader: true
}
},
}
]
},
}
]
},
{
@@ -184,21 +185,23 @@ const routes = [
{
path: '',
name: 'business-hours-list',
component: () => import('@/views/admin/business-hours/BusinessHoursList.vue'),
component: () => import('@/views/admin/business-hours/BusinessHoursList.vue')
},
{
path: 'new',
name: 'new-business-hours',
component: () => import('@/views/admin/business-hours/CreateOrEditBusinessHours.vue'),
component: () =>
import('@/views/admin/business-hours/CreateOrEditBusinessHours.vue'),
meta: { title: 'New Business Hours' }
},
{
path: ':id/edit',
name: 'edit-business-hours',
props: true,
component: () => import('@/views/admin/business-hours/CreateOrEditBusinessHours.vue'),
component: () =>
import('@/views/admin/business-hours/CreateOrEditBusinessHours.vue'),
meta: { title: 'Edit Business Hours' }
},
}
]
},
{
@@ -209,7 +212,7 @@ const routes = [
{
path: '',
name: 'sla-list',
component: () => import('@/views/admin/sla/SLAList.vue'),
component: () => import('@/views/admin/sla/SLAList.vue')
},
{
path: 'new',
@@ -223,7 +226,7 @@ const routes = [
name: 'edit-sla',
component: () => import('@/views/admin/sla/CreateEditSLA.vue'),
meta: { title: 'Edit SLA' }
},
}
]
},
{
@@ -234,7 +237,7 @@ const routes = [
{
path: '',
name: 'inbox-list',
component: () => import('@/views/admin/inbox/InboxList.vue'),
component: () => import('@/views/admin/inbox/InboxList.vue')
},
{
path: 'new',
@@ -248,8 +251,8 @@ const routes = [
name: 'edit-inbox',
component: () => import('@/views/admin/inbox/EditInbox.vue'),
meta: { title: 'Edit Inbox' }
},
],
}
]
},
{
path: 'notification',
@@ -268,7 +271,7 @@ const routes = [
{
path: '',
name: 'agent-list',
component: () => import('@/views/admin/agents/AgentList.vue'),
component: () => import('@/views/admin/agents/AgentList.vue')
},
{
path: 'new',
@@ -281,7 +284,7 @@ const routes = [
props: true,
component: () => import('@/views/admin/agents/EditAgent.vue'),
meta: { title: 'Edit agent' }
},
}
]
},
{
@@ -292,7 +295,7 @@ const routes = [
{
path: '',
name: 'team-list',
component: () => import('@/views/admin/teams/TeamList.vue'),
component: () => import('@/views/admin/teams/TeamList.vue')
},
{
path: 'new',
@@ -306,7 +309,7 @@ const routes = [
name: 'edit-team',
component: () => import('@/views/admin/teams/EditTeamForm.vue'),
meta: { title: 'Edit Team' }
},
}
]
},
{
@@ -317,7 +320,7 @@ const routes = [
{
path: '',
name: 'role-list',
component: () => import('@/views/admin/roles/RoleList.vue'),
component: () => import('@/views/admin/roles/RoleList.vue')
},
{
path: 'new',
@@ -338,7 +341,7 @@ const routes = [
path: 'activity-log',
name: 'activity-log',
component: () => import('@/views/admin/activity-log/ActivityLog.vue'),
meta: { title: 'Activity Log' },
meta: { title: 'Activity Log' }
}
]
},
@@ -395,7 +398,7 @@ const routes = [
{
path: '',
name: 'sso-list',
component: () => import('@/views/admin/oidc/OIDCList.vue'),
component: () => import('@/views/admin/oidc/OIDCList.vue')
},
{
path: ':id/edit',
@@ -412,6 +415,32 @@ const routes = [
}
]
},
{
path: 'webhooks',
component: () => import('@/views/admin/webhooks/Webhooks.vue'),
name: 'webhooks',
meta: { title: 'Webhooks' },
children: [
{
path: '',
name: 'webhook-list',
component: () => import('@/views/admin/webhooks/WebhookList.vue')
},
{
path: ':id/edit',
props: true,
name: 'edit-webhook',
component: () => import('@/views/admin/webhooks/CreateEditWebhook.vue'),
meta: { title: 'Edit Webhook' }
},
{
path: 'new',
name: 'new-webhook',
component: () => import('@/views/admin/webhooks/CreateEditWebhook.vue'),
meta: { title: 'New Webhook' }
}
]
},
{
path: 'conversations',
meta: { title: 'Conversations' },
@@ -434,7 +463,7 @@ const routes = [
{
path: '',
name: 'macro-list',
component: () => import('@/views/admin/macros/MacroList.vue'),
component: () => import('@/views/admin/macros/MacroList.vue')
},
{
path: 'new',
@@ -448,7 +477,7 @@ const routes = [
name: 'edit-macro',
component: () => import('@/views/admin/macros/EditMacro.vue'),
meta: { title: 'Edit Macro' }
},
}
]
}
]

View File

@@ -245,9 +245,11 @@ export const useConversationStore = defineStore('conversation', () => {
if (!conv || !msgData || !inboxEmail) return
const latestMessage = msgData.getLatestMessage(conv.uuid, ['incoming', 'outgoing'], true)
if (!latestMessage) return
if (!["received", "sent"].includes(latestMessage.status)) {
if (!latestMessage) {
// Reset recipients if no latest message is found.
currentTo.value = []
currentCC.value = []
currentBCC.value = []
return
}

View File

@@ -0,0 +1,161 @@
<template>
<div class="mb-5">
<CustomBreadcrumb :links="breadcrumbLinks" />
</div>
<Spinner v-if="isLoading" />
<div :class="{ 'opacity-50 transition-opacity duration-300': isLoading }">
<WebhookForm @submit.prevent="onSubmit" :form="form" :isNewForm="isNewForm">
<template #footer>
<div class="flex space-x-3">
<Button type="submit" :isLoading="formLoading">
{{ isNewForm ? t('globals.messages.create') : t('globals.messages.update') }}
</Button>
<Button
v-if="!isNewForm"
type="button"
variant="outline"
:isLoading="testLoading"
@click="handleTestWebhook"
>
{{
$t('globals.messages.send', {
name: t('globals.terms.test').toLowerCase()
})
}}
</Button>
</div>
</template>
</WebhookForm>
</div>
</template>
<script setup>
import { onMounted, ref, computed } from 'vue'
import api from '@/api'
import WebhookForm from '@/features/admin/webhooks/WebhookForm.vue'
import { Spinner } from '@/components/ui/spinner'
import { CustomBreadcrumb } from '@/components/ui/breadcrumb'
import { Button } from '@/components/ui/button'
import { EMITTER_EVENTS } from '@/constants/emitterEvents.js'
import { useEmitter } from '@/composables/useEmitter'
import { handleHTTPError } from '@/utils/http'
import { useI18n } from 'vue-i18n'
import { useRouter } from 'vue-router'
import { useForm } from 'vee-validate'
import { toTypedSchema } from '@vee-validate/zod'
import { createFormSchema } from '@/features/admin/webhooks/formSchema.js'
const router = useRouter()
const { t } = useI18n()
const emitter = useEmitter()
const isLoading = ref(false)
const formLoading = ref(false)
const testLoading = ref(false)
const props = defineProps({
id: {
type: String,
required: false
}
})
const form = useForm({
validationSchema: toTypedSchema(createFormSchema(t)),
initialValues: {
name: '',
url: '',
events: [],
secret: '',
is_active: true,
headers: '{}'
}
})
const onSubmit = form.handleSubmit(async (values) => {
try {
formLoading.value = true
let toastDescription = ''
if (props.id) {
// If secret contains dummy characters, clear it so backend knows to keep existing secret
if (values.secret && values.secret.includes('•')) {
values.secret = ''
}
await api.updateWebhook(props.id, values)
toastDescription = t('globals.messages.updatedSuccessfully', {
name: t('globals.terms.webhook')
})
} else {
await api.createWebhook(values)
router.push({ name: 'webhook-list' })
toastDescription = t('globals.messages.createdSuccessfully', {
name: t('globals.terms.webhook')
})
}
emitter.emit(EMITTER_EVENTS.SHOW_TOAST, {
variant: 'success',
description: toastDescription
})
} catch (error) {
emitter.emit(EMITTER_EVENTS.SHOW_TOAST, {
variant: 'destructive',
description: handleHTTPError(error).message
})
} finally {
formLoading.value = false
}
})
const handleTestWebhook = async () => {
if (!props.id) return
try {
testLoading.value = true
await api.testWebhook(props.id)
emitter.emit(EMITTER_EVENTS.SHOW_TOAST, {
variant: 'success',
description: t('globals.messages.sentSuccessfully', {
name: t('globals.terms.webhook')
})
})
} catch (error) {
emitter.emit(EMITTER_EVENTS.SHOW_TOAST, {
variant: 'destructive',
description: handleHTTPError(error).message
})
} finally {
testLoading.value = false
}
}
const breadCrumLabel = () => {
return props.id ? t('globals.messages.edit') : t('globals.messages.new')
}
const isNewForm = computed(() => {
return props.id ? false : true
})
const breadcrumbLinks = [
{ path: 'webhook-list', label: t('globals.terms.webhook') },
{ path: '', label: breadCrumLabel() }
]
onMounted(async () => {
if (props.id) {
try {
isLoading.value = true
const resp = await api.getWebhook(props.id)
form.setValues(resp.data.data)
// The secret is already masked by the backend, no need to modify it here
} catch (error) {
emitter.emit(EMITTER_EVENTS.SHOW_TOAST, {
variant: 'destructive',
description: handleHTTPError(error).message
})
} finally {
isLoading.value = false
}
}
})
</script>

View File

@@ -0,0 +1,60 @@
<template>
<Spinner v-if="isLoading" />
<div :class="{ 'opacity-50 transition-opacity duration-300': isLoading }">
<div class="flex justify-between mb-5">
<div></div>
<div>
<RouterLink :to="{ name: 'new-webhook' }">
<Button>{{
$t('globals.messages.new', {
name: $t('globals.terms.webhook')
})
}}</Button>
</RouterLink>
</div>
</div>
<div>
<DataTable :columns="createColumns(t)" :data="webhooks" />
</div>
</div>
</template>
<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import DataTable from '@/components/datatable/DataTable.vue'
import { createColumns } from '@/features/admin/webhooks/dataTableColumns.js'
import { Button } from '@/components/ui/button'
import { useEmitter } from '@/composables/useEmitter'
import { useI18n } from 'vue-i18n'
import { Spinner } from '@/components/ui/spinner'
import { EMITTER_EVENTS } from '@/constants/emitterEvents.js'
import api from '@/api'
const webhooks = ref([])
const { t } = useI18n()
const isLoading = ref(false)
const emit = useEmitter()
onMounted(() => {
fetchAll()
emit.on(EMITTER_EVENTS.REFRESH_LIST, refreshList)
})
onUnmounted(() => {
emit.off(EMITTER_EVENTS.REFRESH_LIST, refreshList)
})
const refreshList = (data) => {
if (data?.model === 'webhook') fetchAll()
}
const fetchAll = async () => {
try {
isLoading.value = true
const resp = await api.getWebhooks()
webhooks.value = resp.data.data
} finally {
isLoading.value = false
}
}
</script>

View File

@@ -0,0 +1,24 @@
<template>
<AdminPageWithHelp>
<template #content>
<router-view />
</template>
<template #help>
<p>Configure webhooks to receive real-time notifications when events occur in your Libredesk workspace.</p>
<p>Webhooks allow you to integrate Libredesk with external services by sending HTTP POST requests when specific events happen.</p>
<a
href="https://libredesk.io/docs/webhooks/"
target="_blank"
rel="noopener noreferrer"
class="link-style"
>
<p>Learn more</p>
</a>
</template>
</AdminPageWithHelp>
</template>
<script setup>
import AdminPageWithHelp from '@/layouts/admin/AdminPageWithHelp.vue'
</script>

View File

@@ -11,6 +11,7 @@
"globals.terms.conversation": "Conversation | Conversations",
"globals.terms.provider": "Provider | Providers",
"globals.terms.state": "State | States",
"globals.terms.webhook": "Webhook | Webhooks",
"globals.terms.session": "Session | Sessions",
"globals.terms.media": "Media | Medias",
"globals.terms.permission": "Permission | Permissions",
@@ -28,6 +29,9 @@
"globals.terms.businessHour": "Business Hour | Business Hours",
"globals.terms.priority": "Priority | Priorities",
"globals.terms.status": "Status | Statuses",
"globals.terms.secret": "Secret | Secrets",
"globals.terms.inactive": "Inactive | Inactives",
"globals.terms.integration": "Integration | Integrations",
"globals.terms.content": "Content | Contents",
"globals.terms.appRootURL": "App Root URL",
"globals.terms.dashboard": "Dashboard | Dashboards",
@@ -127,6 +131,7 @@
"globals.terms.download": "Download | Downloads",
"globals.terms.import": "Import | Imports",
"globals.terms.export": "Export | Exports",
"globals.terms.test": "Test | Tests",
"globals.terms.confirmation": "Confirmation | Confirmations",
"globals.terms.dialog": "Dialog | Dialogs",
"globals.terms.modal": "Modal | Modals",
@@ -155,6 +160,7 @@
"globals.terms.channel": "Channel",
"globals.terms.configure": "Configure",
"globals.terms.date": "Date",
"globals.terms.data": "Data | Datas",
"globals.terms.timestamp": "Timestamp | Timestamps",
"globals.terms.description": "Description | Descriptions",
"globals.terms.fromEmailAddress": "From email address | From email addresses",
@@ -181,6 +187,7 @@
"globals.terms.resolve": "Resolve",
"globals.terms.recipient": "Recipient | Recipients",
"globals.terms.tls": "TLS | TLSs",
"globals.terms.credential": "Credential | Credentials",
"globals.messages.invalid": "Invalid {name}",
"globals.messages.custom": "Custom {name}",
"globals.messages.replying": "Replying",
@@ -225,6 +232,15 @@
"globals.messages.createdSuccessfully": "{name} created successfully",
"globals.messages.blockedSuccessfully": "{name} blocked successfully",
"globals.messages.unblockedSuccessfully": "{name} unblocked successfully",
"globals.messages.sentSuccessfully": "{name} sent successfully",
"globals.messages.revokedSuccessfully": "{name} revoked successfully",
"globals.messages.errorRevoking": "Error revoking {name}",
"globals.messages.generatedSuccessfully": "{name} generated successfully",
"globals.messages.generate": "Generate {name}",
"globals.messages.generated": "{name} generated",
"globals.messages.regenerate": "Regenerate",
"globals.messages.revoke": "Revoke",
"globals.messages.lastUsed": "Last used",
"globals.messages.pageTooLarge": "Page size is too large, should be at most {max}",
"globals.messages.edit": "Edit {name}",
"globals.messages.delete": "Delete {name}",
@@ -240,6 +256,7 @@
"globals.messages.yes": "Yes {name}",
"globals.messages.no": "No {name}",
"globals.messages.select": "Select {name}",
"globals.messages.copied": "Copied to clipboard",
"globals.messages.search": "Search {name}",
"globals.messages.type": "{name} type",
"globals.messages.typeOf": "Type of {name}",
@@ -324,7 +341,7 @@
"csat.alreadySubmitted": "CSAT already submitted",
"auth.csrfTokenMismatch": "CSRF token mismatch",
"auth.invalidOrExpiredSession": "Invalid or expired session",
"auth.invalidOrExpiredSessionClearCookie": "Invalid or expired session, clear cookies and try again",
"auth.invalidOrExpiredSessionClearCookie": "Invalid or expired session. Please clear your cookies and try again.",
"auth.signIn": "Sign in to your account",
"auth.orContinueWith": "Or continue with",
"auth.enterEmail": "Enter your email",
@@ -355,6 +372,8 @@
"navigation.away": "Away",
"navigation.logout": "Logout",
"admin.empty": "Select a section from the sidebar",
"admin.webhook.events.description": "Select the events you want to subscribe to. You can select multiple events.",
"admin.webhook.secret.description": "Optional secret key for webhook signature verification.",
"admin.general.siteName": "Site Name",
"admin.general.siteName.description": "Name for your support desk.",
"admin.general.siteName.min": "Site name should be at least 1 character",
@@ -445,6 +464,9 @@
"admin.inbox.configureChannel": "Configure channel",
"admin.inbox.createEmailInbox": "Create Email Inbox",
"admin.agent.deleteConfirmation": "This will permanently delete the agent. Consider disabling the account instead.",
"admin.agent.apiKey.description": "Generate API keys for this agent to access libredesk programmatically.",
"admin.agent.apiKey.noKey": "No API key has been generated for this agent.",
"admin.agent.apiKey.warningMessage": "This secret will only be shown once. Make sure to copy it now.",
"admin.role.roleForAllSupportAgents": "Role for all support agents",
"admin.role.setPermissionsForThisRole": "Set permissions for this role",
"admin.role.cannotModifyAdminRole": "Cannot modify admin role, Please create a new role.",

View File

@@ -11,6 +11,7 @@
"globals.terms.conversation": "संभाषण | संभाषण",
"globals.terms.provider": "प्रदाता | प्रदाते",
"globals.terms.state": "राज्य | राज्ये",
"globals.terms.webhook": "वेबहुक | वेबहुक्स",
"globals.terms.session": "सत्र | सत्रे",
"globals.terms.media": "मीडिया | मीडिया",
"globals.terms.permission": "परवानगी | परवानग्या",
@@ -44,6 +45,7 @@
"globals.terms.action": "क्रिया | क्रिया",
"globals.terms.value": "मूल्य | मूल्ये",
"globals.terms.event": "घटना | घटना",
"globals.terms.secret": "गुप्त | गुप्त",
"globals.terms.automation": "ऑटोमेशन | ऑटोमेशन",
"globals.terms.oidc": "OIDC | OIDC",
"globals.terms.oidcProvider": "OIDC प्रदाता | OIDC प्रदाते",
@@ -80,6 +82,7 @@
"globals.terms.unassigned": "नियुक्त नसलेले",
"globals.terms.pending": "प्रलंबित",
"globals.terms.active": "सक्रिय",
"globals.terms.inactive": "निष्क्रिय",
"globals.terms.url": "URL | URL",
"globals.terms.rootURL": "मूळ URL",
"globals.terms.key": "की | की",
@@ -135,6 +138,7 @@
"globals.terms.regex": "रेगेक्स | रेगेक्स",
"globals.terms.appliesTo": "यांना लागू होते",
"globals.terms.createdOn": "यावर तयार केले",
"globals.terms.test": "चाचणी | चाचण्या",
"globals.terms.awayReassigning": "दूर आणि पुन्हा नियुक्त करत आहे",
"globals.terms.availabilityStatus": "उपलब्धता स्थिती",
"globals.terms.lastActive": "शेवटी सक्रिय",
@@ -253,6 +257,7 @@
"globals.messages.fileTypeisNotAnImage": "फाइल प्रकार प्रतिमा नाही",
"globals.messages.notFound": "{name} सापडले नाही",
"globals.messages.empty": "{name} रिकामे",
"globals.messages.sentSuccessfully": "{name} यशस्वीरित्या पाठवले",
"globals.messages.mismatch": "{name} जुळत नाही",
"globals.messages.errorSendingPasswordResetEmail": "पासवर्ड रीसेट ईमेल पाठवताना त्रुटी",
"globals.messages.cannotBeEmpty": "{name} रिकामे असू शकत नाही",
@@ -355,6 +360,8 @@
"navigation.away": "दूर",
"navigation.logout": "लॉगआउट",
"admin.empty": "साइडबारमधून एक विभाग निवडा",
"admin.webhook.events.description": "तुम्ही ज्या घटनांवर सदस्यता घेऊ इच्छिता त्या निवडा. तुम्ही एकाधिक घटना निवडू शकता.",
"admin.webhook.secret.description": "वेबहुक सिग्नेचर पडताळणीसाठी वैकल्पिक गुप्त की.",
"admin.general.siteName": "साइट नाव",
"admin.general.siteName.description": "तुमच्या सपोर्ट डेस्कचे नाव.",
"admin.general.siteName.min": "साइट नाव कमीत कमी 1 अक्षर असावे",

View File

@@ -23,5 +23,5 @@ type ActivityLog struct {
TargetModelID int `db:"target_model_id" json:"target_model_id"`
IP string `db:"ip" json:"ip"`
Total int `db:"total" json:"total"`
Total int `db:"total" json:"-"`
}

View File

@@ -43,6 +43,9 @@ const (
// Roles
PermRolesManage = "roles:manage"
// Webhooks
PermWebhooksManage = "webhooks:manage"
// Templates
PermTemplatesManage = "templates:manage"
@@ -125,6 +128,7 @@ var validPermissions = map[string]struct{}{
PermContactNotesWrite: {},
PermContactNotesDelete: {},
PermActivityLogsManage: {},
PermWebhooksManage: {},
}
// PermissionExists returns true if the permission exists else false

View File

@@ -40,9 +40,9 @@ const (
// ConversationTask represents a unit of work for processing conversations.
type ConversationTask struct {
taskType TaskType
eventType string
conversationUUID string
taskType TaskType
eventType string
conversation cmodels.Conversation
}
type Engine struct {
@@ -151,9 +151,9 @@ func (e *Engine) worker(ctx context.Context) {
}
switch task.taskType {
case NewConversation:
e.handleNewConversation(task.conversationUUID)
e.handleNewConversation(task.conversation)
case UpdateConversation:
e.handleUpdateConversation(task.conversationUUID, task.eventType)
e.handleUpdateConversation(task.conversation, task.eventType)
case TimeTrigger:
e.handleTimeTrigger()
}
@@ -272,7 +272,7 @@ func (e *Engine) UpdateRuleExecutionMode(ruleType, mode string) error {
}
// EvaluateNewConversationRules enqueues a new conversation for rule evaluation.
func (e *Engine) EvaluateNewConversationRules(conversationUUID string) {
func (e *Engine) EvaluateNewConversationRules(conversation cmodels.Conversation) {
e.closedMu.RLock()
defer e.closedMu.RUnlock()
if e.closed {
@@ -280,8 +280,8 @@ func (e *Engine) EvaluateNewConversationRules(conversationUUID string) {
}
select {
case e.taskQueue <- ConversationTask{
taskType: NewConversation,
conversationUUID: conversationUUID,
taskType: NewConversation,
conversation: conversation,
}:
default:
// Queue is full.
@@ -289,8 +289,8 @@ func (e *Engine) EvaluateNewConversationRules(conversationUUID string) {
}
}
// EvaluateConversationUpdateRules enqueues an updated conversation for rule evaluation.
func (e *Engine) EvaluateConversationUpdateRules(conversationUUID string, eventType string) {
// EvaluateConversationUpdateRules enqueues a conversation for rule evaluation, this function exists along with EvaluateConversationUpdateRulesByID to reduce DB queries for fetching conversations.
func (e *Engine) EvaluateConversationUpdateRules(conversation cmodels.Conversation, eventType string) {
if eventType == "" {
e.lo.Error("error evaluating conversation update rules: eventType is empty")
return
@@ -302,9 +302,9 @@ func (e *Engine) EvaluateConversationUpdateRules(conversationUUID string, eventT
}
select {
case e.taskQueue <- ConversationTask{
taskType: UpdateConversation,
conversationUUID: conversationUUID,
eventType: eventType,
taskType: UpdateConversation,
eventType: eventType,
conversation: conversation,
}:
default:
// Queue is full.
@@ -312,33 +312,34 @@ func (e *Engine) EvaluateConversationUpdateRules(conversationUUID string, eventT
}
}
// handleNewConversation handles new conversation events.
func (e *Engine) handleNewConversation(conversationUUID string) {
e.lo.Debug("handling new conversation", "uuid", conversationUUID)
conversation, err := e.conversationStore.GetConversation(0, conversationUUID)
// EvaluateConversationUpdateRulesByID fetches conversation by ID and enqueues for rule evaluation,
// This function is useful when callers want to fresh fetch the conversation from the database instead of passing it directly as they might have a stale copy.
func (e *Engine) EvaluateConversationUpdateRulesByID(conversationID int, conversationUUID, eventType string) {
conversation, err := e.conversationStore.GetConversation(conversationID, conversationUUID)
if err != nil {
e.lo.Error("error fetching conversation for new event", "uuid", conversationUUID, "error", err)
e.lo.Error("error fetching conversation", "conversation_id", conversationID, "error", err)
return
}
e.EvaluateConversationUpdateRules(conversation, eventType)
}
// handleNewConversation handles new conversation events.
func (e *Engine) handleNewConversation(conversation cmodels.Conversation) {
e.lo.Debug("handling new conversation for automation rule evaluation", "uuid", conversation.UUID)
rules := e.filterRulesByType(models.RuleTypeNewConversation, "")
if len(rules) == 0 {
e.lo.Warn("no rules to evaluate for new conversation", "uuid", conversationUUID)
e.lo.Warn("no rules to evaluate for new conversation rule evaluation", "uuid", conversation.UUID)
return
}
e.evalConversationRules(rules, conversation)
}
// handleUpdateConversation handles update conversation events with specific eventType.
func (e *Engine) handleUpdateConversation(conversationUUID, eventType string) {
e.lo.Debug("handling update conversation", "uuid", conversationUUID, "event_type", eventType)
conversation, err := e.conversationStore.GetConversation(0, conversationUUID)
if err != nil {
e.lo.Error("error fetching conversation for update event", "uuid", conversationUUID, "error", err)
return
}
func (e *Engine) handleUpdateConversation(conversation cmodels.Conversation, eventType string) {
e.lo.Debug("handling update conversation for automation rule evaluation", "uuid", conversation.UUID, "event_type", eventType)
rules := e.filterRulesByType(models.RuleTypeConversationUpdate, eventType)
if len(rules) == 0 {
e.lo.Warn("no rules to evaluate for conversation update", "uuid", conversationUUID, "event_type", eventType)
e.lo.Warn("no rules to evaluate for conversation update", "uuid", conversation.UUID, "event_type", eventType)
return
}
e.evalConversationRules(rules, conversation)
@@ -346,7 +347,7 @@ func (e *Engine) handleUpdateConversation(conversationUUID, eventType string) {
// handleTimeTrigger handles time trigger events.
func (e *Engine) handleTimeTrigger() {
e.lo.Debug("handling time triggers")
e.lo.Info("running time trigger evaluation for automation rules")
thirtyDaysAgo := time.Now().Add(-30 * 24 * time.Hour)
conversations, err := e.conversationStore.GetConversationsCreatedAfter(thirtyDaysAgo)
if err != nil {
@@ -358,7 +359,7 @@ func (e *Engine) handleTimeTrigger() {
e.lo.Warn("no rules to evaluate for time trigger")
return
}
e.lo.Debug("fetched conversations for evaluating time triggers", "conversations_count", len(conversations), "rules_count", len(rules))
e.lo.Info("fetched conversations for evaluating time triggers", "conversations_count", len(conversations), "rules_count", len(rules))
for _, c := range conversations {
// Fetch entire conversation.
conversation, err := e.conversationStore.GetConversation(0, c.UUID)

View File

@@ -32,6 +32,7 @@ import (
tmodels "github.com/abhinavxd/libredesk/internal/team/models"
"github.com/abhinavxd/libredesk/internal/template"
umodels "github.com/abhinavxd/libredesk/internal/user/models"
wmodels "github.com/abhinavxd/libredesk/internal/webhook/models"
"github.com/abhinavxd/libredesk/internal/ws"
"github.com/jmoiron/sqlx"
"github.com/knadh/go-i18n"
@@ -65,6 +66,7 @@ type Manager struct {
slaStore slaStore
settingsStore settingsStore
csatStore csatStore
webhookStore webhookStore
notifier *notifier.Service
lo *logf.Logger
db *sqlx.DB
@@ -128,6 +130,10 @@ type csatStore interface {
MakePublicURL(appBaseURL, uuid string) string
}
type webhookStore interface {
TriggerEvent(event wmodels.WebhookEvent, data any)
}
// Opts holds the options for creating a new Manager.
type Opts struct {
DB *sqlx.DB
@@ -152,6 +158,7 @@ func New(
csatStore csatStore,
automation *automation.Engine,
template *template.Manager,
webhook webhookStore,
opts Opts) (*Manager, error) {
var q queries
@@ -170,6 +177,7 @@ func New(
mediaStore: mediaStore,
settingsStore: settingsStore,
csatStore: csatStore,
webhookStore: webhook,
slaStore: slaStore,
statusStore: statusStore,
priorityStore: priorityStore,
@@ -221,7 +229,7 @@ type queries struct {
// Message queries.
GetMessage *sqlx.Stmt `query:"get-message"`
GetMessages string `query:"get-messages"`
GetPendingMessages *sqlx.Stmt `query:"get-pending-messages"`
GetOutgoingPendingMessages *sqlx.Stmt `query:"get-outgoing-pending-messages"`
GetMessageSourceIDs *sqlx.Stmt `query:"get-message-source-ids"`
GetConversationUUIDFromMessageUUID *sqlx.Stmt `query:"get-conversation-uuid-from-message-uuid"`
InsertMessage *sqlx.Stmt `query:"insert-message"`
@@ -486,11 +494,21 @@ func (c *Manager) UpdateConversationUserAssignee(uuid string, assigneeID int, ac
return envelope.NewError(envelope.GeneralError, c.i18n.Ts("globals.messages.errorUpdating", "name", "{globals.terms.conversation}"), nil)
}
c.webhookStore.TriggerEvent(wmodels.EventConversationAssigned, map[string]any{
"conversation_uuid": uuid,
"assigned_to": assigneeID,
"actor_id": actor.ID,
})
// Refetch the conversation to get the updated details.
conversation, err := c.GetConversation(0, uuid)
if err != nil {
return err
}
// Evaluate automation rules.
c.automation.EvaluateConversationUpdateRules(conversation, amodels.EventConversationUserAssigned)
// Send email to assignee.
if err := c.SendAssignedConversationEmail([]int{assigneeID}, conversation); err != nil {
c.lo.Error("error sending assigned conversation email", "error", err)
@@ -499,12 +517,13 @@ func (c *Manager) UpdateConversationUserAssignee(uuid string, assigneeID int, ac
if err := c.RecordAssigneeUserChange(uuid, assigneeID, actor); err != nil {
return envelope.NewError(envelope.GeneralError, c.i18n.Ts("globals.messages.errorUpdating", "name", "{globals.terms.conversation}"), nil)
}
return nil
}
// UpdateConversationTeamAssignee sets the assignee of a conversation to a specific team and sets the assigned user id to NULL.
func (c *Manager) UpdateConversationTeamAssignee(uuid string, teamID int, actor umodels.User) error {
// Store previous assigned team ID to apply SLA policy if team has changed.
// Store previously assigned team ID to apply SLA policy if team has changed.
conversation, err := c.GetConversation(0, uuid)
if err != nil {
return err
@@ -520,27 +539,29 @@ func (c *Manager) UpdateConversationTeamAssignee(uuid string, teamID int, actor
return nil
}
// Apply SLA policy if team has changed and the new team has an SLA policy.
if previousAssignedTeamID != teamID && teamID > 0 {
// Team changed?
if previousAssignedTeamID != teamID {
team, err := c.teamStore.Get(teamID)
if err != nil {
return nil
}
// Fetch the conversation again to get the updated details.
conversation, err := c.GetConversation(0, uuid)
if err != nil {
return nil
}
if team.SLAPolicyID.Int > 0 {
systemUser, err := c.userStore.GetSystemUser()
if err != nil {
return nil
}
// Fetch the conversation again to get the updated assignee details.
conversation, err := c.GetConversation(0, uuid)
if err != nil {
return nil
}
if err := c.ApplySLA(conversation, team.SLAPolicyID.Int, systemUser); err != nil {
return nil
}
}
// Evaluate automation rules for conversation team assignment.
c.automation.EvaluateConversationUpdateRules(conversation, amodels.EventConversationTeamAssigned)
}
return nil
}
@@ -583,6 +604,14 @@ func (c *Manager) UpdateConversationPriority(uuid string, priorityID int, priori
c.lo.Error("error updating conversation priority", "error", err)
return envelope.NewError(envelope.GeneralError, c.i18n.Ts("globals.messages.errorUpdating", "name", "{globals.terms.conversation}"), nil)
}
// Evaluate automation rules for conversation priority change.
conversation, err := c.GetConversation(0, uuid)
if err == nil {
c.automation.EvaluateConversationUpdateRules(conversation, amodels.EventConversationPriorityChange)
}
// Record activity.
if err := c.RecordPriorityChange(priority, uuid, actor); err != nil {
return envelope.NewError(envelope.GeneralError, c.i18n.Ts("globals.messages.errorUpdating", "name", "{globals.terms.conversation}"), nil)
}
@@ -635,6 +664,19 @@ func (c *Manager) UpdateConversationStatus(uuid string, statusID int, status, sn
return envelope.NewError(envelope.GeneralError, c.i18n.Ts("globals.messages.errorUpdating", "name", "{globals.terms.conversation}"), nil)
}
// Trigger webhook for conversation status change
var snoozeUntilStr string
if !snoozeUntil.IsZero() {
snoozeUntilStr = snoozeUntil.UTC().Format(time.RFC3339)
}
c.webhookStore.TriggerEvent(wmodels.EventConversationStatusChanged, map[string]any{
"conversation_uuid": uuid,
"previous_status": oldStatus,
"new_status": status,
"snooze_until": snoozeUntilStr,
"actor_id": actor.ID,
})
// Record the status change as an activity.
if err := c.RecordStatusChange(status, uuid, actor); err != nil {
return envelope.NewError(envelope.GeneralError, c.i18n.Ts("globals.messages.errorUpdating", "name", "{globals.terms.conversation}"), nil)
@@ -643,6 +685,14 @@ func (c *Manager) UpdateConversationStatus(uuid string, statusID int, status, sn
// Broadcast updates using websocket.
c.BroadcastConversationUpdate(uuid, "status", status)
// Evaluate automation rules.
conversation, err := c.GetConversation(0, uuid)
if err != nil {
c.lo.Error("error fetching conversation after status change", "uuid", uuid, "error", err)
} else {
c.automation.EvaluateConversationUpdateRules(conversation, amodels.EventConversationStatusChange)
}
// Broadcast `resolved_at` if the status is changed to resolved, `resolved_at` is set only once when the conversation is resolved for the first time.
// Subsequent status changes to resolved will not update the `resolved_at` field.
if oldStatus != models.StatusResolved && status == models.StatusResolved {
@@ -662,6 +712,9 @@ func (c *Manager) SetConversationTags(uuid string, action string, tagNames []str
if err != nil {
return envelope.NewError(envelope.GeneralError, c.i18n.Ts("globals.messages.errorFetching", "name", "{globals.terms.tag}"), nil)
}
if prevTags == nil {
prevTags = []string{}
}
// Add specified tags, ignore existing ones.
if action == amodels.ActionAddTags {
@@ -693,6 +746,17 @@ func (c *Manager) SetConversationTags(uuid string, action string, tagNames []str
return envelope.NewError(envelope.GeneralError, c.i18n.Ts("globals.messages.errorFetching", "name", "{globals.terms.tag}"), nil)
}
// Trigger webhook for conversation tags changed.
if newTags == nil {
newTags = []string{}
}
c.webhookStore.TriggerEvent(wmodels.EventConversationTagsChanged, map[string]any{
"conversation_uuid": uuid,
"previous_tags": prevTags,
"new_tags": newTags,
"actor_id": actor.ID,
})
// Find actually removed tags.
for _, tag := range prevTags {
if slices.Contains(newTags, tag) {
@@ -890,11 +954,20 @@ func (m *Manager) ApplyAction(action amodels.RuleAction, conv models.Conversatio
}
// RemoveConversationAssignee removes the assignee from the conversation.
func (m *Manager) RemoveConversationAssignee(uuid, typ string) error {
func (m *Manager) RemoveConversationAssignee(uuid, typ string, actor umodels.User) error {
if _, err := m.q.RemoveConversationAssignee.Exec(uuid, typ); err != nil {
m.lo.Error("error removing conversation assignee", "error", err)
return envelope.NewError(envelope.GeneralError, m.i18n.T("conversation.errorRemovingConversationAssignee"), nil)
}
// Trigger webhook for conversation unassigned from user.
if typ == models.AssigneeTypeUser {
m.webhookStore.TriggerEvent(wmodels.EventConversationUnassigned, map[string]any{
"conversation_uuid": uuid,
"actor_id": actor.ID,
})
}
return nil
}

View File

@@ -22,6 +22,7 @@ import (
"github.com/abhinavxd/libredesk/internal/sla"
"github.com/abhinavxd/libredesk/internal/stringutil"
umodels "github.com/abhinavxd/libredesk/internal/user/models"
wmodels "github.com/abhinavxd/libredesk/internal/webhook/models"
"github.com/lib/pq"
"github.com/volatiletech/null/v9"
)
@@ -63,7 +64,7 @@ func (m *Manager) Run(ctx context.Context, incomingQWorkers, outgoingQWorkers, s
)
// Get pending outgoing messages and skip the currently processing message ids.
if err := m.q.GetPendingMessages.Select(&pendingMessages, pq.Array(messageIDs)); err != nil {
if err := m.q.GetOutgoingPendingMessages.Select(&pendingMessages, pq.Array(messageIDs)); err != nil {
m.lo.Error("error fetching pending messages from db", "error", err)
continue
}
@@ -213,6 +214,9 @@ func (m *Manager) sendOutgoingMessage(message models.Message) {
} else if !metAt.IsZero() {
m.BroadcastConversationUpdate(message.ConversationUUID, "next_response_met_at", metAt.Format(time.RFC3339))
}
// Evaluate automation rules for outgoing message.
m.automation.EvaluateConversationUpdateRulesByID(message.ConversationID, "", amodels.EventConversationMessageOutgoing)
}
}
@@ -332,6 +336,14 @@ func (m *Manager) UpdateMessageStatus(messageUUID string, status string) error {
// Broadcast message status update to all conversation subscribers.
conversationUUID, _ := m.getConversationUUIDFromMessageUUID(messageUUID)
m.BroadcastMessageUpdate(conversationUUID, messageUUID, "status" /*property*/, status)
// Trigger webhook for message update.
if message, err := m.GetMessage(messageUUID); err != nil {
m.lo.Error("error fetching message for webhook event", "uuid", messageUUID, "error", err)
} else {
m.webhookStore.TriggerEvent(wmodels.EventMessageUpdated, message)
}
return nil
}
@@ -452,6 +464,15 @@ func (m *Manager) InsertMessage(message *models.Message) error {
// Broadcast new message.
m.BroadcastNewMessage(message)
// Refetch message and send webhook event for message created.
updatedMessage, err := m.GetMessage(message.UUID)
if err != nil {
m.lo.Error("error fetching updated message for webhook event", "uuid", message.UUID, "error", err)
} else {
m.webhookStore.TriggerEvent(wmodels.EventMessageCreated, updatedMessage)
}
return nil
}
@@ -604,9 +625,13 @@ func (m *Manager) processIncomingMessage(in models.IncomingMessage) error {
return err
}
// Evaluate automation rules for new conversation.
// Evaluate automation rules & send webhook events.
if isNewConversation {
m.automation.EvaluateNewConversationRules(in.Message.ConversationUUID)
conversation, err := m.GetConversation(in.Message.ConversationID, "")
if err == nil {
m.webhookStore.TriggerEvent(wmodels.EventConversationCreated, conversation)
m.automation.EvaluateNewConversationRules(conversation)
}
return nil
}
@@ -624,26 +649,27 @@ func (m *Manager) processIncomingMessage(in models.IncomingMessage) error {
now := time.Now()
m.UpdateConversationWaitingSince(in.Message.ConversationUUID, &now)
// Trigger automations on incoming message event.
m.automation.EvaluateConversationUpdateRules(in.Message.ConversationUUID, amodels.EventConversationMessageIncoming)
// Create SLA event for next response if a SLA is applied and has next response time set, subsequent agent replies will mark this event as met.
// This cycle continues for next response time SLA metric.
conversation, err := m.GetConversation(in.Message.ConversationID, "")
if err != nil {
m.lo.Error("error fetching conversation", "conversation_id", in.Message.ConversationID, "error", err)
}
if conversation.SLAPolicyID.Int == 0 {
m.lo.Info("no SLA policy applied to conversation, skipping next response SLA event creation")
return nil
}
if deadline, err := m.slaStore.CreateNextResponseSLAEvent(conversation.ID, conversation.AppliedSLAID.Int, conversation.SLAPolicyID.Int, conversation.AssignedTeamID.Int); err != nil && !errors.Is(err, sla.ErrUnmetSLAEventAlreadyExists) {
m.lo.Error("error creating next response SLA event", "conversation_id", conversation.ID, "error", err)
} else if !deadline.IsZero() {
m.lo.Info("next response SLA event created for conversation", "conversation_id", conversation.ID, "deadline", deadline, "sla_policy_id", conversation.SLAPolicyID.Int)
m.BroadcastConversationUpdate(in.Message.ConversationUUID, "next_response_deadline_at", deadline.Format(time.RFC3339))
// Clear next response met at timestamp as this event was just created.
m.BroadcastConversationUpdate(in.Message.ConversationUUID, "next_response_met_at", nil)
} else {
// Trigger automations on incoming message event.
m.automation.EvaluateConversationUpdateRules(conversation, amodels.EventConversationMessageIncoming)
if conversation.SLAPolicyID.Int == 0 {
m.lo.Info("no SLA policy applied to conversation, skipping next response SLA event creation")
return nil
}
if deadline, err := m.slaStore.CreateNextResponseSLAEvent(conversation.ID, conversation.AppliedSLAID.Int, conversation.SLAPolicyID.Int, conversation.AssignedTeamID.Int); err != nil && !errors.Is(err, sla.ErrUnmetSLAEventAlreadyExists) {
m.lo.Error("error creating next response SLA event", "conversation_id", conversation.ID, "error", err)
} else if !deadline.IsZero() {
m.lo.Info("next response SLA event created for conversation", "conversation_id", conversation.ID, "deadline", deadline, "sla_policy_id", conversation.SLAPolicyID.Int)
m.BroadcastConversationUpdate(in.Message.ConversationUUID, "next_response_deadline_at", deadline.Format(time.RFC3339))
// Clear next response met at timestamp as this event was just created.
m.BroadcastConversationUpdate(in.Message.ConversationUUID, "next_response_met_at", nil)
}
}
return nil
}

View File

@@ -92,9 +92,6 @@ type Conversation struct {
ResolutionDueAt null.Time `db:"resolution_deadline_at" json:"resolution_deadline_at"`
NextResponseDueAt null.Time `db:"next_response_deadline_at" json:"next_response_deadline_at"`
NextResponseMetAt null.Time `db:"next_response_met_at" json:"next_response_met_at"`
To json.RawMessage `db:"to" json:"to"`
BCC json.RawMessage `db:"bcc" json:"bcc"`
CC json.RawMessage `db:"cc" json:"cc"`
PreviousConversations []Conversation `db:"-" json:"previous_conversations"`
Total int `db:"total" json:"-"`
}

View File

@@ -117,6 +117,8 @@ SELECT
c.sla_policy_id,
c.meta,
sla.name as sla_policy_name,
c.last_message_at,
c.last_message_sender,
c.last_message,
c.custom_attributes,
(SELECT COALESCE(
@@ -132,10 +134,16 @@ SELECT
ct.first_name as "contact.first_name",
ct.last_name as "contact.last_name",
ct.email as "contact.email",
ct.type as "contact.type",
ct.availability_status as "contact.availability_status",
ct.avatar_url as "contact.avatar_url",
ct.phone_number as "contact.phone_number",
ct.phone_number_calling_code as "contact.phone_number_calling_code",
ct.custom_attributes as "contact.custom_attributes",
ct.avatar_url as "contact.avatar_url",
ct.enabled as "contact.enabled",
ct.last_active_at as "contact.last_active_at",
ct.last_login_at as "contact.last_login_at",
as_latest.first_response_deadline_at,
as_latest.resolution_deadline_at,
as_latest.id as applied_sla_id,
@@ -390,13 +398,14 @@ and source_id > ''
ORDER BY id DESC
LIMIT $2;
-- name: get-pending-messages
-- name: get-outgoing-pending-messages
SELECT
m.created_at,
m.id,
m.uuid,
m.sender_id,
m.type,
m.private,
m.status,
m.content,
m.conversation_id,
@@ -410,16 +419,20 @@ SELECT
c.subject
FROM conversation_messages m
INNER JOIN conversations c ON c.id = m.conversation_id
WHERE m.status = 'pending'
WHERE m.status = 'pending' AND m.type = 'outgoing' AND m.private = false
AND NOT(m.id = ANY($1::INT[]))
-- name: get-message
SELECT
m.id,
m.created_at,
m.updated_at,
m.status,
m.type,
m.content,
m.text_content,
m.content_type,
m.conversation_id,
m.uuid,
m.private,
m.sender_type,

View File

@@ -0,0 +1,86 @@
package migrations
import (
"github.com/jmoiron/sqlx"
"github.com/knadh/koanf/v2"
"github.com/knadh/stuffbin"
)
// V0_7_0 updates the database schema to v0.7.0.
func V0_7_0(db *sqlx.DB, fs stuffbin.FileSystem, ko *koanf.Koanf) error {
// Create webhook_event enum type if it doesn't exist
_, err := db.Exec(`
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM pg_type WHERE typname = 'webhook_event'
) THEN
CREATE TYPE webhook_event AS ENUM (
'conversation.created',
'conversation.status_changed',
'conversation.tags_changed',
'conversation.assigned',
'conversation.unassigned',
'message.created',
'message.updated'
);
END IF;
END
$$;
`)
if err != nil {
return err
}
// Create webhooks table if it doesn't exist
_, err = db.Exec(`
CREATE TABLE IF NOT EXISTS webhooks (
id SERIAL PRIMARY KEY,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW(),
name TEXT NOT NULL,
url TEXT NOT NULL,
events webhook_event[] NOT NULL DEFAULT '{}',
secret TEXT DEFAULT '',
is_active BOOLEAN DEFAULT true,
CONSTRAINT constraint_webhooks_on_name CHECK (length(name) <= 255),
CONSTRAINT constraint_webhooks_on_url CHECK (length(url) <= 2048),
CONSTRAINT constraint_webhooks_on_secret CHECK (length(secret) <= 255),
CONSTRAINT constraint_webhooks_on_events_not_empty CHECK (array_length(events, 1) > 0)
);
`)
if err != nil {
return err
}
// Add webhooks:manage permission to Admin role
_, err = db.Exec(`
UPDATE roles
SET permissions = array_append(permissions, 'webhooks:manage')
WHERE name = 'Admin' AND NOT ('webhooks:manage' = ANY(permissions));
`)
if err != nil {
return err
}
// Add API key authentication fields to users table
_, err = db.Exec(`
ALTER TABLE users
ADD COLUMN IF NOT EXISTS api_key TEXT NULL,
ADD COLUMN IF NOT EXISTS api_secret TEXT NULL,
ADD COLUMN IF NOT EXISTS api_key_last_used_at TIMESTAMPTZ NULL;
`)
if err != nil {
return err
}
// Create index for API key field
_, err = db.Exec(`
CREATE INDEX IF NOT EXISTS index_users_on_api_key ON users(api_key);
`)
if err != nil {
return err
}
return nil
}

View File

@@ -1,5 +1,5 @@
-- name: get-all
SELECT id, name, description FROM roles;
SELECT id, created_at, updated_at, name, description, permissions FROM roles;
-- name: get-role
SELECT * FROM roles where id = $1;

View File

@@ -23,7 +23,7 @@ WHERE id != $1 AND $4 = TRUE;
SELECT id, type, name, body, subject FROM templates WHERE is_default is TRUE;
-- name: get-all
SELECT id, type, name, is_default, updated_at FROM templates WHERE type = $1 ORDER BY updated_at DESC;
SELECT id, created_at, updated_at, type, name, is_default, is_builtin FROM templates WHERE type = $1 ORDER BY updated_at DESC;
-- name: get-template
SELECT id, type, name, body, subject, is_default, type FROM templates WHERE id = $1;

View File

@@ -21,10 +21,10 @@ const (
UserTypeContact = "contact"
// User availability statuses
Online = "online"
Offline = "offline"
Online = "online"
Offline = "offline"
// Away due to inactivity
Away = "away"
Away = "away"
// Away due to manual setting from sidebar
AwayManual = "away_manual"
AwayAndReassigning = "away_and_reassigning"
@@ -58,6 +58,11 @@ type User struct {
SourceChannel null.String `json:"-"`
SourceChannelID null.String `json:"-"`
// API Key fields
APIKey null.String `db:"api_key" json:"api_key"`
APIKeyLastUsedAt null.Time `db:"api_key_last_used_at" json:"api_key_last_used_at"`
APISecret null.String `db:"api_secret" json:"-"`
Total int `json:"total,omitempty"`
}

View File

@@ -48,6 +48,8 @@ SELECT
u.last_login_at,
u.phone_number_calling_code,
u.phone_number,
u.api_key,
u.api_key_last_used_at,
array_agg(DISTINCT r.name) FILTER (WHERE r.name IS NOT NULL) AS roles,
COALESCE(
(SELECT json_agg(json_build_object('id', t.id, 'name', t.name, 'emoji', t.emoji))
@@ -219,4 +221,52 @@ SELECT
u.avatar_url
FROM contact_notes cn
INNER JOIN users u ON u.id = cn.user_id
WHERE cn.id = $1;
WHERE cn.id = $1;
-- name: get-user-by-api-key
SELECT
u.id,
u.created_at,
u.updated_at,
u.email,
u.type,
u.enabled,
u.avatar_url,
u.first_name,
u.last_name,
u.availability_status,
u.last_active_at,
u.last_login_at,
u.phone_number_calling_code,
u.phone_number,
u.api_secret,
array_agg(DISTINCT r.name) FILTER (WHERE r.name IS NOT NULL) AS roles,
COALESCE(
(SELECT json_agg(json_build_object('id', t.id, 'name', t.name, 'emoji', t.emoji))
FROM team_members tm
JOIN teams t ON tm.team_id = t.id
WHERE tm.user_id = u.id),
'[]'
) AS teams,
array_agg(DISTINCT p ORDER BY p) FILTER (WHERE p IS NOT NULL) AS permissions
FROM users u
LEFT JOIN user_roles ur ON ur.user_id = u.id
LEFT JOIN roles r ON r.id = ur.role_id
LEFT JOIN LATERAL unnest(r.permissions) AS p ON true
WHERE u.api_key = $1 AND u.enabled = true AND u.deleted_at IS NULL
GROUP BY u.id;
-- name: generate-api-key
UPDATE users
SET api_key = $2, api_secret = $3, api_key_last_used_at = NULL, updated_at = now()
WHERE id = $1;
-- name: revoke-api-key
UPDATE users
SET api_key = NULL, api_secret = NULL, api_key_last_used_at = NULL, updated_at = now()
WHERE id = $1;
-- name: update-api-key-last-used
UPDATE users
SET api_key_last_used_at = now()
WHERE id = $1;

View File

@@ -82,6 +82,11 @@ type queries struct {
InsertContact *sqlx.Stmt `query:"insert-contact"`
InsertNote *sqlx.Stmt `query:"insert-note"`
ToggleEnable *sqlx.Stmt `query:"toggle-enable"`
// API key queries
GetUserByAPIKey *sqlx.Stmt `query:"get-user-by-api-key"`
GenerateAPIKey *sqlx.Stmt `query:"generate-api-key"`
RevokeAPIKey *sqlx.Stmt `query:"revoke-api-key"`
UpdateAPIKeyLastUsed *sqlx.Stmt `query:"update-api-key-last-used"`
}
// New creates and returns a new instance of the Manager.
@@ -297,6 +302,72 @@ func (u *Manager) ToggleEnabled(id int, typ string, enabled bool) error {
return nil
}
// GenerateAPIKey generates a new API key and secret for a user
func (u *Manager) GenerateAPIKey(userID int) (string, string, error) {
// Generate API key (32 characters)
apiKey, err := stringutil.RandomAlphanumeric(32)
if err != nil {
u.lo.Error("error generating API key", "error", err, "user_id", userID)
return "", "", envelope.NewError(envelope.GeneralError, u.i18n.Ts("globals.messages.errorGenerating", "name", "{globals.terms.apiKey}"), nil)
}
// Generate API secret (64 characters)
apiSecret, err := stringutil.RandomAlphanumeric(64)
if err != nil {
u.lo.Error("error generating API secret", "error", err, "user_id", userID)
return "", "", envelope.NewError(envelope.GeneralError, u.i18n.Ts("globals.messages.errorGenerating", "name", "{globals.terms.apiKey}"), nil)
}
// Hash the API secret for storage
secretHash, err := bcrypt.GenerateFromPassword([]byte(apiSecret), bcrypt.DefaultCost)
if err != nil {
u.lo.Error("error hashing API secret", "error", err, "user_id", userID)
return "", "", envelope.NewError(envelope.GeneralError, u.i18n.Ts("globals.messages.errorGenerating", "name", "{globals.terms.apiKey}"), nil)
}
// Update user with API key.
if _, err := u.q.GenerateAPIKey.Exec(userID, apiKey, string(secretHash)); err != nil {
u.lo.Error("error saving API key", "error", err, "user_id", userID)
return "", "", envelope.NewError(envelope.GeneralError, u.i18n.Ts("globals.messages.errorGenerating", "name", "{globals.terms.apiKey}"), nil)
}
return apiKey, apiSecret, nil
}
// ValidateAPIKey validates API key and secret and returns the user
func (u *Manager) ValidateAPIKey(apiKey, apiSecret string) (models.User, error) {
var user models.User
// Find user by API key.
if err := u.q.GetUserByAPIKey.Get(&user, apiKey); err != nil {
if err == sql.ErrNoRows {
return user, envelope.NewError(envelope.UnauthorizedError, u.i18n.Ts("globals.messages.invalid", "name", u.i18n.P("globals.terms.credential")), nil)
}
return user, envelope.NewError(envelope.GeneralError, u.i18n.Ts("globals.messages.errorFetching", "name", "{globals.terms.user}"), nil)
}
// Verify API secret.
if err := bcrypt.CompareHashAndPassword([]byte(user.APISecret.String), []byte(apiSecret)); err != nil {
return user, envelope.NewError(envelope.UnauthorizedError, u.i18n.Ts("globals.messages.invalid", "name", u.i18n.T("globals.terms.credential")), nil)
}
// Update last used timestamp.
if _, err := u.q.UpdateAPIKeyLastUsed.Exec(user.ID); err != nil {
u.lo.Error("failed to update API key last used timestamp", "error", err, "user_id", user.ID)
}
return user, nil
}
// RevokeAPIKey deactivates the API key for a user
func (u *Manager) RevokeAPIKey(userID int) error {
if _, err := u.q.RevokeAPIKey.Exec(userID); err != nil {
u.lo.Error("error revoking API key", "error", err, "user_id", userID)
return envelope.NewError(envelope.GeneralError, u.i18n.Ts("globals.messages.errorRevoking", "name", "{globals.terms.apiKey}"), nil)
}
return nil
}
// ChangeSystemUserPassword updates the system user's password with a newly prompted one.
func ChangeSystemUserPassword(ctx context.Context, db *sqlx.DB) error {
// Prompt for password and get hashed password

View File

@@ -0,0 +1,5 @@
package version
// Version holds the current version of the application.
// This value is intended to be set at build time using the -X linker flag.
var Version = "unknown-version"

View File

@@ -0,0 +1,46 @@
package models
import (
"encoding/json"
"time"
"github.com/lib/pq"
)
// Webhook represents a webhook configuration
type Webhook struct {
ID int `db:"id" json:"id"`
CreatedAt time.Time `db:"created_at" json:"created_at"`
UpdatedAt time.Time `db:"updated_at" json:"updated_at"`
Name string `db:"name" json:"name"`
URL string `db:"url" json:"url"`
Events pq.StringArray `db:"events" json:"events"`
Secret string `db:"secret" json:"secret,omitempty"`
IsActive bool `db:"is_active" json:"is_active"`
}
// WebhookEvent represents an event that can trigger a webhook
type WebhookEvent string
const (
// Conversation events
EventConversationCreated WebhookEvent = "conversation.created"
EventConversationStatusChanged WebhookEvent = "conversation.status_changed"
EventConversationTagsChanged WebhookEvent = "conversation.tags_changed"
EventConversationAssigned WebhookEvent = "conversation.assigned"
EventConversationUnassigned WebhookEvent = "conversation.unassigned"
// Message events
EventMessageCreated WebhookEvent = "message.created"
EventMessageUpdated WebhookEvent = "message.updated"
// Test event
EventWebhookTest WebhookEvent = "webhook.test"
)
// WebhookPayload represents the payload sent to a webhook
type WebhookPayload struct {
Event WebhookEvent `json:"event"`
Timestamp time.Time `json:"timestamp"`
Data json.RawMessage `json:",inline"`
}

View File

@@ -0,0 +1,95 @@
-- name: get-all-webhooks
SELECT
id,
created_at,
updated_at,
name,
url,
events,
secret,
is_active
FROM
webhooks
ORDER BY created_at DESC;
-- name: get-webhook
SELECT
id,
created_at,
updated_at,
name,
url,
events,
secret,
is_active
FROM
webhooks
WHERE
id = $1;
-- name: get-active-webhooks
SELECT
id,
created_at,
updated_at,
name,
url,
events,
secret,
is_active
FROM
webhooks
WHERE
is_active = true
ORDER BY created_at DESC;
-- name: get-webhooks-by-event
SELECT
id,
created_at,
updated_at,
name,
url,
events,
secret,
is_active
FROM
webhooks
WHERE
is_active = true AND
$1 = ANY(events);
-- name: insert-webhook
INSERT INTO
webhooks (name, url, events, secret, is_active)
VALUES
($1, $2, $3, $4, $5)
RETURNING id;
-- name: update-webhook
UPDATE
webhooks
SET
name = $2,
url = $3,
events = $4,
secret = $5,
is_active = $6,
updated_at = NOW()
WHERE
id = $1;
-- name: delete-webhook
DELETE FROM
webhooks
WHERE
id = $1;
-- name: toggle-webhook
UPDATE
webhooks
SET
is_active = NOT is_active,
updated_at = NOW()
WHERE
id = $1;

347
internal/webhook/webhook.go Normal file
View File

@@ -0,0 +1,347 @@
// Package webhook handles the management of webhooks and webhook deliveries.
package webhook
import (
"bytes"
"context"
"crypto/hmac"
"crypto/sha256"
"database/sql"
"embed"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"sync"
"time"
"github.com/abhinavxd/libredesk/internal/dbutil"
"github.com/abhinavxd/libredesk/internal/envelope"
"github.com/abhinavxd/libredesk/internal/version"
"github.com/abhinavxd/libredesk/internal/webhook/models"
"github.com/jmoiron/sqlx"
"github.com/knadh/go-i18n"
"github.com/lib/pq"
"github.com/zerodha/logf"
)
var (
//go:embed queries.sql
efs embed.FS
)
// Manager handles webhook-related operations.
type Manager struct {
q queries
lo *logf.Logger
i18n *i18n.I18n
db *sqlx.DB
deliveryQueue chan DeliveryTask
httpClient *http.Client
workers int
closed bool
closedMu sync.RWMutex
wg sync.WaitGroup
}
// Opts contains options for initializing the Manager.
type Opts struct {
DB *sqlx.DB
Lo *logf.Logger
I18n *i18n.I18n
Workers int
QueueSize int
Timeout time.Duration
}
// DeliveryTask represents a webhook delivery task
type DeliveryTask struct {
Event models.WebhookEvent
Payload any
}
// queries contains prepared SQL queries.
type queries struct {
GetAllWebhooks *sqlx.Stmt `query:"get-all-webhooks"`
GetWebhook *sqlx.Stmt `query:"get-webhook"`
GetActiveWebhooks *sqlx.Stmt `query:"get-active-webhooks"`
GetWebhooksByEvent *sqlx.Stmt `query:"get-webhooks-by-event"`
InsertWebhook *sqlx.Stmt `query:"insert-webhook"`
UpdateWebhook *sqlx.Stmt `query:"update-webhook"`
DeleteWebhook *sqlx.Stmt `query:"delete-webhook"`
ToggleWebhook *sqlx.Stmt `query:"toggle-webhook"`
}
// New creates and returns a new instance of the Manager.
func New(opts Opts) (*Manager, error) {
var q queries
if err := dbutil.ScanSQLFile("queries.sql", &q, opts.DB, efs); err != nil {
return nil, err
}
return &Manager{
q: q,
lo: opts.Lo,
i18n: opts.I18n,
db: opts.DB,
deliveryQueue: make(chan DeliveryTask, opts.QueueSize),
httpClient: &http.Client{
Timeout: 10 * time.Second,
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: 3 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
TLSHandshakeTimeout: 3 * time.Second,
ResponseHeaderTimeout: 3 * time.Second,
},
},
workers: opts.Workers,
}, nil
}
// GetAll retrieves all webhooks.
func (m *Manager) GetAll() ([]models.Webhook, error) {
var webhooks = make([]models.Webhook, 0)
if err := m.q.GetAllWebhooks.Select(&webhooks); err != nil {
m.lo.Error("error fetching webhooks", "error", err)
return nil, envelope.NewError(envelope.GeneralError, m.i18n.Ts("globals.messages.errorFetching", "name", "webhooks"), nil)
}
return webhooks, nil
}
// Get retrieves a webhook by ID.
func (m *Manager) Get(id int) (models.Webhook, error) {
var webhook models.Webhook
if err := m.q.GetWebhook.Get(&webhook, id); err != nil {
if err == sql.ErrNoRows {
return webhook, envelope.NewError(envelope.NotFoundError, m.i18n.Ts("globals.messages.notFound", "name", "webhook"), nil)
}
m.lo.Error("error fetching webhook", "error", err)
return webhook, envelope.NewError(envelope.GeneralError, m.i18n.Ts("globals.messages.errorFetching", "name", "webhook"), nil)
}
return webhook, nil
}
// Create creates a new webhook.
func (m *Manager) Create(webhook models.Webhook) (int, error) {
var id int
if err := m.q.InsertWebhook.Get(&id, webhook.Name, webhook.URL, pq.Array(webhook.Events), webhook.Secret, webhook.IsActive); err != nil {
if dbutil.IsUniqueViolationError(err) {
return 0, envelope.NewError(envelope.ConflictError, m.i18n.Ts("globals.messages.errorAlreadyExists", "name", "webhook"), nil)
}
m.lo.Error("error inserting webhook", "error", err)
return 0, envelope.NewError(envelope.GeneralError, m.i18n.Ts("globals.messages.errorCreating", "name", "webhook"), nil)
}
return id, nil
}
// Update updates a webhook by ID.
func (m *Manager) Update(id int, webhook models.Webhook) error {
if _, err := m.q.UpdateWebhook.Exec(id, webhook.Name, webhook.URL, pq.Array(webhook.Events), webhook.Secret, webhook.IsActive); err != nil {
m.lo.Error("error updating webhook", "error", err)
return envelope.NewError(envelope.GeneralError, m.i18n.Ts("globals.messages.errorUpdating", "name", "webhook"), nil)
}
return nil
}
// Delete deletes a webhook by ID.
func (m *Manager) Delete(id int) error {
if _, err := m.q.DeleteWebhook.Exec(id); err != nil {
m.lo.Error("error deleting webhook", "error", err)
return envelope.NewError(envelope.GeneralError, m.i18n.Ts("globals.messages.errorDeleting", "name", "webhook"), nil)
}
return nil
}
// Toggle toggles the active status of a webhook by ID.
func (m *Manager) Toggle(id int) error {
if _, err := m.q.ToggleWebhook.Exec(id); err != nil {
m.lo.Error("error toggling webhook", "error", err)
return envelope.NewError(envelope.GeneralError, m.i18n.Ts("globals.messages.errorUpdating", "name", "webhook"), nil)
}
return nil
}
// SendTestWebhook sends a test webhook to the specified webhook ID.
func (m *Manager) SendTestWebhook(id int) error {
webhook, err := m.Get(id)
if err != nil {
return envelope.NewError(envelope.NotFoundError, m.i18n.Ts("globals.messages.notFound", "name", "webhook"), nil)
}
m.deliverSingleWebhook(webhook, DeliveryTask{
Event: models.EventWebhookTest,
Payload: map[string]any{
"id": webhook.ID,
"name": webhook.Name,
},
})
return nil
}
// TriggerEvent triggers webhooks for a specific event with the provided data.
func (m *Manager) TriggerEvent(event models.WebhookEvent, data any) {
m.closedMu.RLock()
defer m.closedMu.RUnlock()
if m.closed {
return
}
select {
case m.deliveryQueue <- DeliveryTask{
Event: event,
Payload: data,
}:
default:
m.lo.Warn("webhook delivery queue is full, dropping webhook delivery", "event", event, "queue_size", len(m.deliveryQueue))
}
}
// Run starts the webhook delivery worker pool.
func (m *Manager) Run(ctx context.Context) {
for i := 0; i < m.workers; i++ {
m.wg.Add(1)
go func() {
defer m.wg.Done()
m.worker(ctx)
}()
}
}
// Close signals the manager to stop processing and waits for all workers to finish.
func (m *Manager) Close() {
m.closedMu.Lock()
defer m.closedMu.Unlock()
if m.closed {
return
}
m.closed = true
close(m.deliveryQueue)
m.wg.Wait()
}
// worker processes webhook delivery tasks from the queue.
func (m *Manager) worker(ctx context.Context) {
for {
select {
case <-ctx.Done():
return
case task, ok := <-m.deliveryQueue:
if !ok {
return
}
m.deliverWebhook(task)
}
}
}
// deliverWebhook delivers webhooks for an event by making HTTP requests.
func (m *Manager) deliverWebhook(task DeliveryTask) {
webhooks, err := m.getWebhooksByEvent(string(task.Event))
if err != nil {
m.lo.Error("error fetching webhooks for event", "event", task.Event, "error", err)
return
}
for _, webhook := range webhooks {
m.deliverSingleWebhook(webhook, task)
}
}
// deliverSingleWebhook delivers a webhook to a single endpoint.
func (m *Manager) deliverSingleWebhook(webhook models.Webhook, task DeliveryTask) {
basePayload := map[string]any{
"event": task.Event,
"timestamp": time.Now().UTC().Format(time.RFC3339),
"payload": task.Payload,
}
payloadBytes, err := json.Marshal(basePayload)
if err != nil {
m.lo.Error("error marshaling webhook payload", "webhook_id", webhook.ID, "event", task.Event, "error", err)
return
}
// Create HTTP request
req, err := http.NewRequest("POST", webhook.URL, bytes.NewReader(payloadBytes))
if err != nil {
m.lo.Error("error creating webhook request", "webhook_id", webhook.ID, "url", webhook.URL, "event", task.Event, "error", err)
return
}
// Set headers
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", "Libredesk-Webhook/"+version.Version)
// Add signature if secret is provided
if webhook.Secret != "" {
signature := m.generateSignature(payloadBytes, webhook.Secret)
req.Header.Set("X-Libredesk-Signature", signature)
}
m.lo.Debug("delivering webhook",
"webhook_id", webhook.ID,
"url", webhook.URL,
"event", task.Event,
"payload", string(payloadBytes),
"headers", req.Header,
)
// Make the request
resp, err := m.httpClient.Do(req)
if err != nil {
m.lo.Error("webhook delivery failed - HTTP request error",
"webhook_id", webhook.ID,
"url", webhook.URL,
"event", task.Event,
"error", err)
return
}
defer resp.Body.Close()
// Read response body
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
m.lo.Error("error reading webhook response", "webhook_id", webhook.ID, "error", err)
responseBody = []byte(fmt.Sprintf("Error reading response: %v", err))
}
// Check if delivery was successful (2xx status codes)
success := resp.StatusCode >= 200 && resp.StatusCode < 300
if success {
m.lo.Info("webhook delivered successfully",
"webhook_id", webhook.ID,
"event", task.Event,
"url", webhook.URL,
"status_code", resp.StatusCode)
} else {
m.lo.Error("webhook delivery failed",
"webhook_id", webhook.ID,
"event", task.Event,
"url", webhook.URL,
"status_code", resp.StatusCode,
"response", string(responseBody))
}
}
// generateSignature generates HMAC-SHA256 signature for webhook payload.
func (m *Manager) generateSignature(payload []byte, secret string) string {
h := hmac.New(sha256.New, []byte(secret))
h.Write(payload)
return "sha256=" + hex.EncodeToString(h.Sum(nil))
}
// getWebhooksByEvent retrieves active webhooks that are subscribed to a specific event.
func (m *Manager) getWebhooksByEvent(event string) ([]models.Webhook, error) {
var webhooks = make([]models.Webhook, 0)
if err := m.q.GetWebhooksByEvent.Select(&webhooks, event); err != nil {
return nil, err
}
return webhooks, nil
}

View File

@@ -20,6 +20,15 @@ DROP TYPE IF EXISTS "sla_metric" CASCADE; CREATE TYPE "sla_metric" AS ENUM ('fir
DROP TYPE IF EXISTS "sla_notification_type" CASCADE; CREATE TYPE "sla_notification_type" AS ENUM ('warning', 'breach');
DROP TYPE IF EXISTS "activity_log_type" CASCADE; CREATE TYPE "activity_log_type" AS ENUM ('agent_login', 'agent_logout', 'agent_away', 'agent_away_reassigned', 'agent_online');
DROP TYPE IF EXISTS "macro_visible_when" CASCADE; CREATE TYPE "macro_visible_when" AS ENUM ('replying', 'starting_conversation', 'adding_private_note');
DROP TYPE IF EXISTS "webhook_event" CASCADE; CREATE TYPE webhook_event AS ENUM (
'conversation.created',
'conversation.status_changed',
'conversation.tags_changed',
'conversation.assigned',
'conversation.unassigned',
'message.created',
'message.updated'
);
-- Sequence to generate reference number for conversations.
DROP SEQUENCE IF EXISTS conversation_reference_number_sequence; CREATE SEQUENCE conversation_reference_number_sequence START 100;
@@ -131,6 +140,10 @@ CREATE TABLE users (
availability_status user_availability_status DEFAULT 'offline' NOT NULL,
last_active_at TIMESTAMPTZ NULL,
last_login_at TIMESTAMPTZ NULL,
-- API key authentication fields
api_key TEXT NULL,
api_secret TEXT NULL,
api_key_last_used_at TIMESTAMPTZ NULL,
CONSTRAINT constraint_users_on_country CHECK (LENGTH(country) <= 140),
CONSTRAINT constraint_users_on_phone_number CHECK (LENGTH(phone_number) <= 20),
CONSTRAINT constraint_users_on_phone_number_calling_code CHECK (LENGTH(phone_number_calling_code) <= 10),
@@ -138,9 +151,10 @@ CREATE TABLE users (
CONSTRAINT constraint_users_on_first_name CHECK (LENGTH(first_name) <= 140),
CONSTRAINT constraint_users_on_last_name CHECK (LENGTH(last_name) <= 140)
);
CREATE UNIQUE INDEX index_unique_users_on_email_and_type_when_deleted_at_is_null ON users (email, type)
CREATE UNIQUE INDEX index_unique_users_on_email_and_type_when_deleted_at_is_null ON users (email, type)
WHERE deleted_at IS NULL;
CREATE INDEX index_tgrm_users_on_email ON users USING GIN (email gin_trgm_ops);
CREATE INDEX index_users_on_api_key ON users(api_key);
DROP TABLE IF EXISTS user_roles CASCADE;
CREATE TABLE user_roles (
@@ -211,8 +225,8 @@ CREATE TABLE conversations (
-- Restrict delete.
contact_channel_id INT REFERENCES contact_channels(id) ON DELETE RESTRICT ON UPDATE CASCADE NOT NULL,
status_id INT REFERENCES conversation_statuses(id) ON DELETE RESTRICT ON UPDATE CASCADE NOT NULL,
priority_id INT REFERENCES conversation_priorities(id) ON DELETE RESTRICT ON UPDATE CASCADE,
priority_id INT REFERENCES conversation_priorities(id) ON DELETE RESTRICT ON UPDATE CASCADE,
meta JSONB DEFAULT '{}'::jsonb NOT NULL,
custom_attributes JSONB DEFAULT '{}'::jsonb NOT NULL,
assignee_last_seen_at TIMESTAMPTZ DEFAULT NOW(),
@@ -406,7 +420,7 @@ CREATE TABLE conversation_tags (
tag_id INT REFERENCES tags(id) ON DELETE CASCADE ON UPDATE CASCADE,
conversation_id BIGINT REFERENCES conversations(id) ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE UNIQUE INDEX index_conversation_tags_on_conversation_id_and_tag_id ON conversation_tags (conversation_id, tag_id);
CREATE UNIQUE INDEX index_conversation_tags_on_conversation_id_and_tag_id ON conversation_tags (conversation_id, tag_id);
DROP TABLE IF EXISTS csat_responses CASCADE;
CREATE TABLE csat_responses (
@@ -570,6 +584,22 @@ CREATE INDEX IF NOT EXISTS index_activity_logs_on_actor_id ON activity_logs (act
CREATE INDEX IF NOT EXISTS index_activity_logs_on_activity_type ON activity_logs (activity_type);
CREATE INDEX IF NOT EXISTS index_activity_logs_on_created_at ON activity_logs (created_at);
DROP TABLE IF EXISTS webhooks CASCADE;
CREATE TABLE webhooks (
id SERIAL PRIMARY KEY,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW(),
name TEXT NOT NULL,
url TEXT NOT NULL,
events webhook_event[] NOT NULL DEFAULT '{}',
secret TEXT DEFAULT '',
is_active BOOLEAN DEFAULT true,
CONSTRAINT constraint_webhooks_on_name CHECK (length(name) <= 255),
CONSTRAINT constraint_webhooks_on_url CHECK (length(url) <= 2048),
CONSTRAINT constraint_webhooks_on_secret CHECK (length(secret) <= 255),
CONSTRAINT constraint_webhooks_on_events_not_empty CHECK (array_length(events, 1) > 0)
);
INSERT INTO ai_providers
("name", provider, config, is_default)
VALUES('openai', 'openai', '{"api_key": ""}'::jsonb, true);
@@ -618,7 +648,7 @@ INSERT INTO conversation_priorities (name) VALUES
-- Default conversation statuses
INSERT INTO conversation_statuses (name) VALUES
('Open'),
('Open'),
('Snoozed'),
('Resolved'),
('Closed');
@@ -639,7 +669,7 @@ VALUES
(
'Admin',
'Role for users who have complete access to everything.',
'{activity_logs:manage,custom_attributes:manage,contacts:read_all,contacts:read,contacts:write,contacts:block,contact_notes:read,contact_notes:write,contact_notes:delete,conversations:write,ai:manage,general_settings:manage,notification_settings:manage,oidc:manage,conversations:read_all,conversations:read_unassigned,conversations:read_assigned,conversations:read_team_inbox,conversations:read,conversations:update_user_assignee,conversations:update_team_assignee,conversations:update_priority,conversations:update_status,conversations:update_tags,messages:read,messages:write,view:manage,status:manage,tags:manage,macros:manage,users:manage,teams:manage,automations:manage,inboxes:manage,roles:manage,reports:manage,templates:manage,business_hours:manage,sla:manage}'
'{webhooks:manage,activity_logs:manage,custom_attributes:manage,contacts:read_all,contacts:read,contacts:write,contacts:block,contact_notes:read,contact_notes:write,contact_notes:delete,conversations:write,ai:manage,general_settings:manage,notification_settings:manage,oidc:manage,conversations:read_all,conversations:read_unassigned,conversations:read_assigned,conversations:read_team_inbox,conversations:read,conversations:update_user_assignee,conversations:update_team_assignee,conversations:update_priority,conversations:update_status,conversations:update_tags,messages:read,messages:write,view:manage,status:manage,tags:manage,macros:manage,users:manage,teams:manage,automations:manage,inboxes:manage,roles:manage,reports:manage,templates:manage,business_hours:manage,sla:manage}'
);