Merge branch 'dev' of codeberg.org:streams/streams into dev

This commit is contained in:
Mike Macgirvin 2023-12-10 06:53:39 +11:00
commit 5da7604d0c
2 changed files with 336 additions and 1 deletions

View file

@ -1067,6 +1067,341 @@ class Activity
}
// the $recurse flag encodes the original non-deleted object of a deleted activity
public static function encode_add_activity($item, $activitypub = false, $recurse = false)
{
$activity = [];
if (intval($item['item_deleted']) && (!$recurse)) {
$is_response = ActivityStreams::is_response_activity($item['verb']);
if ($is_response) {
$activity['type'] = 'Undo';
$fragment = '#undo';
} else {
$activity['type'] = 'Delete';
$fragment = '#delete';
}
$activity['id'] = str_replace('/item/', '/activity/', $item['mid']) . $fragment;
$actor = self::encode_person($item['author'], false);
if ($actor) {
$activity['actor'] = $actor;
} else {
return [];
}
$obj = (($is_response) ? self::encode_add_activity($item, $activitypub, true) : self::encode_item($item, $activitypub));
if ($obj) {
if (array_path_exists('object/id', $obj)) {
$obj['object'] = $obj['object']['id'];
}
if ($obj) {
$activity['object'] = $obj;
}
} else {
return [];
}
$activity['to'] = [ACTIVITY_PUBLIC_INBOX];
return $activity;
}
$activity['type'] = 'Add';
if (str_contains($item['mid'], z_root() . '/item/')) {
$activity['id'] = str_replace('/item/', '/activity/', $item['mid']);
} elseif (str_contains($item['mid'], z_root() . '/event/')) {
$activity['id'] = str_replace('/event/', '/activity/', $item['mid']);
} else {
$activity['id'] = $item['mid'];
}
if ($item['title']) {
$activity['name'] = $item['title'];
}
if ($item['summary']) {
$activity['summary'] = bbcode($item['summary'], ['export' => true]);
}
if ($activity['type'] === 'Announce') {
$tmp = $item['body'];
$activity['content'] = bbcode($tmp, ['export' => true]);
$activity['source'] = [
'content' => $item['body'],
'mediaType' => 'text/x-multicode'
];
if ($item['summary']) {
$activity['source']['summary'] = $item['summary'];
}
}
$activity['published'] = datetime_convert('UTC', 'UTC', $item['created'], ATOM_TIME);
if ($item['created'] !== $item['edited']) {
$activity['updated'] = datetime_convert('UTC', 'UTC', $item['edited'], ATOM_TIME);
if ($activity['type'] === 'Create') {
$activity['type'] = 'Update';
}
}
if ($item['app']) {
$activity['generator'] = ['type' => 'Application', 'name' => $item['app']];
}
if ($item['location'] || $item['lat'] || $item['lon']) {
$place = (new Place())->setType('Place')
->setName(isset($item['location']) ? $item['location'] : null);
if ($item['lat'] || $item['lon']) {
$place->setLatitude(isset($item['lat']) ? $item['lat'] : 0)
->setLongitude(isset($item['lon']) ? $item['lon'] : 0);
}
$activity['location'] = $place->toArray();
}
if ($item['mid'] === $item['parent_mid']) {
$activity['isContainedConversation'] = true;
}
else {
// inReplyTo needs to be set in the activity for followup actions (Like, Dislike, Announce, etc.),
// but *not* for comments and RSVPs, where it should only be present in the object
if (!in_array($activity['type'], ['Create', 'Update'])) {
$activity['inReplyTo'] = $item['thr_parent'];
}
if (in_array($activity['type'], ['Accept', 'Reject', 'TentativeAccept', 'TentativeReject'])) {
$activity['inReplyTo'] = set_activity_mid($item['thr_parent']);
}
// @FIXME FEP-5624 set for comment approvals but not event approvals
// For comment approvals and rejections
// if (in_array($activity['type'], ['Accept','Reject']) && is_string($item['obj']) && strlen($item['obj'])) {
// $activity['inReplyTo'] = $item['thr_parent'];
// }
$cnv = get_iconfig($item['parent'], 'activitypub', 'context');
if (!$cnv) {
$cnv = $item['parent_mid'];
}
}
if (!(isset($cnv) && $cnv)) {
$cnv = get_iconfig($item, 'activitypub', 'context');
if (!$cnv) {
$cnv = $item['parent_mid'];
}
}
if (!empty($cnv)) {
if (is_string($cnv) && str_starts_with($cnv, z_root())) {
$cnv = str_replace(['/item/', '/activity/'], ['/conversation/', '/conversation/'], $cnv);
}
$activity['context'] = $cnv;
$activity['conversation'] = $cnv;
}
if (intval($item['item_private']) === 2) {
$activity['directMessage'] = true;
}
$actor = self::encode_person($item['owner'], false);
if ($actor) {
$activity['actor'] = $actor;
} else {
return [];
}
$replyTo = unserialise($item['replyto']);
if ($replyTo) {
$activity['replyTo'] = $replyTo;
$activity['audience'] = $replyTo;
}
if (!isset($activity['url'])) {
$urls = [];
if (intval($item['item_wall'])) {
$locs = self::nomadic_locations($item);
if ($locs) {
foreach ($locs as $l) {
if (str_contains($activity['id'], $l['hubloc_url'])) {
continue;
}
$urls[] = [
'type' => 'Link',
'href' => str_replace(z_root(), $l['hubloc_url'], $activity['id']),
'rel' => 'alternate',
'mediaType' => 'text/html'
];
$urls[] = [
'type' => 'Link',
'href' => str_replace(z_root(), $l['hubloc_url'], $activity['id']),
'rel' => 'alternate',
'mediaType' => 'application/activity+json'
];
$urls[] = [
'type' => 'Link',
'href' => str_replace(z_root(), $l['hubloc_url'], $activity['id']),
'rel' => 'alternate',
'mediaType' => 'application/x-zot+json'
];
$urls[] = [
'type' => 'Link',
'href' => str_replace(z_root(), $l['hubloc_url'], $activity['id']),
'rel' => 'alternate',
'mediaType' => 'application/x-nomad+json'
];
}
}
}
if ($urls) {
$curr[] = [
'type' => 'Link',
'href' => $activity['id'],
'rel' => 'alternate',
'mediaType' => 'text/html'
];
$activity['url'] = array_merge($curr, $urls);
} else {
$activity['url'] = $activity['id'];
}
}
if ($item['obj']) {
if (is_string($item['obj'])) {
$tmp = json_decode($item['obj'], true);
if ($tmp !== null) {
$item['obj'] = $tmp;
}
}
$obj = self::encode_object($item['obj']);
}
else {
$obj = self::encode_item($item, $activitypub);
}
if ($obj) {
$activity['object'] = $obj;
} else {
return [];
}
if ($item['target']) {
if (is_string($item['target'])) {
$tmp = json_decode($item['target'], true);
if ($tmp !== null) {
$item['target'] = $tmp;
}
}
$tgt = self::encode_object($item['target']);
if ($tgt) {
$activity['target'] = $tgt;
}
}
else {
$item['target'] = $item['parent_mid'];
}
$t = self::encode_taxonomy($item);
if ($t) {
foreach($t as $tag) {
if (strcasecmp($tag['name'], '#nsfw') === 0
|| strcasecmp($tag['name'], '#sensitive') === 0) {
$activity['sensitive'] = true;
}
}
$activity['tag'] = $t;
}
if ($obj && $obj['attachment']) {
$activity['attachment'] = $obj['attachment'];
}
else {
$a = self::encode_attachment($item);
if ($a) {
$activity['attachment'] = $a;
}
}
// addressing madness
if ($activitypub) {
$parent_i = [];
$public = !$item['item_private'];
$top_level = ($item['mid'] === $item['parent_mid']);
$activity['to'] = [];
$activity['cc'] = [];
$recips = get_iconfig($item['parent'], 'activitypub', 'recips');
if ($recips) {
$parent_i['to'] = $recips['to'];
$parent_i['cc'] = $recips['cc'];
}
if ($public) {
$activity['to'] = [ACTIVITY_PUBLIC_INBOX];
if (isset($parent_i['to']) && is_array($parent_i['to'])) {
$activity['to'] = array_values(array_unique(array_merge($activity['to'], $parent_i['to'])));
}
if ($item['item_origin']) {
$activity['cc'] = [z_root() . '/followers/' . substr($item['author']['xchan_addr'], 0, strpos($item['author']['xchan_addr'], '@'))];
}
if (isset($parent_i['cc']) && is_array($parent_i['cc'])) {
$activity['cc'] = array_values(array_unique(array_merge($activity['cc'], $parent_i['cc'])));
}
} else {
// private activity
if ($top_level) {
$activity['to'] = self::map_acl($item);
if (isset($parent_i['to']) && is_array($parent_i['to'])) {
$activity['to'] = array_values(array_unique(array_merge($activity['to'], $parent_i['to'])));
}
} elseif ((int)$item_private === 1) {
$activity['cc'] = self::map_acl($item);
if (isset($parent_i['cc']) && is_array($parent_i['cc'])) {
$activity['cc'] = array_values(array_unique(array_merge($activity['cc'], $parent_i['cc'])));
}
$d = q(
"select hubloc.* from hubloc left join item on hubloc_hash = owner_xchan where item.parent_mid = '%s' and item.uid = %d and hubloc_deleted = 0 order by hubloc_id desc limit 1",
dbesc($item['parent_mid']),
intval($item['uid'])
);
if ($d) {
if ($d[0]['hubloc_network'] === 'activitypub') {
$addr = $d[0]['hubloc_hash'];
} else {
$addr = $d[0]['hubloc_id_url'];
}
$activity['cc'][] = $addr;
}
}
}
$mentions = self::map_mentions($item);
if (count($mentions) > 0) {
if (!$activity['to']) {
$activity['to'] = $mentions;
} else {
$activity['to'] = array_values(array_unique(array_merge($activity['to'], $mentions)));
}
}
}
$cc = [];
if ($activity['cc'] && is_array($activity['cc'])) {
foreach ($activity['cc'] as $e) {
if (!is_array($activity['to'])) {
$cc[] = $e;
} elseif (!in_array($e, $activity['to'])) {
$cc[] = $e;
}
}
}
$activity['cc'] = $cc;
return $activity;
}
public static function nomadic_locations($item)
{
$synchubs = [];

View file

@ -1,2 +1,2 @@
<?php
define ('STD_VERSION', '23.12.07');
define ('STD_VERSION', '23.12.09');