-
Notifications
You must be signed in to change notification settings - Fork 0
/
swarm_people.erl
152 lines (114 loc) · 3.97 KB
/
swarm_people.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
%% @author J.R. Bedard <jrbedard@gmail.com>
%% @copyright 2008 jrbedard.
%% @doc Swarm people processing module.
-module(swarm_people).
-author('jrbedard <jrbedard@gmail.com>').
-include_lib("stdlib/include/qlc.hrl").
-include_lib("xmerl/include/xmerl.hrl").
-include("swarm_schema.hrl").
-export([request/1, insert_test_data/0]).
request(Args) ->
case Args of
"around/lat=" ++ Lat ->
get_people_around(Lat, 32.2, true);
"favorites/" ++ Person ->
get_favorite_people(Person);
"person/" ++ Person ->
get_person_info("test");
"create/" ++ Person ->
get_person_info(Person);
"flag/" ++ Person ->
get_person_info(Person);
"edit_obf6/" ++ Person ->
get_person_info(Person);
"remove_obf6/" ++ Person ->
get_person_info(Person);
_ ->
"<error>" ++ Args ++ "</error>"
end.
%% P E O P L E
%% Get Swarmers around a position
get_people_around(Lat, Lgn, Offline) ->
Rows = swarm_mnesia:do(qlc:q([X || X <- mnesia:table(person)])),
PeopleXML = people_to_xml(Rows).
%% Get Favorite Swarmers
get_favorite_people(p_id) ->
Rows = swarm_mnesia:do(qlc:q([X || X <- mnesia:table(person)])),
PeopleXML = people_to_xml(Rows).
people_to_xml(Rows) ->
%% Create People XML
{RootEl, _} = xmerl_scan:string("<people/>"),
#xmlElement{content = Content} = RootEl,
%% For each Person Row
NewContent = export_person(Rows, Content, 0),
lists:flatten([NewContent]),
%%io:format("people xml : ~p~n", [NewContent]),
NewRootEl=RootEl#xmlElement{content=NewContent},
PeopleXML=xmerl:export_simple([NewRootEl], xmerl_xml),
lists:flatten(PeopleXML).
%%io:format("people xml : ~p~n", [Export]),
%% Recursive person Export
export_person([], Data, Type) ->
Data;
export_person([Row | NewRows], OldData, Type) ->
case Type of
0 -> %% people around / favorites
Data = [{person, [], [
{id, [], ["1"]},%%Row#person.id},
{name, [], [Row#person.name]},
{online, [], ["true"]}, %%[Row#person.online]},
{status, [], [Row#person.status]},
{img_url, [], ["images/" ++ Row#person.image_url]}
]}]
end,
NewData = OldData ++ Data,
export_person(NewRows, NewData, Type).
%% P E R S O N
%% Get Swarmer around a position
get_person_info(P_id) ->
Fun = fun() -> mnesia:read({person, P_id}) end,
{atomic, [Row]} = mnesia:transaction(Fun),
Data = [{person, [], [
{id, [], [Row#person.id]},
{name, [], [Row#person.name]},
{status, [], [Row#person.status]}
]}],
NewRootEl=#xmlElement{content=Data},
PersonXML=xmerl:export_simple([NewRootEl], xmerl_xml),
lists:flatten(PersonXML).
%% Create Swarmer
create_person(Id, Name, Online, Status, ImageURL) ->
Row = #person{id=Id, name=Name, password="", online=Online, status=Status, image_url=ImageURL, bio="", website="", karma=32, location="", event = ""},
F = fun() -> mnesia:write(Row) end,
mnesia:transaction(F).
%% Insert fake users in Mnesia!
insert_test_data() ->
io:format("SWARM: Adding Fake Users~n"),
create_person(0,"Superman", true, "Flying...", "superman_48.png"),
create_person(1,"Spiderman", false, "Climbing...", "spiderman_48.png"),
create_person(2,"Aquaman", false, "Swimming...", "aquaman_48.png").
%% p_at_pos, {person, lat, lgn}).
fake_people() ->
"<people count='2'>
<person id='1' name='test'>
<image path='test'/>
<status online='true'>my status</status>
<position lat='32.3' lgn='12.4' city='San Francisco'/>
</person>
<person id='2' name='test2'>
<image path='test'/>
<status online='false'/>
<position lat='32.3' lgn='12.4' city='San Francisco'/>
</person>
<person id='3' name='test3'>
<image path='test'/>
<status online='false'/>
<position lat='32.3' lgn='12.4' city='San Francisco'/>
</person>
</people>".
%% - (NSString *)createPerson:(Person*)person;
%% - (NSString *)flagPerson:(uint)personID :(NSString *)flag;
%% - (NSString *)votePerson:(uint)personID :(NSString *)vote;
%% - (NSString *)editPerson:(uint)personID :(Person*)person;
%% - (NSString *)removePerson:(uint)personID;
%%- (NSString *)sendPersonImage:(uint)personID :(NSString*)image;