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.
Go email-relayemail-routinghttp-to-smtpmail-deliverymailchimp-alternativemailgun-alternativemailsendermailservermailtemplatemailtestpostmark-alternativesend-emailsendgrid-alternativesendmailtransactional-emailtransactional-outbox
// 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 repositories
import (
"github.com/goposta/posta/internal/models"
"gorm.io/gorm"
)
type ContactListRepository struct {
db *gorm.DB
}
func NewContactListRepository(db *gorm.DB) *ContactListRepository {
return &ContactListRepository{db: db}
}
func (r *ContactListRepository) Create(list *models.ContactList) error {
return r.db.Create(list).Error
}
func (r *ContactListRepository) FindByID(id uint) (*models.ContactList, error) {
var list models.ContactList
if err := r.db.First(&list, id).Error; err != nil {
return nil, err
}
return &list, nil
}
func (r *ContactListRepository) FindByUserID(userID uint, limit, offset int) ([]models.ContactList, int64, error) {
var lists []models.ContactList
var total int64
r.db.Model(&models.ContactList{}).Where("user_id = ? AND workspace_id IS NULL", userID).Count(&total)
err := r.db.Where("user_id = ? AND workspace_id IS NULL", userID).Order("created_at DESC").Limit(limit).Offset(offset).Find(&lists).Error
return lists, total, err
}
func (r *ContactListRepository) FindByWorkspaceID(workspaceID uint, limit, offset int) ([]models.ContactList, int64, error) {
var lists []models.ContactList
var total int64
r.db.Model(&models.ContactList{}).Where("workspace_id = ?", workspaceID).Count(&total)
err := r.db.Where("workspace_id = ?", workspaceID).Order("created_at DESC").Limit(limit).Offset(offset).Find(&lists).Error
return lists, total, err
}
func (r *ContactListRepository) FindByScope(scope ResourceScope, limit, offset int) ([]models.ContactList, int64, error) {
var items []models.ContactList
var total int64
ApplyScope(r.db.Model(&models.ContactList{}), scope).Count(&total)
if err := ApplyScope(r.db, scope).
Order("created_at DESC").
Limit(limit).Offset(offset).
Find(&items).Error; err != nil {
return nil, 0, err
}
return items, total, nil
}
func (r *ContactListRepository) Update(list *models.ContactList) error {
return r.db.Save(list).Error
}
func (r *ContactListRepository) Delete(id uint) error {
return r.db.Delete(&models.ContactList{}, id).Error
}
func (r *ContactListRepository) AddMember(member *models.ContactListMember) error {
return r.db.Create(member).Error
}
func (r *ContactListRepository) RemoveMember(listID uint, email string) error {
return r.db.Where("list_id = ? AND email = ?", listID, email).Delete(&models.ContactListMember{}).Error
}
func (r *ContactListRepository) ListMembers(listID uint, limit, offset int) ([]models.ContactListMember, int64, error) {
var members []models.ContactListMember
var total int64
r.db.Model(&models.ContactListMember{}).Where("list_id = ?", listID).Count(&total)
err := r.db.Where("list_id = ?", listID).Order("created_at DESC").Limit(limit).Offset(offset).Find(&members).Error
return members, total, err
}
func (r *ContactListRepository) GetMemberEmails(listID uint) ([]string, error) {
var emails []string
err := r.db.Model(&models.ContactListMember{}).Where("list_id = ?", listID).Pluck("email", &emails).Error
return emails, err
}
// MemberCount returns the number of members in a list.
func (r *ContactListRepository) MemberCount(listID uint) int64 {
var count int64
r.db.Model(&models.ContactListMember{}).Where("list_id = ?", listID).Count(&count)
return count
}