Skip to content

Commit

Permalink
feat: started work on local follow logic
Browse files Browse the repository at this point in the history
  • Loading branch information
julianlam committed Jul 11, 2023
1 parent 239a1d7 commit c6e8eb7
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/activitypub/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
const request = require('request-promise-native');
const { generateKeyPairSync } = require('crypto');
const winston = require('winston');
const nconf = require('nconf');

const db = require('../database');
const ttl = require('../cache/ttl');
const user = require('../user');

const webfingerCache = ttl({ ttl: 1000 * 60 * 60 * 24 }); // 24 hours

Expand Down Expand Up @@ -65,3 +67,13 @@ Helpers.generateKeys = async (uid) => {
await db.setObject(`uid:${uid}:keys`, { publicKey, privateKey });
return { publicKey, privateKey };
};

Helpers.resolveLocalUid = async (id) => {
const [slug, host] = id.split('@');

if (id.indexOf('@') === -1 || host !== nconf.get('url_parsed').host) {
throw new Error('[[activitypub:invalid-id]]');
}

return await user.getUidByUserslug(slug);
};
1 change: 1 addition & 0 deletions src/activitypub/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const actorCache = ttl({ ttl: 1000 * 60 * 60 * 24 }); // 24 hours
const ActivityPub = module.exports;

ActivityPub.helpers = require('./helpers');
ActivityPub.local = require('./local');

ActivityPub.getActor = async (id) => {
if (actorCache.has(id)) {
Expand Down
45 changes: 45 additions & 0 deletions src/activitypub/local.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';

const db = require('../database');

Check failure on line 3 in src/activitypub/local.js

View workflow job for this annotation

GitHub Actions / Lint and test (ubuntu-latest, 16, mongo-dev)

'db' is assigned a value but never used
const user = require('../user');

Check failure on line 4 in src/activitypub/local.js

View workflow job for this annotation

GitHub Actions / Lint and test (ubuntu-latest, 16, mongo-dev)

'user' is assigned a value but never used

const helpers = require('./helpers');

const local = module.exports;

local.follow = async (actorId, objectId) => {
// Sanity checks
const actorExists = await helpers.query(actorId);
if (!actorId || !actorExists) {
throw new Error('[[error:invalid-uid]]'); // should probably be AP specific
}

if (!objectId) {
throw new Error('[[error:invalid-uid]]'); // should probably be AP specific
}
const localUid = await helpers.resolveLocalUid(objectId);

console.log(actorId, localUid);

// The below logic matches toggleFollow() in src/user/follow.js
// const isFollowing = await user.isFollowing(actorId, localUid);
// if (isFollowing) {
// throw new Error('[[error:already-following]]');

Check failure on line 27 in src/activitypub/local.js

View workflow job for this annotation

GitHub Actions / Lint and test (ubuntu-latest, 16, mongo-dev)

Unexpected tab character
// }
// const now = Date.now();
// await Promise.all([
// db.sortedSetAddBulk([

Check failure on line 31 in src/activitypub/local.js

View workflow job for this annotation

GitHub Actions / Lint and test (ubuntu-latest, 16, mongo-dev)

Unexpected tab character
// [`following:${actorId}`, now, localUid],

Check failure on line 32 in src/activitypub/local.js

View workflow job for this annotation

GitHub Actions / Lint and test (ubuntu-latest, 16, mongo-dev)

Unexpected tab character
// [`followers:${localUid}`, now, actorId],

Check failure on line 33 in src/activitypub/local.js

View workflow job for this annotation

GitHub Actions / Lint and test (ubuntu-latest, 16, mongo-dev)

Unexpected tab character
// ]),

Check failure on line 34 in src/activitypub/local.js

View workflow job for this annotation

GitHub Actions / Lint and test (ubuntu-latest, 16, mongo-dev)

Unexpected tab character
// ]);

// const [followingCount, followerCount] = await Promise.all([
// db.sortedSetCard(`following:${actorId}`),

Check failure on line 38 in src/activitypub/local.js

View workflow job for this annotation

GitHub Actions / Lint and test (ubuntu-latest, 16, mongo-dev)

Unexpected tab character
// db.sortedSetCard(`followers:${localUid}`),

Check failure on line 39 in src/activitypub/local.js

View workflow job for this annotation

GitHub Actions / Lint and test (ubuntu-latest, 16, mongo-dev)

Unexpected tab character
// ]);
// await Promise.all([
// user.setUserField(actorId, 'followingCount', followingCount),

Check failure on line 42 in src/activitypub/local.js

View workflow job for this annotation

GitHub Actions / Lint and test (ubuntu-latest, 16, mongo-dev)

Unexpected tab character
// user.setUserField(localUid, 'followerCount', followerCount),
// ]);
};
6 changes: 6 additions & 0 deletions src/controllers/activitypub/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ Controller.getInbox = async (req, res) => {
Controller.postInbox = async (req, res) => {
console.log('received', req.body);

switch (req.body.type) {
case 'Follow': {
await activitypub.local.follow(req.body.actor.name, req.body.object.name);
}
}

res.sendStatus(201);
};

Expand Down

0 comments on commit c6e8eb7

Please sign in to comment.