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 owner photo bug for v2 drafts API #413

Merged
merged 3 commits into from
Nov 9, 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
25 changes: 20 additions & 5 deletions internal/api/v2/documents.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,20 @@ func DocumentHandler(srv server.Server) http.Handler {
return
}

// If the document was created through Hermes and has a status of "WIP", it
// is a document draft and should be instead accessed through the drafts
// API. We return a 404 to be consistent with v1 of the API, and will
// improve this UX in the future when these APIs are combined.
if doc.AppCreated && doc.Status == "WIP" {
srv.Logger.Warn("attempted to access document draft via documents API",
"method", r.Method,
"path", r.URL.Path,
"doc_id", docID,
)
http.Error(w, "Document not found", http.StatusNotFound)
return
}

// Pass request off to associated subcollection (part of the URL after the
// document ID) handler, if appropriate.
switch reqType {
Expand Down Expand Up @@ -151,18 +165,19 @@ func DocumentHandler(srv server.Server) http.Handler {

// Get owner photo by searching Google Workspace directory.
if len(doc.Owners) > 0 {
people, err := srv.GWService.SearchPeople(doc.Owners[0], "photos")
ppl, err := srv.GWService.SearchPeople(doc.Owners[0], "photos")
if err != nil {
srv.Logger.Error("error searching directory for person",
srv.Logger.Error("error searching directory for owner",
"error", err,
"method", r.Method,
"path", r.URL.Path,
"doc_id", docID,
"person", doc.Owners[0],
)
}
if len(people) > 0 {
if len(people[0].Photos) > 0 {
doc.OwnerPhotos = []string{people[0].Photos[0].Url}
if len(ppl) > 0 {
if len(ppl[0].Photos) > 0 {
doc.OwnerPhotos = []string{ppl[0].Photos[0].Url}
}
}
}
Expand Down
28 changes: 16 additions & 12 deletions internal/api/v2/drafts.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,18 +655,22 @@ func DraftsDocumentHandler(srv server.Server) http.Handler {
doc.ModifiedTime = modifiedTime.Unix()

// Get owner photo by searching Google Workspace directory.
ppl, err := srv.GWService.SearchPeople(userEmail, "photos")
if err != nil {
srv.Logger.Error(
"error searching directory for owner",
"error", err,
"path", r.URL.Path,
"method", r.Method,
"doc_id", docID,
)
}
if len(ppl) > 0 && len(ppl[0].Photos) > 0 {
doc.OwnerPhotos = []string{ppl[0].Photos[0].Url}
if len(doc.Owners) > 0 {
ppl, err := srv.GWService.SearchPeople(doc.Owners[0], "photos")
if err != nil {
srv.Logger.Error("error searching directory for owner",
"error", err,
"method", r.Method,
"path", r.URL.Path,
"doc_id", docID,
"person", doc.Owners[0],
)
}
if len(ppl) > 0 {
if len(ppl[0].Photos) > 0 {
doc.OwnerPhotos = []string{ppl[0].Photos[0].Url}
}
}
}

// Convert document to Algolia object because this is how it is expected
Expand Down