subscriptions

package
v0.1.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 9, 2026 License: MIT Imports: 27 Imported by: 0

Documentation

Overview

Package subscriptions - the subscriptions module: users follow a content item's comments or a term's new content; what happens there becomes their notifications, on the site (the profile's Notifications tab, the user menu) and by e-mail (at once, a daily digest or never).

Index

Constants

View Source
const (
	// KindReply - a reply to the user's comment.
	KindReply = "reply"
	// KindComment - a comment on content the user follows.
	KindComment = "comment"
	// KindContent - new content with a term the user follows.
	KindContent = "content"
)

The kinds of notifications.

View Source
const (
	MailNow   = "now"
	MailDaily = "daily"
	MailNever = "never"
)

The users' e-mail modes.

View Source
const DigestHour = 8

DigestHour - the hour of the site's day the daily digests go out at.

View Source
const MailNotifications = manifest.Name + ".notifications"

MailNotifications - the notifications e-mail (modulekit.AddMail).

Variables

View Source
var Assets embed.FS

Assets - the module's templates/ and static/, built into the binary (modules.Assets, paths.SetModuleAssets): a site runs without the module's source directory.

View Source
var MailPlaceholders = []string{"[subscriptions:subject]", "[subscriptions:list]", "[subscriptions:url]"}

MailPlaceholders - the module's placeholders of the e-mail: its subject (what it tells of, or how many things), the list of what happened and the Notifications tab's address.

Functions

func RegisterRoutes

func RegisterRoutes(r modulekit.Mux, store *sessions.CookieStore, c *Controller)

RegisterRoutes registers the module's pages: following content and terms, the tab's actions, a notification's link (a guest signs in first), the e-mails' unsubscribe link (no sign-in).

func Wire

func Wire(d modulekit.Deps) modulekit.Parts

Wire - the module's tables, the notifier on the core's events (a comment posted, content becoming public), the e-mails' background jobs, the profile's settings and Notifications tab, the e-mail (its text on the Mail page), the buttons of content and terms, the unread count on the user menu.

Types

type Controller

type Controller struct {
	*modulekit.BaseController
	// contains filtered or unexported fields
}

Controller - the module's pages: following content and terms, the Notifications tab, a notification's link, the e-mails' unsubscribe link.

func NewController

func NewController(base *modulekit.BaseController, repo *Repo, d describeDeps, nodes interface {
	ByID(ctx context.Context, nodeID int) (*modulekit.Node, error)
}, terms interface {
	TermByID(ctx context.Context, id int) (*modulekit.TaxonomyTerm, error)
}) *Controller

NewController - the module's pages on base and repo; d names the notifications' items, nodes and terms load what is followed.

func (*Controller) Follow

func (c *Controller) Follow(w http.ResponseWriter, r *http.Request)

Follow handles POST /subscriptions/follow/{kind}/{id} (kind: node or term): action "unsubscribe" stops following, else follows (public content only: the tab shows the titles of what the user follows); back to the page.

func (*Controller) NodePage

NodePage - the module's part of a content page (AddNodePage): its comments' subscription button, on public content only (the others' comments tell nobody).

func (*Controller) Open

func (c *Controller) Open(w http.ResponseWriter, r *http.Request)

Open handles GET /notifications/{id}: the notification (with the rest of its group) read, to what it tells of. A guest signs in first.

func (*Controller) PageData

func (c *Controller) PageData(r *http.Request, data map[string]any)

PageData - every page's data (AddPageData): the signed-in user's unread notifications, data["subscriptionsUnread"] (the user menu's item) and on the user menu's toggle.

func (*Controller) ProfileTab

func (c *Controller) ProfileTab() modulekit.ProfileTab

ProfileTab - the Notifications tab: the viewer's own only.

func (*Controller) ReadAll

func (c *Controller) ReadAll(w http.ResponseWriter, r *http.Request)

ReadAll handles POST /notifications/read: every notification of the user read; back to the tab.

func (*Controller) TermPage

func (c *Controller) TermPage(ctx context.Context, term modulekit.TaxonomyTerm) any

TermPage - the module's part of a term's page (AddTermPage): its follow button.

func (*Controller) Unfollow

func (c *Controller) Unfollow(w http.ResponseWriter, r *http.Request)

Unfollow handles POST /subscriptions/delete/{id}: the tab's unsubscribe button.

func (*Controller) Unsubscribe

func (c *Controller) Unsubscribe(w http.ResponseWriter, r *http.Request)

Unsubscribe handles GET and POST /subscriptions/unsubscribe/{token}: an e-mail's unsubscribe link, no sign-in needed - GET asks, POST does it (a mail client opening links mustn't unsubscribe anyone).

type FollowState

type FollowState struct {
	SignedIn  bool
	Following bool
	// Action - where the button posts; Back - where to come back.
	Action string
	Back   string
}

FollowState - the subscription button of a content item's comments or a term (comments_actions_subscriptions, term_actions_subscriptions).

type Item

type Item struct {
	Notification
	Actor     string
	Actors    []string
	Count     int
	NodeTitle string
	NodeURL   string
	TermName  string
	Excerpt   string
}

Item - a notification as shown: on the Notifications tab (where the comments of one content item are one item: Count, Actors) and in an e-mail.

type MailTemplate

type MailTemplate = modulekit.MailTemplate

MailTemplate - the notifications e-mail's subject and body: the core's placeholders ([site:…], [user:…]) and the module's (MailPlaceholders).

type Mailer

type Mailer struct {
	// contains filtered or unexported fields
}

Mailer - the notifications' e-mails: at once (a minute's events in one e-mail), the daily digest, and the cleanup of old notifications.

func NewMailer

func NewMailer(d MailerDeps) *Mailer

NewMailer - the notifications' e-mails with the repository, users, site, mail sender and visitor of d.

func (*Mailer) Cleanup

func (m *Mailer) Cleanup(ctx context.Context) error

Cleanup deletes the notifications older than keepNotifications.

func (*Mailer) Jobs

func (m *Mailer) Jobs() []modulekit.Job

Jobs - the module's background jobs.

func (*Mailer) SendDigests

func (m *Mailer) SendDigests(ctx context.Context) error

SendDigests mails the daily digests: once a day, from DigestHour of the site's day.

func (*Mailer) SendNow

func (m *Mailer) SendNow(ctx context.Context) error

SendNow mails the waiting notifications of the users mailed at once, one e-mail each; a user who turned the e-mails off has theirs dropped, one with the daily digest waits for it.

type MailerDeps

type MailerDeps struct {
	Repo     *Repo
	Describe describeDeps
	Users    interface {
		ByID(ctx context.Context, id int) (*modulekit.User, error)
	}
	Site    modulekit.SiteInformationRepository
	Mail    mailtemplate.Mailer
	BaseURL func() string
	Logger  *slog.Logger
	// Visitor - what of the content a user may not view
	// (AuthService.ContentVisitor): no e-mail tells them of such content.
	// Nil - nothing.
	Visitor func(ctx context.Context, user *modulekit.User) (modulekit.ContentVisitor, error)
}

MailerDeps - what NewMailer needs.

type Notification

type Notification struct {
	ID        int
	UserID    int
	Kind      string
	NodeID    int
	CommentID int
	ActorID   int
	TermID    int
	CreatedAt time.Time
	Read      bool
}

Notification - something told to a user.

type Notifier

type Notifier struct {
	// contains filtered or unexported fields
}

Notifier turns the core's events into notifications: a comment posted (CommentPosted) and a content item becoming public (NodePublished).

func NewNotifier

func NewNotifier(repo *Repo, nodes notifierNodes, comments notifierComments, terms notifierTerms) *Notifier

NewNotifier - the notifier writing to repo; nodes, comments and terms are the core's, read for the events.

func (*Notifier) CommentPosted

func (n *Notifier) CommentPosted(ctx context.Context, c modulekit.Comment) error

CommentPosted tells of comment c: the author of the comment it replies to (unless they turned the replies off), then the content's followers; nobody twice, nor its own author, who then follows the content (unless they turned that off or unsubscribed from it). Only of public content: the others can't see it.

func (*Notifier) NodePublished

func (n *Notifier) NodePublished(ctx context.Context, node modulekit.Node) error

NodePublished tells of content node becoming public: its author follows its comments (unless they turned that off), and the followers of its terms are told of it, each once (published again, it tells nobody twice), its author not.

type Repo

type Repo struct {
	// contains filtered or unexported fields
}

Repo - the module's tables.

func NewRepo

func NewRepo(db *sql.DB) *Repo

NewRepo - the module's tables on db.

func (*Repo) AddNotifications

func (r *Repo) AddNotifications(ctx context.Context, ns []Notification, mailed map[int]bool) error

AddNotifications stores ns; mailed - whether each user's are to be e-mailed (their mode isn't "never").

