channels.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 ChannelForm(BaseModel):
  42. name: str
  43. description: Optional[str] = None
  44. data: Optional[dict] = None
  45. meta: Optional[dict] = None
  46. access_control: Optional[dict] = None
  47. class ChannelTable:
  48. def insert_new_channel(
  49. self, type: Optional[str], form_data: ChannelForm, user_id: str
  50. ) -> Optional[ChannelModel]:
  51. with get_db() as db:
  52. channel = ChannelModel(
  53. **{
  54. **form_data.model_dump(),
  55. "type": type,
  56. "name": form_data.name.lower(),
  57. "id": str(uuid.uuid4()),
  58. "user_id": user_id,
  59. "created_at": int(time.time_ns()),
  60. "updated_at": int(time.time_ns()),
  61. }
  62. )
  63. new_channel = Channel(**channel.model_dump())
  64. db.add(new_channel)
  65. db.commit()
  66. return channel
  67. def get_channels(self) -> list[ChannelModel]:
  68. with get_db() as db:
  69. channels = db.query(Channel).all()
  70. return [ChannelModel.model_validate(channel) for channel in channels]
  71. def get_channels_by_user_id(
  72. self, user_id: str, permission: str = "read"
  73. ) -> list[ChannelModel]:
  74. channels = self.get_channels()
  75. return [
  76. channel
  77. for channel in channels
  78. if channel.user_id == user_id
  79. or has_access(user_id, permission, channel.access_control)
  80. ]
  81. def get_channel_by_id(self, id: str) -> Optional[ChannelModel]:
  82. with get_db() as db:
  83. channel = db.query(Channel).filter(Channel.id == id).first()
  84. return ChannelModel.model_validate(channel) if channel else None
  85. def update_channel_by_id(
  86. self, id: str, form_data: ChannelForm
  87. ) -> Optional[ChannelModel]:
  88. with get_db() as db:
  89. channel = db.query(Channel).filter(Channel.id == id).first()
  90. if not channel:
  91. return None
  92. channel.name = form_data.name
  93. channel.data = form_data.data
  94. channel.meta = form_data.meta
  95. channel.access_control = form_data.access_control
  96. channel.updated_at = int(time.time_ns())
  97. db.commit()
  98. return ChannelModel.model_validate(channel) if channel else None
  99. def delete_channel_by_id(self, id: str):
  100. with get_db() as db:
  101. db.query(Channel).filter(Channel.id == id).delete()
  102. db.commit()
  103. return True
  104. Channels = ChannelTable()