Skip to content

A simple site to track my kid's computer time, tasks & rewards with bitcoin

License

Notifications You must be signed in to change notification settings

ObjSal/computer-time

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

55 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Computer Time

A simple site to track my kid's computer time, tasks & rewards with bitcoin

Requirements

LNbits Configuration

LNbits has to be running via https or tor ✔️

  1. Create a wallet specific for the reward withdrawals
  2. Copy the Admin key from the wallet. Wallet > API docs > Admin key
  3. Save the Admin key in a lnbits_wallet_admin_key property of your user's user_data (UI coming soon)
  4. Install the LNURLw extension and active it for your user

MongoDB Configuration

1. Create a Cluster in MongoDB

Atlas > Data Services > Database > [Create]; name it mongodb-atlas. If you want to change the name, change the value for CLUSTER_NAME in app.js. Below is my configuration:

* Shared
* Cloud Provider:
* Region: Oregon (us-west-2)
* Cluster Tier: M0 Sandbox (Shared RAM, 512 MB Storage)
* MongoDB 6.0

2. Create a Database in the Cluster

Atlas > Data Services > Database > Cluster0 > Collection > [Create Database]; name it computer-time. If you want to change the name, change the value for DATABASE_NAME in app.js.

3. Create a Real app

Atlas > App Services > [Create a New App]

4. Create collections

Atlas > App Services > App > Schema > Collections > Add a collection. Make sure you create the collections within the computer-time database and create the below schemas:

log

{
  "title": "log",
  "properties": {
    "_id": {
      "bsonType": "objectId"
    },
    "timestamp": {
      "bsonType": "date"
    },
    "type": {
      "bsonType": "string"
    },
    "userAgent": {
      "bsonType": "string"
    },
    "owner_id": {
      "bsonType": "string"
    }
  }
}

tasks

{
  "properties": {
    "_id": {
      "bsonType": "objectId"
    },
    "timestamp": {
      "bsonType": "date"
    },
    "description": {
      "bsonType": "string"
    },
    "sats": {
      "bsonType": "int"
    },
    "qrcode": {
      "bsonType": "string"
    },
    "status": {
      "bsonType": "string"
    },
    "owner_id": {
      "bsonType": "string"
    }
  },
  "required": [
    "_id",
    "timestamp",
    "description",
    "sats",
    "qrcode",
    "status"
  ]
}

Make sure you name the first one log and the other one tasks. otherwise update you app.js.

Setup collection rules

Atlas > App Services > App > Rules > Collections > mongodb-atlas > computer-time

Rules allow set appropriate permissions for reading, writing on data, I have the following rules to each of the collections created above

log

{
  "roles": [
    {
      "name": "admin",
      "apply_when": {
        "%%user.custom_data.isGlobalAdmin": true
      },
      "document_filters": {
        "write": true,
        "read": true
      },
      "read": true,
      "write": true,
      "insert": true,
      "delete": true,
      "search": true
    },
    {
      "name": "user",
      "apply_when": {},
      "document_filters": {
        "write": {
          "owner_id": "%%user.id"
        },
        "read": {
          "owner_id": "%%user.id"
        }
      },
      "read": true,
      "write": true,
      "insert": true,
      "delete": false,
      "search": true
    }
  ]
}

tasks

{
  "roles": [
    {
      "name": "Admin",
      "apply_when": {
        "%%user.custom_data.isGlobalAdmin": true
      },
      "document_filters": {
        "write": true,
        "read": true
      },
      "read": true,
      "write": true,
      "insert": true,
      "delete": true,
      "search": true
    },
    {
      "name": "readAllWriteOwn",
      "apply_when": {},
      "document_filters": {
        "write": {
          "owner_id": {
            "$in": [
              null,
              "%%user.id"
            ]
          }
        },
        "read": true
      },
      "read": true,
      "write": true,
      "insert": true,
      "delete": false,
      "search": true
    }
  ]
}

5. Setup App Users

Atlas > App Services > App > App Users > Authentication Providers; Only enable Email/Password.

Atlas > App Services > App > App Users > UserAPI Settings > Enable Custom UserAPI Data; use the following configuration:

Cluster Name: mongodb-atlas
Database Name: computer-time
Collection Name: user_data
UserAPI ID Field: owener_id 

I use the Custom UserAPI Data to store username and to set admin privileges using the isGlobalAdmin property

6. Enable Device Sync

Atlas > App Services > App > Device Sync

  1. Flexible
  2. Development Mode: Off
  3. Queryable Fields: _id, owner_id

7. Test your MongoDB configuration with the demo site

No compiling necessary, just launch the demo site and enter your App Id, email and password, and you should be all set.

Tips

  • Use the appId URL param to send your Realm App ID, i.e.: index.html?appId=<blah-blah-blah>
  • Use the lnbitsHost URL param to send the LNbits hostname, i.e.: index.html?appId=<blah>&lnbitsHost=https%3A%2F%2Fraspberrypi.local

Notes

  • Atlas, MongoDB and Realm are not free, but they do offer free tiers.
  • This is an experiment, don't use it for production applications. DYOR.
  • A lot of the logic should exist in a proper backend environment.
  • License: GNU GENERAL PUBLIC LICENSE

Project developed with ♥ from AZ with HTML, JS and Mongo Realm Web SDK

About

A simple site to track my kid's computer time, tasks & rewards with bitcoin

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published