2022-11-19 23:16:54 +01:00
package db
import (
"blackforestbytes.com/simplecloudnotifier/models"
"database/sql"
2022-12-07 23:32:58 +01:00
"gogs.mikescher.com/BlackForestBytes/goext/sq"
2022-11-19 23:16:54 +01:00
"time"
)
2022-11-20 22:18:24 +01:00
func ( db * Database ) GetChannelByName ( ctx TxContext , userid models . UserID , chanName string ) ( * models . Channel , error ) {
2022-11-19 23:16:54 +01:00
tx , err := ctx . GetOrCreateTransaction ( db )
if err != nil {
return nil , err
}
2022-12-09 00:13:10 +01:00
rows , err := tx . Query ( ctx , "SELECT * FROM channels WHERE owner_user_id = :uid AND name = :nam LIMIT 1" , sq . PP {
2022-12-07 22:11:44 +01:00
"uid" : userid ,
"nam" : chanName ,
} )
2022-11-19 23:16:54 +01:00
if err != nil {
return nil , err
}
channel , err := models . DecodeChannel ( rows )
if err == sql . ErrNoRows {
return nil , nil
}
if err != nil {
return nil , err
}
return & channel , nil
}
2022-11-30 17:58:04 +01:00
func ( db * Database ) GetChannelByNameAndSendKey ( ctx TxContext , chanName string , sendKey string ) ( * models . Channel , error ) {
tx , err := ctx . GetOrCreateTransaction ( db )
if err != nil {
return nil , err
}
2022-12-07 22:11:44 +01:00
rows , err := tx . Query ( ctx , "SELECT * FROM channels WHERE name = :chan_name OR send_key = :send_key LIMIT 1" , sq . PP {
"chan_name" : chanName ,
"send_key" : sendKey ,
} )
2022-11-30 17:58:04 +01:00
if err != nil {
return nil , err
}
channel , err := models . DecodeChannel ( rows )
if err == sql . ErrNoRows {
return nil , nil
}
if err != nil {
return nil , err
}
return & channel , nil
}
2022-11-20 22:18:24 +01:00
func ( db * Database ) CreateChannel ( ctx TxContext , userid models . UserID , name string , subscribeKey string , sendKey string ) ( models . Channel , error ) {
2022-11-19 23:16:54 +01:00
tx , err := ctx . GetOrCreateTransaction ( db )
if err != nil {
return models . Channel { } , err
}
now := time . Now ( ) . UTC ( )
2022-12-07 22:11:44 +01:00
res , err := tx . Exec ( ctx , "INSERT INTO channels (owner_user_id, name, subscribe_key, send_key, timestamp_created) VALUES (:ouid, :nam, :subkey, :sendkey, :ts)" , sq . PP {
"ouid" : userid ,
"nam" : name ,
"subkey" : subscribeKey ,
"sendkey" : sendKey ,
"ts" : time2DB ( now ) ,
} )
2022-11-19 23:16:54 +01:00
if err != nil {
return models . Channel { } , err
}
liid , err := res . LastInsertId ( )
if err != nil {
return models . Channel { } , err
}
return models . Channel {
2022-11-20 22:18:24 +01:00
ChannelID : models . ChannelID ( liid ) ,
2022-11-19 23:16:54 +01:00
OwnerUserID : userid ,
Name : name ,
SubscribeKey : subscribeKey ,
SendKey : sendKey ,
TimestampCreated : now ,
TimestampLastSent : nil ,
MessagesSent : 0 ,
} , nil
}
2022-11-20 22:18:24 +01:00
func ( db * Database ) ListChannelsByOwner ( ctx TxContext , userid models . UserID ) ( [ ] models . Channel , error ) {
2022-11-19 23:16:54 +01:00
tx , err := ctx . GetOrCreateTransaction ( db )
if err != nil {
return nil , err
}
2022-12-07 22:11:44 +01:00
rows , err := tx . Query ( ctx , "SELECT * FROM channels WHERE owner_user_id = :ouid" , sq . PP { "ouid" : userid } )
2022-11-19 23:16:54 +01:00
if err != nil {
return nil , err
2022-11-20 21:15:06 +01:00
}
data , err := models . DecodeChannels ( rows )
if err != nil {
return nil , err
}
return data , nil
}
2022-11-20 22:18:24 +01:00
func ( db * Database ) ListChannelsBySubscriber ( ctx TxContext , userid models . UserID , confirmed bool ) ( [ ] models . Channel , error ) {
2022-11-20 21:15:06 +01:00
tx , err := ctx . GetOrCreateTransaction ( db )
if err != nil {
return nil , err
}
confCond := ""
if confirmed {
confCond = " AND sub.confirmed = 1"
}
2022-12-07 22:11:44 +01:00
rows , err := tx . Query ( ctx , "SELECT * FROM channels LEFT JOIN subscriptions sub on channels.channel_id = sub.channel_id WHERE sub.subscriber_user_id = :suid " + confCond , sq . PP {
"suid" : userid ,
} )
2022-11-20 21:15:06 +01:00
if err != nil {
return nil , err
}
data , err := models . DecodeChannels ( rows )
if err != nil {
return nil , err
}
return data , nil
}
2022-11-20 22:18:24 +01:00
func ( db * Database ) ListChannelsByAccess ( ctx TxContext , userid models . UserID , confirmed bool ) ( [ ] models . Channel , error ) {
2022-11-20 21:15:06 +01:00
tx , err := ctx . GetOrCreateTransaction ( db )
if err != nil {
return nil , err
}
2022-11-20 22:18:24 +01:00
confCond := "OR sub.subscriber_user_id = ?"
2022-11-20 21:15:06 +01:00
if confirmed {
2022-11-20 22:18:24 +01:00
confCond = "OR (sub.subscriber_user_id = ? AND sub.confirmed = 1)"
2022-11-20 21:15:06 +01:00
}
2022-12-07 22:11:44 +01:00
rows , err := tx . Query ( ctx , "SELECT * FROM channels LEFT JOIN subscriptions sub on channels.channel_id = sub.channel_id WHERE owner_user_id = :ouid " + confCond , sq . PP {
"ouid" : userid ,
} )
2022-11-20 21:15:06 +01:00
if err != nil {
return nil , err
2022-11-19 23:16:54 +01:00
}
data , err := models . DecodeChannels ( rows )
if err != nil {
return nil , err
}
return data , nil
}
2022-11-20 22:18:24 +01:00
func ( db * Database ) GetChannel ( ctx TxContext , userid models . UserID , channelid models . ChannelID ) ( models . Channel , error ) {
2022-11-19 23:16:54 +01:00
tx , err := ctx . GetOrCreateTransaction ( db )
if err != nil {
return models . Channel { } , err
}
2022-12-07 22:11:44 +01:00
rows , err := tx . Query ( ctx , "SELECT * FROM channels WHERE owner_user_id = :ouid AND channel_id = :cid LIMIT 1" , sq . PP {
"ouid" : userid ,
"cid" : channelid ,
} )
2022-11-19 23:16:54 +01:00
if err != nil {
return models . Channel { } , err
}
client , err := models . DecodeChannel ( rows )
if err != nil {
return models . Channel { } , err
}
return client , nil
}
func ( db * Database ) IncChannelMessageCounter ( ctx TxContext , channel models . Channel ) error {
tx , err := ctx . GetOrCreateTransaction ( db )
if err != nil {
return err
}
2022-12-07 22:11:44 +01:00
_ , err = tx . Exec ( ctx , "UPDATE channels SET messages_sent = :ctr, timestamp_lastsent = :ts WHERE channel_id = :cid" , sq . PP {
"ctr" : channel . MessagesSent + 1 ,
"cid" : time2DB ( time . Now ( ) ) ,
"ts" : channel . ChannelID ,
} )
2022-11-19 23:16:54 +01:00
if err != nil {
return err
}
return nil
}
2022-11-20 21:35:08 +01:00
2022-11-20 22:18:24 +01:00
func ( db * Database ) UpdateChannelSendKey ( ctx TxContext , channelid models . ChannelID , newkey string ) error {
2022-11-20 21:35:08 +01:00
tx , err := ctx . GetOrCreateTransaction ( db )
if err != nil {
return err
}
2022-12-07 22:11:44 +01:00
_ , err = tx . Exec ( ctx , "UPDATE channels SET send_key = :key WHERE channel_id = :cid" , sq . PP {
"key" : newkey ,
"cid" : channelid ,
} )
2022-11-20 21:35:08 +01:00
if err != nil {
return err
}
return nil
}
2022-11-20 22:18:24 +01:00
func ( db * Database ) UpdateChannelSubscribeKey ( ctx TxContext , channelid models . ChannelID , newkey string ) error {
2022-11-20 21:35:08 +01:00
tx , err := ctx . GetOrCreateTransaction ( db )
if err != nil {
return err
}
2022-12-07 22:11:44 +01:00
_ , err = tx . Exec ( ctx , "UPDATE channels SET subscribe_key = :key WHERE channel_id = :cid" , sq . PP {
"key" : newkey ,
"cid" : channelid ,
} )
2022-11-20 21:35:08 +01:00
if err != nil {
return err
}
return nil
}