-
Notifications
You must be signed in to change notification settings - Fork 13.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Slack Avatar integration #27849
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
from __future__ import annotations | ||
|
||
import logging | ||
|
||
from flask_appbuilder.security.sqla.models import User | ||
|
||
from superset.daos.base import BaseDAO | ||
from superset.extensions import db | ||
from superset.models.user_attributes import UserAttribute | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class UserDAO(BaseDAO[User]): | ||
@staticmethod | ||
def get_by_id(user_id: int) -> User: | ||
return db.session.query(User).filter_by(id=user_id).one() | ||
|
||
@staticmethod | ||
def set_avatar_url(user: User, url: str) -> None: | ||
if user.extra_attributes: | ||
user.extra_attributes[0].avatar_url = url | ||
else: | ||
attrs = UserAttribute(avatar_url=url, user_id=user.id) | ||
user.extra_attributes = [attrs] | ||
db.session.add(attrs) | ||
db.session.commit() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# # Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
"""empty message | ||
Revision ID: c22cb5c2e546 | ||
Revises: be1b217cd8cd | ||
Create Date: 2024-04-01 22:44:40.386543 | ||
""" | ||
import sqlalchemy as sa | ||
from alembic import op | ||
from sqlalchemy.dialects import postgresql | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "c22cb5c2e546" | ||
down_revision = "be1b217cd8cd" | ||
|
||
|
||
def upgrade(): | ||
op.add_column( | ||
"user_attribute", sa.Column("avatar_url", sa.String(length=100), nullable=True) | ||
) | ||
|
||
|
||
def downgrade(): | ||
op.drop_column("user_attribute", "avatar_url") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
"""empty message | ||
|
||
Revision ID: bbf146925528 | ||
Revises: ('678eefb4ab44', 'c22cb5c2e546') | ||
Create Date: 2024-04-11 00:49:51.592325 | ||
|
||
""" | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "bbf146925528" | ||
down_revision = ("678eefb4ab44", "c22cb5c2e546") | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
|
||
def upgrade(): | ||
pass | ||
|
||
|
||
def downgrade(): | ||
pass |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
"""empty message | ||
|
||
Revision ID: 0dc386701747 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above, what would this migration do? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey, sorry about the confusion, but in alembic the migration "chain" can split and you end up with multiple There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. more about it here -> https://alembic.sqlalchemy.org/en/latest/branches.html |
||
Revises: ('5ad7321c2169', 'bbf146925528') | ||
Create Date: 2024-04-15 16:06:29.946059 | ||
|
||
""" | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "0dc386701747" | ||
down_revision = ("5ad7321c2169", "bbf146925528") | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
|
||
def upgrade(): | ||
pass | ||
|
||
|
||
def downgrade(): | ||
pass |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
|
||
from flask import current_app | ||
from slack_sdk import WebClient | ||
|
||
|
||
class SlackClientError(Exception): | ||
pass | ||
|
||
|
||
def get_slack_client() -> WebClient: | ||
token: str = current_app.config["SLACK_API_TOKEN"] | ||
if callable(token): | ||
token = token() | ||
return WebClient(token=token, proxy=current_app.config["SLACK_PROXY"]) | ||
|
||
|
||
def get_user_avatar(email: str, client: WebClient = None) -> str: | ||
client = client or get_slack_client() | ||
try: | ||
response = client.users_lookupByEmail(email=email) | ||
except Exception as ex: | ||
raise SlackClientError(f"Failed to lookup user by email: {email}") from ex | ||
|
||
user = response.data.get("user") | ||
if user is None: | ||
raise SlackClientError("No user found with that email.") | ||
|
||
profile = user.get("profile") | ||
if profile is None: | ||
raise SlackClientError("User found but no profile available.") | ||
|
||
avatar_url = profile.get("image_192") | ||
if avatar_url is None: | ||
raise SlackClientError("Profile image is not available.") | ||
|
||
return avatar_url |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does this migration do? See that upgrade and downgrade are just pass