Skip to content
Projects
Groups
Snippets
Help
Sign in / Register
Toggle navigation
Minds Backend - Engine
Project overview
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Locked Files
Issues
284
Merge Requests
39
CI / CD
Security & Compliance
Packages
Wiki
Snippets
Members
Collapse sidebar
Close sidebar
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Minds
Minds Backend - Engine
Commits
f9f58fd2
Commit
f9f58fd2
authored
6 minutes ago
by
Guy Thouret
Browse files
Options
Download
Boost Campaigns API Endpoint -
#1201
parent
3ae24ff5
epic/boost-campaign
1 merge request
!417
WIP: Boost Campaigns
Pipeline
#102365437
failed with stages
in 48 seconds
Changes
4
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
227 additions
and
5 deletions
+227
-5
Api/AbstractApi.php
View file @
f9f58fd2
...
...
@@ -139,9 +139,9 @@ abstract class AbstractApi implements Interfaces\Api
$this
->
sendError
(
500
);
}
protected
function
sendBadRequest
()
:
void
protected
function
sendBadRequest
(
string
$message
=
null
)
:
void
{
$this
->
sendError
(
400
);
$this
->
sendError
(
400
,
$message
);
}
protected
function
sendNotImplemented
()
:
void
...
...
@@ -154,9 +154,9 @@ abstract class AbstractApi implements Interfaces\Api
$this
->
sendError
(
304
);
}
protected
function
sendNotAcceptable
()
:
void
protected
function
sendNotAcceptable
(
string
$message
=
null
)
:
void
{
$this
->
sendError
(
406
);
$this
->
sendError
(
406
,
$message
);
}
protected
function
sendUnauthorised
()
:
void
...
...
This diff is collapsed.
Controllers/api/v2/boost/campaigns.php
0 → 100644
View file @
f9f58fd2
<?php
namespace
Minds\Controllers\api\v2\boost
;
use
Exception
;
use
Minds\Common\Urn
;
use
Minds\Core\Boost\Network\Campaign
;
use
Minds\Core\Boost\Network\Manager
;
use
Minds\Core\Di\Di
;
use
Minds\Core\Session
;
use
Minds\Interfaces
;
use
Minds\Api\Factory
;
class
campaigns
implements
Interfaces\Api
{
/**
* Equivalent to HTTP GET method
* @param array $pages
* @return mixed|null
* @throws Exception
*/
public
function
get
(
$pages
)
{
$limit
=
$_GET
[
'limit'
]
??
12
;
$offset
=
$_GET
[
'offset'
]
??
''
;
$urn
=
$pages
[
0
]
??
null
;
if
(
$limit
>
50
||
$limit
<
0
)
{
$limit
=
12
;
}
$guid
=
''
;
if
(
$urn
)
{
$limit
=
1
;
$offset
=
''
;
$urn
=
new
Urn
(
$urn
);
$guid
=
(
string
)
$urn
->
getNss
();
}
/** @var Manager $manager */
$manager
=
Di
::
_
()
->
get
(
'Boost\Network\Manager'
);
$manager
->
setActor
(
Session
::
getLoggedInUser
());
$response
=
$manager
->
getCampaigns
([
'limit'
=>
$limit
,
'offset'
=>
$offset
,
'guid'
=>
$guid
,
]);
Factory
::
response
([
'campaigns'
=>
$response
,
'load-next'
=>
$response
->
getPagingToken
(),
]);
}
/**
* Equivalent to HTTP POST method
* @param array $pages
* @return mixed|null
*/
public
function
post
(
$pages
)
{
$isEditing
=
false
;
$guid
=
null
;
if
(
$pages
[
0
])
{
$isEditing
=
true
;
$guid
=
$pages
[
0
];
}
$campaign
=
new
Campaign
();
if
(
!
$isEditing
)
{
$campaign
->
setType
(
$_POST
[
'type'
]
??
''
)
->
setEntityGuid
(
$_POST
[
'entity_guid'
]
??
[])
->
setChecksum
(
$_POST
[
'checksum'
]
??
''
);
}
else
{
$campaign
->
setGuid
(
$guid
);
}
$campaign
->
setName
(
trim
(
$_POST
[
'name'
]
??
''
))
->
setStart
((
int
)
(
$_POST
[
'start'
]
??
0
))
->
setEnd
((
int
)
(
$_POST
[
'end'
]
??
0
))
->
setBudget
((
float
)
(
$_POST
[
'budget'
]
??
0
));
/** @var Manager $manager */
$manager
=
Di
::
_
()
->
get
(
'Boost\Network\Manager'
);
$manager
->
setActor
(
Session
::
getLoggedInUser
());
try
{
if
(
!
$isEditing
)
{
$campaign
=
$manager
->
createCampaign
(
$campaign
);
}
else
{
$campaign
=
$manager
->
updateCampaign
(
$campaign
);
}
Factory
::
response
([
'campaign'
=>
$campaign
,
]);
}
catch
(
\Exception
$e
)
{
Factory
::
response
([
'status'
=>
'error'
,
'message'
=>
$e
->
getMessage
(),
]);
}
}
/**
* Equivalent to HTTP PUT method
* @param array $pages
*/
public
function
put
(
$pages
)
{
Factory
::
response
([]);
}
/**
* Equivalent to HTTP DELETE method
* @param array $pages
*/
public
function
delete
(
$pages
)
{
$guid
=
$pages
[
0
]
??
null
;
if
(
is_null
(
$guid
))
{
Factory
::
response
([
'status'
=>
'error'
,
'message'
=>
'Missing Guid'
,
]);
return
;
}
$campaign
=
new
Campaign
();
$campaign
->
setGuid
(
$guid
);
/** @var Manager $manager */
$manager
=
Di
::
_
()
->
get
(
'Boost\Network\Manager'
);
$manager
->
setActor
(
Session
::
getLoggedInUser
());
try
{
$campaign
=
$manager
->
cancelCampaign
(
$campaign
);
Factory
::
response
([
'campaign'
=>
$campaign
,
]);
}
catch
(
\Exception
$e
)
{
Factory
::
response
([
'status'
=>
'error'
,
'message'
=>
$e
->
getMessage
(),
]);
}
}
}
This diff is collapsed.
Core/Boost/Network/ElasticRepository.php
View file @
f9f58fd2
...
...
@@ -199,6 +199,9 @@ class ElasticRepository
->
setImpressionsMet
(
$doc
[
'_source'
][
'impressions_met'
])
->
setBid
(
$doc
[
'_source'
][
'bid'
])
->
setBidType
(
$doc
[
'_source'
][
'bid_type'
]);
if
(
isset
(
$doc
[
'_source'
][
'boost_type'
]))
{
$boost
->
setBoostType
(
$doc
[
'_source'
][
'boost_type'
]);
}
$offset
=
$boost
->
getCreatedTimestamp
();
$response
[]
=
$boost
;
}
...
...
@@ -236,7 +239,7 @@ class ElasticRepository
'owner_guid'
=>
$boost
->
getOwnerGuid
(),
'rating'
=>
$boost
->
getRating
(),
'type'
=>
$boost
->
getType
(),
'
priority'
=>
(
bool
)
$boost
->
isPriority
(),
'
boost_type'
=>
$boost
->
getBoostType
(),
],
'doc_as_upsert'
=>
true
,
];
...
...
This diff is collapsed.
Core/Boost/Network/Manager.php
View file @
f9f58fd2
...
...
@@ -3,11 +3,14 @@
namespace
Minds\Core\Boost\Network
;
use
Minds\Common\Repository\Response
;
use
Minds\Core\Boost\Checksum
;
use
Minds\Core\Boost\Delegates\ValidateCampaignDatesDelegate
;
use
Minds\Core\Di\Di
;
use
Minds\Core\EntitiesBuilder
;
use
Minds\Core\GuidBuilder
;
use
Minds\Core\Events\Dispatcher
;
use
Minds\Entities\Entity
;
use
Minds\Entities\User
;
class
Manager
{
...
...
@@ -265,4 +268,63 @@ class Manager
return
$offchainBoosts
;
}
public
function
getCampaigns
(
array
$opts
)
{
$opts
=
array_merge
(
$opts
,
[
'owner_guid'
=>
$this
->
actor
->
getGUID
(),
'boost_type'
=>
Boost
::
BOOST_TYPE_CAMPAIGN
]);
return
$this
->
elasticRepository
->
getList
(
$opts
);
}
public
function
setActor
(
User
$user
)
:
self
{
$this
->
actor
=
$user
;
return
$this
;
}
public
function
createCampaign
(
Campaign
$campaign
)
:
Campaign
{
$campaign
=
$this
->
campaignUrnDelegate
->
onCreate
(
$campaign
);
$campaign
->
setOwner
(
$this
->
actor
);
if
(
!
$campaign
->
getOwnerGuid
())
{
throw
new
CampaignException
(
'Campaign should have an owner'
);
}
if
(
!
$campaign
->
getName
())
{
throw
new
CampaignException
(
'Campaign should have a name'
);
}
$validTypes
=
[
'newsfeed'
,
'content'
,
'banner'
,
'video'
];
if
(
!
in_array
(
$campaign
->
getType
(),
$validTypes
,
true
))
{
throw
new
CampaignException
(
'Invalid campaign type'
);
}
/** TODO: Checksum Verification */
$checksum
=
(
new
Checksum
())
->
setGuid
(
$campaign
->
getGuid
())
->
setEntity
(
$campaign
->
getEntityGuid
())
->
generate
();
if
(
!
$campaign
->
getChecksum
()
||
(
$campaign
->
getChecksum
()
!==
$checksum
))
{
throw
new
CampaignException
(
'Invalid checksum value'
);
}
$campaign
=
(
new
ValidateCampaignDatesDelegate
())
->
onCreate
(
$campaign
);
$this
->
sync
(
$campaign
);
return
$campaign
;
}
public
function
updateCampaign
(
Campaign
$campaign
)
:
Campaign
{
// TODO: Implement this...
return
$campaign
;
}
public
function
cancelCampaign
(
Campaign
$campaign
)
:
Campaign
{
// TODO: Implement this...
return
$campaign
;
}
}
This diff is collapsed.
Please
register
or
sign in
to comment