queries.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import random, secrets, datetime, uuid
  2. from slugify import slugify
  3. from common import log
  4. def create_or_get_and_update_last_event(self, table, event_type, channel_name=None, user_name=None, event_content=None, event_subject_name=None):
  5. # EVENT_TYPE_CHOICES = [
  6. # ('pm', 'private message'),
  7. # ('pa', 'private action'),
  8. # ('pn', 'private notice'),
  9. # ('cm', 'channel message')
  10. # ('ca', 'channel action'),
  11. # ('cn', 'channel notice'),
  12. # ('ct', 'channel topic'),
  13. # ('ck', 'channel password'),
  14. # ('ci', 'channel invite'),
  15. # ('cj', 'channel join'),
  16. # ('cp', 'channel part'),
  17. # ('ck', 'channel kick'),
  18. # ('kd', 'channel kicked'),
  19. # ('mc', 'mode change'),
  20. # ('nc', 'nick change'),
  21. # ('sq', 'quit'),
  22. # ]
  23. if table == 'channel':
  24. name = channel_name
  25. if table == 'user':
  26. name = user_name
  27. record = self.db.one('SELECT * FROM rotbot_' + table + ' WHERE LOWER(name)=LOWER(%(name)s) AND network_id=%(network_id)s', name=name, network_id=self.network.id) # Look up the record.
  28. if record: # On record.
  29. fields = 'name=%(name)s, last_event_type=%(last_event_type)s' # All required fields
  30. event_channel_id = None
  31. event_user_id = None
  32. event_subject_id = None
  33. if 'event_channel' in locals():
  34. event_channel_id = get_channel_id(self, channel_name)
  35. fields += ', last_event_channel_id=%(last_event_channel_id)s'
  36. if 'event_user' in locals():
  37. event_user_id = get_user_id(self, user_name)
  38. fields += ', last_event_user_id=%(last_event_user_id)s'
  39. if 'event_content' in locals():
  40. fields += ', last_event_content=%(last_event_content)s'
  41. if 'event_subject' in locals():
  42. event_subject_id = get_user_id(self, user_name)
  43. fields +- ', last_event_subject_id=%(last_event_subject_id)s'
  44. self.db.run('UPDATE rotbot_' + table + ' SET ' + fields + ' WHERE LOWER(name)=LOWER(%(name)s) AND network_id=%(network_id)s', name=name, network_id=self.network.id, last_event_type=event_type, last_event_channel_id=event_channel_id, last_event_user_id=event_user_id, last_event_content=event_content, last_event_subject_id=event_subject_id) # Correct capitalisation and update last event.
  45. else: # Not on record.
  46. new_slug = slugify('%s-%s' % (name, self.network.name), max_length=50) # Create a slug from the name.
  47. slug_exists = self.db.one('SELECT id FROM rotbot_' + table + ' WHERE slug=%(slug)s AND network_id=%(network_id)s', slug=new_slug, network_id=self.network.id) # Try to lookup a record with the proposed slug.
  48. # Keep adding random letters from the name to the slug until it is unique.
  49. while slug_exists and slug_exists <= 50:
  50. new_slug = new_slug + random.choice(name)
  51. slug_exists = self.db.one('SELECT id FROM rotbot_' + table + ' WHERE slug=%(slug)s AND network_id=%(network_id)s', slug=new_slug, network_id=self.network.id)
  52. log.notice('Requested slug already taken on table %s with network %s: %s' % (table, self.network.name, new_slug))
  53. while slug_exists:
  54. new_slug = name + uuid.uuid4()
  55. slug_exists = self.db.one('SELECT id FROM rotbot_' + table + ' WHERE slug=%(slug)s AND network_id=%(network_id)s', slug=new_slug, network_id=self.network.id)
  56. log.warn('Please clean database. Newly generated uuid4 already taken on table %s with network %s: %s' % (table, self.network.name, new_slug))
  57. fields = 'name, network_id, slug, last_event_type, last_event_datetime'
  58. values = '%(name)s, %(network_id)s, %(slug)s, %(last_event_type)s, %(last_event_datetime)s'
  59. event_datetime = datetime.datetime.now
  60. event_channel_id = None
  61. event_user_id = None
  62. event_subject_id = None
  63. if 'event_channel' in locals():
  64. event_channel_id = get_channel_id(self, channel_name)
  65. fields += ', last_event_channel_id'
  66. values += ', %(last_event_channel_id)s'
  67. if 'event_user' in locals():
  68. event_user_id = get_user_id(self, user_name)
  69. fields += ', last_event_user_id'
  70. values += ', %(last_event_user_id)s'
  71. if 'event_content' in locals():
  72. fields += ', last_event_content'
  73. values += ', %(last_event_content)s'
  74. if 'event_subject' in locals():
  75. event_subject_id = get_user_id(self, user_name)
  76. fields += ', last_event_subject_id'
  77. values += ', %(last_event_subject_id)s'
  78. if table == 'channel':
  79. self.db.run('INSERT INTO rotbot_' + table + ' (' + fields + ', autojoin, key, games) VALUES (' + values + ', %(autojoin)s, %(key)s, %(games)s)', name=name, network_id=self.network.id, slug=new_slug, last_event_type=event_type, last_event_datetime=datetime.datetime.now(), last_event_channel_id=event_channel_id, last_event_user_id=event_user_id, last_event_content=event_content, last_event_subject_id=event_subject_id, autojoin=False, key='', games=False) # Create record.
  80. else:
  81. self.db.run('INSERT INTO rotbot_' + table + ' (' + fields + ') VALUES (' + values + ')', name=name, network_id=self.network.id, slug=new_slug, last_event_type=event_type, last_event_datetime=datetime.datetime.now(), last_event_channel_id=event_channel_id, last_event_user_id=event_user_id, last_event_content=event_content, last_event_subject_id=event_subject_id) # Create record.
  82. record = self.db.one('SELECT * FROM rotbot_' + table + ' WHERE slug=%(slug)s', slug=new_slug) # Lookup newly created record to return to call.
  83. if event_type in ('cm', 'ca', 'cn'): # It's a public message, action or notice
  84. MessageStatistics.update(self, event, 'message', user, channel) # Update message statistics.
  85. return record
  86. def increment_join(self, channel, user):
  87. if not self.db.one('SELECT id FROM rotbot_join WHERE network_id=%(network_id)s AND channel_id=%(channel_id)s AND user_id=%(user_id)s', network_id=self.network.id, channel_id=channel.id, user_id=user.id): # No record yet
  88. self.db.run('INSERT INTO rotbot_join (network_id, channel_id, user_id, amount) VALUES (%(network_id)s, %(channel_id)s, %(user_id)s, 1)', network_id=self.network.id, channel_id=channel.id, user_id=user.id) # Create record.
  89. else:
  90. self.db.run('UPDATE rotbot_join SET amount = amount + 1 WHERE network_id=%(network_id)s AND channel_id=%(channel_id)s AND user_id=%(user_id)s', network_id=self.network.id, channel_id=channel.id, user_id=user.id) # Update existing record.
  91. def get_channel_id(self, channel_name):
  92. return self.db.one('SELECT id FROM rotbot_channel WHERE network_id=%(network_id)s AND LOWER(name)=LOWER(%(channel_name)s)', network_id=self.network.id, channel_name=channel_name)
  93. def get_channel_slug(self, channel_name):
  94. return self.db.one('SELECT slug FROM rotbot_channel WHERE network_id=%(network_id)s AND LOWER(name)=LOWER(%(channel_name)s)', network_id=self.network.id, channel_name=channel_name)
  95. def get_channel_setting_statistic_commands(self, channel_name):
  96. return self.db.one('SELECT statistic_commands FROM rotbot_channel WHERE LOWER(name)=LOWER(%(channel_name)s) AND network_id=%(network_id)s', channel_name=channel_name, network_id=self.network.id)
  97. def get_channel_setting_game_commands(self, channel_name):
  98. return self.db.one('SELECT games FROM rotbot_channel WHERE name=%(channel_name)s AND network_id=%(network_id)', channel_name=channel_name, network_id=self.network.id)
  99. def update_channel_last_event(self, channel, event_type, event_content):
  100. self.db.run('UPDATE rotbot_channel SET last_event_type=%(last_event_type)s, last_event_content=%(last_event_content)s WHERE channel.id=%(channel_id)s', last_event_type=event_type, last_event_content=event_content)
  101. def create_tempchannelkey(self, channel_id):
  102. temp_key = secrets.token_urlsafe(40)[:40]
  103. self.db.run('INSERT INTO rotbot_tempchannelkey (key, network_id, channel_id, created) VALUES (%(key)s, %(network_id)s, %(channel_id)s, %(created)s)', key=temp_key, network_id=self.network.id, channel_id=channel_id, created=datetime.datetime.now())
  104. return temp_key
  105. def get_user_id(self, user_name):
  106. return self.db.one('SELECT id FROM rotbot_user WHERE network_id=%(network_id)s AND LOWER(name)=LOWER(%(user_name)s)', network_id=self.network.id, user_name=user_name)
  107. def get_userl_slug(self, user_name):
  108. return self.db.one('SELECT slug FROM rotbot_user WHERE network_id=%(network_id)s AND LOWER(name)=LOWER(%(user_name)s)', network_id=self.network.id, user_name=user_name)