Giter VIP home page Giter VIP logo

Comments (7)

demchenkoalex avatar demchenkoalex commented on August 18, 2024 1

Not at the moment I think, but we are open for proposals

from flutter_firebase_chat_core.

demchenkoalex avatar demchenkoalex commented on August 18, 2024

Hi @markzakharyan!

We can add lastMessages array to the room type for convenience (we planned to do so anyway for our own backend version) but to make it work with Firebase you will need to write a cloud function which will update that field when new messages come in. Will that work?

from flutter_firebase_chat_core.

markzakharyan avatar markzakharyan commented on August 18, 2024

Hmmm maybe that could be written in the same firebase function as the one you already have in the docs. Would this work?

import * as functions from 'firebase-functions';

const admin = require('firebase-admin');
admin.initializeApp();

const db = admin.firestore();


export const changeLastMessage = functions.firestore
    .document('rooms/{roomId}/messages/{messageId')
    .onWrite((change) => {
        const message = change.after.data()
        if (message) {
            db.doc('rooms/{roomId}').set({
                lastMessage: message,
              });
        } else {
          return null
        }
      })

export const changeMessageStatus = functions.firestore
  .document('rooms/{roomId}/messages/{messageId}')
  .onWrite((change) => {
    const message = change.after.data()
    if (message) {
      if (['delivered', 'read'].includes(message.status)) {
        return null
      } else {
        return change.after.ref.update({
          status: 'delivered',
        });
      }
    } else {
      return null
    }
  })

I'd also be curious about making adding "seen" functionality to it as well.

from flutter_firebase_chat_core.

demchenkoalex avatar demchenkoalex commented on August 18, 2024

maybe, I'm not a cloud function expert and this is more "backend" related part. What we did provide in the example is more like an entry point on what can/should be done via cloud functions and then sky is the limit. We for sure can't provide them out of the box, because they are not related to Flutter in any way. Also, all cloud functions are now requiring paid plan and of course usage will be different for different people, another reason why we can't give you everything. What we can do is receive a tested solution from somebody in the community and add it to the docs, as an example implementation.

Regarding the "seen" status - there is a support for that in types and UI (if UI is used), but again somebody who knows backend and/or cloud functions can help out with the possible implementation and we can add whatever is needed (like a timestamp sent when user opens the chat or anything) to the core package.

from flutter_firebase_chat_core.

markzakharyan avatar markzakharyan commented on August 18, 2024

I made a PR containing firebase functions code adding latestMessage to firestore.

Here's code that works really well that retrieves the latest message:

Widget latestMessageFormat(String text) {
  return Text(text,
      style: TextStyle(color: Colors.grey, fontSize: 15, height: 1.1),
      maxLines: 2,
      overflow: TextOverflow.ellipsis);
}


var latestMessageDisplayText;
Widget _latestMessage(types.Room room) {
  return StreamBuilder<DocumentSnapshot>(
      stream: FirebaseFirestore.instance
          .collection('rooms')
          .doc('${room.id}')
          .snapshots(),
      builder:
          (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
        if (snapshot.hasError) {
          return Text('Something went wrong collecting the latest message');
        }

        if (snapshot.connectionState == ConnectionState.waiting) {
          return latestMessageFormat(latestMessageDisplayText == null
              ? 'Loading...'
              : latestMessageDisplayText);
        }

        Map<String, dynamic> dataMap = snapshot.data!.data() as Map<String, dynamic>; //When in doubt, use a hashmap :D
        latestMessageDisplayText = dataMap.containsKey('lastMessage')
            ? dataMap['lastMessage']['text']
            : 'No messages yet';

        return latestMessageFormat(latestMessageDisplayText);
      });
}

The reason there's latestMessageDisplayText is to prevent flickering "Loading..." when a message is uploaded.

I'm pretty new to dart/flutter so I'm sure it could be improved but I'm actually pretty proud of it. It works well, so if anyone needs it, here it is to save the hassle.

from flutter_firebase_chat_core.

markzakharyan avatar markzakharyan commented on August 18, 2024

@demchenkoalex is there a way I can check if the user has seen the latest message? (or be at the bottom of the scroll) to check for their last read message

from flutter_firebase_chat_core.

demchenkoalex avatar demchenkoalex commented on August 18, 2024

Everything related to the seen status we can continue here #9 and regarding the initial issue discussion is in #16. Closing this one.

from flutter_firebase_chat_core.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.