Returns the body of a revision given a document struct. Checks user access. If the user is not authorized to see the specific revision they asked for, instead returns a minimal deletion or removal revision to let them know it's gone.
(ctx context.Context, doc *Document, revid string, listRevisions bool)
| 820 | // If the user is not authorized to see the specific revision they asked for, |
| 821 | // instead returns a minimal deletion or removal revision to let them know it's gone. |
| 822 | func (db *DatabaseCollectionWithUser) get1xRevFromDoc(ctx context.Context, doc *Document, revid string, listRevisions bool) (bodyBytes []byte, removed bool, err error) { |
| 823 | var attachments AttachmentsMeta |
| 824 | if err := db.authorizeDoc(doc, revid); err != nil { |
| 825 | // As a special case, you don't need channel access to see a deletion revision, |
| 826 | // otherwise the client's replicator can't process the deletion (since deletions |
| 827 | // usually aren't on any channels at all!) But don't show the full body. (See #59) |
| 828 | // Update: this applies to non-deletions too, since the client may have lost access to |
| 829 | // the channel and gotten a "removed" entry in the _changes feed. It then needs to |
| 830 | // incorporate that tombstone and for that it needs to see the _revisions property. |
| 831 | if revid == "" || doc.History[revid] == nil { |
| 832 | return nil, false, err |
| 833 | } |
| 834 | if doc.History[revid].Deleted { |
| 835 | bodyBytes = []byte(base.EmptyDocument) |
| 836 | } else { |
| 837 | bodyBytes = []byte(RemovedRedactedDocument) |
| 838 | removed = true |
| 839 | } |
| 840 | } else { |
| 841 | if revid == "" { |
| 842 | revid = doc.GetRevTreeID() |
| 843 | if doc.History[revid].Deleted == true { |
| 844 | return nil, false, ErrDeleted |
| 845 | } |
| 846 | } |
| 847 | if bodyBytes, attachments, _, err = db.getRevision(ctx, doc, revid); err != nil { |
| 848 | return nil, false, err |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | kvPairs := []base.KVPair{ |
| 853 | {Key: BodyId, Val: doc.ID}, |
| 854 | {Key: BodyRev, Val: revid}, |
| 855 | } |
| 856 | |
| 857 | if len(attachments) > 0 { |
| 858 | kvPairs = append(kvPairs, base.KVPair{Key: BodyAttachments, Val: attachments}) |
| 859 | } |
| 860 | |
| 861 | if doc.History[revid].Deleted { |
| 862 | kvPairs = append(kvPairs, base.KVPair{Key: BodyDeleted, Val: true}) |
| 863 | } |
| 864 | |
| 865 | if listRevisions { |
| 866 | validatedHistory, getHistoryErr := doc.History.getHistory(revid) |
| 867 | if getHistoryErr != nil { |
| 868 | return nil, removed, getHistoryErr |
| 869 | } |
| 870 | kvPairs = append(kvPairs, base.KVPair{Key: BodyRevisions, Val: encodeRevisions(ctx, doc.ID, validatedHistory)}) |
| 871 | } |
| 872 | |
| 873 | bodyBytes, err = base.InjectJSONProperties(bodyBytes, kvPairs...) |
| 874 | if err != nil { |
| 875 | return nil, removed, err |
| 876 | } |
| 877 | |
| 878 | return bodyBytes, removed, nil |
| 879 | } |