Skip to content

Commit b01007c

Browse files
committed
update realtime reference to posts
1 parent 3f895c4 commit b01007c

File tree

7 files changed

+83
-37
lines changed

7 files changed

+83
-37
lines changed

main/scn/home/home.gd

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ var fr_posts : FirestoreCollection = Firebase.Firestore.collection("posts")
2424

2525
var friend_posts : Array = []
2626

27+
var posts_db_reference : FirebaseDatabaseReference
28+
2729
func _connect_signals():
2830
$ShareSomethingContainer.connect("share_post", self, "add_shared_post")
2931
users_list_section.connect("show_user_profile", self, "_on_show_user_profile")
@@ -40,7 +42,6 @@ func _ready():
4042
load_user()
4143
animate_Home(true)
4244
load_posts()
43-
# suggested_users.suggest_user("fenix-hub")
4445
friend_list.load_friend_list()
4546

4647
func load_user():
@@ -58,7 +59,7 @@ func animate_Home(display : bool):
5859
$Tween.start()
5960

6061
func sort_posts(post_a : FirestoreDocument, post_b : FirestoreDocument):
61-
if post_a.doc_fields.timestamp < post_b.doc_fields.timestamp:
62+
if post_a.doc_fields.timestamp > post_b.doc_fields.timestamp:
6263
return true
6364
return false
6465

@@ -69,29 +70,52 @@ func load_posts():
6970
for post in posts:
7071
if post.has("fields"):
7172
if (post.fields.user_id.stringValue in UserData.friend_list) \
72-
or (post.fields.user_id.stringValue == UserData.user_id):
73+
or (post.fields.user_id.stringValue == UserData.user_id) \
74+
and not post in friend_posts:
7375
friend_posts.append(FirestoreDocument.new(post))
7476

7577
friend_posts.sort_custom(self, "sort_posts")
7678

7779
if friend_posts.empty():
7880
pass
7981
else:
80-
for post in friend_posts.slice(0, 10):
81-
if PostsManager.has_post(post.doc_name):
82-
if PostsManager.has_post_container(post.doc_name):
83-
var post_container : PostContainer = PostsManager.get_post_container_by_id(post.doc_name)
82+
for post_idx in range(0, friend_posts.size()):#.slice(0, 10):
83+
var post_container : PostContainer
84+
if PostsManager.has_post(friend_posts[post_idx].doc_name):
85+
if PostsManager.has_post_container(friend_posts[post_idx].doc_name):
86+
post_container = PostsManager.get_post_container_by_id(friend_posts[post_idx].doc_name)
87+
add_post(post_container)
88+
else:
89+
post_container = Activities.post_container_scene.instance()
8490
add_post(post_container)
91+
post_container.load_post(PostsManager.get_post_by_id(friend_posts[post_idx].doc_name))
8592
else:
86-
var post_container : PostContainer = Activities.post_container_scene.instance()
87-
var post_obj : PostsManager.Post = PostsManager.add_post_from_doc(post.doc_name, post)
88-
post_container.load_post(post_obj)
93+
post_container = Activities.post_container_scene.instance()
94+
var post_obj : PostsManager.Post = PostsManager.add_post_from_doc(friend_posts[post_idx].doc_name, friend_posts[post_idx])
8995
add_post(post_container)
96+
post_container.load_post(post_obj)
97+
post_box.move_child(post_container, post_idx)
9098
else:
9199
for post in post_box.get_children():
92100
if post is PostContainer: post.queue_free()
93101
posts_section.get_node("NoFriends").show()
102+
103+
if posts_db_reference == null:
104+
posts_db_reference = Firebase.Database.get_database_reference("sociadot/posts")
105+
posts_db_reference.connect("new_data_update", self, "_on_new_post")
106+
107+
94108

109+
func _on_new_post(post : FirebaseResource):
110+
if post.data.user == UserData.user_id:
111+
return
112+
if PostsManager.has_post(post.key):
113+
return
114+
var post_container : PostContainer = Activities.post_container_scene.instance()
115+
var post_doc : FirestoreDocument = yield(Utilities.get_post_doc(post.key), "get_document")
116+
var post_obj : PostsManager.Post = PostsManager.add_post_from_doc(post.key, post_doc)
117+
add_post(post_container)
118+
post_container.load_post(post_obj)
95119

