Skip to content

Conversation

@markzakharyan
Copy link
Contributor

(Tested, it works :D)

Changed old cloud functions example to js and a newer (not deprecated environment). Also added cloud functions example for changing a room's lastMessage

.set() overrode old room fields which broke everything.

I fixed this issue in this commit.
@SalahAdDin
Copy link
Contributor

It looks cool, but the Room model has no a lastMessage field.

@demchenkoalex
Copy link
Member

It will have soon, and it will be an array

@markzakharyan
Copy link
Contributor Author

markzakharyan commented Jun 29, 2021

It looks cool, but the Room model has no a lastMessage field.

This creates it if it doesn't exist already

@SalahAdDin
Copy link
Contributor

It looks cool, but the Room model has no a lastMessage field.

This creates it if it doesn't exist already

i was talking about the dart model.

@markzakharyan
Copy link
Contributor Author

It looks cool, but the Room model has no a lastMessage field.

This creates it if it doesn't exist already

i was talking about the dart model.

Check out the issue I made, it contains some nice code that gets latest message without using the dart model. I'm using this in the meantime until it gets added to the package.

@SalahAdDin
Copy link
Contributor

Is it ready to be merged?

@mashegoindustries
Copy link

mashegoindustries commented Jul 13, 2021

Hi,

I think we may need to change the onWrite to onUpdate

This will ensure that rooms document will only get updated just once.

At the moment it updates twice, since the other cloud function (changeMessageStatus) will trigger another onWrite

So I simply changed from onWrite to onUpdate

exports.changeLastMessage = functions.firestore
  .document("rooms/{roomId}/messages/{messageId}")
  .**onUpdate**((change, context) => {
    const message = change.after.data();
    if (message) {
      return db.doc("rooms/" + context.params.roomId).update({
        lastMessage: message,
      });
    } else {
      return null;
    }
  });

@demchenkoalex
Copy link
Member

Hi @markzakharyan sorry was on vacation, but now I'm back. If you can please test @mashegoindustries suggestion and update the PR if needed that would be great! I have also updated Dart model (it now has List<Message> lastMessages inside the room class) can you please use this array? (you can just put one value inside like this [message]). It is an array because in our future SaaS solution we will populate it with 10 or 20 latest messages, so when you enter the room you have not one, but complete first page of messages loaded.

@markzakharyan
Copy link
Contributor Author

markzakharyan commented Jul 14, 2021

Hi,

I think we may need to change the onWrite to onUpdate

This will ensure that rooms document will only get updated just once.

At the moment it updates twice, since the other cloud function (changeMessageStatus) will trigger another onWrite

So I simply changed from onWrite to onUpdate

exports.changeLastMessage = functions.firestore
.document("rooms/{roomId}/messages/{messageId}")
.onUpdate((change, context) => {
const message = change.after.data();
if (message) {
return db.doc("rooms/" + context.params.roomId).update({
lastMessage: message,
});
} else {
return null;
}
});

I just tested it, it works well. But maybe it would be better to have just one function maybe onMessageSend or something rather than multiple smaller functions? It just feels more elegant to have one function running on the Firestore update than two. Also, in the case of future functions being added, Functions billing would decrease because of fewer invocations. What do you think?@demchenkoalex @mashegoindustries

@demchenkoalex
Copy link
Member

I guess that's fine if everything still works :)

@SalahAdDin
Copy link
Contributor

@markzakharyan Could you also add a example function for the updateAt feature?

@markzakharyan
Copy link
Contributor Author

@markzakharyan Could you also add a example function for the updateAt feature?

I'm sorry @SalahAdDin what feature are you referring to?

@SalahAdDin
Copy link
Contributor

@markzakharyan Could you also add a example function for the updateAt feature?

I'm sorry @SalahAdDin what feature are you referring to?

@markzakharyan This one: https://github.com/flyerhq/flutter_firebase_chat_core/blob/main/lib/src/firebase_chat_core.dart#L179

@demchenkoalex
Copy link
Member

Can I merge this? Can't find an update to a single function. Request above is not related to this PR.

@markzakharyan
Copy link
Contributor Author

markzakharyan commented Jul 15, 2021

Can I merge this? Can't find an update to a single function. Request above is not related to this PR.

Go ahead.

I think it's better documentation if there's two functions. But in my particular usecase I'll just have one function.

@demchenkoalex demchenkoalex merged commit 8bd39c7 into flyerhq:main Jul 18, 2021
@demchenkoalex
Copy link
Member

Thanks for the PR!

@demchenkoalex
Copy link
Member

Updated lastMessage: message to lastMessages: [message] in this commit 12d47b9 so there is access to lastMessages in Flutter because this is what we have in types. Hopefully doesn't break anything :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants