all repos

onasty @ 0afb97f

a one-time notes service

onasty/internal/service/notesrv/notesrv.go (view raw)

Smirnov Oleksandr Smirnov Oleksandr
ss2316544@gmail.com
feat: add password support to notes (#41)..., 1 year ago
1
package notesrv
2
3
import (
4
	"context"
5
	"log/slog"
6
7
	"github.com/gofrs/uuid/v5"
8
	"github.com/olexsmir/onasty/internal/dtos"
9
	"github.com/olexsmir/onasty/internal/hasher"
10
	"github.com/olexsmir/onasty/internal/models"
11
	"github.com/olexsmir/onasty/internal/store/psql/noterepo"
12
)
13
14
type NoteServicer interface {
15
	// Create creates note
16
	// if slug is empty it will be generated, otherwise used as is
17
	// if userID is empty it means user isn't authorized so it will be used
18
	Create(ctx context.Context, note dtos.CreateNoteDTO, userID uuid.UUID) (dtos.NoteSlugDTO, error)
19
20
	// GetBySlugAndRemoveIfNeeded returns note by slug, and removes if if needed
21
	GetBySlugAndRemoveIfNeeded(ctx context.Context, input GetNoteBySlugInput) (dtos.NoteDTO, error)
22
}
23
24
var _ NoteServicer = (*NoteSrv)(nil)
25
26
type NoteSrv struct {
27
	noterepo noterepo.NoteStorer
28
	hasher   hasher.Hasher
29
}
30
31
func New(noterepo noterepo.NoteStorer, hasher hasher.Hasher) *NoteSrv {
32
	return &NoteSrv{
33
		noterepo: noterepo,
34
		hasher:   hasher,
35
	}
36
}
37
38
func (n *NoteSrv) Create(
39
	ctx context.Context,
40
	inp dtos.CreateNoteDTO,
41
	userID uuid.UUID,
42
) (dtos.NoteSlugDTO, error) {
43
	slog.DebugContext(ctx, "creating", "inp", inp)
44
45
	if inp.Slug == "" {
46
		inp.Slug = uuid.Must(uuid.NewV4()).String()
47
	}
48
49
	if inp.Password != "" {
50
		hashedPassword, err := n.hasher.Hash(inp.Password)
51
		if err != nil {
52
			return "", err
53
		}
54
		inp.Password = hashedPassword
55
	}
56
57
	if err := n.noterepo.Create(ctx, inp); err != nil {
58
		return "", err
59
	}
60
61
	if !userID.IsNil() {
62
		if err := n.noterepo.SetAuthorIDBySlug(ctx, inp.Slug, userID); err != nil {
63
			return "", err
64
		}
65
	}
66
67
	return inp.Slug, nil
68
}
69
70
func (n *NoteSrv) GetBySlugAndRemoveIfNeeded(
71
	ctx context.Context,
72
	inp GetNoteBySlugInput,
73
) (dtos.NoteDTO, error) {
74
	note, err := n.getNoteFromDBasedOnInput(ctx, inp)
75
	if err != nil {
76
		return dtos.NoteDTO{}, err
77
	}
78
79
	m := models.Note{ //nolint:exhaustruct
80
		ExpiresAt:            note.ExpiresAt,
81
		BurnBeforeExpiration: note.BurnBeforeExpiration,
82
	}
83
84
	if m.IsExpired() {
85
		return dtos.NoteDTO{}, models.ErrNoteExpired
86
	}
87
88
	// since not every note should be burn before expiration
89
	// we return early if it's not
90
	if m.ShouldBeBurnt() {
91
		return note, nil
92
	}
93
94
	// TODO: in future not remove, leave some metadata
95
	// to shot user that note was already seen
96
	return note, n.noterepo.DeleteBySlug(ctx, note.Slug)
97
}
98
99
func (n *NoteSrv) getNoteFromDBasedOnInput(
100
	ctx context.Context,
101
	inp GetNoteBySlugInput,
102
) (dtos.NoteDTO, error) {
103
	if inp.HasPassword() {
104
		hashedPassword, err := n.hasher.Hash(inp.Password)
105
		if err != nil {
106
			return dtos.NoteDTO{}, err
107
		}
108
109
		return n.noterepo.GetBySlugAndPassword(ctx, inp.Slug, hashedPassword)
110
	}
111
	return n.noterepo.GetBySlug(ctx, inp.Slug)
112
}