-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmodel.go
126 lines (110 loc) · 3.05 KB
/
model.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"bytes"
"encoding/gob"
"encoding/json"
"time"
"github.com/gofrs/uuid"
log "github.com/sirupsen/logrus"
)
// APIResponse - Envelope to communicate details to the frontent
type APIResponse struct {
Message string `json:"message"`
Success bool `json:"success"`
}
// Note represents a single note stored by the application
type Note struct {
Autosave bool `json:"autosave"`
Content string `json:"content"`
Created string `json:"created"`
Encrypted bool `json:"encrypted"`
Password string `json:"password"`
Subject string `json:"subject"`
Tags string `json:"tags"`
UID string `json:"uid"`
Updated string `json:"updated"`
// Fields largely to support the `-secondary` feature
AheadOfPrimary bool `json:"ahead_of_primary"`
Deleted bool `json:"deleted"`
Time time.Time `json:"time"`
// Private fields
CipherType string `json:"-"`
SecondaryPath string `json:"-"`
}
// Notes is a collection of Note objects
type Notes []Note
// Map of notes key'd by uid
func Map(notes Notes) map[string]Note {
var m = make(map[string]Note)
for _, note := range notes {
m[note.UID] = note
}
return m
}
// TimeSorter sorts notes lines by last updated
type TimeSorter Notes
func (a TimeSorter) Len() int { return len(a) }
func (a TimeSorter) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a TimeSorter) Less(i, j int) bool { return a[i].Updated < a[j].Updated }
// ToJSON converts a (filtered) note into json fields filtered:
// - Content: For performance reasons
// - Password: For security reasons
func (note Note) ToJSON() (string, error) {
note.Content = ""
note.Password = ""
noteJSON, err := json.MarshalIndent(note, "", " ")
if err != nil {
log.Error("Failed to parse note into json", err)
return "", err
}
return string(noteJSON), err
}
// FromBytes converts encoding.Gob bytes into a Note
func (note *Note) FromBytes(b []byte) error {
// Zero byte file represents a logical delete
if len(b) == 0 {
note.Deleted = true
return nil
}
buf := bytes.NewBuffer(b)
dec := gob.NewDecoder(buf)
return dec.Decode(¬e)
}
// ToBytes converts a raw note into encoding.Gob bytes
func (note Note) ToBytes() ([]byte, error) {
// Never include the password in a byte representation
note.Password = ""
buf := new(bytes.Buffer)
enc := gob.NewEncoder(buf)
err := enc.Encode(note)
if err != nil {
return []byte{}, err
}
return buf.Bytes(), nil
}
// Persistable prepares a note for being persisted to storage
func Persistable(note Note) (Note, error) {
note.Time = time.Now()
note.Updated = note.Time.Format(time.RFC3339)
// Generate a uuid if necessary
if note.UID == "" {
uid, err := uuid.NewV4()
if err != nil {
return note, err
}
note.UID = uid.String()
}
// Make sure the contents are encrypted if a password is set
if note.Password != "" {
ciperText, cipherType, err := Encrypt(note)
if err != nil {
return note, err
}
note.CipherType = cipherType
note.Content = ciperText
note.Encrypted = true
} else {
note.Encrypted = false
}
return note, nil
}