2024-01-03 16:29:58 +13:00
from datetime import datetime , timedelta
2023-12-21 22:14:43 +13:00
from time import sleep
2024-05-22 12:16:35 +01:00
from random import randint
2023-10-10 22:25:37 +13:00
from typing import List
2024-09-15 19:30:45 +12:00
import httpx
2023-11-29 20:32:07 +13:00
from PIL import Image , ImageOps
2024-03-15 22:32:48 +00:00
from flask import request , abort , g , current_app , json
2023-11-30 20:57:51 +13:00
from flask_login import current_user
from pillow_heif import register_heif_opener
2023-11-29 20:32:07 +13:00
2023-12-25 21:44:10 +13:00
from app import db , cache , celery
2025-01-19 19:31:31 +00:00
from app . activitypub . signature import post_request , default_context , signed_get_request
2024-05-12 13:02:45 +12:00
from app . activitypub . util import find_actor_or_create , actor_json_to_model , post_json_to_model , ensure_domains_match , \
2025-01-19 19:31:31 +00:00
find_hashtag_or_create , create_post
2024-05-16 21:53:38 +12:00
from app . constants import POST_TYPE_ARTICLE , POST_TYPE_LINK , POST_TYPE_IMAGE , POST_TYPE_VIDEO , NOTIF_POST , \
POST_TYPE_POLL
2024-10-22 19:51:37 +13:00
from app . models import Community , File , BannedInstances , PostReply , Post , utcnow , CommunityMember , Site , \
2024-10-30 09:21:54 +13:00
Instance , Notification , User , ActivityPubLog , NotificationSubscription , PollChoice , Poll , Tag
2024-10-22 19:51:37 +13:00
from app . utils import get_request , gibberish , markdown_to_html , domain_from_url , \
is_image_url , ensure_directory_exists , shorten_string , \
2024-09-22 13:42:02 +00:00
remove_tracking_from_link , ap_datetime , instance_banned , blocked_phrases , url_to_thumbnail_file , opengraph_parse , \
2024-11-14 16:28:38 +13:00
piefed_markdown_to_lemmy_markdown , get_task_session
2024-05-12 13:02:45 +12:00
from sqlalchemy import func , desc , text
2023-11-27 22:05:35 +13:00
import os
2023-08-29 22:01:06 +12:00
2024-12-06 10:21:44 +13:00
allowed_extensions = [ ' .gif ' , ' .jpg ' , ' .jpeg ' , ' .png ' , ' .webp ' , ' .heic ' , ' .mpo ' , ' .avif ' , ' .svg ' ]
2023-12-08 17:13:38 +13:00
2023-12-21 22:14:43 +13:00
2023-08-29 22:01:06 +12:00
def search_for_community ( address : str ) :
if address . startswith ( ' ! ' ) :
name , server = address [ 1 : ] . split ( ' @ ' )
banned = BannedInstances . query . filter_by ( domain = server ) . first ( )
if banned :
reason = f " Reason: { banned . reason } " if banned . reason is not None else ' '
2023-09-05 20:25:02 +12:00
raise Exception ( f " { server } is blocked. { reason } " ) # todo: create custom exception class hierarchy
2023-08-29 22:01:06 +12:00
2024-08-16 11:48:56 +12:00
if current_app . config [ ' SERVER_NAME ' ] == server :
already_exists = Community . query . filter_by ( name = name , ap_id = None ) . first ( )
return already_exists
2023-08-29 22:01:06 +12:00
already_exists = Community . query . filter_by ( ap_id = address [ 1 : ] ) . first ( )
if already_exists :
return already_exists
# Look up the profile address of the community using WebFinger
2024-04-24 16:04:49 +01:00
try :
webfinger_data = get_request ( f " https:// { server } /.well-known/webfinger " ,
params = { ' resource ' : f " acct: { address [ 1 : ] } " } )
2024-09-15 19:30:45 +12:00
except httpx . HTTPError :
2024-05-22 12:16:35 +01:00
sleep ( randint ( 3 , 10 ) )
2024-04-24 16:04:49 +01:00
try :
webfinger_data = get_request ( f " https:// { server } /.well-known/webfinger " ,
params = { ' resource ' : f " acct: { address [ 1 : ] } " } )
2024-09-15 19:30:45 +12:00
except httpx . HTTPError :
2024-04-24 16:04:49 +01:00
return None
2024-09-15 19:30:45 +12:00
2023-08-29 22:01:06 +12:00
if webfinger_data . status_code == 200 :
webfinger_json = webfinger_data . json ( )
for links in webfinger_json [ ' links ' ] :
2023-09-05 20:25:02 +12:00
if ' rel ' in links and links [ ' rel ' ] == ' self ' : # this contains the URL of the activitypub profile
2023-08-29 22:01:06 +12:00
type = links [ ' type ' ] if ' type ' in links else ' application/activity+json '
# retrieve the activitypub profile
community_data = get_request ( links [ ' href ' ] , headers = { ' Accept ' : type } )
# to see the structure of the json contained in community_data, do a GET to https://lemmy.world/c/technology with header Accept: application/activity+json
if community_data . status_code == 200 :
community_json = community_data . json ( )
2023-12-21 22:14:43 +13:00
community_data . close ( )
2023-08-29 22:01:06 +12:00
if community_json [ ' type ' ] == ' Group ' :
2024-01-07 09:29:36 +13:00
community = actor_json_to_model ( community_json , name , server )
2024-01-21 21:04:48 +13:00
if community :
if current_app . debug :
2025-01-19 19:31:31 +00:00
retrieve_mods_and_backfill ( community . id , server , name , community_json )
2024-01-21 21:04:48 +13:00
else :
2025-01-19 19:31:31 +00:00
retrieve_mods_and_backfill . delay ( community . id , server , name , community_json )
2023-08-29 22:01:06 +12:00
return community
return None
2023-09-05 20:25:02 +12:00
2025-01-19 19:31:31 +00:00
def remote_object_to_json ( uri ) :
try :
object_request = get_request ( uri , headers = { ' Accept ' : ' application/activity+json ' } )
except httpx . HTTPError :
time . sleep ( 3 )
try :
object_request = get_request ( uri , headers = { ' Accept ' : ' application/activity+json ' } )
except httpx . HTTPError :
return None
if object_request . status_code == 200 :
try :
object = object_request . json ( )
return object
except :
object_request . close ( )
return None
object_request . close ( )
elif object_request . status_code == 401 :
try :
site = Site . query . get ( 1 )
object_request = signed_get_request ( uri , site . private_key , f " https:// { current_app . config [ ' SERVER_NAME ' ] } /actor#main-key " )
except httpx . HTTPError :
time . sleep ( 3 )
try :
object_request = signed_get_request ( uri , site . private_key , f " https:// { current_app . config [ ' SERVER_NAME ' ] } /actor#main-key " )
except httpx . HTTPError :
return None
try :
object = object_request . json ( )
return object
except :
object_request . close ( )
return None
object_request . close ( )
else :
return None
2024-05-26 01:37:14 +01:00
2023-12-25 21:44:10 +13:00
@celery.task
2025-01-19 19:31:31 +00:00
def retrieve_mods_and_backfill ( community_id : int , server , name , community_json = None ) :
2023-12-25 21:44:10 +13:00
with current_app . app_context ( ) :
community = Community . query . get ( community_id )
2025-01-19 19:31:31 +00:00
if not community :
return
2023-12-25 21:44:10 +13:00
site = Site . query . get ( 1 )
2025-01-19 19:31:31 +00:00
is_peertube = is_guppe = is_wordpress = False
if community . ap_profile_id == f " https:// { server } /video-channels/ { name } " :
is_peertube = True
elif community . ap_profile_id . startswith ( ' https://a.gup.pe/u ' ) :
is_guppe = True
# get mods
if community_json and ' attributedTo ' in community_json :
mods = community_json [ ' attributedTo ' ]
if isinstance ( mods , list ) :
for m in mods :
if ' type ' in m and m [ ' type ' ] == ' Person ' and ' id ' in m :
mod = find_actor_or_create ( m [ ' id ' ] )
if mod :
existing_membership = CommunityMember . query . filter_by ( community_id = community . id , user_id = mod . id ) . first ( )
2023-12-21 22:14:43 +13:00
if existing_membership :
existing_membership . is_moderator = True
else :
2025-01-19 19:31:31 +00:00
new_membership = CommunityMember ( community_id = community . id , user_id = mod . id , is_moderator = True )
2023-12-21 22:14:43 +13:00
db . session . add ( new_membership )
2025-01-19 19:31:31 +00:00
elif community . ap_moderators_url :
mods_data = remote_object_to_json ( community . ap_moderators_url )
if mods_data and mods_data [ ' type ' ] == ' OrderedCollection ' and ' orderedItems ' in mods_data :
for actor in mods_data [ ' orderedItems ' ] :
sleep ( 0.5 )
mod = find_actor_or_create ( actor )
if mod :
existing_membership = CommunityMember . query . filter_by ( community_id = community . id , user_id = mod . id ) . first ( )
if existing_membership :
existing_membership . is_moderator = True
else :
new_membership = CommunityMember ( community_id = community . id , user_id = mod . id , is_moderator = True )
db . session . add ( new_membership )
if is_peertube :
community . restricted_to_mods = True
db . session . commit ( )
2023-12-21 22:14:43 +13:00
# only backfill nsfw if nsfw communities are allowed
2023-12-25 21:44:10 +13:00
if ( community . nsfw and not site . enable_nsfw ) or ( community . nsfl and not site . enable_nsfl ) :
2023-12-21 22:14:43 +13:00
return
2025-01-19 19:31:31 +00:00
# download 50 old posts from unpaginated outboxes or 10 posts from page 1 if outbox is paginated (with Celery, or just 2 without)
2024-11-30 14:22:48 +13:00
if community . ap_outbox_url :
2025-01-19 19:31:31 +00:00
outbox_data = remote_object_to_json ( community . ap_outbox_url )
if not outbox_data or ( ' totalItems ' in outbox_data and outbox_data [ ' totalItems ' ] == 0 ) :
return
if ' first ' in outbox_data :
outbox_data = remote_object_to_json ( outbox_data [ ' first ' ] )
if not outbox_data :
return
max = 10
else :
max = 50
if current_app . debug :
max = 2
if ' type ' in outbox_data and ( outbox_data [ ' type ' ] == ' OrderedCollection ' or outbox_data [ ' type ' ] == ' OrderedCollectionPage ' ) and ' orderedItems ' in outbox_data :
activities_processed = 0
for announce in outbox_data [ ' orderedItems ' ] :
activity = None
if is_peertube or is_guppe :
activity = remote_object_to_json ( announce [ ' object ' ] )
elif ' object ' in announce and ' object ' in announce [ ' object ' ] :
activity = announce [ ' object ' ] [ ' object ' ]
elif ' type ' in announce and announce [ ' type ' ] == ' Create ' :
activity = announce [ ' object ' ]
is_wordpress = True
if not activity :
return
if not ensure_domains_match ( activity ) :
continue
if is_peertube :
user = mod
elif ' attributedTo ' in activity and isinstance ( activity [ ' attributedTo ' ] , str ) :
user = find_actor_or_create ( activity [ ' attributedTo ' ] )
if not user :
continue
else :
continue
if user . is_local ( ) :
continue
if is_peertube or is_guppe :
request_json = { ' id ' : f " https:// { server } /activities/create/ { gibberish ( 15 ) } " , ' object ' : activity }
elif is_wordpress :
request_json = announce
else :
request_json = announce [ ' object ' ]
post = create_post ( True , community , request_json , user , announce [ ' id ' ] )
if post :
if ' published ' in activity :
post . posted_at = activity [ ' published ' ]
post . last_active = activity [ ' published ' ]
2024-03-15 22:32:48 +00:00
db . session . commit ( )
2025-01-19 19:31:31 +00:00
activities_processed + = 1
if activities_processed > = max :
break
if community . post_count > 0 :
community . last_active = Post . query . filter ( Post . community_id == community . id ) . order_by ( desc ( Post . posted_at ) ) . first ( ) . posted_at
2023-12-21 22:14:43 +13:00
db . session . commit ( )
2025-01-19 19:31:31 +00:00
if community . ap_featured_url :
featured_data = remote_object_to_json ( community . ap_featured_url )
if featured_data and ' type ' in featured_data and featured_data [ ' type ' ] == ' OrderedCollection ' and ' orderedItems ' in featured_data :
for item in featured_data [ ' orderedItems ' ] :
featured_id = item [ ' id ' ]
p = Post . query . filter ( Post . ap_id == featured_id ) . first ( )
if p :
p . sticky = True
db . session . commit ( )
2023-12-21 22:14:43 +13:00
2023-09-17 21:19:51 +12:00
def actor_to_community ( actor ) - > Community :
actor = actor . strip ( )
if ' @ ' in actor :
community = Community . query . filter_by ( banned = False , ap_id = actor ) . first ( )
else :
2024-02-09 14:58:51 +13:00
community = Community . query . filter ( func . lower ( Community . name ) == func . lower ( actor ) ) . filter_by ( banned = False , ap_id = None ) . first ( )
2023-09-17 21:19:51 +12:00
return community
2023-10-10 22:25:37 +13:00
2024-05-16 21:53:38 +12:00
def end_poll_date ( end_choice ) :
delta_mapping = {
' 30m ' : timedelta ( minutes = 30 ) ,
' 1h ' : timedelta ( hours = 1 ) ,
' 6h ' : timedelta ( hours = 6 ) ,
' 12h ' : timedelta ( hours = 12 ) ,
' 1d ' : timedelta ( days = 1 ) ,
' 3d ' : timedelta ( days = 3 ) ,
' 7d ' : timedelta ( days = 7 )
}
if end_choice in delta_mapping :
return datetime . utcnow ( ) + delta_mapping [ end_choice ]
else :
raise ValueError ( " Invalid choice " )
2024-10-16 21:42:30 +13:00
def tags_from_string ( tags : str ) - > List [ dict ] :
2024-05-12 13:02:45 +12:00
return_value = [ ]
tags = tags . strip ( )
if tags == ' ' :
return [ ]
tag_list = tags . split ( ' , ' )
tag_list = [ tag . strip ( ) for tag in tag_list ]
for tag in tag_list :
if tag [ 0 ] == ' # ' :
tag = tag [ 1 : ]
tag_to_append = find_hashtag_or_create ( tag )
if tag_to_append :
2024-10-16 21:42:30 +13:00
return_value . append ( { ' type ' : ' Hashtag ' , ' name ' : tag_to_append . name } )
2024-05-12 13:02:45 +12:00
return return_value
2024-10-30 09:19:32 +13:00
def tags_from_string_old ( tags : str ) - > List [ Tag ] :
return_value = [ ]
tags = tags . strip ( )
if tags == ' ' :
return [ ]
tag_list = tags . split ( ' , ' )
tag_list = [ tag . strip ( ) for tag in tag_list ]
for tag in tag_list :
if tag [ 0 ] == ' # ' :
tag = tag [ 1 : ]
tag_to_append = find_hashtag_or_create ( tag )
if tag_to_append :
return_value . append ( tag_to_append )
return return_value
2024-03-15 14:24:45 +13:00
def delete_post_from_community ( post_id ) :
if current_app . debug :
delete_post_from_community_task ( post_id )
else :
delete_post_from_community_task . delay ( post_id )
@celery.task
def delete_post_from_community_task ( post_id ) :
post = Post . query . get ( post_id )
community = post . community
2024-06-02 16:45:21 +12:00
post . deleted = True
2024-10-25 06:17:10 +00:00
post . deleted_by = current_user . id
2024-03-15 14:24:45 +13:00
db . session . commit ( )
if not community . local_only :
delete_json = {
' id ' : f " https:// { current_app . config [ ' SERVER_NAME ' ] } /activities/delete/ { gibberish ( 15 ) } " ,
' type ' : ' Delete ' ,
2024-06-05 13:21:41 +12:00
' actor ' : current_user . public_url ( ) ,
' audience ' : post . community . public_url ( ) ,
' to ' : [ post . community . public_url ( ) , ' https://www.w3.org/ns/activitystreams#Public ' ] ,
2024-03-15 14:24:45 +13:00
' published ' : ap_datetime ( utcnow ( ) ) ,
' cc ' : [
current_user . followers_url ( )
] ,
' object ' : post . ap_id ,
}
if not post . community . is_local ( ) : # this is a remote community, send it to the instance that hosts it
success = post_request ( post . community . ap_inbox_url , delete_json , current_user . private_key ,
2024-06-05 13:21:41 +12:00
current_user . public_url ( ) + ' #main-key ' )
2024-03-15 14:24:45 +13:00
else : # local community - send it to followers on remote instances
announce = {
" id " : f " https:// { current_app . config [ ' SERVER_NAME ' ] } /activities/announce/ { gibberish ( 15 ) } " ,
" type " : ' Announce ' ,
" to " : [
" https://www.w3.org/ns/activitystreams#Public "
] ,
" actor " : post . community . ap_profile_id ,
" cc " : [
post . community . ap_followers_url
] ,
' @context ' : default_context ( ) ,
' object ' : delete_json
}
for instance in post . community . following_instances ( ) :
if instance . inbox and not current_user . has_blocked_instance ( instance . id ) and not instance_banned (
instance . domain ) :
send_to_remote_instance ( instance . id , post . community . id , announce )
def delete_post_reply_from_community ( post_reply_id ) :
if current_app . debug :
delete_post_reply_from_community_task ( post_reply_id )
else :
delete_post_reply_from_community_task . delay ( post_reply_id )
@celery.task
def delete_post_reply_from_community_task ( post_reply_id ) :
post_reply = PostReply . query . get ( post_reply_id )
post = post_reply . post
community = post . community
if post_reply . user_id == current_user . id or community . is_moderator ( ) :
2024-10-18 08:33:50 +00:00
post_reply . deleted = True
post_reply . deleted_by = current_user . id
2024-03-15 14:24:45 +13:00
db . session . commit ( )
2024-04-20 16:26:33 +12:00
2024-03-15 14:24:45 +13:00
# federate delete
if not post . community . local_only :
delete_json = {
' id ' : f " https:// { current_app . config [ ' SERVER_NAME ' ] } /activities/delete/ { gibberish ( 15 ) } " ,
' type ' : ' Delete ' ,
2024-06-05 13:21:41 +12:00
' actor ' : current_user . public_url ( ) ,
' audience ' : post . community . public_url ( ) ,
' to ' : [ post . community . public_url ( ) , ' https://www.w3.org/ns/activitystreams#Public ' ] ,
2024-03-15 14:24:45 +13:00
' published ' : ap_datetime ( utcnow ( ) ) ,
' cc ' : [
current_user . followers_url ( )
] ,
' object ' : post_reply . ap_id ,
}
if not post . community . is_local ( ) : # this is a remote community, send it to the instance that hosts it
success = post_request ( post . community . ap_inbox_url , delete_json , current_user . private_key ,
2024-06-05 13:21:41 +12:00
current_user . public_url ( ) + ' #main-key ' )
2024-03-15 14:24:45 +13:00
else : # local community - send it to followers on remote instances
announce = {
" id " : f " https:// { current_app . config [ ' SERVER_NAME ' ] } /activities/announce/ { gibberish ( 15 ) } " ,
" type " : ' Announce ' ,
" to " : [
" https://www.w3.org/ns/activitystreams#Public "
] ,
" actor " : post . community . ap_profile_id ,
" cc " : [
post . community . ap_followers_url
] ,
' @context ' : default_context ( ) ,
' object ' : delete_json
}
for instance in post . community . following_instances ( ) :
if instance . inbox and not current_user . has_blocked_instance ( instance . id ) and not instance_banned (
instance . domain ) :
send_to_remote_instance ( instance . id , post . community . id , announce )
2023-11-30 20:57:51 +13:00
def remove_old_file ( file_id ) :
remove_file = File . query . get ( file_id )
remove_file . delete_from_disk ( )
2023-12-08 17:13:38 +13:00
2023-12-25 21:44:10 +13:00
def save_icon_file ( icon_file , directory = ' communities ' ) - > File :
2023-12-08 17:13:38 +13:00
# check if this is an allowed type of file
file_ext = os . path . splitext ( icon_file . filename ) [ 1 ]
2024-01-25 20:16:08 +13:00
if file_ext . lower ( ) not in allowed_extensions :
2023-12-08 17:13:38 +13:00
abort ( 400 )
new_filename = gibberish ( 15 )
# set up the storage directory
2023-12-25 21:44:10 +13:00
directory = f ' app/static/media/ { directory } / ' + new_filename [ 0 : 2 ] + ' / ' + new_filename [ 2 : 4 ]
2023-12-08 17:13:38 +13:00
ensure_directory_exists ( directory )
# save the file
final_place = os . path . join ( directory , new_filename + file_ext )
final_place_thumbnail = os . path . join ( directory , new_filename + ' _thumbnail.webp ' )
icon_file . save ( final_place )
if file_ext . lower ( ) == ' .heic ' :
register_heif_opener ( )
2024-12-06 10:21:44 +13:00
elif file_ext . lower ( ) == ' .avif ' :
import pillow_avif
2023-12-08 17:13:38 +13:00
# resize if necessary
2024-12-06 10:21:44 +13:00
if file_ext . lower ( ) in allowed_extensions :
if file_ext . lower ( ) == ' .svg ' : # svgs don't need to be resized
file = File ( file_path = final_place , file_name = new_filename + file_ext , alt_text = f ' { directory } icon ' ,
thumbnail_path = final_place )
db . session . add ( file )
return file
else :
Image . MAX_IMAGE_PIXELS = 89478485
img = Image . open ( final_place )
img = ImageOps . exif_transpose ( img )
2024-01-25 20:16:08 +13:00
img_width = img . width
img_height = img . height
2024-12-06 10:21:44 +13:00
if img . width > 250 or img . height > 250 :
img . thumbnail ( ( 250 , 250 ) )
img . save ( final_place )
img_width = img . width
img_height = img . height
# save a second, smaller, version as a thumbnail
img . thumbnail ( ( 40 , 40 ) )
img . save ( final_place_thumbnail , format = " WebP " , quality = 93 )
thumbnail_width = img . width
thumbnail_height = img . height
file = File ( file_path = final_place , file_name = new_filename + file_ext , alt_text = f ' { directory } icon ' ,
width = img_width , height = img_height , thumbnail_width = thumbnail_width ,
thumbnail_height = thumbnail_height , thumbnail_path = final_place_thumbnail )
db . session . add ( file )
return file
2024-01-25 20:16:08 +13:00
else :
abort ( 400 )
2023-12-08 17:13:38 +13:00
2023-12-25 21:44:10 +13:00
def save_banner_file ( banner_file , directory = ' communities ' ) - > File :
2023-12-08 17:13:38 +13:00
# check if this is an allowed type of file
file_ext = os . path . splitext ( banner_file . filename ) [ 1 ]
2024-01-25 20:16:08 +13:00
if file_ext . lower ( ) not in allowed_extensions :
2023-12-08 17:13:38 +13:00
abort ( 400 )
new_filename = gibberish ( 15 )
# set up the storage directory
2023-12-25 21:44:10 +13:00
directory = f ' app/static/media/ { directory } / ' + new_filename [ 0 : 2 ] + ' / ' + new_filename [ 2 : 4 ]
2023-12-08 17:13:38 +13:00
ensure_directory_exists ( directory )
# save the file
final_place = os . path . join ( directory , new_filename + file_ext )
final_place_thumbnail = os . path . join ( directory , new_filename + ' _thumbnail.webp ' )
banner_file . save ( final_place )
if file_ext . lower ( ) == ' .heic ' :
register_heif_opener ( )
2024-12-06 10:21:44 +13:00
elif file_ext . lower ( ) == ' .avif ' :
import pillow_avif
2023-12-08 17:13:38 +13:00
# resize if necessary
2024-01-25 20:16:08 +13:00
Image . MAX_IMAGE_PIXELS = 89478485
2023-12-08 17:13:38 +13:00
img = Image . open ( final_place )
2024-01-25 20:16:08 +13:00
if ' . ' + img . format . lower ( ) in allowed_extensions :
img = ImageOps . exif_transpose ( img )
2023-12-08 17:13:38 +13:00
img_width = img . width
img_height = img . height
2024-01-25 20:16:08 +13:00
if img . width > 1600 or img . height > 600 :
img . thumbnail ( ( 1600 , 600 ) )
img . save ( final_place )
img_width = img . width
img_height = img . height
2023-12-08 17:13:38 +13:00
2024-01-25 20:16:08 +13:00
# save a second, smaller, version as a thumbnail
2024-02-10 06:41:24 +13:00
img . thumbnail ( ( 878 , 500 ) )
2024-01-25 20:16:08 +13:00
img . save ( final_place_thumbnail , format = " WebP " , quality = 93 )
thumbnail_width = img . width
thumbnail_height = img . height
2023-12-24 16:20:18 +13:00
2024-01-25 20:16:08 +13:00
file = File ( file_path = final_place , file_name = new_filename + file_ext , alt_text = f ' { directory } banner ' ,
2024-02-10 06:41:24 +13:00
width = img_width , height = img_height , thumbnail_path = final_place_thumbnail ,
thumbnail_width = thumbnail_width , thumbnail_height = thumbnail_height )
2024-01-25 20:16:08 +13:00
db . session . add ( file )
return file
else :
abort ( 400 )
2023-12-26 21:39:52 +13:00
2024-01-03 16:29:58 +13:00
# NB this always signs POSTs as the community so is only suitable for Announce activities
def send_to_remote_instance ( instance_id : int , community_id : int , payload ) :
2023-12-26 21:39:52 +13:00
if current_app . debug :
2024-01-03 16:29:58 +13:00
send_to_remote_instance_task ( instance_id , community_id , payload )
2023-12-26 21:39:52 +13:00
else :
2024-01-03 16:29:58 +13:00
send_to_remote_instance_task . delay ( instance_id , community_id , payload )
2023-12-26 21:39:52 +13:00
@celery.task
2024-01-03 16:29:58 +13:00
def send_to_remote_instance_task ( instance_id : int , community_id : int , payload ) :
2024-11-14 16:28:38 +13:00
session = get_task_session ( )
community : Community = session . query ( Community ) . get ( community_id )
2023-12-26 21:39:52 +13:00
if community :
2024-11-14 16:28:38 +13:00
instance : Instance = session . query ( Instance ) . get ( instance_id )
2024-08-19 10:24:49 +12:00
if instance . inbox and instance . online ( ) and not instance_banned ( instance . domain ) :
2024-11-16 21:53:18 +13:00
if post_request ( instance . inbox , payload , community . private_key , community . ap_profile_id + ' #main-key ' , timeout = 10 ) is True :
2024-05-22 06:29:28 +12:00
instance . last_successful_send = utcnow ( )
instance . failures = 0
else :
instance . failures + = 1
instance . most_recent_attempt = utcnow ( )
instance . start_trying_again = utcnow ( ) + timedelta ( seconds = instance . failures * * 4 )
if instance . failures > 10 :
instance . dormant = True
2024-11-14 16:28:38 +13:00
session . commit ( )
session . close ( )
2024-01-07 12:47:06 +13:00
2024-05-08 19:44:23 +12:00
def community_in_list ( community_id , community_list ) :
for tup in community_list :
if community_id == tup [ 0 ] :
return True
return False
2024-07-14 14:01:22 +08:00
def find_local_users ( search : str ) - > List [ User ] :
return User . query . filter ( User . banned == False , User . deleted == False , User . ap_id == None , User . user_name . ilike ( f " % { search } % " ) ) . \
order_by ( desc ( User . reputation ) ) . all ( )