class User(db.Model): __tablename__ = "gino_users" id = db.Column(db.BigInteger(), primary_key=True) nickname = db.Column("name", db.Unicode(), default=_random_name) profile = db.Column("props", JSONB(), nullable=False, server_default="{}") parameter = db.Column("params", JSONB(), nullable=False, server_default="{}") type = db.Column( db.Enum(UserType), nullable=False, default=UserType.USER, ) realname = db.StringProperty() age = db.IntegerProperty(default=18) balance = db.IntegerProperty(default=0) birthday = db.DateTimeProperty( default=lambda i: datetime.utcfromtimestamp(0)) team_id = db.Column(db.ForeignKey("gino_teams.id")) weight = db.IntegerProperty(prop_name='parameter') height = db.IntegerProperty(default=170, prop_name='parameter') bio = db.StringProperty(prop_name='parameter') @balance.after_get def balance(self, val): if val is None: return 0.0 return float(val) def __repr__(self): return "{}<{}>".format(self.nickname, self.id)
class PropsTest(db.Model): __tablename__ = "props_test" profile = db.Column(JSONB(), nullable=False, server_default="{}") raw = db.JSONProperty() bool = db.BooleanProperty() obj = db.ObjectProperty() arr = db.ArrayProperty() parameter = db.Column(JSONB(), nullable=False, server_default="{}") raw_param = db.JSONProperty(prop_name="parameter") bool_param = db.BooleanProperty(prop_name="parameter") obj_param = db.ObjectProperty(prop_name="parameter") arr_param = db.ArrayProperty(prop_name="parameter")
class PropsTest(db.Model): __tablename__ = 'props_test_291' profile = db.Column(JSONB(), nullable=False, server_default='{}') profile1 = db.Column(JSON(), nullable=False, server_default='{}') bool = db.BooleanProperty() bool1 = db.BooleanProperty(column_name='profile1')
class Object(db.Model): """Objects are the Things in the fediverse. From the ActivityPub specifications: https://www.w3.org/TR/activitypub/#obj """ __tablename__ = 'objects' id = db.Column(db.Integer(), primary_key=True) uri = db.Column(db.String()) actor_uri = db.Column(db.String()) reply_to_uri = db.Column(db.String()) object_type = db.Column(db.String()) created = db.Column(db.DateTime()) created_by_actor_id = db.Column( db.Integer(), db.ForeignKey('actors.id', ondelete='SET NULL', name='fk_object_created_by_actor'), nullable=True, ) last_updated = db.Column(db.DateTime()) data = db.Column(JSONB())
class User(db.Model): __tablename__ = 'gino_users' id = db.Column(db.BigInteger(), primary_key=True) nickname = db.Column('name', db.Unicode(), default=_random_name) profile = db.Column('props', JSONB(), nullable=False, server_default='{}') type = db.Column( db.Enum(UserType), nullable=False, default=UserType.USER, ) realname = db.StringProperty() age = db.IntegerProperty(default=18) balance = db.IntegerProperty(default=0) birthday = db.DateTimeProperty( default=lambda i: datetime.utcfromtimestamp(0)) team_id = db.Column(db.ForeignKey('gino_teams.id')) @balance.after_get def balance(self, val): if val is None: return 0.0 return float(val) def __repr__(self): return '{}<{}>'.format(self.nickname, self.id)
class Account(db.Model): __tablename__ = 'account' id = db.Column(db.Integer(), primary_key=True) name = db.Column(db.String(), nullable=False) places = db.Column(JSONB(), nullable=False, server_default='{}') @staticmethod async def redis_get_id(redis, access_token): id = await redis.get('%s_id' % access_token) return int(id) if id is not None else id @staticmethod async def redis_set_id(redis, access_token, id): await redis.set('%s_id' % access_token, str(id)) @staticmethod async def redis_get_username(redis, access_token): username = await redis.get('%s_username' % access_token) return username.decode('utf-8') if username is not None else username @staticmethod async def redis_set_username(redis, access_token, username): await redis.set('%s_username' % access_token, username) @staticmethod async def redis_delete(redis, access_token): await redis.delete('%s_id' % access_token) await redis.delete('%s_username' % access_token)
class Import(db.Model): """Followed, muted, and blocked actors can be imported from ActivityPub files. These imports should not run automatically because of the potential for abuse, particularly from "follow" actions. Instead, they are stored here and can be selectively authorized to move forward by moderators. """ __tablename__ = 'imports' id = db.Column(db.Integer(), primary_key=True) request_by_account_id = db.Column( db.Integer(), db.ForeignKey( 'accounts.id', ondelete='CASCADE', name='fk_import_requested_by'), ) request_for_identity_id = db.Column( db.Integer(), db.ForeignKey( 'identities.id', ondelete='CASCADE', name='fk_import_for_identity'), ) data_to_import = db.Column(JSONB()) created = db.Column(db.DateTime()) last_updated = db.Column(db.DateTime()) processed = db.Column(db.DateTime()) allowed = db.Column(db.Boolean())
class ExchangeRates(db.Model): __tablename__ = "exchange_rates" date = db.Column(db.Date(), primary_key=True) rates = db.Column(JSONB()) def __repr__(self): return "Rates [{}]".format(self.date)
class ExchangeRates(db.Model): # type: ignore __tablename__ = 'exchange_rates' date = db.Column(db.Date(), primary_key=True) rates = db.Column(JSONB()) def __repr__(self): return 'Rate [{self.date}]'
class PropsTest(db.Model): __tablename__ = 'props_test' profile = db.Column(JSONB(), nullable=False, server_default='{}') raw = db.JSONProperty() bool = db.BooleanProperty() obj = db.ObjectProperty() arr = db.ArrayProperty()
class IndexTest(db.Model): __tablename__ = "index_test" profile = db.Column(JSONB()) age = db.IntegerProperty() @db.declared_attr def age_idx(cls): return db.Index("age_idx", cls.age)
class PropsTest(db.Model): __tablename__ = "props_test_291" profile = db.Column(JSONB(), nullable=False, server_default="{}") profile1 = db.Column(JSON(), nullable=False, server_default="{}") profile2 = db.Column(CustomJSONB(), nullable=False, server_default="{}") bool = db.BooleanProperty() bool1 = db.BooleanProperty(prop_name="profile1")
class Actor(db.Model): """Actually, actors are objects (or ActivityStreams), but I call them out explicitly, because they are a key component in how lamia sees the fediverse. From the ActivityPub specifications: https://www.w3.org/TR/activitypub/#actors All the world’s a stage, And all the identities and blogs merely actors - Snakespeare, As You Lamia It """ __tablename__ = 'actors' id = db.Column(db.Integer(), primary_key=True) actor_type = db.Column(db.String()) private_key = db.Column(db.String(), nullable=True) display_name = db.Column(db.String()) user_name = db.Column(db.String()) uri = db.Column(db.String()) local = db.Column(db.Boolean()) created = db.Column(db.DateTime()) last_updated = db.Column(db.DateTime()) data = db.Column(JSONB()) def generate_keys(self): """Create new keys and stuff them into an already created actor""" key = RSA.generate(2048) self.private_key = key.export_key("PEM").decode() try: del self.data['publicKey'] except KeyError: pass self.data['publicKey'] = { 'id': f'{BASE_URL}/u/{self.user_name}#main-key', 'owner': f'{BASE_URL}/u/{self.user_name}', 'publicKeyPem': key.publickey().export_key("PEM").decode() } # Convenience fields for local actors. identity_id = db.Column(db.Integer(), db.ForeignKey('identities.id', ondelete='SET NULL', name='fk_actor_identity'), nullable=True) blog_id = db.Column(db.Integer(), db.ForeignKey('blogs.id', ondelete='SET NULL', name='fk_actor_blog'), nullable=True)
class Setting(db.Model): """A basic key and value storage for settings. Note: From a philosophical standpoint, settings should all be optional, and built around non-essential functionality. The installation of lamia should require as little gymnastics as possible and should be, dare I say, Fun. TODO: figure out settings we may need here. """ __tablename__ = 'settings' id = db.Column(db.Integer(), primary_key=True) key = db.Column(db.String()) value = db.Column(JSONB())
class Activity(db.Model): """Activities are things that happen to other things on the fediverse. From the ActivityPub specifications: * https://www.w3.org/TR/activitypub/#client-to-server-interactions * https://www.w3.org/TR/activitypub/#server-to-server-interactions """ __tablename__ = 'activities' id = db.Column(db.Integer(), primary_key=True) uri = db.Column(db.String()) object_uri = db.Column(db.String()) actor_uri = db.Column(db.String()) activity_type = db.Column(db.String()) created = db.Column(db.DateTime()) data = db.Column(JSONB())
class Result(db.Model): __tablename__ = 'results' id = db.Column(db.BigInteger(), primary_key=True) url = db.Column(db.String(), nullable=False) entities = db.Column(JSONB())
class Base: id = db.Column(db.Integer(), primary_key=True) profile = db.Column(JSONB())
class PropsTest1(db.Model): __tablename__ = 'props_test1' profile = db.Column(JSONB(), nullable=False, server_default='{}') bool = db.BooleanProperty()
class PathTest(db.Model): __tablename__ = "path_test_json_path" data = db.Column(JSONB())
class PropsTest2(db.Model): __tablename__ = "props_test2" profile = db.Column(JSONB(), nullable=False, server_default="{}") bool_profile = db.BooleanProperty() bool_attr = db.Column(db.Boolean)
class GuildPrefs(Base): __tablename__ = 'guild_prefs' guild_id = db.Column(Snowflake(), primary_key=True) prefix = db.Column(db.String()) mute_role = db.Column(Snowflake()) admin_roles = db.Column(db.ARRAY(Snowflake())) mod_roles = db.Column(db.ARRAY(Snowflake())) invite_prefs: InvitePrefsColumn = db.Column( JSONB(), nullable=False, server_default='{"max_age": 0, "max_uses": 0, ' '"temporary": false, "unique": true}', ) max_age = db.IntegerProperty(prop_name='invite_prefs', default=0) max_uses = db.IntegerProperty(prop_name='invite_prefs', default=0) temporary = db.BooleanProperty(prop_name='invite_prefs', default=False) unique = db.BooleanProperty(prop_name='invite_prefs', default=True) time_out_role = db.Column(Snowflake()) __guild: discord.Guild @property def guild(self) -> discord.Guild: return self.__guild @property def guild_admin_roles(self) -> Iterator[discord.Role]: return (filter( None, map(lambda role_id: self.__guild.get_role(role_id), self.admin_roles), ) if self.admin_roles else iter([])) @property def guild_mod_roles(self) -> Iterator[discord.Role]: return (filter( None, map(lambda role_id: self.__guild.get_role(role_id), self.mod_roles), ) if self.mod_roles else iter([])) @property def guild_mute_role(self) -> Optional[discord.Role]: mute_role = self.__guild.get_role(self.mute_role or -1) return (discord.utils.get(self.__guild.roles, name='Muted') if mute_role is None else mute_role) @property def guild_time_out_role(self) -> Optional[discord.Role]: time_out_role = self.__guild.get_role(self.time_out_role or -1) return (discord.utils.get(self.__guild.roles, name='Time Out') if time_out_role is None else time_out_role) async def add_admin_role(self, role: discord.Role) -> None: if self.admin_roles is not None and role.id in self.admin_roles: return await self.update(admin_roles=db.func.array_append( GuildPrefs.admin_roles, str(role.id))).apply() async def remove_admin_role(self, role: discord.Role) -> None: await self.update(admin_roles=db.func.array_remove( GuildPrefs.admin_roles, str(role.id))).apply() async def add_mod_role(self, role: discord.Role) -> None: if self.mod_roles is not None and role.id in self.mod_roles: return await self.update( mod_roles=db.func.array_append(GuildPrefs.mod_roles, str(role.id)) ).apply() async def remove_mod_role(self, role: discord.Role) -> None: await self.update( mod_roles=db.func.array_remove(GuildPrefs.mod_roles, str(role.id)) ).apply() async def set_mute_role(self, role: Optional[discord.Role]) -> None: if role is None: role = discord.utils.get(self.__guild.roles, name='Muted') await self.update(mute_role=role.id if role is not None else None ).apply() @staticmethod async def for_guild(guild: discord.Guild) -> GuildPrefs: prefs = await GuildPrefs.query.where(GuildPrefs.guild_id == guild.id ).gino.first() assert prefs is not None prefs.__guild = guild return prefs