'Args:
room (JoinedSyncResult|ArchivedSyncResult): sync result for a
single room
time_now (int): current time - used as a baseline for age
calculations
token_id (int): ID of the user\'s auth token - used for namespacing
of transaction IDs
joined (bool): True if the user is joined to this room - will mean
we handle ephemeral events
only_fields(list<str>): Optional. The list of event fields to include.
Returns:
dict[str, object]: the room, encoded in our response format'

@staticmethod
def encode_room(room, time_now, token_id, joined=True, only_fields=None):

    def serialize(event):
        return serialize_event(event, time_now, token_id=token_id, event_format=format_event_for_client_v2_without_room_id, only_event_fields=only_fields)
    state_dict = room.state
    timeline_events = room.timeline.events
    state_events = state_dict.values()
    for event in itertools.chain(state_events, timeline_events):
        if (event.room_id != room.room_id):
            logger.warn('Event    %r    is    under    room    %r    instead    of    %r', event.event_id, room.room_id, event.room_id)
    serialized_state = [serialize(e) for e in state_events]
    serialized_timeline = [serialize(e) for e in timeline_events]
    account_data = room.account_data
    result = {'timeline': {'events': serialized_timeline, 'prev_batch': room.timeline.prev_batch.to_string(), 'limited': room.timeline.limited}, 'state': {'events': serialized_state}, 'account_data': {'events': account_data}}
    if joined:
        ephemeral_events = room.ephemeral
        result['ephemeral'] = {'events': ephemeral_events}
        result['unread_notifications'] = room.unread_notifications
    return result
