streams/Code/Widget/Suggestions.php

62 lines
1.8 KiB
PHP
Raw Normal View History

2017-03-16 01:31:34 +00:00
<?php
2022-02-16 04:08:28 +00:00
namespace Code\Widget;
2017-03-16 01:31:34 +00:00
use App;
2022-02-16 04:08:28 +00:00
use Code\Lib\Apps;
use Code\Render\Theme;
use Code\Lib\Socgraph;
2017-03-16 01:31:34 +00:00
2022-10-23 21:18:44 +00:00
class Suggestions implements WidgetInterface
2021-12-02 23:02:31 +00:00
{
2022-10-24 03:39:49 +00:00
public function widget(array $arguments): string
2021-12-02 23:02:31 +00:00
{
if ((!local_channel()) || (!Apps::system_app_installed(local_channel(), 'Suggest Channels'))) {
return EMPTY_STR;
}
2022-02-14 19:55:01 +00:00
$r = Socgraph::suggestion_query(local_channel(), get_observer_hash(), 0, 20);
2021-12-02 23:02:31 +00:00
if (!$r) {
return EMPTY_STR;
}
2022-10-24 03:39:49 +00:00
$arguments = [];
2021-12-02 23:02:31 +00:00
// Get two random entries from the top 20 returned.
// We'll grab the first one and the one immediately following.
2022-11-20 06:44:13 +00:00
// This will throw some entropy into the situation, so you won't
2021-12-02 23:02:31 +00:00
// be looking at the same two mug shots every time the widget runs
$index = ((count($r) > 2) ? mt_rand(0, count($r) - 2) : 0);
for ($x = $index; $x <= ($index + 1); $x++) {
$rr = $r[$x];
2021-12-03 03:01:39 +00:00
if (!$rr['xchan_url']) {
2021-12-02 23:02:31 +00:00
break;
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
$connlnk = z_root() . '/follow/?url=' . $rr['xchan_addr'];
2022-10-24 03:39:49 +00:00
$arguments[] = [
2021-12-02 23:02:31 +00:00
'url' => chanlink_url($rr['xchan_url']),
'profile' => $rr['xchan_url'],
'name' => $rr['xchan_name'],
'photo' => $rr['xchan_photo_m'],
'ignlnk' => z_root() . '/directory?return=' . base64_encode(App::$query_string) . '&ignore=' . $rr['xchan_hash'],
'conntxt' => intval($rr['xchan_type']) === 1 ? t('Join') : t('Connect'),
2021-12-02 23:02:31 +00:00
'connlnk' => $connlnk,
'ignore' => t('Ignore/Hide')
];
}
2022-11-20 06:44:13 +00:00
return replace_macros(Theme::get_template('suggest_widget.tpl'), [
2021-12-02 23:02:31 +00:00
'$title' => t('Suggestions'),
'$more' => t('See more...'),
2022-10-24 03:39:49 +00:00
'$entries' => $arguments
2021-12-02 23:02:31 +00:00
]);
}
2017-03-16 01:31:34 +00:00
}