License:
I don't care too much, just add credits to me in the forum post and the actual program for the API. That's all I ask.
This code is all mine other than the code I supply to allow you to install on-the-fly.
I wrote a post a little while ago, and after the amount of positive comments, I implemented the API. Please use this for good, and I hope you like it:) It does work, but I am still adding to the php script so if it errors one time and not again, this may be me playing with the PHP scripts.
The API can be downloaded or you can use the code I shall supply to download and install it on the fly. This is thanks to: (Geforce Fan) for the original source code and has been modified to work with just my API :D/>.
Errors go to my github repo (below) and then submit an issue. If you wish to get the code then:
If you wish to download the code then please go to my github repo or alternatively you can use the on-the-fly method and download it and run it on boot? :D/>
NOTE:
Please don't hate as it is an alpha version and it is my first release of the code, I do know oeed is planning to make a client but of course I would love to have more than one client out there, so people can choose from. Also when you make a client it will go on my app store.
How the chat works:
As you know I implemented a chat system which has a global, group and personal messenger, if you wish to make it asynchronous then please look here. The PHP script will keep your messages down to 25/26 messages stored, so that is the latest history, of course you can make your own history and make it local like caching, but to make it asynchronous then it needs to be small. This will count for all chats. Global is for everyone so remember anyone can talk. Group can make a group chat that you and your friends can talk in, make it different so people can't guess the group if you are talking about projects etc. The personal messenger is just like a user to user messaging system.
Here is the code to run the API on the fly:
Spoiler
function loadapi()
if fs.exists("snet") then
os.loadAPI("snet")
return true
end
aa = aa or {}
local a = http.get("https://vault.dannysmc.com/lua/api/socialnetapi.lua")
a = a.readAll()
local env = {}
a = loadstring(a)
local env = getfenv()
setfenv(a,env)
local status, err = pcall(a, unpack(aa))
if (not status) and err then
printError("Error loading api")
return false
end
local returned = err
env = env
_G["snet"] = env
end
loadapi()
If a request comes back with an error (posting data and user login/register) have an error code which is already formatted on the way back so use a code like:
local req = user.login(username, password)
req = req.readAll()
if req == "true" then
-- Do the code if they exist
else
print(req)
-- prints the error message
end
Anyway the api is simple and pretty easy to use, here is the documentation I wrote (which is a huge comment in the actual API) when making the API:
Error codes are at the bottom!
—
Glossary:
+ MDA: Multi-Dimensional Array
—
—
All functions will require a username and a password, this is to check they exist, on every function. If I was you I would store it in a username and password variable, so the user (or you) do not have to keep adding it.
—
->
user.login(username, password)
This will attempt to log a user in, by checking their username and password against the user database, if they exist, it will return true, otherwise it will return with the error.->
user.register(username, password, email)
This will register a new user by sending up the username, password and email. If it worked then it will return true, if it didn't it will turn the reason why, either an error occured or the username was already taken.->
user.list(username, password)
This will list all users that are registered, this will ONLY, list the username and this is a normal array. If you still are confused please look at the multidimensional arrays heading which will explain how to handle normal and multi-dimensional arrays.->
store.appsall()
This will get a list of all apps that are stored in the database that are public. It will return as a multidimesional array, you will have to readAll() then unserialize to be able to index it with ComputerCraft. Examples of multidimensional arrays and how to index them are found at further down.->
store.appsuser()
This will return a multidimensional array of all apps that have been made by the user you passed as an argument.->
social.post.new(username, password, header, message)
This will let you post a status, you will add a header and a message, so a header would be say the subject of the status, and the message will be the main body content. If you wish to use a small post then you can optionally use tweet, which will send up a small sized status. The character limit on a new post is 4096 characters.->
social.post.tweet(username, password, message)
Same as a new post, the differences being that it will only allow accept 255 characters and doesn't support any type of subjects/headers.->
social.post.getall(username, password)
This will get all posts that are on the system, this is any user and all posts. Please note if you do use a friends list then please the scripter of the program will need to set this as I am not keen on the idea of having friends list because not may people will use the API. So for now I guess you can use the friends sub-functions to allow you to display content that your friends have posted.->
social.post.getuser(username, password, usertoget)
This will get all posts that a user has posted and return it as a MDA. This is good to use for viewing a users profile.->
social.post.delete(username, password, postid)
This will delete a post that is owned by you, please note that you will need to get the id of the post/tweet, this will be the first value in the MDA.->
social.profile.edit(username, password, fullname, age, bio, forumsusername)
This will of course allow you to edit your profile, please note that the only things that can be saved are username, fullname, age, bio and forums username. These are optional and don't have to be added.->
social.profile.get(username, password, usertoget)
This will get a users profile as a MDA, these can be used in templates you create.->
social.friends.add(username, password, frienduser)
This is for anyone that wishes to use a friends list, this allows you to add a username to your friends list.->
social.friends.get(username, password, usertoget)
This will grab a users followers/friends list.->
social.chat_global.new(username, password, message)
This will use the global chat which is a huge chat that will allow everyone to talk together, kind of like IRC. This will take a message and then will be added please remember when you add a message make sure to download the chat again, this can be of course automatically updated every few seconds but this WILL CAUSE DELAYS.->
social.chat_global.get(username, password)
This will grab the global chat. Please note messages will slowly get longer and longer therefore the chat may become slower and slower to download.->
social.chat_group.new(username, password, groupname, message)
This is a group chat allowing a set of users to chat in a group. You can define a groupname and then chat in it with whoever you want, please note that groups can be accessed by anyone, so always make sure to name chats very differently, even maybe use a hash as a groupname.->
social.chat_group.get(username, password, groupname)
This will download the current chat of the group you choose, by giving it a groupname argument this will allow you to chat in that group.->
social.chat_pm.new(username, password, recipient, message)
This is a personal messenger and works the same as all others but instead you give it a recipient instead of a groupname and then this will add a new message to that user.->
social.chat_pm.get(username, password, recipient)
You will only be able to see a chat from your their username, if you want you would have to make some very in-depth code to allow a user to view their own chat and use the message id's to figure out where the newest message is.———————————————————————-
Multi-Dimensional Arrays (How they work / How to index them):
———————————————————————-
Here we shall talk about arrays and how they work and how to index them.
Standard Arrays:
{
"hello",
"there",
"my",
"friends",
}
or
{
user = "a_username",
pass = 0123456789,
}
Multi-Dimensional Arrays:
mdarray = {
{
"first array",
"second value",
"third value",
},
{
"second array",
"second value",
"third value",
},
{
"third array",
"second value",
"third value",
},
{
"fourth array",
"second value",
"third value",
}
}
so for example: "first array" this value would be grabbed using: mdarray[1][1], but if we wanted the value just below the "third array" value then we would use: mdarray[3][2], so the first number in the square brackets is the simple arrays, and then second number is the value in that array, if we wanted to index the second array but not a specific array then we would use mdarray[2].
———————————————————————-
How to (A list of explanations on how to do things:
———————————————————————-
+ Friends lists/Followers:
As I am not keen on the whole friends thing I have made it so instead you can have followers and follow someone else. If you wish to use this as a way of a friends list or to display posts/tweets by your followers, then you will need to get the users following/friends list, and then loop through all posts, matching them with users in the friends list. This is all possible and pretty simple, you would need two loops both for loops of course. The first one will loop through all posts, and the second one to loop through your friends/following list, then if a username matches a following username, then add it to a new table etc.
+ Registering users:
Please make sure they add an email, as this is a big thing to have because the new forgotten password system uses emails to allow users to change their passwords by sending them an email, at least warn them if they don't add one.
+ Auto-Login:
To make an auto-login, store a username and password in a file and then on boot, open it and send it up.
+ Encryption:
This is automatic before any password goes up to the database, it is hashed using sha256 and then sent up, please note you do not need to hash anything before you send it up as it is done in the API. You would only need to hash it if you have modified the API.
———————————————————————-
All Possible Return Errors & Explanations:
———————————————————————-
All errors can occur from the database playing up as the smallest error will break the lua table, so if it does occur do add an issue on the GitHub repo.
Here is a list of errors and other possibilities other than a PHP error:
Could not create new post. -> This could be that the post data sent up may contain some kind of illegal character and the PHP script has escaped it.
Could not retrieve posts. -> This may be that there wasn't posts to display with your criteria.
Could not post new tweet. -> This could be that the post data sent up may contain some kind of illegal character and the PHP script has escaped it.
Could not get all posts/tweets from user. -> This may be that there wasn't tweets to display with your criteria.
Could not delete post. -> This can happen and this could mean you have deleted it before but the feed didn't update so you tried again, or you didn't supply the correct post/tweet ID.
Could not send chat message. -> This may be that there was an illegal character.
Could not get chat messages. -> This may be that there wasn't chat to display with your criteria.
Could not edit profile. -> This could be that the client has some how using the incorrect profile name.
Could not get profile.-> This could be that when editing the profile you broke the serialization.
Could not add friend. -> This could be that the lua table is broken therefore you can not add the friend.
Could not get friends list.-> Friends lua table may be broken.
You do not have permission to delete this. -> This will go when you do not have the correct permission to delete that post.
The username you wish to use is already taken! -> This can occur when registering and would mean your username is already taken.
Could not verify credentials. -> This is a login error, meaning your login is wrong.
Could not insert new user.-> This will be down to some kind of PHP error.