func (*Repo) AutoSubscribe

func (r *Repo) AutoSubscribe(ctx context.Context, userID int, t Target) error

AutoSubscribe makes userID follow t unless they ever subscribed to it or unsubscribed from it.

func (*Repo) DeleteOlder

func (r *Repo) DeleteOlder(ctx context.Context, t time.Time) (int, error)

DeleteOlder deletes the notifications from before t; how many.

func (*Repo) Followers

func (r *Repo) Followers(ctx context.Context, t Target) (map[int]Subscription, error)

Followers - the users following t, with their subscriptions (by user).

func (*Repo) MailDef

func (r *Repo) MailDef() modulekit.MailDef

MailDef - the notifications e-mail on the Mail page: its text kept in the module's settings (MailTemplate, SaveMailTemplate).

func (*Repo) MailTemplate

func (r *Repo) MailTemplate(ctx context.Context) (MailTemplate, error)

MailTemplate - the e-mail's template: a blank subject or body (never saved, or cleared) is the default's.

func (*Repo) MarkAllRead

func (r *Repo) MarkAllRead(ctx context.Context, userID int) error

MarkAllRead marks every notification of userID read.

func (*Repo) MarkRead

func (r *Repo) MarkRead(ctx context.Context, n Notification) error

MarkRead marks n read with the unread ones of the same content and kind (a group of comments is read at once).

func (*Repo) Notification