96120
func add_post(post : PostContainer):
97121
posts_section.get_node("NoFriends").hide()

main/scn/post/post.gd

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ func load_post(post) -> void:
3737
set_user_id(post.user_id)
3838
set_description(post.description)
3939
set_timestamp(post.timestamp)
40-
set_image(post.image)
40+
if post.image_name == "":
41+
set_image(null)
4142
PostsManager.add_post_scene(self)
4243

4344
func set_post_id(p : String):
@@ -99,9 +100,15 @@ func set_description(d : String):
99100

100101

101102
func set_image(img : ImageTexture):
103+
if not post_image:
104+
return
102105
image = img
103106
if img == null:
107+
post_image.material = null
108+
post_image.hide()
104109
return
110+
else:
111+
post_image.show()
105112
post_image.set_texture(img)
106113
if img.get_size().y < 100:
107114
post_image.stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_CENTERED

main/scn/share/share.gd

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ onready var post_image : TextureRect = $ShareSomething/Container/Image
66
onready var post_description : TextEdit = $ShareSomething/Container/Description
77

88
var image_path : String
9-
var image : ImageTexture
9+
var image : ImageTexture = null
1010

1111
var y_limit : int = 400
1212
var x_limit : int = 500
@@ -48,8 +48,9 @@ func _on_ShareBtn_pressed():
4848
Activities.loading(true)
4949
var share_task : FirestoreTask = Utilities.add_post_doc(post_description.get_text().c_escape(), image_path)
5050
var post_doc : FirestoreDocument = yield(share_task, "task_finished")
51-
var image_task : StorageTask = Utilities.add_post_image(post_doc.doc_name, image_path, image.get_data().save_png_to_buffer())
52-
yield(image_task, "task_finished")
51+
if image != null:
52+
var image_task : StorageTask = Utilities.add_post_image(post_doc.doc_name, image_path, image.get_data().save_png_to_buffer())
53+
yield(image_task, "task_finished")
5354
emit_signal("share_post", post_doc.doc_name, post_doc, image)
5455
hide()
5556
clear()
@@ -87,6 +88,7 @@ func _on_ShareSomethingContainer_gui_input(event):
8788
hide()
8889

8990
func clear():
91+
image = null
9092
post_image.set_texture(null)
9193
post_image.hide()
9294
post_description.set_text("")

main/scn/signin/signin.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func _ready():
1818
$UpdateProfile.hide()
1919
animate_SignContainer(true)
2020
yield(get_tree(), "idle_frame")
21-
return
21+
# return
2222
Firebase.Auth.load_auth()
2323
if not Firebase.Auth.auth.empty():
2424
Activities.loading( true)

main/scripts/posts_manager.gd

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,32 @@ class Post:
2020
signal update_post(post)
2121

2222
func _init(id : String, doc_task : FirestoreTask = null, img_task : StorageTask = null):
23-
self.id = id
24-
if doc_task != null:
25-
document_task = doc_task
26-
document_task.connect("get_document", self, "_on_get_document")
27-
if img_task != null:
28-
image_task = img_task
29-
image_task.connect("task_finished", self, "_on_image_received")
30-
23+
self.id = id
24+
if doc_task != null:
25+
document_task = doc_task
26+
document_task.connect("get_document", self, "_on_get_document")
27+
if img_task != null:
28+
image_task = img_task
29+
image_task.connect("task_finished", self, "_on_image_received")
30+
3131
func _on_get_document(doc : FirestoreDocument):
3232
emit_signal("update_document", doc)
3333
document = doc
3434
user = doc.doc_fields.user
3535
user_id = doc.doc_fields.user_id
3636
description = doc.doc_fields.description
3737
timestamp = doc.doc_fields.timestamp
38-
# likes = doc.doc_fields.likes
39-
# comments = doc.doc_fields.comments
4038
image_name = doc.doc_fields.image
4139
emit_signal("update_post", self)
4240

