Coder Social home page Coder Social logo

Message class about chatexchange HOT 8 CLOSED

manishearth avatar manishearth commented on June 22, 2024
Message class

from chatexchange.

Comments (8)

Manishearth avatar Manishearth commented on June 22, 2024

So in essence the MessageEvent class gets converted to a shell that contains a Message object and reexports some Message methods?

from chatexchange.

banksJeremy avatar banksJeremy commented on June 22, 2024

It probably wouldn't even re-export any methods. You'd need to do event.message.reply(foo).

Having a distinct, de-duplicated Message object would let you do things like:

def on_event(event, wrapper):
    if isinstance(event, events.MessageStarred):
        print event.message.owner_name, "has a message starred!"

which would currently be impossible because the user_name associated with the MessageStarred event is the user doing the starring, not the owner of the starred message. This new approach would have that information available as long as we had previously observed.

Similarly, you would be able to easily:

    print "in reply to:", event.message.parent.text_content

instead of needing to keep track of the messages to find the one to associate with the parent_id yourself.

from chatexchange.

Manishearth avatar Manishearth commented on June 22, 2024

This would be nice.

We'd need to be able to fetch messages if they're not cached, though -- maybe by fetching the history. (there may be a json endpoint, too).

I'll look into this tomorrow.

-----Original Message-----
From: "Jeremy Banks" [email protected]
Sent: ‎5/‎4/‎2014 11:02 AM
To: "Manishearth/ChatExchange" [email protected]
Cc: "Manish Goregaokar" [email protected]
Subject: Re: [ChatExchange] Message class (#51)

We might also want a message.watch(on_change) method, which can be used to be notified when any properties on a message have changed, in case you need to re-render it in a client, for example.

Reply to this email directly or view it on GitHub.

from chatexchange.

banksJeremy avatar banksJeremy commented on June 22, 2024

I've implemented the basic Message class described above, without any capability to automatically fetch missing data (fields are left as None for now). The Message is just built out of information from Events seen by a wrapper.

We'll need to test different events to find out what the user_id represents for each. If edit events represent the editor, not the owner, then we should use those edit events to update .editor_user_name and .editor_user_id properties.

from chatexchange.

Manishearth avatar Manishearth commented on June 22, 2024

r+, if you're waiting for my review, otherwise, carry on :)

from chatexchange.

banksJeremy avatar banksJeremy commented on June 22, 2024

I was hoping for a review to make sure I wasn't doing anything disagreeable, but I'm not finished yet. :)

I'm adding a simple web-based chat viewer example, which I think will make a good way to show changes to previous Messages, when they're edited or whatever.

from chatexchange.

banksJeremy avatar banksJeremy commented on June 22, 2024

Keeping in mind your comments on #43...

Basically, I envision something like this:

def init(self):
  self.gettergroups={"access":False,"info":False,...}
  self.info()
def access(self):
  # do some GETting
  # initialize related params

@lazy('access','owner')
@property
def owner(self):
  "NOP" #The decorator does the magic.
#etc etc

I've implemented a pretty simple accessor for us to use when implementing lazy lookup of missing values.

class Message(object):
    # ...

    content_source = LazyFrom('_scrape_source')

    def _scrape_source(self):
        self.content_source = self.wrapper.getSomething(
            '/mesages/%s?plain=true' % (self.message_id,))

    room_id = LazyFrom('_scrape_timeline')
    content = LazyFrom('_scrape_timeline')

    def _scrape_timeline(self):
        # ...
        self.content = ...
        self.room_id = ...

The update methods just assign to the attributes as normal, the only difference is that when a attribute is missing, we invoke a specified method to populate it. Our Event code can continue setting/updating known attributes on their message instances, as they have been.

Here's the current implementation of LazyFrom:

class LazyFrom(object):
    """
    A accessor used when multiple lazy attributes depend on a common
    source of data.
    """
    def __init__(self, method_name, backing_attr_name):
        """
        method_name is the name of the method that will be invoked if
        the value is not known. It must asign a value for the attribute
        attribute (through this accessor).
        """
        self.method_name = method_name
        self.values = weakref.WeakKeyDictionary()

    def __get__(self, obj, cls):
        if obj is None:
            raise AttributeError()

        if obj not in self.values:
            method = getattr(obj, self.method_name)
            method()

        return self.values[obj]

    def __set__(self, obj, value):
        self.values[obj] = value

    def __delete__(self, obj):
        del self.values[obj]

Does this seem like an reasonable approach, @Manishearth?

from chatexchange.

Manishearth avatar Manishearth commented on June 22, 2024

This seems to be a good way of doing it. I find descriptors a bit annoying to work with, but that's a personal preference :)

from chatexchange.

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.