func (r *Repo) Notification(ctx context.Context, userID, id int) (*Notification, error)

Notification - userID's notification id; nil - none of theirs.

func (*Repo) Notifications

func (r *Repo) Notifications(ctx context.Context, userID, offset, limit int) ([]Notification, int, error)

Notifications - a page of userID's notifications, newest first, and how many they have.

func (*Repo) SaveMailTemplate

func (r *Repo) SaveMailTemplate(ctx context.Context, t MailTemplate) error

SaveMailTemplate stores the e-mail's template.

func (*Repo) SaveUserSettings

func (r *Repo) SaveUserSettings(ctx context.Context, userID int, s UserSettings) error

SaveUserSettings stores userID's settings (the row made on the first save).

func (*Repo) Subscribe

func (r *Repo) Subscribe(ctx context.Context, userID int, t Target) error

Subscribe makes userID follow t.

func (*Repo) Subscribed

func (r *Repo) Subscribed(ctx context.Context, userID int, t Target) (bool, error)

Subscribed - userID follows t (and didn't unsubscribe).

func (*Repo) SubscriptionByID

func (r *Repo) SubscriptionByID(ctx context.Context, userID, id int) (*Subscription, error)

SubscriptionByID - userID's subscription id; nil - none of theirs.

func (*Repo) SubscriptionByToken

func (r *Repo) SubscriptionByToken(ctx context.Context, token string) (*Subscription, error)

SubscriptionByToken - the subscription of an e-mail's unsubscribe link; nil - none.

func (*Repo) TermFollowers

func (r *Repo) TermFollowers(ctx context.Context, termIDs []int) (map[int]Subscription, error)

TermFollowers - the users following any of termIDs, with one of the subscriptions each (the first term's of the list they follow).

func (*Repo) ToldOfContent

func (r *Repo) ToldOfContent(ctx context.Context, nodeID int, userIDs []int) (map[int]bool, error)

ToldOfContent - those of userIDs already told of the new content nodeID (a node published again tells nobody twice).

func (*Repo) UnreadCount

func (r *Repo) UnreadCount(ctx context.Context, userID int) (int, error)

UnreadCount - userID's notifications not opened yet.

func (*Repo) Unsubscribe

func (r *Repo) Unsubscribe(ctx context.Context, userID int, t Target) error

Unsubscribe makes userID stop following t: the row stays muted, so an automatic subscription doesn't come back.

func (*Repo) UserSettings

func (r *Repo) UserSettings(ctx context.Context, userIDs []int) (map[int]UserSettings, error)

UserSettings - the settings of userIDs (the defaults for those who never saved theirs).

func (*Repo) UserSettingsSection

func (r *Repo) UserSettingsSection() modulekit.UserSettingsSection

UserSettingsSection - the user's notification settings on their profile form (template "user_settings_subscriptions"): how often the e-mails come, the replies, the automatic subscriptions. The form marks the section shown (subscriptions_user_settings): a form without it (a theme's own) leaves them as they are.

func (*Repo) UserSubscriptions

func (r *Repo) UserSubscriptions(ctx context.Context, userID int) ([]Subscription, error)

UserSubscriptions - what userID follows, newest first.

func (*Repo) WaitingMail

func (r *Repo) WaitingMail(ctx context.Context) (map[int][]Notification, error)

WaitingMail - the notifications waiting for their e-mail, by user, oldest first.

type Subscription

type Subscription struct {
	ID     int
	UserID int
	Target
	Token string
}

Subscription - a user following a Target.

type SubscriptionView

type SubscriptionView struct {
	Subscription
	Title string
	URL   string
	// Kind - the term's vocabulary, or empty for content.
	Kind string
}

SubscriptionView - a subscription as the tab lists it.

type Target

type Target struct {
	NodeID int
	TermID int
}

Target - what a subscription follows: a content item's comments (NodeID) or a term's new content (TermID).

type UserSettings

type UserSettings struct {
	Mail          string
	Replies       bool
	AutoOwn       bool
	AutoCommented bool
}

UserSettings - a user's settings of the module; the zero row's are the defaults (DefaultUserSettings).

func DefaultUserSettings

func DefaultUserSettings() UserSettings

DefaultUserSettings - the settings of a user who never saved theirs.

Directories

Path Synopsis
ent
Package ent - the subscriptions module's own ent schema (schema/), its generated client (generated/, go generate) and migration files (migrations/<dialect>/, scripts/migrations_gen.sh writes them), apart from the core's: the module's set of migrations (Schema).
Package ent - the subscriptions module's own ent schema (schema/), its generated client (generated/, go generate) and migration files (migrations/<dialect>/, scripts/migrations_gen.sh writes them), apart from the core's: the module's set of migrations (Schema).
schema
Package schema - the subscriptions module's own ent schema: its tables, apart from the core's.
Package schema - the subscriptions module's own ent schema: its tables, apart from the core's.
Package manifest declares the subscriptions module (its registry entry): subscriptions to content's comments and to terms, the notifications of them on the site and by e-mail.
Package manifest declares the subscriptions module (its registry entry): subscriptions to content's comments and to terms, the notifications of them on the site and by e-mail.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL