summary refs log tree commit diff
path: root/index.js
blob: 3b826f340fe9f46febde0fc5ed803cbb8cff104e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
var mastodon = require('mastodon');
var pg = require('pg');

var query = `SELECT id 
FROM statuses 
WHERE favourites_count > (
  SELECT avg(favourites_count) 
  FROM statuses 
  WHERE favourites_count > 1
  AND created_at > NOW() - INTERVAL '30 days'
)
AND created_at > NOW() - INTERVAL '30 days';`

var config = {
  user: process.env.DB_USER || 'mastodon',
  database: process.env.DB_NAME || 'mastodon_production',
  password: process.env.DB_PASSWORD || '',
  host: process.env.DB_HOST || '/var/run/postgresql',
  port: 5432, //env var: PGPORT
  max: 2, // max number of clients in the pool
  idleTimeoutMillis: 30000, // how long a client is allowed to remain idle before being closed
};

// instantiate a new client
var client = new pg.Client(config);

function cycle() {
  // connect to our database
  client.connect(function (err) {
    if (err) throw err;

    // execute a query on our database
    client.query(query, [], function (err, result) {
      if(err) {
        return console.error('error running query', err);
      }

      boost(result.rows);

      // disconnect the client
      client.end(function (err) {
        if (err) throw err;
      });
    });
  });
}

var M = new mastodon({
  access_token: process.env.AMBASSADOR_TOKEN,
  api_url: `${process.env.INSTANCE_HOST}/api/v1/`
});


var boosted = {};
function boost(rows) {
  rows.map(function(row) {
    return row.id;
  })
  .filter(function(id) {
    return !boosted[id];
  })
  .forEach(function(id) {
    M.post(`statuses/${id}/reblog`, function(err, result) {
      if (err) {
        if (err.message === 'Validation failed: Reblog of status already exists') {
          boosted[id] = true;
          return console.log(`Warning: tried to boost #${id} but it had already been boosted by this account. Adding to cache.`);
        }

        return console.error(err);
      }
      boosted[id] = true;
      console.log(`boosted status #${id}`);
    });
  })
}

cycle();
setInterval(cycle, 1000 * 60 * 15);