You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

51 lines
1.6 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014 matrix.org
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from synapse.api.events.room import (
  16. RoomTopicEvent, MessageEvent, RoomMemberEvent, FeedbackEvent,
  17. InviteJoinEvent, RoomConfigEvent
  18. )
  19. from synapse.util.stringutils import random_string
  20. class EventFactory(object):
  21. _event_classes = [
  22. RoomTopicEvent,
  23. MessageEvent,
  24. RoomMemberEvent,
  25. FeedbackEvent,
  26. InviteJoinEvent,
  27. RoomConfigEvent
  28. ]
  29. def __init__(self):
  30. self._event_list = {} # dict of TYPE to event class
  31. for event_class in EventFactory._event_classes:
  32. self._event_list[event_class.TYPE] = event_class
  33. def create_event(self, etype=None, **kwargs):
  34. kwargs["type"] = etype
  35. if "event_id" not in kwargs:
  36. kwargs["event_id"] = random_string(10)
  37. try:
  38. handler = self._event_list[etype]
  39. except KeyError: # unknown event type
  40. # TODO allow custom event types.
  41. raise NotImplementedError("Unknown etype=%s" % etype)
  42. return handler(**kwargs)