### Setup



Install RVM to manage ruby version:
```bash
\curl -sSL https://get.rvm.io | bash
rvm install 2.6.3
```

Install external dependencies:

#### For MacOS:
```bash
brew tap heroku/brew && brew install heroku
brew install postgres
brew install redis
brew install ffmpeg
brew install yarn
```

#### For Linux (Ubuntu):
```bash
curl https://cli-assets.heroku.com/install.sh | sh
sudo apt-get install postgresql postgresql-contrib
sudo apt-get install redis-server
sudo add-apt-repository ppa:mc3man/trusty-media
sudo apt-get install ffmpeg
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt-get update
sudo apt-get install yarn
```

Then run:
```bash
bin/setup
```
to create your databases

You will need to:
- Get environment variables from fellow teammates to populate into `.env` (See env.sample for examples)
- AWS account to have access to S3 buckets
- Heroku account to deploy
- Set DOMAIN and WEB_PORT variables - otherwise application won't work properly. Use DOMAIN=localhost and WEB_PORT=3000 for the default values
- Set ZOOM_API_KEY and ZOOM_API_SECRET to integrate with Zoom.us for automatic meeting creation.

After filling up the .env with correct values
Run setup again:
```bash
bin/setup
```


### Running tests
```bash
rspec
yarn test
```

### Running Data migrations
```bash
rails data:migrate
```

### Current deployment pipeline on Heroku
- Review -> Staging |-> Production
                    |-> Demo

#### Working with environments using Parity

[Parity](https://github.com/thoughtbot/parity) provides Heroku command wrappers for each environment under the `bin` folder.

### Useful Heroku commands
##### Manually Deploying to Review

The Review environment is auto-deployed from GitHub `master`. But if you need to manually deploy:
```bash
bin/review deploy
```

##### Running migrations manually (should be done automatically by Heroku via Procfile)
```bash
bin/review run db:migrate
```

##### Rails console into Review environment
```bash
bin/review console
```

##### We keep locale files sorted in alphabetical order. This can be done programmatically
```
rake i18n:sort
```

## Zoom.us integration
DirectMe app offers live broadcasting feature. Users are offered to paralelly connect to the Zoom meeting to have a video conference while the streaming happens. In order to use the Zoom functionality, the app needs to have the API and verification token keys provided along with the account number that is available after login into the Zoom account. You need to have Zoom PRO subscription in order to use this feature.

#### Zoom.us api keys
1. Log in to you zoom.us account
2. Go to https://marketplace.zoom.us/develop/create
3. Choose JWT application
4. Copy API Key and API Secret
5. Set up ZOOM_API_KEY and ZOOM_API_SECRET environment variables

#### Setup
There is some configuration that has to be done through the API on the Zoom account so that you can use the feature. Run `rails zoom:setup` rake task to do it.

#### Zoom.us webhooks
To ensure integrity in between different Zoom environments, the app uses Zoom webhooks. To set them up, go to https://marketplace.czoom.us -> Develop -> JWT app -> Feature -> Event Subscriptions and enable following hooks:
* Start Meeting
* End Meeting
* All Recordings have completed
* User has been created
* User had been deleted

#### Syncing app with Zoom account configuration
If you are setting up the app to use Zoom account that has been previously used with DirectME, it is a good idea to make sure that the db state reflects the account situation. To do that, run `rails zoom:sync` rake task.

## Working Locally

#### Polling for analysis status updates

When videos are uploaded, a video/audio analysis process is initiated. Typically the analysis process sends an SNS notification when it is completed. Those SNS notifications hit a webhook URL within the app to update the status accordingly. This requires a public URL, which you likely won't have in development. 

As an alternative, we can poll for status updates using the following Rake task.
```bash
rake dev:poll_for_analysis_updates
```

To run this continuously, there is a [Clockwork](https://github.com/adamwiggins/clockwork) scheduler included.
```bash
bin/clockwork lib/dev_clockwork.rb
```

#### Running a real async background queue locally
1. Verify redis has been installed
1. Set REDIS_URL in your `.env` file
    ```bash
    REDIS_URL=redis://127.0.0.1:6379
    ```
1. Start Sidekiq
    ```bash
    bundle exec sidekiq -q default -q mailers -c 5
    ```

### Setup pairing script
[Check out the repo here](https://github.com/pivotal-legacy/git_scripts)
- Tool to add two or more authors on a commit message quickly
- Make sure to add yourself or new devs to the `.pairs` file in the repo

`cd /usr/local/bin && curl -L http://github.com/pivotal/git_scripts/tarball/master | gunzip | tar xvf - --strip=2`

then `git pair XX` where XX are your initials from the pairs file

Enjoy!

## Problems with setting up environment


### Database setup

If the setup fails in the point after at least one of the databases
have been set up.
1. Drop the databases
   ```bash
   rails db:drop
   ```
1. Run the setup again.

## Optional helpers

# Run rubocop fix script as a pre-commit git hook

Add following as .git/hooks/pre-commit file

```
#!/bin/sh
exec ./bin/rubocopfix.sh
```
