in app/lib/publisher/backend.dart [129:213]
Future<api.PublisherInfo> createPublisher(
String publisherId,
api.CreatePublisherRequest body,
) async {
checkPublisherIdParam(publisherId);
final user = await requireAuthenticatedUser();
InvalidInputException.checkMatchPattern(
publisherId,
'publisherId',
_publisherIdRegExp,
);
InvalidInputException.checkStringLength(
publisherId,
'publisherId',
maximum: maxPublisherIdLength, // Some upper limit for sanity.
);
InvalidInputException.checkNotNull(body.accessToken, 'accessToken');
InvalidInputException.checkStringLength(
body.accessToken,
'accessToken',
minimum: 1,
maximum: 4096,
);
await accountBackend.verifyAccessTokenOwnership(body.accessToken, user);
// Verify ownership of domain.
final isOwner = await domainVerifier.verifyDomainOwnership(
publisherId,
body.accessToken,
);
if (!isOwner) {
throw AuthorizationException.userIsNotDomainOwner(publisherId);
}
// Create the publisher
final now = clock.now().toUtc();
await withRetryTransaction(_db, (tx) async {
final key = _db.emptyKey.append(Publisher, id: publisherId);
final p = await tx.lookupOrNull<Publisher>(key);
if (p != null) {
// Check that publisher is the same as what we would create.
if (p.created!.isBefore(now.subtract(Duration(minutes: 10))) ||
p.updated!.isBefore(now.subtract(Duration(minutes: 10))) ||
p.contactEmail != user.email ||
p.description != '' ||
p.websiteUrl != _publisherWebsite(publisherId)) {
throw ConflictException.publisherAlreadyExists(publisherId);
}
// Avoid creating the same publisher again, this end-point is idempotent
// if we just do nothing here.
return;
}
// Create publisher
tx.queueMutations(inserts: [
Publisher()
..parentKey = _db.emptyKey
..id = publisherId
..created = now
..description = ''
..contactEmail = user.email
..updated = now
..websiteUrl = _publisherWebsite(publisherId)
..isAbandoned = false,
PublisherMember()
..parentKey = _db.emptyKey.append(Publisher, id: publisherId)
..id = user.userId
..userId = user.userId
..created = now
..updated = now
..role = PublisherMemberRole.admin,
AuditLogRecord.publisherCreated(
user: user,
publisherId: publisherId,
),
]);
});
await purgeAccountCache(userId: user.userId);
await cache.allPublishersPage().purge();
// Return publisher as it was created
final key = _db.emptyKey.append(Publisher, id: publisherId);
final p = await _db.lookupValue<Publisher>(key);
return _asPublisherInfo(p);
}