collectionBlipHandler wraps another blip handler to specify a collection
(next blipHandlerFunc)
| 162 | |
| 163 | // collectionBlipHandler wraps another blip handler to specify a collection |
| 164 | func collectionBlipHandler(next blipHandlerFunc) blipHandlerFunc { |
| 165 | return func(bh *blipHandler, bm *blip.Message) error { |
| 166 | collectionIndexStr, ok := bm.Properties[BlipCollection] |
| 167 | if !ok { |
| 168 | if !bh.db.HasDefaultCollection() { |
| 169 | return base.HTTPErrorf(http.StatusBadRequest, "Collection property not specified and default collection is not configured for this database") |
| 170 | } |
| 171 | if bh.collections.hasNamedCollections() { |
| 172 | return base.HTTPErrorf(http.StatusBadRequest, "GetCollections already occurred, subsequent messages need a Collection property") |
| 173 | } |
| 174 | var err error |
| 175 | bh.collection, err = bh.db.GetDefaultDatabaseCollectionWithUser() |
| 176 | if err != nil { |
| 177 | return err |
| 178 | } |
| 179 | bh.collectionCtx, err = bh.collections.get(nil) |
| 180 | if err != nil { |
| 181 | bh.collections.setNonCollectionAware(newBlipSyncCollectionContext(bh.loggingCtx, bh.collection.DatabaseCollection)) |
| 182 | bh.collectionCtx, _ = bh.collections.get(nil) |
| 183 | } |
| 184 | bh.loggingCtx = bh.collection.AddCollectionContext(bh.BlipSyncContext.loggingCtx) |
| 185 | return next(bh, bm) |
| 186 | } |
| 187 | if !bh.collections.hasNamedCollections() { |
| 188 | return base.HTTPErrorf(http.StatusBadRequest, "Passing collection requires calling %s first", MessageGetCollections) |
| 189 | } |
| 190 | |
| 191 | collectionIndex, err := strconv.Atoi(collectionIndexStr) |
| 192 | if err != nil { |
| 193 | return base.HTTPErrorf(http.StatusBadRequest, "collection property needs to be an int, was %q", collectionIndexStr) |
| 194 | } |
| 195 | |
| 196 | bh.collectionIdx = &collectionIndex |
| 197 | bh.collectionCtx, err = bh.collections.get(&collectionIndex) |
| 198 | if err != nil { |
| 199 | return base.HTTPErrorf(http.StatusBadRequest, "%s", err) |
| 200 | } |
| 201 | bh.collection = &DatabaseCollectionWithUser{ |
| 202 | DatabaseCollection: bh.collectionCtx.dbCollection, |
| 203 | user: bh.db.user, |
| 204 | } |
| 205 | bh.loggingCtx = bh.collection.AddCollectionContext(bh.BlipSyncContext.loggingCtx) |
| 206 | // Call down to the underlying handler and return it's value |
| 207 | return next(bh, bm) |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | // ////// CHECKPOINTS |
| 212 |