aboutsummaryrefslogtreecommitdiffstats
path: root/include/ratenotif.php
blob: e94f30247040cf70783eb24fc9d66bec8fc1dd49 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php

require_once('include/cli_startup.php');
require_once('include/zot.php');
require_once('include/queue_fn.php');


function ratenotif_run($argv, $argc){

	cli_startup();

	$a = get_app();

	require_once("session.php");
	require_once("datetime.php");
	require_once('include/items.php');
	require_once('include/Contact.php');

	if($argc < 3)
		return;


	logger('ratenotif: invoked: ' . print_r($argv,true), LOGGER_DEBUG);

	$cmd = $argv[1];

	$item_id = $argv[2];


	if($cmd === 'rating') {
		$r = q("select * from xlink where xlink_id = %d and xlink_static = 1 limit 1",
			intval($item_id)
		);
		if(! $r) {
			logger('rating not found');
			return;
		}

		$encoded_item = array(
			'type' => 'rating', 
			'encoding' => 'zot',
			'target' => $r[0]['xlink_link'],
			'rating' => intval($r[0]['xlink_rating']),
			'rating_text' => $r[0]['xlink_rating_text'],
			'signature' => $r[0]['xlink_sig'],
			'edited' => $r[0]['xlink_updated']
		);
	}

	$channel = channelx_by_hash($r[0]['xlink_xchan']);
	if(! $channel) {
		logger('no channel');
		return;
	}


	$primary = get_directory_primary();

	if(! $primary)
		return;


	$interval = ((get_config('system','delivery_interval') !== false) 
		? intval(get_config('system','delivery_interval')) : 2 );

	$deliveries_per_process = intval(get_config('system','delivery_batch_count'));

	if($deliveries_per_process <= 0)
		$deliveries_per_process = 1;

	$deliver = array();

	$x = z_fetch_url($primary . '/regdir');
	if($x['success']) {
		$j = json_decode($x['body'],true);
		if($j && $j['success'] && is_array($j['directories'])) {

			foreach($j['directories'] as $h) {
				if($h == z_root())
					continue;

				$hash = random_string();
				$n = zot_build_packet($channel,'notify',null,null,$hash);

				queue_insert(array(
					'hash'       => $hash,
					'account_id' => $channel['channel_account_id'],
					'channel_id' => $channel['channel_id'],
					'posturl'    => $h . '/post',
					'notify'     => $n,
					'msg'        => json_encode($encoded_item)
				));

				$deliver[] = $hash;

				if(count($deliver) >= $deliveries_per_process) {
					proc_run('php','include/deliver.php',$deliver);
					$deliver = array();
					if($interval)
						@time_sleep_until(microtime(true) + (float) $interval);
				}
			}

			// catch any stragglers

			if(count($deliver)) {
				proc_run('php','include/deliver.php',$deliver);
			}
		}
	}
		
	logger('ratenotif: complete.');
	return;

}

if (array_search(__file__,get_included_files())===0){
  ratenotif_run($argv,$argc);
  killme();
}