Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: follow/unfollow logic and receipt
- Loading branch information
Showing
7 changed files
with
154 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
'use strict'; | ||
|
||
const db = require('../database'); | ||
const user = require('../user'); | ||
|
||
const helpers = require('./helpers'); | ||
|
||
const inbox = module.exports; | ||
|
||
inbox.follow = async (actorId, objectId) => { | ||
await handleFollow('follow', actorId, objectId); | ||
}; | ||
|
||
inbox.unfollow = async (actorId, objectId) => { | ||
await handleFollow('unfollow', actorId, objectId); | ||
}; | ||
|
||
inbox.isFollowed = async (actorId, uid) => { | ||
if (actorId.indexOf('@') === -1 || parseInt(uid, 10) <= 0) { | ||
return false; | ||
} | ||
return await db.isSortedSetMember(`followersRemote:${uid}`, actorId); | ||
}; | ||
|
||
async function handleFollow(type, 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); | ||
if (!localUid) { | ||
throw new Error('[[error:invalid-uid]]'); | ||
} | ||
|
||
// matches toggleFollow() in src/user/follow.js | ||
const isFollowed = await inbox.isFollowed(actorId, localUid); | ||
if (type === 'follow') { | ||
if (isFollowed) { | ||
throw new Error('[[error:already-following]]'); | ||
} | ||
const now = Date.now(); | ||
await db.sortedSetAdd(`followersRemote:${localUid}`, now, actorId); | ||
} else { | ||
if (!isFollowed) { | ||
throw new Error('[[error:not-following]]'); | ||
} | ||
await db.sortedSetRemove(`followersRemote:${localUid}`, actorId); | ||
} | ||
|
||
const [followerCount, followerRemoteCount] = await Promise.all([ | ||
db.sortedSetCard(`followers:${localUid}`), | ||
db.sortedSetCard(`followersRemote:${localUid}`), | ||
]); | ||
await user.setUserField(localUid, 'followerCount', followerCount + followerRemoteCount); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use strict'; | ||
|
||
const db = require('../database'); | ||
|
||
const outbox = module.exports; | ||
|
||
outbox.isFollowing = async (uid, actorId) => { | ||
if (parseInt(uid, 10) <= 0 || actorId.indexOf('@') === -1) { | ||
return false; | ||
} | ||
return await db.isSortedSetMember(`followingRemote:${uid}`, actorId); | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Might be better to store followerRemoteCount separately, so that the count isn't wrong when federation is disabled.
We can display
followerCount + followerRemoteCount
if it's enabled.