Skip to content

Commit 84ecafd

Browse files
committed
Merge: Tnitter: read the latest tnits on the go with the new Tnitter portable app!
Intro a portable client for Tnitter listing the more recent 16 tnits. It is more of a test and example on using `AsyncHttpRequest` to do both simple request to a REST server and implement push notifications using an open request. By design, this client does not support posting Tnits, this feature would require much more code and it may be simplified by future services in the lib. To support the client the server has a few new REST interfaces. One to list the tnits and the other for push notifications. This PR also fixes a few bugs on the Tnitter server: losing the session on posting and empty post being accepted by the server. ----- Note for the reviewers: You may be better to read commits individually. There are 2 bigs commits, one extracts the database logic from the model module and the other introduces code generated by Jwrapper for the native layer of that Android ui implementation. Pull-Request: #1834 Reviewed-by: Jean Privat <jean@pryen.org> Reviewed-by: Romain Chanoir <romain.chanoir@viacesi.fr>
2 parents bc7defd + ff7b14f commit 84ecafd

File tree

18 files changed

+1491
-96
lines changed

18 files changed

+1491
-96
lines changed

contrib/tnitter/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
res/
2+
tnitter.db

contrib/tnitter/Makefile

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1-
all:
1+
SERVER ?= localhost:8080
2+
3+
all: bin/tnitter_server bin/tnitter
4+
5+
bin/tnitter_server: $(shell ../../bin/nitls -M src/tnitter.nit)
26
mkdir -p bin/
3-
../../bin/nitc --dir bin src/tnitter.nit
7+
../../bin/nitc -o bin/tnitter_server src/tnitter.nit -D tnitter_interface=$(SERVER)
8+
9+
bin/tnitter: $(shell ../../bin/nitls -M src/tnitter_app.nit)
10+
mkdir -p bin/
11+
../../bin/nitc -o bin/tnitter src/tnitter_app.nit -m linux -D tnitter_server_uri=http://$(SERVER)
12+
13+
android: bin/tnitter.apk
14+
bin/tnitter.apk: $(shell ../../bin/nitls -M src/tnitter_app_android.nit) res/drawable-ldpi/icon.png
15+
mkdir -p bin/
16+
../../bin/nitc -o bin/tnitter.apk src/tnitter_app_android.nit -D tnitter_server_uri=http://$(SERVER)
17+
18+
android-release: $(shell ../../bin/nitls -M src/tnitter_app_android.nit) res/drawable-ldpi/icon.png
19+
mkdir -p bin/
20+
../../bin/nitc -o bin/tnitter.apk src/tnitter_app_android.nit --release -D tnitter_server_uri=http://tnitter.xymus.net
21+
22+
res/drawable-ldpi/icon.png: art/icon.svg
23+
mkdir -p res
24+
../inkscape_tools/bin/svg_to_icons art/icon.svg --android --out res/
25+
26+
clean:
27+
rm -r res bin

contrib/tnitter/art/icon.svg

Lines changed: 177 additions & 0 deletions
Loading
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Categories:Nit,Internet
2+
License:Apache2
3+
Web Site:http://tnitter.xymus.net/
4+
Source Code:http://nitlanguage.org/nit.git/tree/HEAD:/contrib/tnitter
5+
Issue Tracker:https://github.com/nitlang/nit/issues
6+
7+
Summary:Minimal client for the Tnitter open source micro-blogging platform
8+
Description:
9+
Read the latest Tnits published on Tnitter and keep up to date with push notification support.
10+
This is a sample application to do asynchronous HTTP requests from portable Nit code.

contrib/tnitter/package.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ git.directory=contrib/tnitter/
1010
homepage=http://nitlanguage.org
1111
issues=https://github.com/nitlang/nit/issues
1212
tryit=http://tnitter.xymus.net/
13+
apk=http://nitlanguage.org/fdroid/apk/tnitter.apk

contrib/tnitter/src/action.nit

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,23 @@
1818
module action
1919

2020
import nitcorn
21+
import json::serialization
2122

2223
import model
24+
import database
25+
26+
# Path to the Sqlite3 database
27+
fun tnitter_db_path: String do return "tnitter.db"
2328

2429
redef class Session
2530
# User logged in
2631
var user: nullable String = null
2732
end
2833

2934
# Main Tnitter Action
30-
class Tnitter
35+
class TnitterWeb
3136
super Action
3237

33-
var db_path = "tnitter.db"
34-
var db = new DB.open(db_path)
35-
3638
# Header on pages served by this `Action`
3739
#
3840
# Keywords to `Text::replace`:
@@ -99,6 +101,8 @@ class Tnitter
99101
# Error to display on page as a dismissable panel
100102
var error = null
101103

104+
var db = new DB.open(tnitter_db_path)
105+
102106
# Login/logout
103107
if turi == "/login" and request.post_args.keys.has("user") and
104108
request.post_args.keys.has("pass") then
@@ -132,14 +136,16 @@ class Tnitter
132136
session = null
133137
else if turi == "/post" and request.post_args.keys.has("text") and session != null then
134138
var user = session.user
135-
if user != null then
139+
var text = request.post_args["text"].trim
140+
if user != null and not text.is_empty then
136141
# Post a Tnit!
137-
var text = request.post_args["text"]
138142
db.post(user, text)
143+
db.close
139144

140145
# Redirect the user to avoid double posting
141146
var response = new HttpResponse(303)
142147
response.header["Location"] = request.uri
148+
response.session = session
143149
return response
144150
end
145151
end
@@ -203,7 +209,8 @@ class Tnitter
203209
else error_html = ""
204210

205211
# Load the last 16 Tnits
206-
var posts = db.latest_posts(16)
212+
var posts = db.list_posts(0, 16)
213+
db.close
207214

208215
var html_posts = new Array[String]
209216
for post in posts do
@@ -235,3 +242,32 @@ class Tnitter
235242
return response
236243
end
237244
end
245+
246+
# Tnitter RESTful interface
247+
class TnitterREST
248+
super Action
249+
250+
redef fun answer(request, turi)
251+
do
252+
if turi == "/list" then
253+
# list?from=1&count=2 -> Error | Array[Post]
254+
255+
var from = request.int_arg("from") or else 0
256+
var count = request.int_arg("count") or else 8
257+
258+
var db = new DB.open(tnitter_db_path)
259+
var posts = db.list_posts(from, count)
260+
db.close
261+
262+
var response = new HttpResponse(200)
263+
response.body = posts.to_json_string
264+
return response
265+
end
266+
267+
# Format not recognized
268+
var error = new Error("Bad Request")
269+
var response = new HttpResponse(400)
270+
response.body = error.to_json_string
271+
return response
272+
end
273+
end

0 commit comments

Comments
 (0)