Skip to main content

Add voteWhere Smart Function

That's right... there's nothing stopping Soft Cell from changing their username without a vote.

If we decide that users cannot change their usernames without a vote, then we need to add a smart function to the _user/username predicate.

For example, let's say Soft Cell issues the below transaction:

"_id": ["_user/username", "softCell"],
"username": "hardCell"

To decide whether that is valid, our smart functions need to:

  1. Try and find a proposed change (a subject in the change collection) for the subject, ["_user/username", "softCell"], the predicate _user/username, and the object hardCell.
  2. If there is a proposed change, we'll need the smart function to look up the number of yes and no votes.
  3. Finally, the smart function needs to have guidance on what is required to pass a vote (i.e. a majority of votes, or 10 yes votes regardless of the number of no votes).
  4. If the vote threshhold is met, the function should return true and allow the transaction to go through. Otherwise, it should return false and stop the transaction from happening.

We'll start by working on part 1 - try to find the relevant change.

If we were to issue a query for the relevant change, it would like the following, where (?sid) becomes the relevant subject id and (?pid) is the subject id for the relevant predicate.

"select": {"?vote": ["*"]},
"where": [
["?change", "change/subject", "(?sid)"],
["?change", "change/predicate", "(?pid)"],
["?change", "change/object", "(?o)"],
["?change", "change/vote", "?vote"]

For the select clause, we use {"?vote": ["*"]}, which will find the vote, as well as retrieve all of that vote's predicates.

In order to maximize legibility, we will create a smart function that simply returns the proper value of the where clause as a string. We will subsequently be using this smart functions in other smart functions.

"_id": "_fn",
"name": "voteWhere",

To concatenate multiple strings, we can use the str function, which takes an infinite number of string arguments (or function arguments that return strings). The start of this smart function will be (str \"[[\\\"?change\\\", \\\"change/subject\\\", \" (?sid) \"],[\\\"?change\\\", \\\"change/predicate\\\" \", ..... Can you finish it?

Note that because _fn/code is a string type predicate, we have to escape all quotation marks.

You'll need several of the following Context-Dependent Smart Functions to write the smart function code. Note, you may not need all of these functions:

  • (?sid): The _id for the subject being updated.
  • (?pid): The _id of the predicate being updated.
  • (?auth_id) : The _id of the auth making the update.
  • (?o): The object (value) of the predicate that is being edited.

Write The Code for voteWhere

Follow the instructions above to write the code for voteWhere.