2023-01-06 00:39:21 +01:00
package primary
2022-11-19 23:16:54 +01:00
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-22 11:22:36 +01:00
rows , err := tx . Query ( ctx , "SELECT * FROM channels WHERE owner_user_id = :uid AND internal_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-22 11:22:36 +01:00
rows , err := tx . Query ( ctx , "SELECT * FROM channels WHERE internal_name = :chan_name OR send_key = :send_key LIMIT 1" , sq . PP {
2022-12-07 22:11:44 +01:00
"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-12-22 12:43:40 +01:00
func ( db * Database ) GetChannelByID ( ctx TxContext , chanid models . ChannelID ) ( * models . Channel , error ) {
tx , err := ctx . GetOrCreateTransaction ( db )
if err != nil {
return nil , err
}
rows , err := tx . Query ( ctx , "SELECT * FROM channels WHERE channel_id = :cid LIMIT 1" , sq . PP {
"cid" : chanid ,
} )
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-12-22 11:22:36 +01:00
func ( db * Database ) CreateChannel ( ctx TxContext , userid models . UserID , dispName string , intName 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 ( )
2023-01-14 00:48:51 +01:00
channelid := models . NewChannelID ( )
2023-01-15 06:30:30 +01:00
_ , err = tx . Exec ( ctx , "INSERT INTO channels (channel_id, owner_user_id, display_name, internal_name, description_name, subscribe_key, send_key, timestamp_created) VALUES (:cid, :ouid, :dnam, :inam, :hnam, :subkey, :sendkey, :ts)" , sq . PP {
2023-01-14 00:48:51 +01:00
"cid" : channelid ,
2022-12-07 22:11:44 +01:00
"ouid" : userid ,
2022-12-22 11:22:36 +01:00
"dnam" : dispName ,
"inam" : intName ,
2023-01-15 06:30:30 +01:00
"hnam" : nil ,
2022-12-07 22:11:44 +01:00
"subkey" : subscribeKey ,
"sendkey" : sendKey ,
"ts" : time2DB ( now ) ,
} )
2022-11-19 23:16:54 +01:00
if err != nil {
return models . Channel { } , err
}
return models . Channel {
2023-01-14 00:48:51 +01:00
ChannelID : channelid ,
2022-11-19 23:16:54 +01:00
OwnerUserID : userid ,
2022-12-22 11:22:36 +01:00
DisplayName : dispName ,
InternalName : intName ,
2022-11-19 23:16:54 +01:00
SubscribeKey : subscribeKey ,
SendKey : sendKey ,
TimestampCreated : now ,
TimestampLastSent : nil ,
MessagesSent : 0 ,
} , nil
}
2022-12-21 18:14:13 +01:00
func ( db * Database ) ListChannelsByOwner ( ctx TxContext , userid models . UserID , subUserID models . UserID ) ( [ ] models . ChannelWithSubscription , error ) {
2022-11-19 23:16:54 +01:00
tx , err := ctx . GetOrCreateTransaction ( db )
if err != nil {
return nil , err
}
2023-01-14 00:48:51 +01:00
order := " ORDER BY channels.timestamp_created ASC, channels.channel_id ASC "
rows , err := tx . Query ( ctx , "SELECT channels.*, sub.* FROM channels LEFT JOIN subscriptions AS sub ON channels.channel_id = sub.channel_id AND sub.subscriber_user_id = :subuid WHERE owner_user_id = :ouid" + order , sq . PP {
2022-12-21 18:14:13 +01:00
"ouid" : userid ,
"subuid" : subUserID ,
} )
2022-11-19 23:16:54 +01:00
if err != nil {
return nil , err
2022-11-20 21:15:06 +01:00
}
2022-12-21 18:14:13 +01:00
data , err := models . DecodeChannelsWithSubscription ( rows )
2022-11-20 21:15:06 +01:00
if err != nil {
return nil , err
}
return data , nil
}
2022-12-21 18:14:13 +01:00
func ( db * Database ) ListChannelsBySubscriber ( ctx TxContext , userid models . UserID , confirmed * bool ) ( [ ] models . ChannelWithSubscription , error ) {
2022-11-20 21:15:06 +01:00
tx , err := ctx . GetOrCreateTransaction ( db )
if err != nil {
return nil , err
}
confCond := ""
2022-12-21 18:14:13 +01:00
if confirmed != nil && * confirmed {
2022-11-20 21:15:06 +01:00
confCond = " AND sub.confirmed = 1"
2022-12-21 18:14:13 +01:00
} else if confirmed != nil && ! * confirmed {
confCond = " AND sub.confirmed = 0"
2022-11-20 21:15:06 +01:00
}
2023-01-14 00:48:51 +01:00
order := " ORDER BY channels.timestamp_created ASC, channels.channel_id ASC "
rows , err := tx . Query ( ctx , "SELECT channels.*, sub.* FROM channels LEFT JOIN subscriptions AS sub on channels.channel_id = sub.channel_id AND sub.subscriber_user_id = :subuid WHERE sub.subscription_id IS NOT NULL " + confCond + order , sq . PP {
2022-12-21 18:14:13 +01:00
"subuid" : userid ,
2022-12-07 22:11:44 +01:00
} )
2022-11-20 21:15:06 +01:00
if err != nil {
return nil , err
}
2022-12-21 18:14:13 +01:00
data , err := models . DecodeChannelsWithSubscription ( rows )
2022-11-20 21:15:06 +01:00
if err != nil {
return nil , err
}
return data , nil
}
2022-12-21 18:14:13 +01:00
func ( db * Database ) ListChannelsByAccess ( ctx TxContext , userid models . UserID , confirmed * bool ) ( [ ] models . ChannelWithSubscription , error ) {
2022-11-20 21:15:06 +01:00
tx , err := ctx . GetOrCreateTransaction ( db )
if err != nil {
return nil , err
}
2022-12-22 17:29:59 +01:00
confCond := "OR (sub.subscription_id IS NOT NULL)"
2022-12-21 18:14:13 +01:00
if confirmed != nil && * confirmed {
2022-12-22 17:29:59 +01:00
confCond = "OR (sub.subscription_id IS NOT NULL AND sub.confirmed = 1)"
2022-12-21 18:14:13 +01:00
} else if confirmed != nil && ! * confirmed {
2022-12-22 17:29:59 +01:00
confCond = "OR (sub.subscription_id IS NOT NULL AND sub.confirmed = 0)"
2022-11-20 21:15:06 +01:00
}
2023-01-14 00:48:51 +01:00
order := " ORDER BY channels.timestamp_created ASC, channels.channel_id ASC "
rows , err := tx . Query ( ctx , "SELECT channels.*, sub.* FROM channels LEFT JOIN subscriptions AS sub on channels.channel_id = sub.channel_id AND sub.subscriber_user_id = :subuid WHERE owner_user_id = :ouid " + confCond + order , sq . PP {
2022-12-21 18:14:13 +01:00
"ouid" : userid ,
"subuid" : userid ,
2022-12-07 22:11:44 +01:00
} )
2022-11-20 21:15:06 +01:00
if err != nil {
return nil , err
2022-11-19 23:16:54 +01:00
}
2022-12-21 18:14:13 +01:00
data , err := models . DecodeChannelsWithSubscription ( rows )
2022-11-19 23:16:54 +01:00
if err != nil {
return nil , err
}
return data , nil
}
2022-12-21 18:14:13 +01:00
func ( db * Database ) GetChannel ( ctx TxContext , userid models . UserID , channelid models . ChannelID ) ( models . ChannelWithSubscription , error ) {
2022-11-19 23:16:54 +01:00
tx , err := ctx . GetOrCreateTransaction ( db )
if err != nil {
2022-12-21 18:14:13 +01:00
return models . ChannelWithSubscription { } , err
2022-11-19 23:16:54 +01:00
}
2022-12-21 18:14:13 +01:00
rows , err := tx . Query ( ctx , "SELECT channels.*, sub.* FROM channels LEFT JOIN subscriptions AS sub on channels.channel_id = sub.channel_id AND sub.subscriber_user_id = :subuid WHERE owner_user_id = :ouid AND channels.channel_id = :cid LIMIT 1" , sq . PP {
"ouid" : userid ,
"cid" : channelid ,
"subuid" : userid ,
2022-12-07 22:11:44 +01:00
} )
2022-11-19 23:16:54 +01:00
if err != nil {
2022-12-21 18:14:13 +01:00
return models . ChannelWithSubscription { } , err
2022-11-19 23:16:54 +01:00
}
2022-12-21 18:14:13 +01:00
channel , err := models . DecodeChannelWithSubscription ( rows )
2022-11-19 23:16:54 +01:00
if err != nil {
2022-12-21 18:14:13 +01:00
return models . ChannelWithSubscription { } , err
2022-11-19 23:16:54 +01:00
}
2022-12-21 18:14:13 +01:00
return channel , nil
2022-11-19 23:16:54 +01:00
}
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
}
2022-12-22 11:22:36 +01:00
func ( db * Database ) UpdateChannelDisplayName ( ctx TxContext , channelid models . ChannelID , dispname string ) error {
tx , err := ctx . GetOrCreateTransaction ( db )
if err != nil {
return err
}
_ , err = tx . Exec ( ctx , "UPDATE channels SET display_name = :nam WHERE channel_id = :cid" , sq . PP {
"nam" : dispname ,
"cid" : channelid ,
} )
if err != nil {
return err
}
return nil
}
2023-01-13 12:43:20 +01:00
func ( db * Database ) UpdateChannelDescriptionName ( ctx TxContext , channelid models . ChannelID , descname * string ) error {
tx , err := ctx . GetOrCreateTransaction ( db )
if err != nil {
return err
}
_ , err = tx . Exec ( ctx , "UPDATE channels SET description_name = :nam WHERE channel_id = :cid" , sq . PP {
"nam" : descname ,
"cid" : channelid ,
} )
if err != nil {
return err
}
return nil
}