1 module konnexengine.ui.utilities;
2 
3 import std.conv : to;
4 import std.uuid: UUID, parseUUID;
5 
6 import vibe.db.redis.redis: RedisDatabase;
7 import vibe.data.json : Json, parseJsonString;
8 import vibe.http.server : HTTPServerRequest;
9 import konnexengine.app.data : AuthState, AppData;
10 import konnexengine.user.blueprint;
11 
12 import dotenv;
13 
14 T prepare(T)(T t, AppData appData, AuthState authState, Json videoData)
15 {
16 	t.ui.name = appData.name;
17 	t.ui.page = appData.page;
18 	t.ui.auth["signup"] = authState.signup;
19 	t.ui.auth["pass_reqd"] = authState.passwordRequired;
20 	t.ui.auth["success"] = authState.success;
21 	t.ui.auth["login"] = authState.login;
22 	t.ui.auth["valid"] = authState.valid;
23 
24 	t.content.videos = videoData["items"];
25 	t.content.currentVideo = appData.currentVideo;
26 
27 	t.user.accessToken = appData.accessToken;
28 	t.user.email = "";
29 	t.user.username = appData.username;
30 
31 	return t;
32 }
33 
34 void configure(AppData* appData, Json videoData)
35 {
36 	Env.load(".env", false);
37 	appData.page = "index";
38 
39 	appData.name = "Konnex Accounts";
40 	// channel.name(videos[0]["snippet"]["channelTitle"].get!string);
41 	appData.currentVideo = videoData["items"][0];
42 	// logInfo(videoData.to!string);
43 
44 	import std.file;
45 
46 	if (!exists("videos.json"))
47 	{
48 		writeVideosToFile(videoData);
49 	}
50 }
51 
52 void writeVideosToFile(Json videoData)
53 {
54 	string vids = videoData.toPrettyString();
55 
56 	import std.file : write;
57 
58 	write("videos.json", vids);
59 }
60 
61 void cacheUser(string r, RedisDatabase* _user, RedisDatabase* _db)
62 {
63 	Json data = parseJsonString(r);
64 	string id = data["id"].to!string;
65 	string email = data["email"].to!string;
66 	string namespace = data["namespace"].to!string;
67 
68 	_user.set(email,id);
69 	// _user.set(username, id);
70 
71 
72 	_user.hset("user: " ~ id, "id", id);
73 	_user.hset("user: " ~ id, "email", email);
74 	// foreach (eml; user.email())
75 	// {
76 	// 	_user.hset("user:" ~ id, "email", eml.get());
77 	// }
78 	_user.hset("user: " ~ id, "namespace", namespace);
79 	// _user.hset("user: " ~ id, "password", password);
80 
81 	_user.hgetAll("user:" ~ id);
82 }
83 
84 void cacheUserTokens(string user, string accessToken, RedisDatabase* _user)
85 {
86 	string id = user;
87 	_user.set(id,accessToken);
88 }
89 
90 void cacheUserPage(string user, string page, RedisDatabase* _page)
91 {
92 	string id = user;
93 	_page.set(id,page);
94 }
95 
96 void cacheUserKomponent(string user, string komponent, RedisDatabase* _komponent)
97 {
98 	string id = user;
99 	_komponent.set(id,komponent);
100 }
101 
102 //