Add client field (#396)

* Record client

* Fix test
This commit is contained in:
Sung Won Cho 2020-01-31 08:49:12 +10:00 committed by GitHub
commit e6b5aea147
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 7 additions and 3 deletions

View file

@ -27,7 +27,7 @@ import (
// CreateNote creates a note with the next usn and updates the user's max_usn.
// It returns the created note.
func (a *App) CreateNote(user database.User, bookUUID, content string, addedOn *int64, editedOn *int64, public bool) (database.Note, error) {
func (a *App) CreateNote(user database.User, bookUUID, content string, addedOn *int64, editedOn *int64, public bool, origin string) (database.Note, error) {
tx := a.DB.Begin()
nextUSN, err := incrementUserUSN(tx, user.ID)
@ -65,6 +65,7 @@ func (a *App) CreateNote(user database.User, bookUUID, content string, addedOn *
Body: content,
Public: public,
Encrypted: false,
Client: origin,
}
if err := tx.Create(&note).Error; err != nil {
tx.Rollback()

View file

@ -90,7 +90,7 @@ func TestCreateNote(t *testing.T) {
})
tx := testutils.DB.Begin()
if _, err := a.CreateNote(user, b1.UUID, "note content", tc.addedOn, tc.editedOn, false); err != nil {
if _, err := a.CreateNote(user, b1.UUID, "note content", tc.addedOn, tc.editedOn, false, ""); err != nil {
tx.Rollback()
t.Fatal(errors.Wrap(err, "deleting note"))
}

View file

@ -60,6 +60,7 @@ type Note struct {
Deleted bool `json:"-" gorm:"default:false"`
Encrypted bool `json:"-" gorm:"default:false"`
NoteReview NoteReview `json:"-"`
Client string `gorm:"index"`
}
// User is a model for a user

View file

@ -281,6 +281,7 @@ func logging(inner http.Handler) http.HandlerFunc {
inner.ServeHTTP(&lw, r)
log.WithFields(log.Fields{
"origin": r.Header.Get("Origin"),
"remoteAddr": lookupIP(r),
"uri": r.RequestURI,
"statusCode": lw.statusCode,

View file

@ -195,7 +195,8 @@ func (a *API) CreateNote(w http.ResponseWriter, r *http.Request) {
return
}
note, err := a.App.CreateNote(user, params.BookUUID, params.Content, params.AddedOn, params.EditedOn, false)
origin := r.Header.Get("Origin")
note, err := a.App.CreateNote(user, params.BookUUID, params.Content, params.AddedOn, params.EditedOn, false, origin)
if err != nil {
HandleError(w, "creating note", err, http.StatusInternalServerError)
return