Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support ssb-tribes #2

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@
"promisify-4loc": "1.0.0",
"rimraf": "^3.0.2",
"secret-stack": "6.3.1",
"ssb-backlinks": "2.1.1",
"ssb-caps": "1.1.0",
"ssb-config": "3.4.5",
"ssb-db": "20.3.0",
"ssb-keys": "8.0.0",
"ssb-logging": "1.0.0",
"ssb-master": "1.0.3",
"ssb-private1": "1.0.1",
"ssb-query": "2.4.5",
"ssb-tribes": "0.4.1",
"yargs": "^16.1.0"
},
"devDependencies": {
Expand Down
14 changes: 14 additions & 0 deletions src/frequencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ export = {
*/
POST_MENTIONS_FREQUENCY: 0.6,

/**
* Distribution of "private threads" versus "private groups (tribes)".
* Not based on real world data because we (as of 2020-12) don't have
* tribes deployed in production.
*
* Normalized.
*/
PRIVATE_FREQUENCIES: {
direct_message: 0.7,
tribe_creation: 0.05,
tribe_invitation: 0.1,
tribe_message: 0.15,
},

/**
* Distribution of types of mentions in msgs of type 'post'.
* Based on staltz's intuition.
Expand Down
93 changes: 76 additions & 17 deletions src/generate.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pify = require('promisify-4loc');
import crypto = require('crypto');
const ssbKeys = require('ssb-keys');
import {LoremIpsum} from 'lorem-ipsum';
Expand All @@ -20,7 +21,7 @@ import {
somewhatGaussian,
random,
} from './sample';
import {Author, Blocks, Follows, MsgsByType} from './types';
import {Author, Blocks, Follows, MsgsByType, TribesByAuthor} from './types';

let __lorem: any;

Expand Down Expand Up @@ -149,26 +150,83 @@ function generatePostContent(
return content;
}

function generatePrivate(
async function generatePrivate(
ssb: any,
seed: string,
i: number,
latestmsg: number,
msgsByType: MsgsByType,
tribesByAuthors: TribesByAuthor,
author: Author,
authors: Array<Author>,
): string {
const content: Privatable<PostContent> = generatePostContent(
seed,
i,
latestmsg,
msgsByType,
authors,
'private',
);
const recps = generateRecipients(seed, author, authors);
content.recps = recps;
return ssbKeys.box(content, content.recps);
): Promise<Msg | Privatable<Content>> {
const type = sampleCollection(seed, freq.PRIVATE_FREQUENCIES);
if (type === 'tribe_creation' || tribesByAuthors.size === 0) {
const {groupId, groupInitMsg} = await pify<any>(ssb.tribes.create)(null);
const groupIds = tribesByAuthors.get(author.id) ?? new Set();
groupIds.add(groupId);
tribesByAuthors.set(author.id, groupIds);
// console.log(
// `${author.id.slice(0, 6)} created tribe ${groupId.slice(0, 6)}`,
// );
return groupInitMsg as Msg;
} else if (
type === 'tribe_invitation' &&
tribesByAuthors.has(author.id) &&
authors.length > 1
) {
const groupId = uniformSample(
seed,
Array.from(tribesByAuthors.get(author.id)?.keys() ?? []),
);
let inviteeId: FeedId;
do {
inviteeId = paretoSample(seed, authors).id;
} while (inviteeId === author.id);
const text = __lorem.generateWords(randomInt(seed, 1, 9));
const msg = await pify(ssb.tribes.invite)(groupId, [inviteeId], {text});
staltz marked this conversation as resolved.
Show resolved Hide resolved
const groupIds = tribesByAuthors.get(inviteeId) ?? new Set();
groupIds.add(groupId);
tribesByAuthors.set(inviteeId, groupIds);
// console.log(
// `${author.id.slice(0, 6)} invited ${inviteeId.slice(
// 0,
// 6,
// )} to tribe ${groupId.slice(0, 6)}`,
// );
return msg as Msg;
} else if (type === 'tribe_message' && tribesByAuthors.has(author.id)) {
const content: Privatable<PostContent> = generatePostContent(
seed,
i,
latestmsg,
msgsByType,
authors,
'private',
);
const groupId = uniformSample(
seed,
Array.from(tribesByAuthors.get(author.id)?.keys() ?? []),
);
const recps = [groupId, author.id];
staltz marked this conversation as resolved.
Show resolved Hide resolved
content.recps = recps;
// console.log(
// `${author.id.slice(0, 6)} posted to tribe ${groupId.slice(0, 6)}`,
// );
return content;
} else {
const content: Privatable<PostContent> = generatePostContent(
seed,
i,
latestmsg,
msgsByType,
authors,
'private',
);
const recps = generateRecipients(seed, author, authors);
staltz marked this conversation as resolved.
Show resolved Hide resolved
content.recps = recps;
return content;
}
}

function generateVoteContent(
Expand Down Expand Up @@ -288,17 +346,18 @@ function generateAboutContent(
return content;
}

export async function generateMsgContent(
export async function generateMsgOrContent(
ssb: any,
seed: string,
i: number,
latestmsg: number,
author: Author,
msgsByType: MsgsByType,
authors: Array<Author>,
tribes: TribesByAuthor,
follows: Follows,
blocks: Blocks,
): Promise<Content | string> {
): Promise<Msg | Content> {
__lorem = new LoremIpsum({
random: random,
sentencesPerParagraph: {
Expand All @@ -323,7 +382,7 @@ export async function generateMsgContent(
return generateAboutContent(seed, author, authors);
} else if (type === 'private') {
const [a, as] = [author, authors]; // sorry Prettier, i want a one-liner
return generatePrivate(ssb, seed, i, latestmsg, msgsByType, a, as);
return generatePrivate(ssb, seed, i, latestmsg, msgsByType, tribes, a, as);
} else if (type === 'post') {
return generatePostContent(seed, i, latestmsg, msgsByType, authors);
} else {
Expand Down
14 changes: 10 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import pify = require('promisify-4loc');
import {ContactContent, Msg} from 'ssb-typescript';
import {makeSSB} from './ssb';
import {generateAuthors, generateMsgContent} from './generate';
import {Opts, MsgsByType, Follows, Blocks} from './types';
import {generateAuthors, generateMsgOrContent} from './generate';
import {Opts, MsgsByType, Follows, Blocks, TribesByAuthor} from './types';
import {paretoSample} from './sample';
import slimify from './slimify';
import writeReportFile from './report';
Expand Down Expand Up @@ -33,6 +33,7 @@ export = async function generateFixture(opts?: Partial<Opts>) {
const msgs: Array<Msg> = [];
const msgsByType: MsgsByType = {};
const authors = authorsKeys.map((keys) => ssb.createFeed(keys));
const tribesByAuthor: TribesByAuthor = new Map();

const follows: Follows = new Map(authors.map((a) => [a.id, new Set()]));
const blocks: Blocks = new Map(authors.map((a) => [a.id, new Set()]));
Expand All @@ -55,18 +56,23 @@ export = async function generateFixture(opts?: Partial<Opts>) {
let author = paretoSample(seed, authors);
// OLDESTMSG and LATESTMSG are always authored by database owner
if (i === 0 || i === latestmsg) author = authors[0];
const content = await generateMsgContent(
const msgOrContent = await generateMsgOrContent(
ssb,
seed,
i,
latestmsg,
author,
msgsByType,
authors,
tribesByAuthor,
follows,
blocks,
);
const posted: Msg = await pify<any>(author.add)(content);
const maybeMsg = msgOrContent as Partial<Msg>;
const posted: Msg =
maybeMsg?.key && maybeMsg?.timestamp && maybeMsg?.value
? maybeMsg!
: await pify<any>(author.add)(msgOrContent);

if (posted?.value.content) {
msgs.push(posted);
Expand Down
4 changes: 4 additions & 0 deletions src/ssb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export function makeSSB(authorsKeys: Array<any>, outputDir: string): any {
.use(require('ssb-master'))
.use(require('ssb-logging'))
.use(require('ssb-db'))
.use(require('ssb-backlinks'))
.use(require('ssb-query'))
.use(require('ssb-tribes'))
.use(require('ssb-private1'))
.call(
null,
makeConfig('ssb', {
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export type Opts = {
verbose: boolean;
};

export type GroupIp = string;

export type MsgType = keyof typeof freq.MSG_TYPE_FREQUENCIES;

export type MsgsByType = Partial<Record<MsgType, Array<Msg>>>;
Expand All @@ -21,5 +23,6 @@ export type Author = {
id: FeedId;
};

export type TribesByAuthor = Map<FeedId, Set<GroupIp>>;
export type Follows = Map<FeedId, Set<FeedId>>;
export type Blocks = Map<FeedId, Set<FeedId>>;
21 changes: 20 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,29 @@ test('can generate about msgs', (t) => {
);
});

test('latestmsg and same seed can continue generating', (t) => {
test('can generate tribes', (t) => {
generateAndTest(
{
outputDir: 'ssb-fixtures-test-4',
seed: 'dog8',
messages: 5,
authors: 3,
},
(err, msgs, cleanup) => {
t.error(err, 'no error');
t.equals(typeof msgs[3].value.content, 'string', '4th msg is encrypted');
t.true(msgs[3].value.content.endsWith('.box2'), 'encrypted with box2');
cleanup(() => {
t.end();
});
},
);
});

test('latestmsg and same seed can continue generating', (t) => {
generateAndTest(
{
outputDir: 'ssb-fixtures-test-5',
seed: 'deterministic',
messages: 5,
authors: 2,
Expand Down