Skip to content

armgit / posta

Public Imported from GitHub
0

Self-hosted email delivery platform that enables applications to send emails via HTTP APIs while handling SMTP delivery, templates, storage, security, and analytics. Fully self-hostable alternative to services like SendGrid or Mailgun.

5.8 KB
Raw Blame
// Copyright (c) 2026 Jonas Kaninda.
//
// This file is part of Posta.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: AGPL-3.0-or-later

package handlers

import (
	"github.com/goposta/posta/internal/models"
	"github.com/goposta/posta/internal/storage/repositories"
	"github.com/jkaninda/okapi"
)

type ContactListHandler struct {
	repo *repositories.ContactListRepository
}

func NewContactListHandler(repo *repositories.ContactListRepository) *ContactListHandler {
	return &ContactListHandler{repo: repo}
}

type CreateContactListRequest struct {
	Body struct {
		Name        string `json:"name" required:"true"`
		Description string `json:"description"`
	} `json:"body"`
}

type UpdateContactListRequest struct {
	ID   int `param:"id"`
	Body struct {
		Name        string `json:"name" required:"true"`
		Description string `json:"description"`
	} `json:"body"`
}

type AddMemberRequest struct {
	ID   int `param:"id"`
	Body struct {
		Email string `json:"email" required:"true" format:"email"`
		Name  string `json:"name"`
	} `json:"body"`
}

type RemoveMemberRequest struct {
	ID   int `param:"id"`
	Body struct {
		Email string `json:"email" required:"true" format:"email"`
	} `json:"body"`
}

type ListMembersRequest struct {
	ID   int `param:"id"`
	Page int `query:"page" default:"0"`
	Size int `query:"size" default:"20"`
}

type ContactListWithCount struct {
	models.ContactList
	MemberCount int64 `json:"member_count"`
}

func (h *ContactListHandler) Create(c *okapi.Context, req *CreateContactListRequest) error {
	if err := requireEdit(c); err != nil {
		return c.AbortForbidden("insufficient workspace permissions", err)
	}
	scope := getScope(c)
	list := &models.ContactList{
		UserID:      scope.UserID,
		WorkspaceID: scope.WorkspaceID,
		Name:        req.Body.Name,
		Description: req.Body.Description,
	}
	if err := h.repo.Create(list); err != nil {
		return c.AbortInternalServerError("failed to create contact list")
	}
	return created(c, list)
}

func (h *ContactListHandler) List(c *okapi.Context, req *ListRequest) error {
	page, size, offset := normalizePageParams(req.Page, req.Size)
	lists, total, err := h.repo.FindByScope(getScope(c), size, offset)
	if err != nil {
		return c.AbortInternalServerError("failed to list contact lists")
	}

	// Add member counts
	result := make([]ContactListWithCount, len(lists))
	for i, l := range lists {
		result[i] = ContactListWithCount{
			ContactList: l,
			MemberCount: h.repo.MemberCount(l.ID),
		}
	}

	return paginated(c, result, total, page, size)
}

func (h *ContactListHandler) Update(c *okapi.Context, req *UpdateContactListRequest) error {
	if err := requireEdit(c); err != nil {
		return c.AbortForbidden("insufficient workspace permissions", err)
	}
	list, err := h.repo.FindByID(uint(req.ID))
	if err != nil || !ownsResource(c, list.UserID, list.WorkspaceID) {
		return c.AbortNotFound("contact list not found")
	}
	list.Name = req.Body.Name
	list.Description = req.Body.Description
	if err := h.repo.Update(list); err != nil {
		return c.AbortInternalServerError("failed to update contact list")
	}
	return ok(c, list)
}

func (h *ContactListHandler) Delete(c *okapi.Context, req *GetByIDRequest) error {
	if err := requireEdit(c); err != nil {
		return c.AbortForbidden("insufficient workspace permissions", err)
	}
	list, err := h.repo.FindByID(uint(req.ID))
	if err != nil || !ownsResource(c, list.UserID, list.WorkspaceID) {
		return c.AbortNotFound("contact list not found")
	}
	if err := h.repo.Delete(list.ID); err != nil {
		return c.AbortInternalServerError("failed to delete contact list")
	}
	return noContent(c)
}

func (h *ContactListHandler) AddMember(c *okapi.Context, req *AddMemberRequest) error {
	if err := requireEdit(c); err != nil {
		return c.AbortForbidden("insufficient workspace permissions", err)
	}
	list, err := h.repo.FindByID(uint(req.ID))
	if err != nil || !ownsResource(c, list.UserID, list.WorkspaceID) {
		return c.AbortNotFound("contact list not found")
	}
	member := &models.ContactListMember{
		ListID: list.ID,
		Email:  req.Body.Email,
		Name:   req.Body.Name,
	}
	if err := h.repo.AddMember(member); err != nil {
		return c.AbortConflict("member already exists in list")
	}
	return created(c, member)
}

func (h *ContactListHandler) RemoveMember(c *okapi.Context, req *RemoveMemberRequest) error {
	if err := requireEdit(c); err != nil {
		return c.AbortForbidden("insufficient workspace permissions", err)
	}
	list, err := h.repo.FindByID(uint(req.ID))
	if err != nil || !ownsResource(c, list.UserID, list.WorkspaceID) {
		return c.AbortNotFound("contact list not found")
	}
	if err := h.repo.RemoveMember(list.ID, req.Body.Email); err != nil {
		return c.AbortInternalServerError("failed to remove member")
	}
	return noContent(c)
}

func (h *ContactListHandler) ListMembers(c *okapi.Context, req *ListMembersRequest) error {
	list, err := h.repo.FindByID(uint(req.ID))
	if err != nil || !ownsResource(c, list.UserID, list.WorkspaceID) {
		return c.AbortNotFound("contact list not found")
	}
	page, size, offset := normalizePageParams(req.Page, req.Size)
	members, total, err := h.repo.ListMembers(list.ID, size, offset)
	if err != nil {
		return c.AbortInternalServerError("failed to list members")
	}
	return paginated(c, members, total, page, size)
}