From Funky Kitten, 6 Years ago, written in Plain Text.
Embed
  1.     def addUserToRoomm(self, room, user):
  2.         query = {"rid": room['_id'], "u._id": user['_id']}
  3.         subscription = self.db.rocketchat_subscription.find_one(query)
  4.         if subscription:
  5.             roles = []
  6.             if "roles" in subscription:
  7.                 if "owner" in subscription['roles']:
  8.                     raise ValueError("Already owner")
  9.                 roles = subscription['roles']
  10.             roles.append("owner")
  11.             self.db.rocketchat_subscription.update_one(query, {"$set": { "roles": roles }})
  12.         else:
  13.             subscription = {
  14.                 "open": True,
  15.                 "alert": True,
  16.                 "unread": 0,
  17.                 "userMentions": 0,
  18.                 "groupMentions": 0,
  19.                 "ts": room['ts'],
  20.                 "rid": room['_id'],
  21.                 "name": room['name'],
  22.                 "fname": room['fname'],
  23.                 "customFields": room['customFields'],
  24.                 "t": room['t'],
  25.                 "u": {
  26.                         "_id": user['_id'],
  27.                         "username": user['username'],
  28.                         "name": user['name'],
  29.                 },
  30.                 "roles": ["owner"]
  31.  
  32.             }
  33.             self.db.rocketchat_subscription.insert_one(subscription)
  34.             self.db.rocketchat_room.update({"_id": room['_id']}, {"$inc": {"usersCount": 1}})
  35.