seed.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * Railway: demo content for a fresh install, so production and previews are not empty.
  7. * Runs from entrypoint.sh when UNA_DEMO_SEED=1. Idempotent: skips everything once the
  8. * first demo account exists. Content goes through module services (not raw SQL), so
  9. * timeline events, counters and notifications are created the same way as by users.
  10. *
  11. * Environment:
  12. * UNA_DEMO_PASSWORD password for the demo accounts (random if unset)
  13. */
  14. ob_start(); // bx_login() sets cookies; nothing to send them to in CLI
  15. $_SERVER['HTTP_HOST'] = getenv('RAILWAY_PUBLIC_DOMAIN') ?: 'localhost';
  16. $_SERVER['REQUEST_URI'] = '/';
  17. $_SERVER['SERVER_NAME'] = $_SERVER['HTTP_HOST'];
  18. $GLOBALS['bx_profiler_disable'] = true;
  19. require_once('/var/www/html/inc/header.inc.php');
  20. function seedLog(string $s): void
  21. {
  22. fwrite(STDERR, "[seed] $s\n");
  23. }
  24. $aUsers = [
  25. ['name' => 'Lily Carter', 'email' => 'lily@example.com'],
  26. ['name' => 'Eric Novak', 'email' => 'eric@example.com'],
  27. ['name' => 'Mila Santos', 'email' => 'mila@example.com'],
  28. ['name' => 'Tom Walsh', 'email' => 'tom@example.com'],
  29. ['name' => 'Ella Kim', 'email' => 'ella@example.com'],
  30. ];
  31. if (BxDolAccount::getInstance($aUsers[0]['email'])) {
  32. seedLog('demo content already present, skipping');
  33. exit(0);
  34. }
  35. $sPassword = getenv('UNA_DEMO_PASSWORD') ?: bin2hex(random_bytes(12));
  36. $oAccountForms = bx_instance('BxTemplAccountForms');
  37. // --- people: account + person profile each
  38. $aProfiles = []; // [account_id, person profile id]
  39. foreach ($aUsers as $aUser) {
  40. $a = $oAccountForms->createAccount([
  41. 'name' => $aUser['name'],
  42. 'email' => $aUser['email'],
  43. 'password' => $sPassword,
  44. 'email_confirmed' => 1,
  45. ], BX_PROFILE_ACTION_AUTO, false);
  46. if (empty($a['account_id'])) {
  47. seedLog("account {$aUser['email']} failed: " . ($a['error'] ?? 'unknown'));
  48. continue;
  49. }
  50. $aFields = bx_srv('bx_persons', 'prepare_fields', [['author' => $a['profile_id'], 'name' => $aUser['name']]]);
  51. $aFields['allow_view_to'] = BX_DOL_PG_ALL;
  52. $r = bx_srv('bx_persons', 'entity_add_forcedly', [$a['profile_id'], $aFields]);
  53. if (empty($r['content']['profile_id'])) {
  54. seedLog("person {$aUser['name']} failed: " . ($r['message'] ?? 'unknown'));
  55. continue;
  56. }
  57. $aProfiles[] = [$a['account_id'], (int)$r['content']['profile_id']];
  58. seedLog("person {$aUser['name']}");
  59. }
  60. if (!$aProfiles) {
  61. seedLog('no profiles created, stopping');
  62. exit(1);
  63. }
  64. /** Act as a demo user, so ACL checks and authorship match a real post. */
  65. function seedAs(array $aProfile): int
  66. {
  67. bx_login($aProfile[0], false);
  68. return $aProfile[1];
  69. }
  70. // --- a public group (when the package has the Groups module)
  71. $iGroupProfileId = 0;
  72. $iOwner = seedAs($aProfiles[0]);
  73. $r = !BxDolModuleQuery::getInstance()->isEnabledByName('bx_groups') ? ['message' => 'bx_groups is not installed'] : bx_srv('bx_groups', 'entity_add_forcedly', [$iOwner, [
  74. 'group_name' => 'UNA Community',
  75. 'group_desc' => 'Introduce yourself, share what you are building with UNA and NEO, and ask questions.',
  76. 'allow_view_to' => BX_DOL_PG_ALL,
  77. ]]);
  78. if (!empty($r['content']['profile_id'])) {
  79. $iGroupProfileId = (int)$r['content']['profile_id'];
  80. seedLog('group UNA Community');
  81. }
  82. else
  83. seedLog('group skipped: ' . ($r['message'] ?? 'unknown'));
  84. // --- posts, each with a couple of comments from other people
  85. $aPosts = [
  86. ['Hello from the demo site', "This site is a fresh UNA install deployed from the master branch. Everything here is sample content."],
  87. ['What NEO is', "NEO is the universal UNA client: one React Native codebase for web, iOS and Android, talking to UNA through its API."],
  88. ['Preview environments', "Every pull request to UNA gets its own backend and client on unacms.app, created by CI and removed when the PR closes."],
  89. ['Weekend reading', "A few articles about building communities that stay useful: keep threads short, answer quickly, and welcome new members by name."],
  90. ['Photo walk this Saturday', "Meeting at the old harbour at 10:00. Bring any camera; phones are fine. Coffee afterwards."],
  91. ['Tips for a good profile', "Add a picture, write two sentences about what you do, and follow a few groups. That's enough to get a useful feed."],
  92. ];
  93. $aComments = [
  94. 'Great to see this running.',
  95. 'Thanks for sharing!',
  96. 'Count me in.',
  97. 'Very helpful, bookmarking this.',
  98. ];
  99. foreach ($aPosts as $i => [$sTitle, $sText]) {
  100. $aAuthor = $aProfiles[$i % count($aProfiles)];
  101. $iAuthor = seedAs($aAuthor);
  102. $r = bx_srv('bx_posts', 'entity_add_forcedly', [$iAuthor, [
  103. 'title' => $sTitle,
  104. 'text' => '<p>' . $sText . '</p>',
  105. 'allow_view_to' => BX_DOL_PG_ALL,
  106. ]]);
  107. if (empty($r['content']['id'])) {
  108. seedLog("post '$sTitle' failed: " . ($r['message'] ?? 'unknown'));
  109. continue;
  110. }
  111. seedLog("post '$sTitle'");
  112. if (!($oCmts = BxDolCmts::getObjectInstance('bx_posts', (int)$r['content']['id'])))
  113. continue;
  114. for ($j = 1; $j <= 2; $j++) {
  115. $aCommenter = $aProfiles[($i + $j) % count($aProfiles)];
  116. $oCmts->add([
  117. 'cmt_author_id' => seedAs($aCommenter),
  118. 'cmt_parent_id' => 0,
  119. 'cmt_text' => $aComments[($i + $j) % count($aComments)],
  120. ]);
  121. }
  122. }
  123. seedLog('done: ' . count($aProfiles) . ' people, ' . ($iGroupProfileId ? 1 : 0) . ' group, ' . count($aPosts) . ' posts');
  124. ob_end_clean();