1
0

channels.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import json
  2. import time
  3. import uuid
  4. from typing import Optional
  5. from open_webui.internal.db import Base, get_db
  6. from open_webui.utils.access_control import has_access
  7. from pydantic import BaseModel, ConfigDict
  8. from sqlalchemy import BigInteger, Boolean, Column, String, Text, JSON
  9. from sqlalchemy import or_, func, select, and_, text
  10. from sqlalchemy.sql import exists
  11. ####################
  12. # Channel DB Schema
  13. ####################
  14. class Channel(Base):
  15. __tablename__ = "channel"
  16. id = Column(Text, primary_key=True)
  17. user_id = Column(Text)
  18. type = Column(Text, nullable=True)
  19. name = Column(Text)
  20. description = Column(Text, nullable=True)
  21. data = Column(JSON, nullable=True)
  22. meta = Column(JSON, nullable=True)
  23. access_control = Column(JSON, nullable=True)
  24. created_at = Column(BigInteger)
  25. updated_at = Column(BigInteger)
  26. class ChannelModel(BaseModel):
  27. model_config = ConfigDict(from_attributes=True)
  28. id: str
  29. user_id: str
  30. type: Optional[str] = None
  31. name: str
  32. description: Optional[str] = None
  33. data: Optional[dict] = None
  34. meta: Optional[dict] = None
  35. access_control: Optional[dict] = None
  36. created_at: int # timestamp in epoch
  37. updated_at: int # timestamp in epoch
  38. ####################
  39. # Forms
  40. ####################
  41. class ChannelResponse(ChannelModel):
  42. write_access: bool = False
  43. class ChannelForm(BaseModel):
  44. name: str
  45. description: Optional[str] = None
  46. data: Optional[dict] = None
  47. meta: Optional[dict] = None
  48. access_control: Optional[dict] = None
  49. class ChannelTable:
  50. def insert_new_channel(
  51. self, type: Optional[str], form_data: ChannelForm, user_id: str
  52. ) -> Optional[ChannelModel]:
  53. with get_db() as db:
  54. channel = ChannelModel(
  55. **{
  56. **form_data.model_dump(),
  57. "type": type,
  58. "name": form_data.name.lower(),
  59. "id": str(uuid.uuid4()),
  60. "user_id": user_id,
  61. "created_at": int(time.time_ns()),
  62. "updated_at": int(time.time_ns()),
  63. }
  64. )
  65. new_channel = Channel(**channel.model_dump())
  66. db.add(new_channel)
  67. db.commit()
  68. return channel
  69. def get_channels(self) -> list[ChannelModel]:
  70. with get_db() as db:
  71. channels = db.query(Channel).all()
  72. return [ChannelModel.model_validate(channel) for channel in channels]
  73. def get_channels_by_user_id(
  74. self, user_id: str, permission: str = "read"
  75. ) -> list[ChannelModel]:
  76. channels = self.get_channels()
  77. return [
  78. channel
  79. for channel in channels
  80. if channel.user_id == user_id
  81. or has_access(user_id, permission, channel.access_control)
  82. ]
  83. def get_channel_by_id(self, id: str) -> Optional[ChannelModel]:
  84. with get_db() as db:
  85. channel = db.query(Channel).filter(Channel.id == id).first()
  86. return ChannelModel.model_validate(channel) if channel else None
  87. def update_channel_by_id(
  88. self, id: str, form_data: ChannelForm
  89. ) -> Optional[ChannelModel]:
  90. with get_db() as db:
  91. channel = db.query(Channel).filter(Channel.id == id).first()
  92. if not channel:
  93. return None
  94. channel.name = form_data.name
  95. channel.data = form_data.data
  96. channel.meta = form_data.meta
  97. channel.access_control = form_data.access_control
  98. channel.updated_at = int(time.time_ns())
  99. db.commit()
  100. return ChannelModel.model_validate(channel) if channel else None
  101. def delete_channel_by_id(self, id: str):
  102. with get_db() as db:
  103. db.query(Channel).filter(Channel.id == id).delete()
  104. db.commit()
  105. return True
  106. Channels = ChannelTable()