Re Writting Event Processing

This commit is contained in:
Luis Fernando Molina committed 2021-08-29 11:45:30 +02:00
1 parent 4aa11efddf
commit 648f699b56
3 files changed
+267 -215

No files matched your search

File renamed without changes.
+255 -201
View File
@@ -1,24 +1,37 @@
exports.newEvent = function newEvent() {
/*
An Event is anything that happens system wide that
needs to be delivered to relevant entities via
the Superalgos Network.
An Event (in contrast to a Query) produces a change
on the state of the social graph.
*/
let thisObject = {
/* Unique Key */
eventId: undefined,
/* Referenced Entities Unique Keys */
emitterUserProfileId: undefined,
targetUserProfileId: undefined,
emitterBotProfileId: undefined,
targetBotProfileId: undefined,
emitterPostHash: undefined,
targetPostHash: undefined,
/* Event Unitque Properties */
eventType: undefined,
timestamp: undefined,
botId: undefined,
/* Bot Related Properties */
botProfileId: undefined,
botAsset: undefined,
botExchange: undefined,
botEnabled: undefined,
/* Framework Functions */
initialize: initialize,
finalize: finalize
}
const EVENT_TYPES = {
/* Users Posts and Following Events */
NEW_USER_POST: 10,
REPLY_TO_USER_POST: 11,
REPOST_USER_POST: 12,
@@ -26,7 +39,7 @@ exports.newEvent = function newEvent() {
REMOVE_USER_POST: 14,
FOLLOW_USER_PROFILE: 15,
UNFOLLOW_USER_PROFILE: 16,
/* Bots Posts and Following Events */
NEW_BOT_POST: 20,
REPLY_TO_BOT_POST: 21,
REPOST_BOT_POST: 22,
@@ -34,34 +47,33 @@ exports.newEvent = function newEvent() {
REMOVE_BOT_POST: 24,
FOLLOW_BOT_PROFILE: 25,
UNFOLLOW_BOT_PROFILE: 26,
/* Bots Management Events */
ADD_BOT: 50,
REMOVE_BOT: 51,
ENABLE_BOT: 52,
DISABLE_BOT: 53,
/* Add Reaction to Posts Events */
ADD_REACTION_LIKE: 100,
ADD_REACTION_LOVE: 101,
ADD_REACTION_HAHA: 102,
ADD_REACTION_WOW: 103,
ADD_REACTION_SAD: 104,
ADD_REACTION_ANGRY: 105,
ADD_REACTION_HUG: 106,
ADD_REACTION_CARE: 106,
/* Remove Reaction to Posts Events */
REMOVE_REACTION_LIKE: 200,
REMOVE_REACTION_LOVE: 201,
REMOVE_REACTION_HAHA: 202,
REMOVE_REACTION_WOW: 203,
REMOVE_REACTION_SAD: 204,
REMOVE_REACTION_ANGRY: 205,
REMOVE_REACTION_HUG: 206
REMOVE_REACTION_CARE: 206
}
return thisObject
function finalize() {
emitterUserProfile = undefined
targetUserProfile = undefined
}
function initialize(eventReceived) {
@@ -75,250 +87,292 @@ exports.newEvent = function newEvent() {
thisObject.targetBotProfileId = eventReceived.targetBotProfileId
thisObject.emitterPostHash = eventReceived.emitterPostHash
thisObject.targetPostHash = eventReceived.targetPostHash
thisObject.eventType = eventReceived.eventType
thisObject.timestamp = eventReceived.timestamp
thisObject.botId = eventReceived.botId
thisObject.botProfileId = eventReceived.botProfileId
thisObject.botAsset = eventReceived.botAsset
thisObject.botExchange = eventReceived.botExchange
thisObject.botEnabled = eventReceived.botEnabled
/*
Local Variables
*/
let emitterUserProfile
let targetUserProfile
/*
Validate Emitter User Profile.
*/
let emitterUserProfile = NT.memory.maps.USER_PROFILES_BY_ID.get(thisObject.emitterUserProfileId)
if (emitterUserProfileId === undefined) {
throw ('Emitter User Profile Id Not Provided.')
}
emitterUserProfile = NT.memory.maps.USER_PROFILES_BY_ID.get(thisObject.emitterUserProfileId)
if (emitterUserProfile === undefined) {
throw ('Emitter User Profile Not Found.')
}
emitterUserProfile.emitterEventsCount++
/*
Validate Target User Profile.
*/
let targetUserProfile = NT.memory.maps.USER_PROFILES_BY_ID.get(thisObject.targetUserProfileId)
if (targetUserProfile === undefined) {
/* We thow an exception when it does not have a target user profile and is required*/
if (
thisObject.eventType !== EVENT_TYPES.NEW_USER_POST &&
thisObject.eventType !== EVENT_TYPES.NEW_BOT_POST
) {
if (thisObject.targetUserProfileId !== undefined) {
targetUserProfile = NT.memory.maps.USER_PROFILES_BY_ID.get(thisObject.targetUserProfileId)
if (targetUserProfile === undefined) {
throw ('Target User Profile Not Found.')
}
} else {
targetUserProfile.targetEventsCount++
}
/*
Validate Emitter Bot Profile.
*/
if (emitterBotProfileId !== undefined) {
let botProfile = emitterUserProfile.bots.get(emitterBotProfileId)
if (
botProfile === undefined
) {
throw ('Emitter BOt Profile Not Found.')
if (botProfile === undefined) {
throw ('Emitter Bot Profile Not Found.')
}
}
/*
Is is related to Posting?
*/
if (
thisObject.eventType === EVENT_TYPES.NEW_USER_POST ||
thisObject.eventType === EVENT_TYPES.REPLY_TO_USER_POST ||
thisObject.eventType === EVENT_TYPES.REPOST_USER_POST ||
thisObject.eventType === EVENT_TYPES.QUOTE_REPOST_USER_POST ||
thisObject.eventType === EVENT_TYPES.NEW_BOT_POST ||
thisObject.eventType === EVENT_TYPES.REPLY_TO_BOT_POST ||
thisObject.eventType === EVENT_TYPES.REPOST_BOT_POST ||
thisObject.eventType === EVENT_TYPES.QUOTE_REPOST_BOT_POST
) {
postingEvents()
followingEvents()
reactionEvents()
botEvents()
eventCounters()
function postingEvents() {
/*
New User Post
Is is related to Posting?
*/
if (
thisObject.eventType === EVENT_TYPES.NEW_USER_POST ||
thisObject.eventType === EVENT_TYPES.REPLY_TO_USER_POST ||
thisObject.eventType === EVENT_TYPES.REPOST_USER_POST ||
thisObject.eventType === EVENT_TYPES.QUOTE_REPOST_USER_POST
) {
emitterUserProfile.addPost(
thisObject.emitterUserProfileId,
thisObject.targetUserProfileId,
thisObject.emitterBotProfileId,
thisObject.targetBotProfileId,
thisObject.emitterPostHash,
thisObject.targetPostHash,
thisObject.eventType - 10,
thisObject.timestamp
)
return
}
/*
New Bot Post
*/
if (
thisObject.eventType === EVENT_TYPES.QUOTE_REPOST_USER_POST ||
thisObject.eventType === EVENT_TYPES.NEW_BOT_POST ||
thisObject.eventType === EVENT_TYPES.REPLY_TO_BOT_POST ||
thisObject.eventType === EVENT_TYPES.REPOST_BOT_POST ||
thisObject.eventType === EVENT_TYPES.QUOTE_REPOST_BOT_POST
) {
emitterBotProfile.addPost(
thisObject.emitterUserProfileId,
thisObject.targetUserProfileId,
thisObject.emitterBotProfileId,
thisObject.targetBotProfileId,
thisObject.emitterPostHash,
thisObject.targetPostHash,
thisObject.eventType - 20,
thisObject.timestamp
)
return
/*
New User Post
*/
if (
thisObject.eventType === EVENT_TYPES.NEW_USER_POST ||
thisObject.eventType === EVENT_TYPES.REPLY_TO_USER_POST ||
thisObject.eventType === EVENT_TYPES.REPOST_USER_POST ||
thisObject.eventType === EVENT_TYPES.QUOTE_REPOST_USER_POST
) {
emitterUserProfile.addPost(
thisObject.emitterUserProfileId,
thisObject.targetUserProfileId,
thisObject.emitterBotProfileId,
thisObject.targetBotProfileId,
thisObject.emitterPostHash,
thisObject.targetPostHash,
thisObject.eventType - 10,
thisObject.timestamp
)
return
}
/*
New Bot Post
*/
if (
thisObject.eventType === EVENT_TYPES.NEW_BOT_POST ||
thisObject.eventType === EVENT_TYPES.REPLY_TO_BOT_POST ||
thisObject.eventType === EVENT_TYPES.REPOST_BOT_POST ||
thisObject.eventType === EVENT_TYPES.QUOTE_REPOST_BOT_POST
) {
if (emitterBotProfile === undefined) {
throw ('Emitter Bot Profile Id Not Provided.')
}
emitterBotProfile.addPost(
thisObject.emitterUserProfileId,
thisObject.targetUserProfileId,
thisObject.emitterBotProfileId,
thisObject.targetBotProfileId,
thisObject.emitterPostHash,
thisObject.targetPostHash,
thisObject.eventType - 20,
thisObject.timestamp
)
return
}
/*
Remove User Post
*/
if (
thisObject.eventType === EVENT_TYPES.REMOVE_USER_POST
) {
emitterUserProfile.removePost(
thisObject.emitterPostHash
)
return
}
/*
Remove Bot Post
*/
if (
thisObject.eventType === EVENT_TYPES.REMOVE_BOT_POST
) {
if (emitterBotProfile === undefined) {
throw ('Emitter Bot Profile Id Not Provided.')
}
emitterBotProfile.removePost(
thisObject.emitterPostHash
)
return
}
}
}
function followingEvents() {
/*
Remove User Post
Is is a following?
*/
if (
thisObject.eventType === EVENT_TYPES.REMOVE_USER_POST
thisObject.eventType === EVENT_TYPES.FOLLOW_USER_PROFILE ||
thisObject.eventType === EVENT_TYPES.UNFOLLOW_USER_PROFILE ||
thisObject.eventType === EVENT_TYPES.FOLLOW_BOT_PROFILE ||
thisObject.eventType === EVENT_TYPES.UNFOLLOW_BOT_PROFILE
) {
emitterUserProfile.removePost(
thisObject.emitterPostHash
)
switch (thisObject.eventType) {
case EVENT_TYPES.FOLLOW_USER_PROFILE: {
if (thisObject.emitterUserProfileId === undefined) {
throw ('Emitter User Profile Id Not Provided.')
}
if (thisObject.targetUserProfileId === undefined) {
throw ('Target User Profile Id Not Provided.')
}
emitterUserProfile.addFollowing(
targetUserProfileId
)
targetUserProfile.addFollower(
emitterUserProfileId
)
break
}
case EVENT_TYPES.UNFOLLOW_USER_PROFILE: {
emitterUserProfile.removeFollowing(
targetUserProfileId
)
targetUserProfile.removeFollower(
emitterUserProfileId
)
break
}
case EVENT_TYPES.FOLLOW_BOT_PROFILE: {
emitterUserProfile.addTradePostsFollowing(
targetBotProfileId
)
targetUserProfile.addTradePostsFollower(
emitterUserProfileId
)
break
}
case EVENT_TYPES.UNFOLLOW_BOT_PROFILE: {
emitterUserProfile.removeTradePostsFollowing(
targetBotProfileId
)
targetUserProfile.removeTradePostsFollower(
emitterBotProfileId
)
break
}
}
return
}
}
function reactionEvents() {
/*
Remove Bot Post
Is is a Reaction?
*/
if (
thisObject.eventType === EVENT_TYPES.REMOVE_USER_POST
thisObject.eventType === EVENT_TYPES.ADD_REACTION_LIKE ||
thisObject.eventType === EVENT_TYPES.ADD_REACTION_LOVE ||
thisObject.eventType === EVENT_TYPES.ADD_REACTION_HAHA ||
thisObject.eventType === EVENT_TYPES.ADD_REACTION_WOW ||
thisObject.eventType === EVENT_TYPES.ADD_REACTION_SAD ||
thisObject.eventType === EVENT_TYPES.ADD_REACTION_ANGRY ||
thisObject.eventType === EVENT_TYPES.ADD_REACTION_CARE
) {
emitterBotProfile.removePost(
thisObject.emitterPostHash
)
let targetPost = NT.memory.maps.POSTS.get(thisObject.targetPostHash)
if (targetPost === undefined) {
throw ('Target Post Not Found')
}
targetPost.addReaction(thisObject.eventType - 100)
return
}
if (
thisObject.eventType === EVENT_TYPES.REMOVE_REACTION_LIKE ||
thisObject.eventType === EVENT_TYPES.REMOVE_REACTION_LOVE ||
thisObject.eventType === EVENT_TYPES.REMOVE_REACTION_HAHA ||
thisObject.eventType === EVENT_TYPES.REMOVE_REACTION_WOW ||
thisObject.eventType === EVENT_TYPES.REMOVE_REACTION_SAD ||
thisObject.eventType === EVENT_TYPES.REMOVE_REACTION_ANGRY ||
thisObject.eventType === EVENT_TYPES.REMOVE_REACTION_CARE
) {
let targetPost = NT.memory.maps.POSTS.get(thisObject.targetPostHash)
if (targetPost === undefined) {
throw ('Target Post Not Found')
}
targetPost.removeReaction(thisObject.eventType - 200)
return
}
}
/*
Is is a following?
*/
if (
thisObject.eventType === EVENT_TYPES.FOLLOW_USER_PROFILE ||
thisObject.eventType === EVENT_TYPES.UNFOLLOW_USER_PROFILE ||
thisObject.eventType === EVENT_TYPES.FOLLOW_BOT_PROFILE ||
thisObject.eventType === EVENT_TYPES.UNFOLLOW_BOT_PROFILE
) {
switch (thisObject.eventType) {
case EVENT_TYPES.FOLLOW_USER_PROFILE: {
emitterUserProfile.addFollowing(
targetUserProfileId
)
targetUserProfile.addFollower(
emitterUserProfileId
)
break
}
case EVENT_TYPES.UNFOLLOW_USER_PROFILE: {
emitterUserProfile.removeFollowing(
targetUserProfileId
)
targetUserProfile.removeFollower(
emitterUserProfileId
)
break
}
case EVENT_TYPES.FOLLOW_BOT_PROFILE: {
emitterUserProfile.addTradePostsFollowing(
targetBotProfileId
)
targetUserProfile.addTradePostsFollower(
emitterUserProfileId
)
break
}
case EVENT_TYPES.UNFOLLOW_BOT_PROFILE: {
emitterUserProfile.removeTradePostsFollowing(
targetBotProfileId
)
targetUserProfile.removeTradePostsFollower(
emitterBotProfileId
)
break
function botEvents() {
/*
Is is a Bot?
*/
if (
thisObject.eventType === EVENT_TYPES.ADD_BOT ||
thisObject.eventType === EVENT_TYPES.REMOVE_BOT ||
thisObject.eventType === EVENT_TYPES.ENABLE_BOT ||
thisObject.eventType === EVENT_TYPES.DISABLE_BOT
) {
switch (thisObject.eventType) {
case EVENT_TYPES.ADD_BOT: {
emitterUserProfile.addBot(
thisObject.botProfileId,
thisObject.botAsset,
thisObject.botExchange,
thisObject.botEnabled
)
break
}
case EVENT_TYPES.REMOVE_BOT: {
emitterUserProfile.removeBot(
thisObject.botProfileId
)
break
}
case EVENT_TYPES.ENABLE_BOT: {
emitterUserProfile.enableBot(
thisObject.botProfileId
)
break
}
case EVENT_TYPES.DISABLE_BOT: {
emitterUserProfile.disableBot(
thisObject.botProfileId
)
break
}
}
return
}
return
}
/*
Is is a Reaction?
*/
if (
thisObject.eventType === EVENT_TYPES.ADD_REACTION_LIKE ||
thisObject.eventType === EVENT_TYPES.ADD_REACTION_LOVE ||
thisObject.eventType === EVENT_TYPES.ADD_REACTION_HAHA ||
thisObject.eventType === EVENT_TYPES.ADD_REACTION_WOW ||
thisObject.eventType === EVENT_TYPES.ADD_REACTION_SAD ||
thisObject.eventType === EVENT_TYPES.ADD_REACTION_ANGRY ||
thisObject.eventType === EVENT_TYPES.ADD_REACTION_HUG
) {
let targetPost = NT.memory.maps.POSTS.get(thisObject.targetPostHash)
if (targetPost === undefined) {
throw ('Target Post Not Found')
function eventCounters() {
emitterUserProfile.emitterEventsCount++
if (targetUserProfile !== undefined) {
targetUserProfile.targetEventsCount++
}
targetPost.addReaction(thisObject.eventType - 100)
return
}
if (
thisObject.eventType === EVENT_TYPES.REMOVE_REACTION_LIKE ||
thisObject.eventType === EVENT_TYPES.REMOVE_REACTION_LOVE ||
thisObject.eventType === EVENT_TYPES.REMOVE_REACTION_HAHA ||
thisObject.eventType === EVENT_TYPES.REMOVE_REACTION_WOW ||
thisObject.eventType === EVENT_TYPES.REMOVE_REACTION_SAD ||
thisObject.eventType === EVENT_TYPES.REMOVE_REACTION_ANGRY ||
thisObject.eventType === EVENT_TYPES.REMOVE_REACTION_HUG
) {
let targetPost = NT.memory.maps.POSTS.get(thisObject.targetPostHash)
if (targetPost === undefined) {
throw ('Target Post Not Found')
}
targetPost.removeReaction(thisObject.eventType - 200)
return
}
/*
Is is a Bot?
*/
if (
thisObject.eventType === EVENT_TYPES.ADD_BOT ||
thisObject.eventType === EVENT_TYPES.REMOVE_BOT ||
thisObject.eventType === EVENT_TYPES.ENABLE_BOT ||
thisObject.eventType === EVENT_TYPES.DISABLE_BOT
) {
switch (thisObject.eventType) {
case EVENT_TYPES.ADD_BOT: {
emitterUserProfile.addBot(
thisObject.botId,
thisObject.botAsset,
thisObject.botExchange,
thisObject.botEnabled
)
break
}
case EVENT_TYPES.REMOVE_BOT: {
emitterUserProfile.removeBot(
thisObject.botId
)
break
}
case EVENT_TYPES.ENABLE_BOT: {
emitterUserProfile.enableBot(
thisObject.botId
)
break
}
case EVENT_TYPES.DISABLE_BOT: {
emitterUserProfile.disableBot(
thisObject.botId
)
break
}
}
return
}
}
}
+12 -14
View File
@@ -37,7 +37,7 @@ exports.newPost = function newPost() {
REACTION_WOW: 3,
REACTION_SAD: 4,
REACTION_ANGRY: 5,
REACTION_HUG: 6
REACTION_CARE: 6
}
thisObject.reactions = new Map()
@@ -47,7 +47,7 @@ exports.newPost = function newPost() {
thisObject.reactions.set(REACTION_TYPES.REACTION_WOW, 0)
thisObject.reactions.set(REACTION_TYPES.REACTION_SAD, 0)
thisObject.reactions.set(REACTION_TYPES.REACTION_ANGRY, 0)
thisObject.reactions.set(REACTION_TYPES.REACTION_HUG, 0)
thisObject.reactions.set(REACTION_TYPES.REACTION_CARE, 0)
return thisObject
@@ -78,8 +78,6 @@ exports.newPost = function newPost() {
/*
Let's find the Target Post
*/
let targetPost
if (
thisObject.postType === POST_TYPES.REPLY_TO_POST ||
thisObject.postType === POST_TYPES.REPOST_ ||
@@ -91,21 +89,21 @@ exports.newPost = function newPost() {
let targetUserProfile = NT.memory.maps.USER_PROFILES_BY_ID.get(thisObject.targetUserProfileId)
if (targetUserProfile === undefined) {
throw ('Target User Profile Not Found.')
}
}
targetPost = targetUserProfile.posts.get(thisObject.targetPostHash)
let targetPost = targetUserProfile.posts.get(thisObject.targetPostHash)
if (targetPost === undefined) {
throw ('Target Post Not Found.')
}
}
/*
Let's add this post to the replies of the Target Post
*/
if (
thisObject.postType === POST_TYPES.REPLY_TO_POST
) {
targetPost.replies.set(thisObject.targetPostHash, thisObject.targetPostHash)
/*
Let's add this post to the replies of the Target Post
*/
if (
thisObject.postType === POST_TYPES.REPLY_TO_POST
) {
targetPost.replies.set(thisObject.targetPostHash, thisObject.targetPostHash)
}
}
}