query.sql 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. -- Queries added to query.sql must be compiled into Go code with sqlc. Read README.md for details.
  2. -- Federation related queries.
  3. -- name: GetFollowerCount :one
  4. SElECT count(*) FROM ap_followers WHERE approved_at is not null;
  5. -- name: GetLocalPostCount :one
  6. SElECT count(*) FROM ap_outbox;
  7. -- name: GetFederationFollowersWithOffset :many
  8. SELECT iri, inbox, name, username, image, created_at FROM ap_followers WHERE approved_at is not null ORDER BY created_at DESC LIMIT $1 OFFSET $2;
  9. -- name: GetRejectedAndBlockedFollowers :many
  10. SELECT iri, name, username, image, created_at, disabled_at FROM ap_followers WHERE disabled_at is not null;
  11. -- name: GetFederationFollowerApprovalRequests :many
  12. SELECT iri, inbox, name, username, image, created_at FROM ap_followers WHERE approved_at IS null AND disabled_at is null;
  13. -- name: ApproveFederationFollower :exec
  14. UPDATE ap_followers SET approved_at = $1, disabled_at = null WHERE iri = $2;
  15. -- name: RejectFederationFollower :exec
  16. UPDATE ap_followers SET approved_at = null, disabled_at = $1 WHERE iri = $2;
  17. -- name: GetFollowerByIRI :one
  18. SELECT iri, inbox, name, username, image, request, request_object, created_at, approved_at, disabled_at FROM ap_followers WHERE iri = $1;
  19. -- name: GetOutboxWithOffset :many
  20. SELECT value FROM ap_outbox LIMIT $1 OFFSET $2;
  21. -- name: GetObjectFromOutboxByIRI :one
  22. SELECT value, live_notification, created_at FROM ap_outbox WHERE iri = $1;
  23. -- name: RemoveFollowerByIRI :exec
  24. DELETE FROM ap_followers WHERE iri = $1;
  25. -- name: AddFollower :exec
  26. INSERT INTO ap_followers(iri, inbox, request, request_object, name, username, image, approved_at) values($1, $2, $3, $4, $5, $6, $7, $8);
  27. -- name: AddToOutbox :exec
  28. INSERT INTO ap_outbox(iri, value, type, live_notification) values($1, $2, $3, $4);
  29. -- name: AddToAcceptedActivities :exec
  30. INSERT INTO ap_accepted_activities(iri, actor, type, timestamp) values($1, $2, $3, $4);
  31. -- name: GetInboundActivityCount :one
  32. SELECT count(*) FROM ap_accepted_activities;
  33. -- name: GetInboundActivitiesWithOffset :many
  34. SELECT iri, actor, type, timestamp FROM ap_accepted_activities ORDER BY timestamp DESC LIMIT $1 OFFSET $2;
  35. -- name: DoesInboundActivityExist :one
  36. SELECT count(*) FROM ap_accepted_activities WHERE iri = $1 AND actor = $2 AND TYPE = $3;
  37. -- name: UpdateFollowerByIRI :exec
  38. UPDATE ap_followers SET inbox = $1, name = $2, username = $3, image = $4 WHERE iri = $5;
  39. -- name: BanIPAddress :exec
  40. INSERT INTO ip_bans(ip_address, notes) values($1, $2);
  41. -- name: RemoveIPAddressBan :exec
  42. DELETE FROM ip_bans WHERE ip_address = $1;
  43. -- name: IsIPAddressBlocked :one
  44. SELECT count(*) FROM ip_bans WHERE ip_address = $1;
  45. -- name: GetIPAddressBans :many
  46. SELECT * FROM ip_bans;
  47. -- name: AddNotification :exec
  48. INSERT INTO notifications (channel, destination) VALUES($1, $2);
  49. -- name: GetNotificationDestinationsForChannel :many
  50. SELECT destination FROM notifications WHERE channel = $1;
  51. -- name: RemoveNotificationDestinationForChannel :exec
  52. DELETE FROM notifications WHERE channel = $1 AND destination = $2;
  53. -- name: AddAuthForUser :exec
  54. INSERT INTO auth(user_id, token, type) values($1, $2, $3);
  55. -- name: GetUserByAuth :one
  56. SELECT users.id, display_name, display_color, users.created_at, disabled_at, previous_names, namechanged_at, authenticated_at, scopes FROM auth, users WHERE token = $1 AND auth.type = $2 AND users.id = auth.user_id;
  57. -- name: AddAccessTokenForUser :exec
  58. INSERT INTO user_access_tokens(token, user_id) values($1, $2);
  59. -- name: GetUserByAccessToken :one
  60. SELECT users.id, display_name, display_color, users.created_at, disabled_at, previous_names, namechanged_at, authenticated_at, scopes FROM users, user_access_tokens WHERE token = $1 AND users.id = user_id;
  61. -- name: GetUserDisplayNameByToken :one
  62. SELECT display_name FROM users, user_access_tokens WHERE token = $1 AND users.id = user_id AND disabled_at = NULL;
  63. -- name: SetAccessTokenToOwner :exec
  64. UPDATE user_access_tokens SET user_id = $1 WHERE token = $2;
  65. -- name: SetUserAsAuthenticated :exec
  66. UPDATE users SET authenticated_at = CURRENT_TIMESTAMP WHERE id = $1;
  67. -- name: GetMessagesFromUser :many
  68. SELECT id, body, hidden_at, timestamp FROM messages WHERE eventType = 'CHAT' AND user_id = $1 ORDER BY TIMESTAMP DESC;
  69. -- name: IsDisplayNameAvailable :one
  70. SELECT count(*) FROM users WHERE display_name = $1 AND ( type='API' OR authenticated_at IS NOT NULL ) AND disabled_at IS NULL;
  71. -- name: ChangeDisplayName :exec
  72. UPDATE users SET display_name = $1, previous_names = previous_names || $2, namechanged_at = $3 WHERE id = $4;
  73. -- name: ChangeDisplayColor :exec
  74. UPDATE users SET display_color = $1 WHERE id = $2;
  75. -- Recording and clip related queries.
  76. -- name: GetStreams :many
  77. SELECT id, stream_title, start_time, end_time FROM streams ORDER BY start_time DESC;
  78. -- name: GetStreamById :one
  79. SELECT id, stream_title, start_time, end_time FROM streams WHERE id = $1 LIMIT 1;
  80. -- name: GetOutputConfigurationsForStreamId :many
  81. SELECT id, stream_id, variant_id, name, segment_duration, bitrate, framerate, resolution_width, resolution_height FROM video_segment_output_configuration WHERE stream_id = $1;
  82. -- name: GetOutputConfigurationForId :one
  83. SELECT id, stream_id, variant_id, name, segment_duration, bitrate, framerate, resolution_width, resolution_height FROM video_segment_output_configuration WHERE id = $1;
  84. -- name: GetSegmentsForOutputId :many
  85. SELECT id, stream_id, output_configuration_id, path, timestamp FROM video_segments WHERE output_configuration_id = $1 ORDER BY timestamp ASC;
  86. -- name: GetSegmentsForOutputIdAndWindow :many
  87. SELECT id, stream_id, output_configuration_id, path, relative_timestamp, timestamp FROM video_segments WHERE output_configuration_id = $1 AND (cast ( relative_timestamp as int ) - ( relative_timestamp < cast ( relative_timestamp as int ))) >= @start_seconds::REAL AND (cast ( relative_timestamp as int ) + ( relative_timestamp > cast ( relative_timestamp as int ))) <= @end_seconds::REAL ORDER BY relative_timestamp ASC;
  88. -- name: InsertStream :exec
  89. INSERT INTO streams (id, stream_title, start_time, end_time) VALUES($1, $2, $3, $4);
  90. -- name: InsertOutputConfiguration :exec
  91. INSERT INTO video_segment_output_configuration (id, variant_id, stream_id, name, segment_duration, bitrate, framerate, resolution_width, resolution_height, timestamp) VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10);
  92. -- name: InsertSegment :exec
  93. INSERT INTO video_segments (id, stream_id, output_configuration_id, path, relative_timestamp, timestamp) VALUES($1, $2, $3, $4, $5, $6);
  94. -- name: SetStreamEnded :exec
  95. UPDATE streams SET end_time = $1 WHERE id = $2;
  96. -- name: InsertClip :exec
  97. INSERT INTO replay_clips (id, stream_id, clip_title, relative_start_time, relative_end_time, timestamp) VALUES($1, $2, $3, $4, $5, $6);
  98. -- name: GetAllClips :many
  99. SELECT rc.id AS id, rc.clip_title, rc.stream_id, rc.relative_start_time, rc.relative_end_time, (rc.relative_end_time - rc.relative_start_time) AS duration_seconds, rc.timestamp, s.stream_title AS stream_title
  100. FROM replay_clips rc
  101. JOIN streams s ON rc.stream_id = s.id
  102. ORDER BY timestamp DESC;
  103. -- name: GetAllClipsForStream :many
  104. SELECT rc.id AS clip_id, rc.stream_id, rc.clipped_by, rc.clip_title, rc.relative_start_time, rc.relative_end_time, rc.timestamp,
  105. s.id AS stream_id, s.stream_title AS stream_title
  106. FROM replay_clips rc
  107. JOIN streams s ON rc.stream_id = s.id
  108. WHERE rc.stream_id = $1
  109. ORDER BY timestamp DESC;
  110. -- name: GetClip :one
  111. SELECT id AS clip_id, stream_id, clipped_by, clip_title, timestamp AS clip_timestamp, relative_start_time, relative_end_time FROM replay_clips WHERE id = $1;
  112. -- name: GetFinalSegmentForStream :one
  113. SELECT id, stream_id, output_configuration_id, path, relative_timestamp, timestamp FROM video_segments WHERE stream_id = $1 ORDER BY relative_timestamp DESC LIMIT 1;
  114. -- name: FixUnfinishedStreams :exec
  115. UPDATE streams SET end_time = (SELECT timestamp FROM video_segments WHERE stream_id = streams.id) WHERE end_time IS NULL;