SupB - A social network in 60 minutes with NetFluid

Introduction

The idea it's to make a simple social network to post text, pictures and generic files from pc and mobile about a generic topic, without registration and app to be downloaded.

The solution flows naturally: email, the most implemented comunication system of any device, including my old, not smart, telephone.

So the system will build in this way: an smtp server linked to a web interface, whenever a mail is recieved it will be posted on the corresponding group.

Example: if we send an email to genova@supb.eu it will be shown with others in supb.eu/genova.

Background

This project run upon NetFluid Application Server, the free version is downloadable here

Using the code

Step 1: Setting up the project

First of we need to create a new NetFluid Web App, a Visual Studio template is available at link above.Our web app is compilable as portable executable (*.exe) with embedded web interface and server or as library (*.dll) to be loaded by another instance of NetFluid.

Inthis casewe opt for executable so we need to set up the AppConfig to set basics NetFluid settting:

With the Route Attribute we tell to the NetFluid engine to call this page on the main uri.

With the parametrized flag set on true, we tell to engine to take arguments the of method from the uri, instead from post or get parameters.

Now we got four possibilities:

http://supb.eu/

The main page of our site

group=null and postId=null

http://supb.eu/style.css

And other public files

group="style.css" and postId=null

http://supb.eu/group

The user requested a group

group="group" and postId=null

http://supb.eu/group/message

The user requested a single post

group="group" and postId="message"

The first and the fourth case are obvious, we can distinguish the second and the third case with the IsPublicFile function, or resolve the problem at his root moving contents (images, styles, javascript, etc.) on another domain.

//no group specified, let's show the index
if (group==null)
{
Render("SupB.UI.Master");
return;
}
//because actually we can't know if /style.css mean the css of the page
//or the group style.css@supb.eu
//the problem it's solved by moving resources on other domain
//ex: content.netfluid.org/style.css
if (IsPublicFile("/"+group))
{
//Disabling the Blocking value we tell to the engine to go on after the execution
//of this page, to public files check and send
Blocking = false;
return;
}
//normalizing the input
group = group.ToLowerInvariant();
//Fecthing the messages for this group
//http://supb.ue/<group>
var messages = Database.Group(@group);
if (!messages.Any())
{
Render("SupB.UI.Error", new { Message = "Empty group" });
return;
}
#region SHOW A SINGLE POST IF REQUESTED
// http://supb.ue/<group>/<post>
// show the specified post inside this group
if (postId!=null)
{
//take the messages and pass them to the template
var post = Database.Message(postId);
if (post != null)
Render("SupB.UI.Post", new { Post = post });
else
Render("SupB.UI.Error", new { Message = "Post not found" });
return;
}
#endregion
Render("SupB.UI.Group", new {Name = @group, Post = messages});

Step 6: assemblethe bodywork

Has we seen above, after taking the messages posted we pass them to a function called Renderthat display an *.html page to the output, passing an object translated into a dictionary.

NOTE: HTML page are runtime compiled by the NetFluid engine, so to use them remember to mark them as EMBEDDED RESOURCES

In this project we got four pages:

Master: The main page and index of the web app, others page will inherit this one

Error: To show error messages

Post: To show a single post

Group: To show post inside a group

Here we can see the structure of Master, with the two NetFluid instruction definewiththese we can define two field of the page overridable by derived types.

In this case we defined two fields Column and Center

Any other C# function can be inserted into the html via the % symbol for single line instruction and <% %> for multiline instruction.