2023-01-06 00:39:21 +01:00
package primary
2022-11-19 23:16:54 +01:00
import (
2023-07-27 17:44:06 +02:00
"blackforestbytes.com/simplecloudnotifier/db"
2022-11-19 23:16:54 +01:00
"blackforestbytes.com/simplecloudnotifier/models"
2022-12-07 23:32:58 +01:00
"gogs.mikescher.com/BlackForestBytes/goext/sq"
2022-11-19 23:16:54 +01:00
"time"
)
2023-07-27 17:44:06 +02:00
func ( db * Database ) GetChannelByName ( ctx db . 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
}
2024-09-15 21:07:46 +02:00
return sq . QuerySingleOpt [ models . Channel ] ( ctx , tx , "SELECT * FROM channels WHERE owner_user_id = :uid AND internal_name = :nam LIMIT 1" , sq . PP { "uid" : userid , "nam" : chanName } , sq . SModeExtended , sq . Safe )
2022-11-19 23:16:54 +01:00
}
2023-07-27 17:44:06 +02:00
func ( db * Database ) GetChannelByID ( ctx db . TxContext , chanid models . ChannelID ) ( * models . Channel , error ) {
2022-12-22 12:43:40 +01:00
tx , err := ctx . GetOrCreateTransaction ( db )
if err != nil {
return nil , err
}
2024-09-15 21:07:46 +02:00
return sq . QuerySingleOpt [ models . Channel ] ( ctx , tx , "SELECT * FROM channels WHERE channel_id = :cid LIMIT 1" , sq . PP { "cid" : chanid } , sq . SModeExtended , sq . Safe )
2022-12-22 12:43:40 +01:00
}
2024-02-24 12:20:41 +01:00
type CreateChanel struct {
UserId models . UserID
DisplayName string
IntName string
SubscribeKey string
Description * string
}
2024-05-26 19:30:13 +02:00
func ( db * Database ) CreateChannel ( ctx db . TxContext , userid models . UserID , dispName string , intName string , subscribeKey string , description * string ) ( models . Channel , error ) {
2022-11-19 23:16:54 +01:00
tx , err := ctx . GetOrCreateTransaction ( db )
if err != nil {
return models . Channel { } , err
}
2024-09-15 21:07:46 +02:00
entity := models . Channel {
2023-05-28 22:27:38 +02:00
ChannelID : models . NewChannelID ( ) ,
2024-05-26 19:30:13 +02:00
OwnerUserID : userid ,
DisplayName : dispName ,
InternalName : intName ,
SubscribeKey : subscribeKey ,
DescriptionName : description ,
2024-09-15 21:07:46 +02:00
TimestampCreated : models . NowSCNTime ( ) ,
2022-11-19 23:16:54 +01:00
TimestampLastSent : nil ,
MessagesSent : 0 ,
2023-05-28 22:27:38 +02:00
}
_ , err = sq . InsertSingle ( ctx , tx , "channels" , entity )
if err != nil {
return models . Channel { } , err
}
2024-09-15 21:07:46 +02:00
return entity , nil
2022-11-19 23:16:54 +01:00
}
2023-07-27 17:44:06 +02:00
func ( db * Database ) ListChannelsByOwner ( ctx db . 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 "
2024-09-15 21:07:46 +02:00
sql := "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
pp := sq . PP {
2022-12-21 18:14:13 +01:00
"ouid" : userid ,
"subuid" : subUserID ,
2022-11-20 21:15:06 +01:00
}
2024-09-15 21:07:46 +02:00
return sq . QueryAll [ models . ChannelWithSubscription ] ( ctx , tx , sql , pp , sq . SModeExtended , sq . Safe )
2022-11-20 21:15:06 +01:00
}
2023-07-27 17:44:06 +02:00
func ( db * Database ) ListChannelsBySubscriber ( ctx db . 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 "
2024-09-15 21:07:46 +02:00
sql := "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
2022-11-20 21:15:06 +01:00
2024-09-15 21:07:46 +02:00
pp := sq . PP {
"subuid" : userid ,
2022-11-20 21:15:06 +01:00
}
2024-09-15 21:07:46 +02:00
return sq . QueryAll [ models . ChannelWithSubscription ] ( ctx , tx , sql , pp , sq . SModeExtended , sq . Safe )
2022-11-20 21:15:06 +01:00
}
2023-07-27 17:44:06 +02:00
func ( db * Database ) ListChannelsByAccess ( ctx db . 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 "
2024-09-15 21:07:46 +02:00
sql := "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
pp := sq . PP {
2022-12-21 18:14:13 +01:00
"ouid" : userid ,
"subuid" : userid ,
2022-11-19 23:16:54 +01:00
}
2024-09-15 21:07:46 +02:00
return sq . QueryAll [ models . ChannelWithSubscription ] ( ctx , tx , sql , pp , sq . SModeExtended , sq . Safe )
2022-11-19 23:16:54 +01:00
}
2023-07-27 17:44:06 +02:00
func ( db * Database ) GetChannel ( ctx db . TxContext , userid models . UserID , channelid models . ChannelID , enforceOwner bool ) ( 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
}
2023-05-28 03:38:33 +02:00
params := sq . PP {
2022-12-21 18:14:13 +01:00
"cid" : channelid ,
"subuid" : userid ,
2023-05-28 03:38:33 +02:00
}
selectors := "channels.*, sub.*"
join := "LEFT JOIN subscriptions AS sub on channels.channel_id = sub.channel_id AND sub.subscriber_user_id = :subuid"
cond := "channels.channel_id = :cid"
if enforceOwner {
cond = "owner_user_id = :ouid AND channels.channel_id = :cid"
params [ "ouid" ] = userid
}
2024-09-15 21:07:46 +02:00
sql := "SELECT " + selectors + " FROM channels " + join + " WHERE " + cond + " LIMIT 1"
2022-11-19 23:16:54 +01:00
2024-09-15 21:07:46 +02:00
return sq . QuerySingle [ models . ChannelWithSubscription ] ( ctx , tx , sql , params , sq . SModeExtended , sq . Safe )
2022-11-19 23:16:54 +01:00
}
2023-07-27 17:44:06 +02:00
func ( db * Database ) IncChannelMessageCounter ( ctx db . TxContext , channel * models . Channel ) error {
2022-11-19 23:16:54 +01:00
tx , err := ctx . GetOrCreateTransaction ( db )
if err != nil {
return err
}
2023-06-17 20:08:39 +02:00
now := time . Now ( )
2023-06-10 03:41:54 +02:00
_ , err = tx . Exec ( ctx , "UPDATE channels SET messages_sent = messages_sent+1, timestamp_lastsent = :ts WHERE channel_id = :cid" , sq . PP {
2023-06-17 20:08:39 +02:00
"ts" : time2DB ( now ) ,
"cid" : channel . ChannelID ,
2022-12-07 22:11:44 +01:00
} )
2022-11-19 23:16:54 +01:00
if err != nil {
return err
}
2023-06-17 20:08:39 +02:00
channel . MessagesSent += 1
2024-09-15 21:07:46 +02:00
channel . TimestampLastSent = models . NewSCNTimePtr ( & now )
2023-06-17 20:08:39 +02:00
2022-11-19 23:16:54 +01:00
return nil
}
2022-11-20 21:35:08 +01:00
2023-07-27 17:44:06 +02:00
func ( db * Database ) UpdateChannelSubscribeKey ( ctx db . 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
2023-07-27 17:44:06 +02:00
func ( db * Database ) UpdateChannelDisplayName ( ctx db . TxContext , channelid models . ChannelID , dispname string ) error {
2022-12-22 11:22:36 +01:00
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
2023-07-27 17:44:06 +02:00
func ( db * Database ) UpdateChannelDescriptionName ( ctx db . TxContext , channelid models . ChannelID , descname * string ) error {
2023-01-13 12:43:20 +01:00
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
}