Skip to content
GitLab
Menu
Why GitLab
Pricing
Contact Sales
Explore
Why GitLab
Pricing
Contact Sales
Explore
Sign in
Get free trial
Commits on Source (2)
(refactor): Use Response to stack period fallbacks
· 314531ef
Emiliano Balbuena
authored
Dec 02, 2019
314531ef
(chore): Check for period_callback query param
· 0ed3b3bd
Emiliano Balbuena
authored
Dec 02, 2019
0ed3b3bd
Hide whitespace changes
Inline
Side-by-side
Common/Repository/Response.php
View file @
0ed3b3bd
...
...
@@ -259,6 +259,16 @@ class Response implements \Iterator, \ArrayAccess, \Countable, \JsonSerializable
return
count
(
$this
->
data
);
}
/**
* @param array $data
* @return Response
*/
public
function
pushArray
(
array
$data
)
{
array_push
(
$this
->
data
,
...
$data
);
return
$this
;
}
/**
* Exports the data array
* @return array
...
...
Controllers/api/v2/feeds.php
View file @
0ed3b3bd
...
...
@@ -4,6 +4,7 @@ namespace Minds\Controllers\api\v2;
use
Minds\Api\Exportable
;
use
Minds\Api\Factory
;
use
Minds\Common\Repository\Response
;
use
Minds\Core
;
use
Minds\Core\Di\Di
;
use
Minds\Entities\Factory
as
EntitiesFactory
;
...
...
@@ -129,6 +130,8 @@ class feeds implements Interfaces\Api
$sync
=
(
bool
)
(
$_GET
[
'sync'
]
??
false
);
$periodFallback
=
(
bool
)
(
$_GET
[
'period_fallback'
]
??
false
);
$asActivities
=
(
bool
)
(
$_GET
[
'as_activities'
]
??
true
);
$query
=
isset
(
$_GET
[
'query'
])
?
urldecode
(
$_GET
[
'query'
])
:
null
;
...
...
@@ -196,27 +199,17 @@ class feeds implements Interfaces\Api
}
try
{
$entities
=
[]
;
$entities
=
new
Response
()
;
$fallbackAt
=
null
;
$i
=
0
;
while
(
count
(
$entities
)
<
$limit
)
{
while
(
$entities
->
count
(
)
<
$limit
)
{
$rows
=
$manager
->
getList
(
$opts
);
if
(
!
$sync
)
{
// Remove all unlisted content, if ES document is not in sync, it'll
// also remove pending activities
$rows
=
$rows
->
filter
([
$elasticEntities
,
'filter'
]);
if
(
$asActivities
)
{
// Cast to ephemeral Activity entities, if another type
$rows
=
$rows
->
map
([
$elasticEntities
,
'cast'
]);
}
}
$entities
=
array_merge
(
$entities
,
$rows
->
toArray
());
$entities
=
$entities
->
pushArray
(
$rows
->
toArray
());
if
(
!
$periodFallback
||
$opts
[
'algorithm'
]
!==
'top'
||
!
isset
(
static
::
PERIOD_FALLBACK
[
$opts
[
'period'
]])
||
++
$i
>
2
// Stop at 2nd fallback (i.e. 12h > 7d > 30d)
...
...
@@ -234,6 +227,17 @@ class feeds implements Interfaces\Api
}
}
if
(
!
$sync
)
{
// Remove all unlisted content, if ES document is not in sync, it'll
// also remove pending activities
$entities
=
$entities
->
filter
([
$elasticEntities
,
'filter'
]);
if
(
$asActivities
)
{
// Cast to ephemeral Activity entities, if another type
$entities
=
$entities
->
map
([
$elasticEntities
,
'cast'
]);
}
}
return
Factory
::
response
([
'status'
=>
'success'
,
'entities'
=>
Exportable
::
_
(
$entities
),
...
...