Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix logic to update recently viewed documents #179

Merged
merged 3 commits into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 40 additions & 15 deletions internal/api/documents.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,30 +375,55 @@ func updateRecentlyViewedDocs(
return fmt.Errorf("error getting user in database: %w", err)
}

// Prepend document to recently viewed documents.
rvd := append(
[]models.Document{{GoogleFileID: docID}},
u.RecentlyViewedDocs...)
// Get viewed document in database.
doc := models.Document{
GoogleFileID: docID,
}
if err := doc.Get(db); err != nil {
return fmt.Errorf("error getting viewed document: %w", err)
}

// Find recently viewed documents.
var rvd []models.RecentlyViewedDoc
if err := db.Where(&models.RecentlyViewedDoc{UserID: int(u.ID)}).
Order("viewed_at desc").
Find(&rvd).Error; err != nil {
return fmt.Errorf("error finding recently viewed docs for user: %w", err)
}

// Prepend viewed document to recently viewed documents.
rvd = append(
[]models.RecentlyViewedDoc{{
DocumentID: int(doc.ID),
UserID: int(u.ID),
}},
rvd...)

// Get document records for recently viewed docs.
docs := []models.Document{}
for _, d := range rvd {
dd := models.Document{
Model: gorm.Model{
ID: uint(d.DocumentID),
},
}
if err := dd.Get(db); err != nil {
return fmt.Errorf("error getting document: %w", err)
}
docs = append(docs, dd)
}

// Trim recently viewed documents to a length of 5.
if len(rvd) > 5 {
rvd = rvd[:5]
if len(docs) > 5 {
docs = docs[:5]
}

// Update user.
u.RecentlyViewedDocs = rvd
u.RecentlyViewedDocs = docs
if err := u.Upsert(db); err != nil {
return fmt.Errorf("error upserting user: %w", err)
}

// Get document in database to get the ID.
doc := models.Document{
GoogleFileID: docID,
}
if err := doc.Get(db); err != nil {
return fmt.Errorf("error getting document: %w", err)
}

// Update ViewedAt time for this document.
viewedDoc := models.RecentlyViewedDoc{
UserID: int(u.ID),
Expand Down
32 changes: 18 additions & 14 deletions internal/api/drafts.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,20 +567,24 @@ func DraftsDocumentHandler(
return
}

// Update recently viewed docs for the user.
if err := updateRecentlyViewedDocs(userEmail, docId, db, now); err != nil {
// If we get an error, log it but don't return an error response because
// this would degrade UX.
// TODO: change this log back to an error when this handles incomplete
// data in the database.
l.Warn("error updating recently viewed docs",
"error", err,
"path", r.URL.Path,
"method", r.Method,
"doc_id", docId,
)
return

// Update recently viewed documents if this is a document view event. The
// Add-To-Recently-Viewed header is set in the request from the frontend
// to differentiate between document views and requests to only retrieve
// document metadata.
if r.Header.Get("Add-To-Recently-Viewed") != "" {
if err := updateRecentlyViewedDocs(userEmail, docId, db, now); err != nil {
// If we get an error, log it but don't return an error response because
// this would degrade UX.
// TODO: change this log back to an error when this handles incomplete
// data in the database.
l.Warn("error updating recently viewed docs",
"error", err,
"path", r.URL.Path,
"method", r.Method,
"doc_id", docId,
)
return
}
}

l.Info("retrieved document draft", "doc_id", docId)
Expand Down
10 changes: 9 additions & 1 deletion web/app/routes/authenticated/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,16 @@ export default class DocumentRoute extends Route {
if (params.draft) {
try {
doc = await this.fetchSvc
.fetch("/api/v1/drafts/" + params.document_id)
.fetch("/api/v1/drafts/" + params.document_id, {
method: "GET",
headers: {
// We set this header to differentiate between document views and
// requests to only retrieve document metadata.
"Add-To-Recently-Viewed": "true",
},
})
.then((r) => r.json());

doc.isDraft = params.draft;
draftFetched = true;
} catch (err) {
Expand Down