If you only want one chat message per author, that implies some grouping going on. So the primary key of the view needs to be `author`.
The reduce function will need to pick the newest message, and the easiest way to do that is to sort them by date.
So that implies a map function that emits a key `[author, created_at]`, and a reduce function that returns the last item of `values`.
The drawback is that the results are going to be sorted by author, not by date, but you can sort them yourself if you emit the date as part of the value. (Unfortunately, a key/value index doesn’t allow you to group or subrange by one criterion and sort by another.) CBL/iOS has a `sortDescriptors` property you can use to do this postprocessing automatically.
Another approach is to emit `created_at` as the primary key, without any reduce function. Then query descending. You’ll have to keep track of the author values you’ve seen already (e.g. using a set) and skip duplicate authors. This approach takes a bit more code but I suspect it’ll be more efficient.
—Jens