summaryrefslogtreecommitdiff
path: root/src/invidious/database/playlists.cr
blob: c6754a1efb3ebc4569dddceb8215dcb2c92a3358 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
require "./base.cr"

#
# This module contains functions related to the "playlists" table.
#
module Invidious::Database::Playlists
  extend self

  # -------------------
  #  Insert / delete
  # -------------------

  def insert(playlist : InvidiousPlaylist)
    playlist_array = playlist.to_a

    request = <<-SQL
      INSERT INTO playlists
      VALUES (#{arg_array(playlist_array)})
    SQL

    PG_DB.exec(request, args: playlist_array)
  end

  # deletes the given playlist and connected playlist videos
  def delete(id : String)
    PlaylistVideos.delete_by_playlist(id)
    request = <<-SQL
      DELETE FROM playlists *
      WHERE id = $1
    SQL

    PG_DB.exec(request, id)
  end

  # -------------------
  #  Update
  # -------------------

  def update(id : String, title : String, privacy, description, updated)
    request = <<-SQL
      UPDATE playlists
      SET title = $1, privacy = $2, description = $3, updated = $4
      WHERE id = $5
    SQL

    PG_DB.exec(request, title, privacy, description, updated, id)
  end

  def update_description(id : String, description)
    request = <<-SQL
      UPDATE playlists
      SET description = $1
      WHERE id = $2
    SQL

    PG_DB.exec(request, description, id)
  end

  def update_subscription_time(id : String)
    request = <<-SQL
      UPDATE playlists
      SET subscribed = now()
      WHERE id = $1
    SQL

    PG_DB.exec(request, id)
  end

  def update_video_added(id : String, index : String | Int64)
    request = <<-SQL
      UPDATE playlists
      SET index = array_append(index, $1),
          video_count = cardinality(index) + 1,
          updated = now()
      WHERE id = $2
    SQL

    PG_DB.exec(request, index, id)
  end

  def update_video_removed(id : String, index : String | Int64)
    request = <<-SQL
      UPDATE playlists
      SET index = array_remove(index, $1),
          video_count = cardinality(index) - 1,
          updated = now()
      WHERE id = $2
    SQL

    PG_DB.exec(request, index, id)
  end

  # -------------------
  #  Salect
  # -------------------

  def select(*, id : String) : InvidiousPlaylist?
    request = <<-SQL
      SELECT * FROM playlists
      WHERE id = $1
    SQL

    return PG_DB.query_one?(request, id, as: InvidiousPlaylist)
  end

  def select_all(*, author : String) : Array(InvidiousPlaylist)
    request = <<-SQL
      SELECT * FROM playlists
      WHERE author = $1
    SQL

    return PG_DB.query_all(request, author, as: InvidiousPlaylist)
  end

  # -------------------
  #  Salect (filtered)
  # -------------------

  def select_like_iv(email : String) : Array(InvidiousPlaylist)
    request = <<-SQL
      SELECT * FROM playlists
      WHERE author = $1 AND id LIKE 'IV%'
      ORDER BY created
    SQL

    PG_DB.query_all(request, email, as: InvidiousPlaylist)
  end

  def select_not_like_iv(email : String) : Array(InvidiousPlaylist)
    request = <<-SQL
      SELECT * FROM playlists
      WHERE author = $1 AND id NOT LIKE 'IV%'
      ORDER BY created
    SQL

    PG_DB.query_all(request, email, as: InvidiousPlaylist)
  end

  def select_user_created_playlists(email : String) : Array({String, String})
    request = <<-SQL
      SELECT id,title FROM playlists
      WHERE author = $1 AND id LIKE 'IV%'
    SQL

    PG_DB.query_all(request, email, as: {String, String})
  end

  # -------------------
  #  Misc checks
  # -------------------

  # Check if given playlist ID exists
  def exists?(id : String) : Bool
    request = <<-SQL
      SELECT id FROM playlists
      WHERE id = $1
    SQL

    return PG_DB.query_one?(request, id, as: String).nil?
  end

  # Count how many playlist a user has created.
  def count_owned_by(author : String) : Int64
    request = <<-SQL
      SELECT count(*) FROM playlists
      WHERE author = $1
    SQL

    return PG_DB.query_one?(request, author, as: Int64) || 0_i64
  end
end

#
# This module contains functions related to the "playlist_videos" table.
#
module Invidious::Database::PlaylistVideos
  extend self

  private alias VideoIndex = Int64 | Array(Int64)

  # -------------------
  #  Insert / Delete
  # -------------------

  def insert(video : PlaylistVideo)
    video_array = video.to_a

    request = <<-SQL
      INSERT INTO playlist_videos
      VALUES (#{arg_array(video_array)})
    SQL

    PG_DB.exec(request, args: video_array)
  end

  def delete(index)
    request = <<-SQL
      DELETE FROM playlist_videos *
      WHERE index = $1
    SQL

    PG_DB.exec(request, index)
  end

  def delete_by_playlist(plid : String)
    request = <<-SQL
      DELETE FROM playlist_videos *
      WHERE plid = $1
    SQL

    PG_DB.exec(request, plid)
  end

  # -------------------
  #  Salect
  # -------------------

  def select(plid : String, index : VideoIndex, offset, limit = 100) : Array(PlaylistVideo)
    request = <<-SQL
      SELECT * FROM playlist_videos
      WHERE plid = $1
      ORDER BY array_position($2, index)
      LIMIT $3
      OFFSET $4
    SQL

    return PG_DB.query_all(request, plid, index, limit, offset, as: PlaylistVideo)
  end

  def select_index(plid : String, vid : String) : Int64?
    request = <<-SQL
      SELECT index FROM playlist_videos
      WHERE plid = $1 AND id = $2
      LIMIT 1
    SQL

    return PG_DB.query_one?(request, plid, vid, as: Int64)
  end

  def select_one_id(plid : String, index : VideoIndex) : String?
    request = <<-SQL
      SELECT id FROM playlist_videos
      WHERE plid = $1
      ORDER BY array_position($2, index)
      LIMIT 1
    SQL

    return PG_DB.query_one?(request, plid, index, as: String)
  end

  def select_ids(plid : String, index : VideoIndex, limit = 500) : Array(String)
    request = <<-SQL
      SELECT id FROM playlist_videos
      WHERE plid = $1
      ORDER BY array_position($2, index)
      LIMIT $3
    SQL

    return PG_DB.query_all(request, plid, index, limit, as: String)
  end
end