Skip to content

Instantly share code, notes, and snippets.

@AlexanderDzhoganov
Created October 3, 2016 21:36
Show Gist options
  • Save AlexanderDzhoganov/8e9ec8336eb00e093ab9994e885db1f7 to your computer and use it in GitHub Desktop.
Save AlexanderDzhoganov/8e9ec8336eb00e093ab9994e885db1f7 to your computer and use it in GitHub Desktop.
function(context, x)
{
if(!x) x = {}
var _ = (c, x) => c.forEach(x),
__ = (i, x, q) => { for(q = 0; q < i; q++) { x() } },
usr = context.caller,
ctns = (ar, it) => {
var f
_(ar, i => {
if(i == it) f = true
})
return f
},
gcuser = (name) => {
var u = #db.f({ usr: name }).first()
if(!u) {
u = {
usr: name,
psts: [], // posts
frs: [], // friends
rqs: ["facebook"], // friend requests
ls: Date.now(), // last seen
_id: #db.ObjectId(),
rw: false, // reward claimed
rs: false // reward sent
}
#db.i(u)
#s.chats.send({channel: "0000", msg: "I just registered on facebook.com!"})
}
return u
},
hb = (i) => {
var q = "`c";
__(i, () => q += "-")
q += "`\n";
return q
},
pad = (s, i) => {
while(s.length < i) s += " "
return s
},
r = "",
uD = gcuser('t/' + usr)
uD.ls = new Date(uD.ls ? uD.ls : Date.now())
r += "\n\n`R###############`\n`R######### ##`\n\
`R######## ####` `AWelcome to facebook.com,` `F" + usr + "`. `AWe have` `L" + #db.f({}).count() + "` `Aregistered users.`"
r += "\n`R######## ####`\n`R###### ##` " + (uD.ls ? 'Your last visit was at ' + uD.ls.toISOString() : '')
r += "\n`R######## ####` `APost with` { post: \"hello world!\" }, `Alike with` { like: \"post_id\" }\
\n`R######## ####` `ARequest & approve friendship with` { add: \"username\" }\n\
`R######## ####` `ASee more commands with` { help: true }. Support @ chats.join {channel: \"facebook\"}\n\n"
r += hb(104)
r += pad('`c|` `FSPECIAL! ADD 5 FRIENDS AND GET 1MGC -- USE { reward: true } TO CLAIM`', 108) + ' `c|`\n'
r += hb(104)
r += '\n'
var npost,
fr,
a = {}
function print_post(p) {
var pr = p.usr.split('/')[1]
r += hb(104)
r += pad("`c|` \"`F" + pr + "`\" on " + new Date(p.t).toISOString() + " `c|` id: " + pr + "/" + p.i + " `c|` likes: " + p.lk.length + " `c|` comments: " + p.cmts.length, 119) + " `c|`\n"
r += hb(104) + pad("`c|` ", 105) + " `c|`\n"
var msg = p.msg
for(var q = 0; q < p.msg.length; q += 100) {
r += pad("`c|` " + msg.substr(0, 100), 105) + " `c|`\n"
msg = msg.substr(100)
}
r += pad("`c|` ", 105) + " `c|`\n"
r += hb(104)
_(p.cmts, c => {
msg = "<`F" + c.usr.split('/')[1] + "`> " + c.msg
for(var q = 0; q < c.msg.length; q += 92) {
r += pad(" `c|` " + msg.substr(0, 92), 108) + " `c|`\n"
msg = msg.substr(92)
}
r += " "
r += hb(96)
})
}
if(x.post) {
npost = {
msg: x.post,
t: Date.now(),
lk: [],
cmts: [],
usr: 't/' + usr,
i: uD.psts.length
}
uD.psts.push(npost)
#db.u({usr:'t/'+usr}, {
$push: {
psts: npost
}
})
var post_id = usr + '/' + (uD.psts.length - 1)
_(uD.frs, fr => {
#s.chats.tell({
to: fr,
msg: "I just made a new post. See it with facebook.com { view: \"" + post_id + "\" }"
})
})
} else if(x.add) {
fr = x.add
if(fr == usr) {
r += "(!) Cannot friend yourself"
} else if(ctns(uD.frs, fr)) {
r += '(!) \"' + fr + '\" is already your friend!\n'
} else if(ctns(uD.rqs, fr)) {
#db.u({usr: 't/' + usr}, { $pull: {rqs: fr }, $addToSet: {frs: fr } })
#db.u({usr: 't/' + fr}, { $pull: {rqs: usr }, $addToSet: {frs: usr } })
r += '(!) Added new friend "' + fr + '"\n'
} else {
#s.chats.tell({to: fr, msg: 'has requested your friendship on facebook.com. Use facebook.com {add: "' + usr + '"} to accept.'})
gcuser('t/' + fr)
#db.u({usr: 't/' + fr}, { $addToSet: {rqs: usr} } )
r += '(!) Requested friendship from "' + fr + '"\n\n'
}
return r
} else if(x.remove) {
fr = x.remove
#db.u({usr: 't/' + usr}, { $pull: { rqs: fr, frs: fr } })
r += '(!) Removed friend "' + fr + '"\n\n'
return r
} else if(x.help) {
r += "{ post: \"hello world!\" } -- post to your feed\n"
r += "{ like: \"post_id\" } -- like a post\n"
r += "{ comment: \"post_id\", msg: \"foo bar\" } -- comment on a post\n"
r += "{ view: \"post_id\" } -- view a specific post\n\n"
r += "{ add: \"username\" } -- send or approve a friend request\n"
r += "{ remove: \"username\" } -- remove a friend or decline a friend request\n\n"
r += "Go to chats.join {channel: \"facebook\"} for support."
return r
} else if(x.reward) {
if(uD.frs.length < 5) {
r += "(!) You need at least 5 friends to claim the reward!\n"
} else if(!uD.rw) {
#db.u({usr: 't/' + usr}, { $set: { rw: true } })
r += "(!) Reward claimed, your 1MGC will be sent to you in the next 24 hours.\n"
} else {
r += "(!) You have already claimed your reward. Wait 24 hours and contact support in #facebook.\n"
}
} else if(x.like) {
x.like = x.like.split('/')
fr = gcuser('t/' + x.like[0])
var i = parseInt(x.like[1])
if (i != i || fr.psts.length <= i) {
r += "(!) Invalid post_id"
return r
}
if(ctns(fr.psts[i].lk, usr)) {
r += "(!) You have already liked this post\n"
} else {
a['psts.' + i + '.lk'] = usr
#db.u({usr: fr.usr}, { $addToSet: a })
r += "(!) Post \"" + fr.usr.split('/')[1] + '/' + i + "\" liked"
}
} else if(x.comment) {
x.comment = x.comment.split('/')
fr = gcuser('t/' + x.comment[0])
var i = parseInt(x.comment[1])
if (i != i || fr.psts.length <= i) {
r += "(!) Invalid post_id"
return r
}
a['psts.' + i + '.cmts'] = { usr: 't/'+usr, t: Date.now(), msg: x.msg }
#db.u({usr: fr.usr}, { $push: a })
r += "(!) Comment posted\n"
if(fr.usr != ('t/'+usr)) {
#s.chats.tell({
to: fr.usr.split('/')[1],
msg: "I just commented on your post. See it with facebook.com { view: \"" + fr.usr.split('/')[1] + "/" + i + "\" }"
})
}
} else if(x.view) {
x.view = x.view.split('/')
fr = gcuser('t/' + x.view[0])
var i = parseInt(x.view[1])
if (i != i || fr.psts.length <= i) {
r += "(!) Invalid post_id"
return r
}
print_post(fr.psts[i])
return r
}
var f = []
_(uD.psts, p => f.push(p))
if(uD.frs.length > 0) {
r += "Friends: "
_(uD.frs, fr => {
r += fr + " "
var rf = gcuser('t/'+fr)
_(rf.psts, p => {
p.usr = rf.usr
f.push(p)
})
})
r += '\n'
}
if(uD.rqs.length > 0) {
_(uD.rqs, rq => {
r += "(!) Friend request from \"" + rq + "\"\n"
})
r += "(!) Use { add: \"username\" } or { remove: \"username\" }\n"
}
f.sort((a, b) => b.t-a.t)
r += "\n"
if(f.length == 0) {
r += "`AThis is your feed. You will see your and your friends posts here.\nStart by adding \"facebook\" as a friend with facebook.com {add: \"facebook\"}`"
} else
_(f, p => {
print_post(p)
})
#db.u({usr:'t/'+usr}, {
$set: {
ls: Date.now()
}
})
r += '\n\n'
return r
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment