some more commits?

This commit is contained in:
Abhinav Raut
2024-07-01 03:08:45 +05:30
parent 6d027c92ab
commit d7fb9be211
64 changed files with 1546 additions and 1013 deletions
+3 -3
View File
@@ -38,7 +38,7 @@ func handleAttachmentUpload(r *fastglue.Request) error {
file, err := form.File["files"][0].Open()
srcFileName := form.File["files"][0].Filename
srcContentType := form.File["files"][0].Header.Get("Content-Type")
srcFileSize := form.File["files"][0].Size
srcFileSize := strconv.FormatInt(form.File["files"][0].Size, 10)
srcDisposition := form.Value["disposition"][0]
if err != nil {
app.lo.Error("reading file into the memory", "error", err)
@@ -57,7 +57,7 @@ func handleAttachmentUpload(r *fastglue.Request) error {
// Reset the ptr.
file.Seek(0, 0)
url, mediaUUID, _, err := app.attachmentMgr.Upload("" /**message uuid**/, srcFileName, srcContentType, srcDisposition, strconv.FormatInt(srcFileSize, 10), file)
url, mediaUUID, _, err := app.attachmentMgr.Upload("" /**message uuid**/, srcFileName, srcContentType, srcDisposition, srcFileSize, file)
if err != nil {
app.lo.Error("error uploading file", "error", err)
return r.SendErrorEnvelope(http.StatusInternalServerError, "Error uploading file", nil, "GeneralException")
@@ -68,7 +68,7 @@ func handleAttachmentUpload(r *fastglue.Request) error {
"uuid": mediaUUID,
"content_type": srcContentType,
"name": srcFileName,
"size": strconv.FormatInt(srcFileSize, 10),
"size": srcFileSize,
})
}
+11 -11
View File
@@ -125,16 +125,16 @@ func handleUpdateAssignee(r *fastglue.Request) error {
userUUID = r.RequestCtx.UserValue("user_uuid").(string)
)
if err := app.conversationMgr.UpdateAssignee(convUUID, assigneeUUID, assigneeType); err != nil {
return r.SendErrorEnvelope(http.StatusInternalServerError, err.Error(), nil, "")
}
if assigneeType == "agent" {
app.msgMgr.RecordAssigneeUserChange(string(assigneeUUID), convUUID, userUUID)
}
if assigneeType == "team" {
app.msgMgr.RecordAssigneeTeamChange(string(assigneeUUID), convUUID, userUUID)
if assigneeType == "user" {
if err := app.conversationMgr.UpdateUserAssignee(convUUID, assigneeUUID); err != nil {
return r.SendErrorEnvelope(http.StatusInternalServerError, err.Error(), nil, "")
}
app.msgMgr.RecordAssigneeUserChange(convUUID, string(assigneeUUID), userUUID)
} else if assigneeType == "team" {
if err := app.conversationMgr.UpdateTeamAssignee(convUUID, assigneeUUID); err != nil {
return r.SendErrorEnvelope(http.StatusInternalServerError, err.Error(), nil, "")
}
app.msgMgr.RecordAssigneeTeamChange(convUUID, string(assigneeUUID), userUUID)
}
return r.SendEnvelope("ok")
@@ -188,7 +188,7 @@ func handlAddConversationTags(r *fastglue.Request) error {
return r.SendErrorEnvelope(http.StatusInternalServerError, "error adding tags", nil, "")
}
if err := app.conversationTagsMgr.AddTags(uuid, tagIDs); err != nil {
if err := app.conversationMgr.AddTags(uuid, tagIDs); err != nil {
return r.SendErrorEnvelope(http.StatusInternalServerError, err.Error(), nil, "")
}
return r.SendEnvelope("ok")
-1
View File
@@ -28,7 +28,6 @@ func initHandlers(g *fastglue.Fastglue, hub *ws.Hub) {
g.GET("/api/canned-responses", auth(handleGetCannedResponses))
g.GET("/api/attachment/{conversation_uuid}", auth(handleGetAttachment))
g.GET("/api/users/me", auth(handleGetCurrentUser))
g.GET("/api/users/filters/{page}", auth(handleGetUserFilters))
g.GET("/api/users", auth(handleGetUsers))
g.GET("/api/teams", auth(handleGetTeams))
g.GET("/api/tags", auth(handleGetTags))
+36 -27
View File
@@ -8,21 +8,22 @@ import (
"github.com/abhinavxd/artemis/internal/attachment"
"github.com/abhinavxd/artemis/internal/attachment/stores/s3"
uauth "github.com/abhinavxd/artemis/internal/auth"
"github.com/abhinavxd/artemis/internal/autoassigner"
"github.com/abhinavxd/artemis/internal/automation"
"github.com/abhinavxd/artemis/internal/cannedresp"
"github.com/abhinavxd/artemis/internal/contact"
"github.com/abhinavxd/artemis/internal/conversation"
convtag "github.com/abhinavxd/artemis/internal/conversation/tag"
"github.com/abhinavxd/artemis/internal/inbox"
"github.com/abhinavxd/artemis/internal/inbox/channel/email"
"github.com/abhinavxd/artemis/internal/initz"
"github.com/abhinavxd/artemis/internal/message"
"github.com/abhinavxd/artemis/internal/rbac"
notifier "github.com/abhinavxd/artemis/internal/notification"
emailnotifier "github.com/abhinavxd/artemis/internal/notification/providers/email"
"github.com/abhinavxd/artemis/internal/tag"
"github.com/abhinavxd/artemis/internal/team"
"github.com/abhinavxd/artemis/internal/template"
"github.com/abhinavxd/artemis/internal/user"
"github.com/abhinavxd/artemis/internal/user/filterstore"
"github.com/abhinavxd/artemis/internal/ws"
"github.com/go-redis/redis/v8"
"github.com/jmoiron/sqlx"
@@ -112,8 +113,8 @@ func initUserDB(DB *sqlx.DB, lo *logf.Logger) *user.Manager {
return mgr
}
func initConversations(db *sqlx.DB, lo *logf.Logger) *conversation.Manager {
c, err := conversation.New(conversation.Opts{
func initConversations(hub *ws.Hub, db *sqlx.DB, lo *logf.Logger) *conversation.Manager {
c, err := conversation.New(hub, conversation.Opts{
DB: db,
Lo: lo,
ReferenceNumPattern: ko.String("app.constants.conversation_reference_number_pattern"),
@@ -124,17 +125,6 @@ func initConversations(db *sqlx.DB, lo *logf.Logger) *conversation.Manager {
return c
}
func initConversationTags(db *sqlx.DB, lo *logf.Logger) *convtag.Manager {
mgr, err := convtag.New(convtag.Opts{
DB: db,
Lo: lo,
})
if err != nil {
log.Fatalf("error initializing conversation tags: %v", err)
}
return mgr
}
func initTags(db *sqlx.DB, lo *logf.Logger) *tag.Manager {
mgr, err := tag.New(tag.Opts{
DB: db,
@@ -168,6 +158,14 @@ func initContactManager(db *sqlx.DB, lo *logf.Logger) *contact.Manager {
return m
}
func initTemplateMgr(db *sqlx.DB) *template.Manager {
m, err := template.New(db)
if err != nil {
log.Fatalf("error initializing template manager: %v", err)
}
return m
}
func initMessages(db *sqlx.DB,
lo *logf.Logger,
wsHub *ws.Hub,
@@ -177,7 +175,9 @@ func initMessages(db *sqlx.DB,
attachmentMgr *attachment.Manager,
conversationMgr *conversation.Manager,
inboxMgr *inbox.Manager,
automationEngine *automation.Engine) *message.Manager {
automationEngine *automation.Engine,
templateManager *template.Manager,
) *message.Manager {
mgr, err := message.New(
wsHub,
userMgr,
@@ -187,6 +187,7 @@ func initMessages(db *sqlx.DB,
inboxMgr,
conversationMgr,
automationEngine,
templateManager,
message.Opts{
DB: db,
Lo: lo,
@@ -268,28 +269,36 @@ func initAutomationEngine(db *sqlx.DB, lo *logf.Logger) *automation.Engine {
return engine
}
func initAutoAssignmentEngine(teamMgr *team.Manager, convMgr *conversation.Manager, msgMgr *message.Manager, lo *logf.Logger) *autoassigner.Engine {
engine, err := autoassigner.New(teamMgr, convMgr, msgMgr, lo)
func initAutoAssignmentEngine(teamMgr *team.Manager, convMgr *conversation.Manager, msgMgr *message.Manager,
notifier notifier.Notifier, hub *ws.Hub, lo *logf.Logger) *autoassigner.Engine {
engine, err := autoassigner.New(teamMgr, convMgr, msgMgr, notifier, hub, lo)
if err != nil {
log.Fatalf("error initializing auto assignment engine: %v", err)
}
return engine
}
func initRBACEngine(db *sqlx.DB) *rbac.Engine {
engine, err := rbac.New(db)
func initRBACEngine(db *sqlx.DB) *uauth.Engine {
engine, err := uauth.New(db, &logf.Logger{})
if err != nil {
log.Fatalf("error initializing rbac enginer: %v", err)
}
return engine
}
func initUserFilterMgr(db *sqlx.DB) *filterstore.Manager {
filterMgr, err := filterstore.New(db)
if err != nil {
log.Fatalf("error initializing user filter manager: %v", err)
func initNotifier(userStore notifier.UserStore, templateRenderer notifier.TemplateRenderer) notifier.Notifier {
var smtpCfg email.SMTPConfig
if err := ko.UnmarshalWithConf("notification.provider.email", &smtpCfg, koanf.UnmarshalConf{Tag: "json"}); err != nil {
log.Fatalf("error unmarshalling email notification provider config: %v", err)
}
return filterMgr
notifier, err := emailnotifier.New([]email.SMTPConfig{smtpCfg}, userStore, templateRenderer, emailnotifier.Opts{
Lo: initz.Logger(ko.MustString("app.log_level"), ko.MustString("app.env"), "email-notifier"),
FromEmail: ko.String("notification.provider.email.email_address"),
})
if err != nil {
log.Fatalf("error initializing email notifier: %v", err)
}
return notifier
}
// initEmailInbox initializes the email inbox.
@@ -327,7 +336,7 @@ func initEmailInbox(inboxRecord inbox.InboxRecord, store inbox.MessageStore) (in
return nil, err
}
log.Printf("`%s` inbox: `%s` successfully initalized. %d smtp servers. %d imap clients.", inboxRecord.Channel, inboxRecord.Name, len(config.SMTP), len(config.IMAP))
log.Printf("`%s` inbox successfully initalized. %d smtp servers. %d imap clients.", inboxRecord.Name, len(config.SMTP), len(config.IMAP))
return inbox, nil
}
+65 -60
View File
@@ -9,18 +9,16 @@ import (
"time"
"github.com/abhinavxd/artemis/internal/attachment"
uauth "github.com/abhinavxd/artemis/internal/auth"
"github.com/abhinavxd/artemis/internal/cannedresp"
"github.com/abhinavxd/artemis/internal/contact"
"github.com/abhinavxd/artemis/internal/conversation"
convtag "github.com/abhinavxd/artemis/internal/conversation/tag"
"github.com/abhinavxd/artemis/internal/inbox"
"github.com/abhinavxd/artemis/internal/initz"
"github.com/abhinavxd/artemis/internal/message"
"github.com/abhinavxd/artemis/internal/rbac"
"github.com/abhinavxd/artemis/internal/tag"
"github.com/abhinavxd/artemis/internal/team"
"github.com/abhinavxd/artemis/internal/user"
"github.com/abhinavxd/artemis/internal/user/filterstore"
"github.com/abhinavxd/artemis/internal/ws"
"github.com/knadh/koanf/v2"
"github.com/valyala/fasthttp"
@@ -29,25 +27,31 @@ import (
"github.com/zerodha/logf"
)
var ko = koanf.New(".")
var (
ko = koanf.New(".")
)
const (
// ANSI escape colour codes.
colourRed = "\x1b[31m"
colourGreen = "\x1b[32m"
)
// App is the global app context which is passed and injected in the http handlers.
type App struct {
constants consts
lo *logf.Logger
cntctMgr *contact.Manager
userMgr *user.Manager
teamMgr *team.Manager
sessMgr *simplesessions.Manager
tagMgr *tag.Manager
msgMgr *message.Manager
rbac *rbac.Engine
userFilterMgr *filterstore.Manager
inboxMgr *inbox.Manager
attachmentMgr *attachment.Manager
cannedRespMgr *cannedresp.Manager
conversationMgr *conversation.Manager
conversationTagsMgr *convtag.Manager
constants consts
lo *logf.Logger
cntctMgr *contact.Manager
userMgr *user.Manager
teamMgr *team.Manager
sessMgr *simplesessions.Manager
tagMgr *tag.Manager
msgMgr *message.Manager
rbac *uauth.Engine
inboxMgr *inbox.Manager
attachmentMgr *attachment.Manager
cannedRespMgr *cannedresp.Manager
conversationMgr *conversation.Manager
}
func main() {
@@ -58,62 +62,65 @@ func main() {
initz.Config(ko)
var (
shutdownCh = make(chan struct{})
ctx, stop = signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
lo = initz.Logger(ko.MustString("app.log_level"), ko.MustString("app.env"), "artemis")
rd = initz.Redis(ko)
db = initz.DB(ko)
shutdownCh = make(chan struct{})
ctx, stop = signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
lo = initz.Logger(ko.MustString("app.log_level"), ko.MustString("app.env"), "artemis")
rd = initz.Redis(ko)
db = initz.DB(ko)
wsHub = ws.NewHub()
templateMgr = initTemplateMgr(db)
attachmentMgr = initAttachmentsManager(db, lo)
cntctMgr = initContactManager(db, lo)
inboxMgr = initInboxManager(db, lo)
teamMgr = initTeamMgr(db, lo)
userMgr = initUserDB(db, lo)
conversationMgr = initConversations(db, lo)
notifier = initNotifier(userMgr, templateMgr)
conversationMgr = initConversations(wsHub, db, lo)
automationEngine = initAutomationEngine(db, lo)
msgMgr = initMessages(db, lo, wsHub, userMgr, teamMgr, cntctMgr, attachmentMgr, conversationMgr, inboxMgr, automationEngine)
autoAssignerEngine = initAutoAssignmentEngine(teamMgr, conversationMgr, msgMgr, lo)
msgMgr = initMessages(db, lo, wsHub, userMgr, teamMgr, cntctMgr, attachmentMgr, conversationMgr, inboxMgr, automationEngine, templateMgr)
autoAssignerEngine = initAutoAssignmentEngine(teamMgr, conversationMgr, msgMgr, notifier, wsHub, lo)
)
// Init the app
var app = &App{
lo: lo,
cntctMgr: cntctMgr,
inboxMgr: inboxMgr,
userMgr: userMgr,
teamMgr: teamMgr,
attachmentMgr: attachmentMgr,
conversationMgr: conversationMgr,
msgMgr: msgMgr,
constants: initConstants(),
rbac: initRBACEngine(db),
tagMgr: initTags(db, lo),
userFilterMgr: initUserFilterMgr(db),
sessMgr: initSessionManager(rd),
cannedRespMgr: initCannedResponse(db, lo),
conversationTagsMgr: initConversationTags(db, lo),
}
// Register all inboxes with the inbox manager.
registerInboxes(inboxMgr, msgMgr)
automationEngine.SetMsgRecorder(msgMgr)
automationEngine.SetConvUpdater(conversationMgr)
// Set conversation store for the websocket hub.
wsHub.SetConversationStore(conversationMgr)
// Set stores for the automation engine.
automationEngine.SetMessageStore(msgMgr)
automationEngine.SetConversationStore(conversationMgr)
// Start receivers for all active inboxes.
inboxMgr.Receive(ctx)
// Start inserting incoming msgs and dispatch pending outgoing messages.
go app.msgMgr.StartDBInserts(ctx, ko.MustInt("message.reader_concurrency"))
go app.msgMgr.StartDispatcher(ctx, ko.MustInt("message.dispatch_concurrency"), ko.MustDuration("message.dispatch_read_interval"))
// Start automation rule engine.
// Start automation rule evaluation engine.
go automationEngine.Serve(ctx)
// Start conversation auto assigner engine.
go autoAssignerEngine.Serve(ctx, ko.MustDuration("autoassigner.assign_interval"))
// Start inserting incoming messages from all active inboxes and dispatch pending outgoing messages.
go msgMgr.StartDBInserts(ctx, ko.MustInt("message.reader_concurrency"))
go msgMgr.StartDispatcher(ctx, ko.MustInt("message.dispatch_concurrency"), ko.MustDuration("message.dispatch_read_interval"))
// Init the app
var app = &App{
lo: lo,
cntctMgr: cntctMgr,
inboxMgr: inboxMgr,
userMgr: userMgr,
teamMgr: teamMgr,
attachmentMgr: attachmentMgr,
conversationMgr: conversationMgr,
msgMgr: msgMgr,
constants: initConstants(),
rbac: initRBACEngine(db),
tagMgr: initTags(db, lo),
sessMgr: initSessionManager(rd),
cannedRespMgr: initCannedResponse(db, lo),
}
// Init fastglue http server.
g := fastglue.NewGlue()
@@ -137,20 +144,18 @@ func main() {
// Wait for the interruption signal
<-ctx.Done()
log.Printf("\x1b[%dm%s\x1b[0m", 31, "Shutting down the server please wait...")
log.Printf("%sShutting down the server. Please wait.\x1b[0m", colourRed)
// Additional grace period before triggering shutdown
time.Sleep(7 * time.Second)
time.Sleep(5 * time.Second)
// Signal to shutdown the server
shutdownCh <- struct{}{}
stop()
}()
// Starting the server and waiting for the shutdown signal
log.Printf("🚀 server listening on %s %s", ko.String("app.server.address"), ko.String("app.server.socket"))
log.Printf("%s🚀 server listening on %s %s\x1b[0m", colourGreen, ko.String("app.server.address"), ko.String("app.server.socket"))
if err := g.ListenServeAndWaitGracefully(ko.String("app.server.address"), ko.String("server.socket"), s, shutdownCh); err != nil {
log.Fatalf("error starting frontend server: %v", err)
}
log.Println("Server shutdown completed")
}
+2 -6
View File
@@ -3,7 +3,6 @@ package main
import (
"encoding/json"
"net/http"
"time"
"github.com/abhinavxd/artemis/internal/attachment/models"
"github.com/abhinavxd/artemis/internal/message"
@@ -108,13 +107,10 @@ func handleSendMessage(r *fastglue.Request) error {
// Update conversation meta with the last message details.
trimmedMessage := app.msgMgr.TrimMsg(msg.Content)
app.conversationMgr.UpdateMeta(0, conversationUUID, map[string]string{
"last_message": trimmedMessage,
"last_message_at": msg.CreatedAt.Format(time.RFC3339),
})
app.conversationMgr.UpdateLastMessage(0, conversationUUID, trimmedMessage, msg.CreatedAt)
// Send WS update.
app.msgMgr.BroadcastNewMsg(msg, trimmedMessage)
app.msgMgr.BroadcastNewConversationMessage(msg, trimmedMessage)
return r.SendEnvelope("Message sent")
}
-13
View File
@@ -28,16 +28,3 @@ func handleGetCurrentUser(r *fastglue.Request) error {
}
return r.SendEnvelope(u)
}
func handleGetUserFilters(r *fastglue.Request) error {
var (
app = r.Context.(*App)
userID = r.RequestCtx.UserValue("user_id").(int)
page = r.RequestCtx.UserValue("page").(string)
)
filters, err := app.userFilterMgr.GetFilters(userID, page)
if err != nil {
return r.SendErrorEnvelope(http.StatusInternalServerError, err.Error(), nil, "")
}
return r.SendEnvelope(filters)
}
+1 -15
View File
@@ -34,23 +34,9 @@ func handleWS(r *fastglue.Request, hub *ws.Hub) error {
ID: userID,
Hub: hub,
Conn: conn,
Send: make(chan ws.Message, 100000),
Send: make(chan ws.Message, 10000),
}
// Sub this client to all assigned conversations.
convs, err := app.conversationMgr.GetAssignedConversations(userID)
if err != nil {
return
}
// Extract uuids.
uuids := make([]string, len(convs))
for i, conv := range convs {
uuids[i] = conv.UUID
}
c.SubConv(userID, uuids...)
hub.AddClient(&c)
go c.Listen()
c.Serve(2 * time.Second)
})