4341
func _on_image_received():
4442
if typeof(image_task.data) == TYPE_RAW_ARRAY:
4543
image = Utilities.byte2image(image_task.data)
4644
emit_signal("update_image", image)
47-
45+
else:
46+
if image_task.data.has("error") or image_name == "":
47+
image = null
48+
emit_signal("update_image", image)
4849

4950
var posts : Array = []
5051
var post_containers : Array = []
@@ -58,9 +59,13 @@ func add_post(id : String, document_task : FirestoreTask = null, image_task : St
5859
return post
5960

6061
func add_post_from_doc(id : String, doc : FirestoreDocument, image_task : StorageTask = null) -> Post:
62+
var post : Post
6163
if image_task == null:
62-
image_task = Utilities.get_post_image(doc.doc_fields.user_id, id, doc.doc_fields.image)
63-
var post : Post = Post.new(id, null, image_task)
64+
if doc.doc_fields.image != "":
65+
image_task = Utilities.get_post_image(doc.doc_fields.user_id, id, doc.doc_fields.image)
66+
post = Post.new(id, null, image_task)
67+
else:
68+
post = Post.new(id)
6469
post._on_get_document(doc)
6570
posts.append(post)
6671
return post

main/scripts/utilities.gd

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,28 @@ func update_friend_list(friend_id : String) -> FirestoreTask:
6767
friend_list = UserData.friend_list
6868
})
6969

70-
70+
# Get a single post document by ID
71+
func get_post_doc(post_id : String) -> FirestoreTask:
72+
return posts_collection.get(post_id)
7173

7274
# Get a post's Image from Storage
7375
func get_post_image(user_id : String, post_id : String, post_image : String) -> StorageTask:
7476
return Firebase.Storage.ref("sociadot/posts/{user_id}/{post_id}/{post_image}".format({user_id = user_id, post_id = post_id, post_image = post_image})).get_data()
7577

7678
# Add a new post Document
7779
func add_post_doc(description : String, image_path : String, timestamp : int = Utilities.get_time()) -> FirestoreTask:
78-
return posts_collection.add("", {
80+
var add_post_task : FirestoreTask = posts_collection.add("", {
7981
user = UserData.user_name,
8082
user_id = UserData.user_id,
8183
description = description,
8284
image = get_image_name(image_path),
8385
timestamp = timestamp
8486
})
87+
add_post_task.connect("add_document", self, "_on_post_added")
88+
return add_post_task
89+
90+
func _on_post_added(post_doc : FirestoreDocument):
91+
Firebase.Database.get_database_reference("sociadot/posts").update(post_doc.doc_name, { user = post_doc.doc_fields.user_id })
8592

8693
# Add a post image to Storage
8794
func add_post_image(post_id : String, image_path : String, image : PoolByteArray) -> StorageTask:

project.godot

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,16 +201,17 @@ enabled=PoolStringArray( "godot-firebase" )
201201

202202
[firebase]
203203

204-
environment_variables/apiKey="AIzaSyBdarlkNQO0rqZnDgv8P914R_dkSfp3S1c"
205-
environment_variables/authDomain="instadot.firebaseapp.com"
206-
environment_variables/databaseURL="https://instadot-default-rtdb.firebaseio.com"
207-
environment_variables/projectId="instadot"
208-
environment_variables/storageBucket="instadot.appspot.com"
209-
environment_variables/messagingSenderId="283726603535"
210-
environment_variables/appId="1:283726603535:web:14132220da1c119f62ca7a"
204+
environment_variables/apiKey="AIzaSyCgrmTwdsjR0zY0hHR_WyuSX4cJri1wiBo"
205+
environment_variables/authDomain="roundtable-5c241.firebaseapp.com"
206+
environment_variables/databaseURL="https://roundtable-5c241.firebaseio.com"
207+
environment_variables/projectId="roundtable-5c241"
208+
environment_variables/storageBucket="roundtable-5c241.appspot.com"
209+
environment_variables/messagingSenderId="567850593967"
210+
environment_variables/appId="1:567850593967:web:5395b274d9da6a919f2e5a"
211211
environment_variables/measurementId=""
212212
environment_variables/clientId=""
213213
environment_variables/clientSecret=""
214+
environment_variables/domainUriPrefix=""
214215

215216
[gui]
216217

0 commit comments

Comments
 (0)